Skip to content

Commit 6c3b2f4

Browse files
Merge pull request #63 from youwe-petervanderwal/chore/clear-cache-after-save
Chore: clear cache after save
2 parents 983c70d + bb92499 commit 6c3b2f4

File tree

4 files changed

+59
-32
lines changed

4 files changed

+59
-32
lines changed

src/WorkflowGui/Controller/WorkflowController.php

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
use Pimcore\Bundle\AdminBundle\Controller\AdminController;
2020
use Pimcore\Bundle\CoreBundle\DependencyInjection\Configuration;
21+
use Pimcore\Cache\Symfony\CacheClearer;
2122
use Pimcore\Model\User;
2223
use Pimcore\Tool\Console;
2324
use Symfony\Component\Config\Definition\Processor;
@@ -26,7 +27,6 @@
2627
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2728
use Symfony\Component\HttpKernel\KernelInterface;
2829
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
29-
use Symfony\Component\Yaml\Yaml;
3030
use Youwe\Pimcore\WorkflowGui\Repository\WorkflowRepositoryInterface;
3131
use Youwe\Pimcore\WorkflowGui\Resolver\ConfigFileResolver;
3232

@@ -47,19 +47,27 @@ class WorkflowController extends AdminController
4747
*/
4848
protected $kernel;
4949

50+
/**
51+
* @var CacheClearer
52+
*/
53+
protected $cacheClearer;
54+
5055
/**
5156
* @param WorkflowRepositoryInterface $repository
5257
* @param ConfigFileResolver $configFileResolver
5358
* @param KernelInterface $kernel
59+
* @param CacheClearer $cacheClearer
5460
*/
5561
public function __construct(
5662
WorkflowRepositoryInterface $repository,
5763
ConfigFileResolver $configFileResolver,
58-
KernelInterface $kernel
64+
KernelInterface $kernel,
65+
CacheClearer $cacheClearer
5966
) {
6067
$this->repository = $repository;
6168
$this->configResolver = $configFileResolver;
6269
$this->kernel = $kernel;
70+
$this->cacheClearer = $cacheClearer;
6371
}
6472

6573
/**
@@ -122,14 +130,11 @@ public function cloneAction(Request $request)
122130
return $this->json(['success' => false, 'message' => $this->trans('workflow_gui_workflow_with_name_already_exists')]);
123131
}
124132

125-
$configPath = $this->configResolver->getConfigPath();
126-
127-
$contents = Yaml::parseFile($configPath);
128-
$newWorkflow = $contents['pimcore']['workflows'][$id];
129-
130-
$contents['pimcore']['workflows'][$name] = $newWorkflow;
131-
132-
file_put_contents($configPath, Yaml::dump($contents, 100));
133+
$this->repository->updateConfig(function (array $workflows) use ($id, $name): array {
134+
$workflows[$name] = $workflows[$id];
135+
return $workflows;
136+
});
137+
$this->cacheClearer->clear($this->kernel->getEnvironment());
133138

134139
return $this->json(['success' => true, 'id' => $name]);
135140
}
@@ -167,17 +172,15 @@ public function saveAction(Request $request)
167172
return $this->json(['success' => false, 'message' => $ex->getMessage()]);
168173
}
169174

170-
$configPath = $this->configResolver->getConfigPath();
171-
172-
$contents = Yaml::parseFile($configPath);
173-
174-
if (isset($contents['pimcore']['workflows'][$id])) {
175-
unset($contents['pimcore']['workflows'][$id]);
176-
}
177-
178-
$contents['pimcore']['workflows'][$newId] = $newConfiguration;
175+
$this->repository->updateConfig(function (array $workflows) use ($id, $newId, $newConfiguration): array {
176+
if (isset($workflows[$id])) {
177+
unset($workflows[$id]);
178+
}
179179

180-
file_put_contents($configPath, Yaml::dump($contents, 100));
180+
$workflows[$newId] = $newConfiguration;
181+
return $workflows;
182+
});
183+
$this->cacheClearer->clear($this->kernel->getEnvironment());
181184

182185
$workflow = $this->repository->find($id);
183186

@@ -194,15 +197,13 @@ public function deleteAction(Request $request)
194197

195198
$id = $request->get('id');
196199

197-
$configPath = $this->configResolver->getConfigPath();
198-
199-
$contents = Yaml::parseFile($configPath);
200-
201-
if (isset($contents['pimcore']['workflows'][$id])) {
202-
unset($contents['pimcore']['workflows'][$id]);
203-
}
204-
205-
file_put_contents($configPath, Yaml::dump($contents, 100));
200+
$this->repository->updateConfig(function (array $workflows) use ($id): array {
201+
if (isset($workflows[$id])) {
202+
unset($workflows[$id]);
203+
}
204+
return $workflows;
205+
});
206+
$this->cacheClearer->clear($this->kernel->getEnvironment());
206207

207208
return $this->json(['success' => true]);
208209
}

src/WorkflowGui/Repository/WorkflowRepository.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,20 @@ function ($key) use ($id) {
6161
return reset($filtered);
6262
}
6363

64+
public function updateConfig(callable $workflowsRewriter): void
65+
{
66+
$config = $this->loadConfig();
67+
$config['pimcore']['workflows'] = $workflowsRewriter($config['pimcore']['workflows']);
68+
$this->storeConfig($config);
69+
}
70+
6471
/**
6572
* @param string $configFile
6673
* @return array
6774
*/
6875
protected function processConfiguration()
6976
{
70-
$config = Yaml::parse(
71-
file_get_contents($this->configFileResolver->getConfigPath())
72-
);
77+
$config = $this->loadConfig();
7378

7479
$configuration = new Configuration();
7580
$processor = new Processor();
@@ -78,4 +83,19 @@ protected function processConfiguration()
7883

7984
return $config['workflows'];
8085
}
86+
87+
protected function loadConfig(): array
88+
{
89+
return Yaml::parse(
90+
file_get_contents($this->configFileResolver->getConfigPath())
91+
);
92+
}
93+
94+
protected function storeConfig(array $config): void
95+
{
96+
file_put_contents(
97+
$this->configFileResolver->getConfigPath(),
98+
Yaml::dump($config, 100)
99+
);
100+
}
81101
}

src/WorkflowGui/Repository/WorkflowRepositoryInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,9 @@ public function findAll();
2828
* @return array
2929
*/
3030
public function find($id);
31+
32+
/**
33+
* @param callable $workflowsRewriter
34+
*/
35+
public function updateConfig(callable $workflowsRewriter): void;
3136
}

src/WorkflowGui/Resources/config/services.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ services:
1515
- '@Youwe\Pimcore\WorkflowGui\Repository\WorkflowRepository'
1616
- '@Youwe\Pimcore\WorkflowGui\Resolver\ConfigFileResolver'
1717
- '@kernel'
18+
- '@Pimcore\Cache\Symfony\CacheClearer'
1819
tags:
1920
- { name: controller.service_arguments }

0 commit comments

Comments
 (0)