|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* (c) 2019, Wesley O. Nichols |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Wesnick\WorkflowBundle\DependencyInjection\Compiler; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
|
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
16
|
|
|
use Symfony\Component\Workflow\SupportStrategy\InstanceOfSupportStrategy; |
|
17
|
|
|
use Wesnick\WorkflowBundle\EventListener\SubjectValidatorListener; |
|
18
|
|
|
use Wesnick\WorkflowBundle\EventListener\WorkflowOperationListener; |
|
19
|
|
|
use Wesnick\WorkflowBundle\Validation\WorkflowValidationStrategyInterface; |
|
20
|
|
|
use Wesnick\WorkflowBundle\WorkflowActionGenerator; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class WorkflowPass. |
|
24
|
|
|
* |
|
25
|
|
|
* @author Wesley O. Nichols <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
class WorkflowPass implements CompilerPassInterface |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* {@inheritdoc} |
|
31
|
|
|
*/ |
|
32
|
|
|
public function process(ContainerBuilder $container) |
|
33
|
|
|
{ |
|
34
|
|
|
if (!$container->hasDefinition('workflow.registry')) { |
|
35
|
|
|
return; |
|
36
|
|
|
} |
|
37
|
|
|
// @TODO: not sure if required to add Models to resource class directories. |
|
38
|
|
|
// $directories = $container->getParameter('api_platform.resource_class_directories'); |
|
39
|
|
|
// $directories[] = realpath(__DIR__.'/../../Model'); |
|
40
|
|
|
// $container->setParameter('api_platform.resource_class_directories', $directories); |
|
41
|
|
|
|
|
42
|
|
|
$config = $container->getExtensionConfig('wesnick_workflow'); |
|
43
|
|
|
|
|
44
|
|
|
$classMap = []; |
|
45
|
|
|
|
|
46
|
|
|
// Iterate over workflows and create services |
|
47
|
|
|
foreach ($this->workflowGenerator($container) as [$workflow, $supportStrategy]) { |
|
48
|
|
|
// only support InstanceOfSupportStrategy for now |
|
49
|
|
|
if (InstanceOfSupportStrategy::class !== $supportStrategy->getClass()) { |
|
50
|
|
|
throw new \RuntimeException(sprintf('Wesnick Workflow Bundle requires use of InstanceOfSupportStrategy, workflow %s is using strategy %s', (string) $workflow, $supportStrategy->getClass())); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$className = $supportStrategy->getArgument(0); |
|
54
|
|
|
$workflowShortName = $workflow->getArgument(3); |
|
55
|
|
|
$classMap[$className][] = $workflowShortName; |
|
56
|
|
|
|
|
57
|
|
|
if ($config[0]['workflow_validation_guard']) { |
|
58
|
|
|
$container |
|
59
|
|
|
->getDefinition(SubjectValidatorListener::class) |
|
60
|
|
|
->addTag('kernel.event_listener', ['event' => 'workflow.'.$workflowShortName.'.guard', 'method' => 'onGuard']); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$container->getDefinition(WorkflowActionGenerator::class)->setArgument('$enabledWorkflowMap', $classMap); |
|
65
|
|
|
$container->getDefinition(WorkflowOperationListener::class)->setArgument('$enabledWorkflowMap', $classMap); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
private function workflowGenerator(ContainerBuilder $container): \Generator |
|
69
|
|
|
{ |
|
70
|
|
|
$registry = $container->getDefinition('workflow.registry'); |
|
71
|
|
|
foreach ($registry->getMethodCalls() as $call) { |
|
72
|
|
|
[, [$workflowReference, $supportStrategy]] = $call; |
|
73
|
|
|
yield [$container->getDefinition($workflowReference), $supportStrategy]; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|