diff --git a/composer.json b/composer.json index 677e1eb..ee8b19a 100755 --- a/composer.json +++ b/composer.json @@ -40,7 +40,6 @@ }, "branch-alias": { "dev-master": "2.0-dev", - "dev-1.x": "1.2-dev" } } } diff --git a/src/WorkflowGui/Controller/WorkflowController.php b/src/WorkflowGui/Controller/WorkflowController.php index 2ccc83a..b418994 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; @@ -28,7 +29,6 @@ use Symfony\Component\HttpKernel\KernelInterface; use Symfony\Component\Process\Process; use Symfony\Component\Security\Core\Exception\AccessDeniedException; -use Symfony\Component\Yaml\Yaml; use Youwe\Pimcore\WorkflowGui\Repository\WorkflowRepositoryInterface; use Youwe\Pimcore\WorkflowGui\Resolver\ConfigFileResolverInterface; @@ -37,15 +37,18 @@ class WorkflowController extends AdminController protected WorkflowRepositoryInterface $repository; protected ConfigFileResolverInterface $configResolver; protected KernelInterface $kernel; + protected CacheClearer $cacheClearer; public function __construct( WorkflowRepositoryInterface $repository, ConfigFileResolverInterface $configFileResolver, - KernelInterface $kernel + KernelInterface $kernel, + CacheClearer $cacheClearer ) { $this->repository = $repository; $this->configResolver = $configFileResolver; $this->kernel = $kernel; + $this->cacheClearer = $cacheClearer; } public function listAction(): JsonResponse @@ -99,14 +102,11 @@ public function cloneAction(Request $request): JsonResponse ]); } - $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]); } @@ -140,17 +140,15 @@ public function saveAction(Request $request): JsonResponse 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); @@ -163,15 +161,13 @@ public function deleteAction(Request $request): JsonResponse $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 b79ab45..3de0280 100644 --- a/src/WorkflowGui/Repository/WorkflowRepository.php +++ b/src/WorkflowGui/Repository/WorkflowRepository.php @@ -49,11 +49,16 @@ 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); + } + protected function processConfiguration(): array { - $config = Yaml::parse( - file_get_contents($this->configFileResolver->getConfigPath()) - ); + $config = $this->loadConfig(); $configuration = new Configuration(); $processor = new Processor(); @@ -62,4 +67,19 @@ protected function processConfiguration(): array 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 8255652..cad312d 100644 --- a/src/WorkflowGui/Repository/WorkflowRepositoryInterface.php +++ b/src/WorkflowGui/Repository/WorkflowRepositoryInterface.php @@ -20,4 +20,5 @@ interface WorkflowRepositoryInterface { public function findAll(): array; public function find($id): array; + public function updateConfig(callable $workflowsRewriter): void; } diff --git a/src/WorkflowGui/Resources/config/services.yml b/src/WorkflowGui/Resources/config/services.yml index 707ea02..d302720 100644 --- a/src/WorkflowGui/Resources/config/services.yml +++ b/src/WorkflowGui/Resources/config/services.yml @@ -15,6 +15,7 @@ services: - '@Youwe\Pimcore\WorkflowGui\Repository\WorkflowRepository' - '@Youwe\Pimcore\WorkflowGui\Resolver\ConfigFileResolver' - '@kernel' + - '@Pimcore\Cache\Symfony\CacheClearer' tags: - { name: controller.service_arguments } calls: