Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\DependencyInjection\Extension;
|
Chris@0
|
13
|
Chris@17
|
14 use Symfony\Component\Config\Definition\ConfigurationInterface;
|
Chris@17
|
15 use Symfony\Component\Config\Definition\Processor;
|
Chris@0
|
16 use Symfony\Component\DependencyInjection\Container;
|
Chris@17
|
17 use Symfony\Component\DependencyInjection\ContainerBuilder;
|
Chris@0
|
18 use Symfony\Component\DependencyInjection\Exception\BadMethodCallException;
|
Chris@0
|
19 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
Chris@0
|
20
|
Chris@0
|
21 /**
|
Chris@0
|
22 * Provides useful features shared by many extensions.
|
Chris@0
|
23 *
|
Chris@0
|
24 * @author Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
25 */
|
Chris@0
|
26 abstract class Extension implements ExtensionInterface, ConfigurationExtensionInterface
|
Chris@0
|
27 {
|
Chris@17
|
28 private $processedConfigs = [];
|
Chris@14
|
29
|
Chris@0
|
30 /**
|
Chris@0
|
31 * {@inheritdoc}
|
Chris@0
|
32 */
|
Chris@0
|
33 public function getXsdValidationBasePath()
|
Chris@0
|
34 {
|
Chris@0
|
35 return false;
|
Chris@0
|
36 }
|
Chris@0
|
37
|
Chris@0
|
38 /**
|
Chris@0
|
39 * {@inheritdoc}
|
Chris@0
|
40 */
|
Chris@0
|
41 public function getNamespace()
|
Chris@0
|
42 {
|
Chris@0
|
43 return 'http://example.org/schema/dic/'.$this->getAlias();
|
Chris@0
|
44 }
|
Chris@0
|
45
|
Chris@0
|
46 /**
|
Chris@0
|
47 * Returns the recommended alias to use in XML.
|
Chris@0
|
48 *
|
Chris@0
|
49 * This alias is also the mandatory prefix to use when using YAML.
|
Chris@0
|
50 *
|
Chris@0
|
51 * This convention is to remove the "Extension" postfix from the class
|
Chris@0
|
52 * name and then lowercase and underscore the result. So:
|
Chris@0
|
53 *
|
Chris@0
|
54 * AcmeHelloExtension
|
Chris@0
|
55 *
|
Chris@0
|
56 * becomes
|
Chris@0
|
57 *
|
Chris@0
|
58 * acme_hello
|
Chris@0
|
59 *
|
Chris@0
|
60 * This can be overridden in a sub-class to specify the alias manually.
|
Chris@0
|
61 *
|
Chris@0
|
62 * @return string The alias
|
Chris@0
|
63 *
|
Chris@0
|
64 * @throws BadMethodCallException When the extension name does not follow conventions
|
Chris@0
|
65 */
|
Chris@0
|
66 public function getAlias()
|
Chris@0
|
67 {
|
Chris@17
|
68 $className = \get_class($this);
|
Chris@14
|
69 if ('Extension' != substr($className, -9)) {
|
Chris@0
|
70 throw new BadMethodCallException('This extension does not follow the naming convention; you must overwrite the getAlias() method.');
|
Chris@0
|
71 }
|
Chris@0
|
72 $classBaseName = substr(strrchr($className, '\\'), 1, -9);
|
Chris@0
|
73
|
Chris@0
|
74 return Container::underscore($classBaseName);
|
Chris@0
|
75 }
|
Chris@0
|
76
|
Chris@0
|
77 /**
|
Chris@0
|
78 * {@inheritdoc}
|
Chris@0
|
79 */
|
Chris@0
|
80 public function getConfiguration(array $config, ContainerBuilder $container)
|
Chris@0
|
81 {
|
Chris@17
|
82 $class = \get_class($this);
|
Chris@14
|
83 $class = substr_replace($class, '\Configuration', strrpos($class, '\\'));
|
Chris@14
|
84 $class = $container->getReflectionClass($class);
|
Chris@14
|
85 $constructor = $class ? $class->getConstructor() : null;
|
Chris@0
|
86
|
Chris@14
|
87 if ($class && (!$constructor || !$constructor->getNumberOfRequiredParameters())) {
|
Chris@14
|
88 return $class->newInstance();
|
Chris@0
|
89 }
|
Chris@0
|
90 }
|
Chris@0
|
91
|
Chris@0
|
92 final protected function processConfiguration(ConfigurationInterface $configuration, array $configs)
|
Chris@0
|
93 {
|
Chris@0
|
94 $processor = new Processor();
|
Chris@0
|
95
|
Chris@14
|
96 return $this->processedConfigs[] = $processor->processConfiguration($configuration, $configs);
|
Chris@0
|
97 }
|
Chris@0
|
98
|
Chris@0
|
99 /**
|
Chris@14
|
100 * @internal
|
Chris@14
|
101 */
|
Chris@14
|
102 final public function getProcessedConfigs()
|
Chris@14
|
103 {
|
Chris@14
|
104 try {
|
Chris@14
|
105 return $this->processedConfigs;
|
Chris@14
|
106 } finally {
|
Chris@17
|
107 $this->processedConfigs = [];
|
Chris@14
|
108 }
|
Chris@14
|
109 }
|
Chris@14
|
110
|
Chris@14
|
111 /**
|
Chris@0
|
112 * @return bool Whether the configuration is enabled
|
Chris@0
|
113 *
|
Chris@0
|
114 * @throws InvalidArgumentException When the config is not enableable
|
Chris@0
|
115 */
|
Chris@0
|
116 protected function isConfigEnabled(ContainerBuilder $container, array $config)
|
Chris@0
|
117 {
|
Chris@18
|
118 if (!\array_key_exists('enabled', $config)) {
|
Chris@0
|
119 throw new InvalidArgumentException("The config array has no 'enabled' key.");
|
Chris@0
|
120 }
|
Chris@0
|
121
|
Chris@0
|
122 return (bool) $container->getParameterBag()->resolveValue($config['enabled']);
|
Chris@0
|
123 }
|
Chris@0
|
124 }
|