Browse Source

add the 'site:clear-cache' command for http/api interfaces

Olivier Massot 4 years ago
parent
commit
488be0d4c0

+ 71 - 0
ot_admin/Classes/Command/ClearSiteCacheCommand.php

@@ -0,0 +1,71 @@
+<?php
+
+namespace Opentalent\OtAdmin\Command;
+
+
+use Opentalent\OtAdmin\Controller\SiteController;
+use Symfony\Component\Console\Command\Command;
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Output\OutputInterface;
+use Symfony\Component\Console\Style\SymfonyStyle;
+
+/**
+ * This CLI command clears the cache of an existing organization's website
+ *
+ * By default, this command will only clear the frontend cache.
+ * Pass the '-a / --all' option to clear all of the typo3 caches.
+ *
+ * @package Opentalent\OtAdmin\Command
+ */
+class ClearSiteCacheCommand extends Command
+{
+    /**
+     * -- This method is expected by Typo3, do not rename ou remove --
+     *
+     * Allows to configure the command.
+     * Allows to add a description, a help text, and / or define arguments.
+     *
+     */
+    protected function configure()
+    {
+        $this
+            ->setName("ot:site:clear-cache")
+            ->setDescription("Clear the cache of an organization website")
+            ->setHelp("This CLI command clears the cache of an existing organization's website")
+            ->addArgument(
+                'organization_id',
+                InputArgument::REQUIRED,
+                "The organization's id in the opentalent DB"
+            )->addOption(
+                'all',
+                'a',
+                InputOption::VALUE_NONE,
+                'Use this option to clear all the typo3 caches, and not only the frontend one'
+            );
+    }
+
+    /**
+     * -- This method is expected by Typo3, do not rename ou remove --
+     *
+     * Executes the command for clearing the cache of the organization's website
+     *
+     * @param InputInterface $input
+     * @param OutputInterface $output
+     * @throws \Exception
+     */
+    protected function execute(InputInterface $input, OutputInterface $output)
+    {
+        $org_id = $input->getArgument('organization_id');
+        $clearAll = $input->getOption('all');
+
+        $io = new SymfonyStyle($input, $output);
+
+        $siteController = new SiteController();
+        $rootUid = $siteController->clearSiteCacheAction($org_id, $clearAll);
+
+        $io->success(sprintf("The cache has been cleared for the website with root uid " . $rootUid . ""));
+    }
+
+}

+ 16 - 0
ot_admin/Classes/Controller/SiteController.php

@@ -3,6 +3,7 @@
 namespace Opentalent\OtAdmin\Controller;
 namespace Opentalent\OtAdmin\Controller;
 
 
 use http\Exception\RuntimeException;
 use http\Exception\RuntimeException;
+use Opentalent\OtCore\Cache\OtCacheManager;
 use Opentalent\OtCore\Domain\Model\Organization;
 use Opentalent\OtCore\Domain\Model\Organization;
 use Opentalent\OtCore\Domain\Repository\OrganizationRepository;
 use Opentalent\OtCore\Domain\Repository\OrganizationRepository;
 use Opentalent\OtCore\Exception\ApiRequestException;
 use Opentalent\OtCore\Exception\ApiRequestException;
@@ -888,6 +889,21 @@ class SiteController extends ActionController
         }
         }
     }
     }
 
 
+    /**
+     * Clear the cache of the organization's website
+     *
+     * @param int $organizationId the organization's id whom site cache should be cleared
+     * @param bool $clearAll if true, all caches will be cleared, and not only the frontend one
+     * @return int
+     */
+    public function clearSiteCacheAction($organizationId, $clearAll=false) {
+        $rootUid = $this->findRootUidFor($organizationId);
+
+        OtCacheManager::clearSiteCache($rootUid, $clearAll);
+
+        return $rootUid;
+    }
+
     /**
     /**
      * Retrieve the Organization object from the repository and then,
      * Retrieve the Organization object from the repository and then,
      * from the Opentalent API
      * from the Opentalent API

+ 27 - 0
ot_admin/Classes/Http/ApiController.php

@@ -194,4 +194,31 @@ class ApiController implements LoggerAwareInterface
             ]
             ]
         );
         );
     }
     }
+
+    /**
+     * -- Target of the route 'site_clearcache' --
+     * >> Requires a query param named 'organization-id' (int)
+     *
+     * Clear the cache of the organization's website
+     *
+     * @param ServerRequest $request
+     * @return JsonResponse
+     * @throws \Exception
+     */
+    public function clearSiteCacheAction(ServerRequest $request) {
+        $this->assertIpAllowed();
+
+        $organizationId = $this->getOrganizationId($request);
+
+        $controller = new SiteController();
+        $rootUid = $controller->clearSiteCacheAction($organizationId);
+
+        return new JsonResponse(
+            [
+                'organization_id' => $organizationId,
+                'msg' => "The cache has been cleared for the website with root uid " . $rootUid . "",
+                'root_uid' => $rootUid
+            ]
+        );
+    }
 }
 }

