Skip to content

Commit

Permalink
Merge pull request #63 from youwe-petervanderwal/chore/clear-cache-af…
Browse files Browse the repository at this point in the history
…ter-save

Chore: clear cache after save
  • Loading branch information
dpfaffenbauer authored Aug 6, 2021
2 parents 983c70d + bb92499 commit 6c3b2f4
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 32 deletions.
59 changes: 30 additions & 29 deletions src/WorkflowGui/Controller/WorkflowController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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]);
}
Expand Down Expand Up @@ -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);

Expand All @@ -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]);
}
Expand Down
26 changes: 23 additions & 3 deletions src/WorkflowGui/Repository/WorkflowRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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)
);
}
}
5 changes: 5 additions & 0 deletions src/WorkflowGui/Repository/WorkflowRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ public function findAll();
* @return array
*/
public function find($id);

/**
* @param callable $workflowsRewriter
*/
public function updateConfig(callable $workflowsRewriter): void;
}
1 change: 1 addition & 0 deletions src/WorkflowGui/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

0 comments on commit 6c3b2f4

Please sign in to comment.