|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the FacadeBundle |
|
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\FacadeBundle\DependencyInjection\Compiler; |
|
13
|
|
|
|
|
14
|
|
|
use Indragunawan\FacadeBundle\AbstractFacade; |
|
15
|
|
|
use Symfony\Component\DependencyInjection\Alias; |
|
16
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
|
17
|
|
|
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass; |
|
18
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
19
|
|
|
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; |
|
20
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @author Indra Gunawan <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
final class AddFacadePass implements CompilerPassInterface |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* {@inheritdoc} |
|
29
|
|
|
*/ |
|
30
|
4 |
|
public function process(ContainerBuilder $container) |
|
31
|
|
|
{ |
|
32
|
4 |
|
$facades = []; |
|
33
|
4 |
|
foreach ($container->findTaggedServiceIds('indragunawan.facade') as $id => $attr) { |
|
34
|
3 |
|
$class = $container->getDefinition($id)->getClass(); |
|
35
|
3 |
|
$class = null !== $class ? $class : $id; |
|
36
|
|
|
|
|
37
|
3 |
|
if (!is_subclass_of($class, AbstractFacade::class)) { |
|
|
|
|
|
|
38
|
1 |
|
throw new InvalidArgumentException(sprintf('The service "%s" must extend AbstractFacade.', $class)); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
2 |
|
$r = new \ReflectionMethod($class, 'getFacadeAccessor'); |
|
42
|
2 |
|
$r->setAccessible(true); |
|
43
|
2 |
|
$ref = $r->invoke(null); |
|
44
|
|
|
|
|
45
|
2 |
|
if (!\is_string($ref)) { |
|
46
|
1 |
|
throw new InvalidArgumentException(sprintf('Facade accessor must be string, "%s" given.', \is_object($ref) ? \get_class($ref) : \gettype($ref))); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
1 |
|
$ref = \is_string($ref) && 0 === strpos($ref, '@') ? substr($ref, 1) : $ref; |
|
50
|
1 |
|
$facades[$id] = new Reference($ref); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
2 |
|
$container->setAlias('indragunawan.facade.container', new Alias(ServiceLocatorTagPass::register($container, $facades), true)); |
|
54
|
2 |
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|