+ 26 - 21
ot_admin/Configuration/Backend/Routes.php

@@ -6,25 +6,30 @@ use Opentalent\OtAdmin\Http\ApiController;
 // @see https://docs.typo3.org/m/typo3/reference-coreapi/master/en-us/ApiOverview/BackendRouting/Index.html
 // @see https://docs.typo3.org/m/typo3/reference-coreapi/master/en-us/ApiOverview/BackendRouting/Index.html
 
 
 return [
 return [
-        // Create a new organization's website
-        'site_create' => [
-            'path' => '/otadmin/site/create',
-            'target' => ApiController::class . '::createSiteAction',
-            'access' => 'public'
-        ],
-        'site_delete' => [
-            'path' => '/otadmin/site/delete',
-            'target' => ApiController::class . '::deleteSiteAction',
-            'access' => 'public'
-        ],
-        'site_undelete' => [
-            'path' => '/otadmin/site/undelete',
-            'target' => ApiController::class . '::undeleteSiteAction',
-            'access' => 'public'
-        ],
-        'site_update' => [
-            'path' => '/otadmin/site/update',
-            'target' => ApiController::class . '::updateSiteConstantsAction',
-            'access' => 'public'
-        ],
+    // Create a new organization's website
+    'site_create' => [
+        'path' => '/otadmin/site/create',
+        'target' => ApiController::class . '::createSiteAction',
+        'access' => 'public'
+    ],
+    'site_delete' => [
+        'path' => '/otadmin/site/delete',
+        'target' => ApiController::class . '::deleteSiteAction',
+        'access' => 'public'
+    ],
+    'site_undelete' => [
+        'path' => '/otadmin/site/undelete',
+        'target' => ApiController::class . '::undeleteSiteAction',
+        'access' => 'public'
+    ],
+    'site_update' => [
+        'path' => '/otadmin/site/update',
+        'target' => ApiController::class . '::updateSiteConstantsAction',
+        'access' => 'public'
+    ],
+    'site_clearcache' => [
+        'path' => '/otadmin/site/clear-cache',
+        'target' => ApiController::class . '::clearSiteCacheAction',
+        'access' => 'public'
+    ],
 ];
 ];

+ 3 - 0
ot_admin/Configuration/Commands.php

@@ -21,6 +21,9 @@ return [
     ],
     ],
     'ot:site:update' => [
     'ot:site:update' => [
         'class' => Opentalent\OtAdmin\Command\UpdateSiteCommand::class
         'class' => Opentalent\OtAdmin\Command\UpdateSiteCommand::class
+    ],
+    'ot:site:clear-cache' => [
+        'class' => Opentalent\OtAdmin\Command\ClearSiteCacheCommand::class
     ]
     ]
 ];
 ];
 
 

+ 6 - 0
ot_admin/Configuration/Services.yaml

@@ -24,3 +24,9 @@ services:
       - name: 'ot:site:undelete'
       - name: 'ot:site:undelete'
         command: 'ot:site:undelete'
         command: 'ot:site:undelete'
         schedulable: false
         schedulable: false
+
+  Opentalent\OtAdmin\Command\ClearSiteCacheCommand:
+    tags:
+      - name: 'ot:site:clear-cache'
+        command: 'ot:site:clear-cache'
+        schedulable: true

+ 1 - 0
ot_admin/Readme.md

@@ -49,4 +49,5 @@ Les commandes disponibles sont:
 | Update an organization | `<typo3_host>/typo3/index.php?route=/otadmin/site/update&organization-id=<organization_id>` |
 | Update an organization | `<typo3_host>/typo3/index.php?route=/otadmin/site/update&organization-id=<organization_id>` |
 | Soft-delete an organization | `<typo3_host>/typo3/index.php?route=/otadmin/site/delete&organization-id=<organization_id>` |
 | Soft-delete an organization | `<typo3_host>/typo3/index.php?route=/otadmin/site/delete&organization-id=<organization_id>` |
 | Restore a soft-deleted organization | `<typo3_host>/typo3/index.php?route=/otadmin/site/undelete&organization-id=<organization_id>` |
 | Restore a soft-deleted organization | `<typo3_host>/typo3/index.php?route=/otadmin/site/undelete&organization-id=<organization_id>` |
+| Clear the website's cache | `<typo3_host>/typo3/index.php?route=/otadmin/site/clear-cache&organization-id=<organization_id>` |