From 6d11954c0ba6be49f9b26a499440270a8efb6e8a Mon Sep 17 00:00:00 2001 From: Peter van der Wal Date: Fri, 23 Jul 2021 15:09:40 +0200 Subject: [PATCH 1/3] fix: force environment in workflow visualization console command Error: The workflow visualization didn't work on our client-acceptance environment Caused by: Wrong Pimcore environment was used in the command used to build the visualization Resolved by: Explicitly passing the enviroment in the console command call Cherry-picks 647b72f7cfeb394d70eb11ce809acdad9d87549c to Pimcore 6 --- .../Controller/WorkflowController.php | 22 ++++++++++++++----- src/WorkflowGui/Resources/config/services.yml | 1 + 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/WorkflowGui/Controller/WorkflowController.php b/src/WorkflowGui/Controller/WorkflowController.php index 062a0a4..ccccc66 100644 --- a/src/WorkflowGui/Controller/WorkflowController.php +++ b/src/WorkflowGui/Controller/WorkflowController.php @@ -21,10 +21,11 @@ use Pimcore\Model\User; use Pimcore\Tool\Console; use Symfony\Component\Config\Definition\Processor; -use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\Security\Core\Exception\AccessDeniedException; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; 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; @@ -41,14 +42,24 @@ class WorkflowController extends AdminController */ protected $configResolver; + /** + * @var KernelInterface + */ + protected $kernel; + /** * @param WorkflowRepositoryInterface $repository * @param ConfigFileResolver $configFileResolver + * @param KernelInterface $kernel */ - public function __construct(WorkflowRepositoryInterface $repository, ConfigFileResolver $configFileResolver) - { + public function __construct( + WorkflowRepositoryInterface $repository, + ConfigFileResolver $configFileResolver, + KernelInterface $kernel + ) { $this->repository = $repository; $this->configResolver = $configFileResolver; + $this->kernel = $kernel; } /** @@ -367,7 +378,8 @@ private function getVisualization($workflow, $format) { throw new \Exception('Please install graphviz to visualize workflows'); } - $cliCommand = '"'.Console::getPhpCli().'" "'.PIMCORE_PROJECT_ROOT . '/bin/console" pimcore:workflow:dump '.$workflow.' | "'.$dotExecutable.'" -T'.$format; + $environment = $this->kernel->getEnvironment(); + $cliCommand = '"'.Console::getPhpCli().'" "'.PIMCORE_PROJECT_ROOT . '/bin/console" --env="'.$environment.'" pimcore:workflow:dump '.$workflow.' | "'.$dotExecutable.'" -T'.$format; return Console::exec($cliCommand); } } diff --git a/src/WorkflowGui/Resources/config/services.yml b/src/WorkflowGui/Resources/config/services.yml index f8da75c..44c4417 100644 --- a/src/WorkflowGui/Resources/config/services.yml +++ b/src/WorkflowGui/Resources/config/services.yml @@ -14,5 +14,6 @@ services: arguments: - '@Youwe\Pimcore\WorkflowGui\Repository\WorkflowRepository' - '@Youwe\Pimcore\WorkflowGui\Resolver\ConfigFileResolver' + - '@kernel' tags: - { name: controller.service_arguments } From 983c70da17c58e4b7fcc9e1cf4d9cb13aacad378 Mon Sep 17 00:00:00 2001 From: Dominik Date: Mon, 26 Jul 2021 15:30:53 +0200 Subject: [PATCH 2/3] Update composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index a357041..8fca3e0 100755 --- a/composer.json +++ b/composer.json @@ -33,7 +33,7 @@ ] }, "branch-alias": { - "dev-master": "1.2-dev" + "dev-1.x": "1.2-dev" } } } 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 3/3] 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 }