1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the MiddlewareBundle |
5
|
|
|
* |
6
|
|
|
* (c) Indra Gunawan <[email protected]> |
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 Indragunawan\MiddlewareBundle\DependencyInjection\Compiler; |
13
|
|
|
|
14
|
|
|
use Indragunawan\MiddlewareBundle\Middleware\MiddlewareInterface; |
15
|
|
|
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument; |
16
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
17
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
18
|
|
|
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; |
19
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author Indra Gunawan <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
final class AddMiddlewarePass implements CompilerPassInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
|
|
public function process(ContainerBuilder $container) |
30
|
|
|
{ |
31
|
|
|
if (!$container->hasDefinition('indragunawan_middleware.middleware')) { |
32
|
|
|
return; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$definition = $container->findDefinition('indragunawan_middleware.middleware'); |
36
|
|
|
foreach ($container->findTaggedServiceIds('indragunawan.middleware') as $id => $attrs) { |
37
|
|
|
foreach ($attrs as $attr) { |
38
|
|
|
$class = $container->getDefinition($id)->getClass(); |
39
|
|
|
if (!$r = $container->getReflectionClass($class)) { |
40
|
|
|
throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id)); |
41
|
|
|
} elseif (!$r->isSubclassOf(MiddlewareInterface::class)) { |
42
|
|
|
throw new InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, MiddlewareInterface::class)); |
43
|
|
|
} elseif (empty($attr['event'])) { |
44
|
|
|
throw new InvalidArgumentException('"event" tag attribute must be set'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$priority = 0; |
48
|
|
|
$name = null; |
49
|
|
|
$supports = forward_static_call([$class, 'supports']); |
50
|
|
|
if (\is_string($supports)) { |
51
|
|
|
$name = $supports; |
52
|
|
|
} elseif (\is_string($supports[0])) { |
53
|
|
|
$name = $supports[0]; |
54
|
|
|
$priority = isset($supports[1]) ? $supports[1] : 0; |
55
|
|
|
} else { |
56
|
|
|
throw InvalidArgumentException(sprintf('Invalid return format "supports" of class "%s".', $class)); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$definition->addMethodCall('addMiddleware', [$attr['event'], $name, new ServiceClosureArgument(new Reference($id)), $priority]); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|