1 | <?php |
||
9 | abstract class BasePackagesCompilerPass extends BaseCompilerPass |
||
10 | { |
||
11 | abstract protected function getTaggedPackages($container); |
||
13 | |||
14 | 12 | public function process(ContainerBuilder $container) |
|
15 | { |
||
16 | 12 | $packages = array(); |
|
17 | 12 | $registeredPackages = $this->getRegisteredPackages($container); |
|
18 | |||
19 | 12 | foreach ($this->getTaggedPackages($container) as $id => $tags) { |
|
20 | 5 | if (empty($tags) || !isset($tags[0]['alias'])) { |
|
21 | throw new \LogicException( |
||
22 | "The tag for the service with id '$id' must define an 'alias' attribute" |
||
|
|||
23 | ); |
||
24 | } |
||
25 | |||
26 | 5 | $packageName = $tags[0]['alias']; |
|
27 | |||
28 | 5 | if (isset($registeredPackages[$packageName])) { |
|
29 | throw new \LogicException( |
||
30 | "A package named '$packageName' has already been registered" |
||
31 | ); |
||
32 | } |
||
33 | |||
34 | 5 | if (isset($packages[$packageName])) { |
|
35 | throw new \LogicException( |
||
36 | "Multiple packages were found with alias '$packageName'. Package alias' must be unique" |
||
37 | ); |
||
38 | } |
||
39 | |||
40 | 5 | $packages[$packageName] = $id; |
|
41 | } |
||
42 | |||
43 | 12 | $this->addPackages($packages, $container); |
|
44 | |||
45 | 12 | if ($container->hasDefinition($this->namespaceService('package.fallback'))) { |
|
46 | 11 | $this->setDefaultPackage($container); |
|
47 | } |
||
48 | 12 | } |
|
49 | |||
50 | 12 | protected function addPackages($packages, $container) |
|
61 | |||
62 | 11 | protected function setDefaultPackage($container) |
|
72 | |||
73 | /** |
||
74 | * Retrieve packages that have already been registered. |
||
75 | * |
||
76 | * @return array with the packages' name as keys |
||
77 | */ |
||
78 | 12 | protected function getRegisteredPackages($container) |
|
96 | |||
97 | 11 | protected function getRegisteredDefaultPackage($container) |
|
107 | } |
||
108 |
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.