1818
1919use Pimcore \Bundle \AdminBundle \Controller \AdminController ;
2020use Pimcore \Bundle \CoreBundle \DependencyInjection \Configuration ;
21+ use Pimcore \Cache \Symfony \CacheClearer ;
2122use Pimcore \Model \User ;
2223use Pimcore \Tool \Console ;
2324use Symfony \Component \Config \Definition \Processor ;
2627use Symfony \Component \HttpKernel \Exception \NotFoundHttpException ;
2728use Symfony \Component \HttpKernel \KernelInterface ;
2829use Symfony \Component \Security \Core \Exception \AccessDeniedException ;
29- use Symfony \Component \Yaml \Yaml ;
3030use Youwe \Pimcore \WorkflowGui \Repository \WorkflowRepositoryInterface ;
3131use 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 }
0 commit comments