From bb92499bc9fdc312fc016819156de9bc97710e8e Mon Sep 17 00:00:00 2001 From: Peter van der Wal Date: Wed, 28 Jul 2021 16:11:49 +0200 Subject: [PATCH] chore: clear cache on workflow changes --- .../Controller/WorkflowController.php | 59 ++++++++++--------- .../Repository/WorkflowRepository.php | 26 +++++++- .../WorkflowRepositoryInterface.php | 5 ++ src/WorkflowGui/Resources/config/services.yml | 1 + 4 files changed, 59 insertions(+), 32 deletions(-) diff --git a/src/WorkflowGui/Controller/WorkflowController.php b/src/WorkflowGui/Controller/WorkflowController.php index ccccc66..d3f98f7 100644 --- a/src/WorkflowGui/Controller/WorkflowController.php +++ b/src/WorkflowGui/Controller/WorkflowController.php @@ -18,6 +18,7 @@ use Pimcore\Bundle\AdminBundle\Controller\AdminController; use Pimcore\Bundle\CoreBundle\DependencyInjection\Configuration; +use Pimcore\Cache\Symfony\CacheClearer; use Pimcore\Model\User; use Pimcore\Tool\Console; use Symfony\Component\Config\Definition\Processor; @@ -26,7 +27,6 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpKernel\KernelInterface; use Symfony\Component\Security\Core\Exception\AccessDeniedException; -use Symfony\Component\Yaml\Yaml; use Youwe\Pimcore\WorkflowGui\Repository\WorkflowRepositoryInterface; use Youwe\Pimcore\WorkflowGui\Resolver\ConfigFileResolver; @@ -47,19 +47,27 @@ class WorkflowController extends AdminController */ protected $kernel; + /** + * @var CacheClearer + */ + protected $cacheClearer; + /** * @param WorkflowRepositoryInterface $repository * @param ConfigFileResolver $configFileResolver * @param KernelInterface $kernel + * @param CacheClearer $cacheClearer */ public function __construct( WorkflowRepositoryInterface $repository, ConfigFileResolver $configFileResolver, - KernelInterface $kernel + KernelInterface $kernel, + CacheClearer $cacheClearer ) { $this->repository = $repository; $this->configResolver = $configFileResolver; $this->kernel = $kernel; + $this->cacheClearer = $cacheClearer; } /** @@ -122,14 +130,11 @@ public function cloneAction(Request $request) return $this->json(['success' => false, 'message' => $this->trans('workflow_gui_workflow_with_name_already_exists')]); } - $configPath = $this->configResolver->getConfigPath(); - - $contents = Yaml::parseFile($configPath); - $newWorkflow = $contents['pimcore']['workflows'][$id]; - - $contents['pimcore']['workflows'][$name] = $newWorkflow; - - file_put_contents($configPath, Yaml::dump($contents, 100)); + $this->repository->updateConfig(function (array $workflows) use ($id, $name): array { + $workflows[$name] = $workflows[$id]; + return $workflows; + }); + $this->cacheClearer->clear($this->kernel->getEnvironment()); return $this->json(['success' => true, 'id' => $name]); } @@ -167,17 +172,15 @@ public function saveAction(Request $request) return $this->json(['success' => false, 'message' => $ex->getMessage()]); } - $configPath = $this->configResolver->getConfigPath(); - - $contents = Yaml::parseFile($configPath); - - if (isset($contents['pimcore']['workflows'][$id])) { - unset($contents['pimcore']['workflows'][$id]); - } - - $contents['pimcore']['workflows'][$newId] = $newConfiguration; + $this->repository->updateConfig(function (array $workflows) use ($id, $newId, $newConfiguration): array { + if (isset($workflows[$id])) { + unset($workflows[$id]); + } - file_put_contents($configPath, Yaml::dump($contents, 100)); + $workflows[$newId] = $newConfiguration; + return $workflows; + }); + $this->cacheClearer->clear($this->kernel->getEnvironment()); $workflow = $this->repository->find($id); @@ -194,15 +197,13 @@ public function deleteAction(Request $request) $id = $request->get('id'); - $configPath = $this->configResolver->getConfigPath(); - - $contents = Yaml::parseFile($configPath); - - if (isset($contents['pimcore']['workflows'][$id])) { - unset($contents['pimcore']['workflows'][$id]); - } - - file_put_contents($configPath, Yaml::dump($contents, 100)); + $this->repository->updateConfig(function (array $workflows) use ($id): array { + if (isset($workflows[$id])) { + unset($workflows[$id]); + } + return $workflows; + }); + $this->cacheClearer->clear($this->kernel->getEnvironment()); return $this->json(['success' => true]); } diff --git a/src/WorkflowGui/Repository/WorkflowRepository.php b/src/WorkflowGui/Repository/WorkflowRepository.php index 85c1b0c..29750eb 100644 --- a/src/WorkflowGui/Repository/WorkflowRepository.php +++ b/src/WorkflowGui/Repository/WorkflowRepository.php @@ -61,15 +61,20 @@ function ($key) use ($id) { return reset($filtered); } + public function updateConfig(callable $workflowsRewriter): void + { + $config = $this->loadConfig(); + $config['pimcore']['workflows'] = $workflowsRewriter($config['pimcore']['workflows']); + $this->storeConfig($config); + } + /** * @param string $configFile * @return array */ protected function processConfiguration() { - $config = Yaml::parse( - file_get_contents($this->configFileResolver->getConfigPath()) - ); + $config = $this->loadConfig(); $configuration = new Configuration(); $processor = new Processor(); @@ -78,4 +83,19 @@ protected function processConfiguration() return $config['workflows']; } + + protected function loadConfig(): array + { + return Yaml::parse( + file_get_contents($this->configFileResolver->getConfigPath()) + ); + } + + protected function storeConfig(array $config): void + { + file_put_contents( + $this->configFileResolver->getConfigPath(), + Yaml::dump($config, 100) + ); + } } diff --git a/src/WorkflowGui/Repository/WorkflowRepositoryInterface.php b/src/WorkflowGui/Repository/WorkflowRepositoryInterface.php index 0dafc59..cb08622 100644 --- a/src/WorkflowGui/Repository/WorkflowRepositoryInterface.php +++ b/src/WorkflowGui/Repository/WorkflowRepositoryInterface.php @@ -28,4 +28,9 @@ public function findAll(); * @return array */ public function find($id); + + /** + * @param callable $workflowsRewriter + */ + public function updateConfig(callable $workflowsRewriter): void; } diff --git a/src/WorkflowGui/Resources/config/services.yml b/src/WorkflowGui/Resources/config/services.yml index 44c4417..db805f3 100644 --- a/src/WorkflowGui/Resources/config/services.yml +++ b/src/WorkflowGui/Resources/config/services.yml @@ -15,5 +15,6 @@ services: - '@Youwe\Pimcore\WorkflowGui\Repository\WorkflowRepository' - '@Youwe\Pimcore\WorkflowGui\Resolver\ConfigFileResolver' - '@kernel' + - '@Pimcore\Cache\Symfony\CacheClearer' tags: - { name: controller.service_arguments }