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@0
|
14 use Symfony\Component\DependencyInjection\Container;
|
Chris@0
|
15 use Symfony\Component\DependencyInjection\Exception\BadMethodCallException;
|
Chris@0
|
16 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
Chris@0
|
17 use Symfony\Component\Config\Resource\FileResource;
|
Chris@0
|
18 use Symfony\Component\DependencyInjection\ContainerBuilder;
|
Chris@0
|
19 use Symfony\Component\Config\Definition\Processor;
|
Chris@0
|
20 use Symfony\Component\Config\Definition\ConfigurationInterface;
|
Chris@0
|
21
|
Chris@0
|
22 /**
|
Chris@0
|
23 * Provides useful features shared by many extensions.
|
Chris@0
|
24 *
|
Chris@0
|
25 * @author Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
26 */
|
Chris@0
|
27 abstract class Extension implements ExtensionInterface, ConfigurationExtensionInterface
|
Chris@0
|
28 {
|
Chris@0
|
29 /**
|
Chris@0
|
30 * {@inheritdoc}
|
Chris@0
|
31 */
|
Chris@0
|
32 public function getXsdValidationBasePath()
|
Chris@0
|
33 {
|
Chris@0
|
34 return false;
|
Chris@0
|
35 }
|
Chris@0
|
36
|
Chris@0
|
37 /**
|
Chris@0
|
38 * {@inheritdoc}
|
Chris@0
|
39 */
|
Chris@0
|
40 public function getNamespace()
|
Chris@0
|
41 {
|
Chris@0
|
42 return 'http://example.org/schema/dic/'.$this->getAlias();
|
Chris@0
|
43 }
|
Chris@0
|
44
|
Chris@0
|
45 /**
|
Chris@0
|
46 * Returns the recommended alias to use in XML.
|
Chris@0
|
47 *
|
Chris@0
|
48 * This alias is also the mandatory prefix to use when using YAML.
|
Chris@0
|
49 *
|
Chris@0
|
50 * This convention is to remove the "Extension" postfix from the class
|
Chris@0
|
51 * name and then lowercase and underscore the result. So:
|
Chris@0
|
52 *
|
Chris@0
|
53 * AcmeHelloExtension
|
Chris@0
|
54 *
|
Chris@0
|
55 * becomes
|
Chris@0
|
56 *
|
Chris@0
|
57 * acme_hello
|
Chris@0
|
58 *
|
Chris@0
|
59 * This can be overridden in a sub-class to specify the alias manually.
|
Chris@0
|
60 *
|
Chris@0
|
61 * @return string The alias
|
Chris@0
|
62 *
|
Chris@0
|
63 * @throws BadMethodCallException When the extension name does not follow conventions
|
Chris@0
|
64 */
|
Chris@0
|
65 public function getAlias()
|
Chris@0
|
66 {
|
Chris@0
|
67 $className = get_class($this);
|
Chris@0
|
68 if (substr($className, -9) != 'Extension') {
|
Chris@0
|
69 throw new BadMethodCallException('This extension does not follow the naming convention; you must overwrite the getAlias() method.');
|
Chris@0
|
70 }
|
Chris@0
|
71 $classBaseName = substr(strrchr($className, '\\'), 1, -9);
|
Chris@0
|
72
|
Chris@0
|
73 return Container::underscore($classBaseName);
|
Chris@0
|
74 }
|
Chris@0
|
75
|
Chris@0
|
76 /**
|
Chris@0
|
77 * {@inheritdoc}
|
Chris@0
|
78 */
|
Chris@0
|
79 public function getConfiguration(array $config, ContainerBuilder $container)
|
Chris@0
|
80 {
|
Chris@0
|
81 $reflected = new \ReflectionClass($this);
|
Chris@0
|
82 $namespace = $reflected->getNamespaceName();
|
Chris@0
|
83
|
Chris@0
|
84 $class = $namespace.'\\Configuration';
|
Chris@0
|
85 if (class_exists($class)) {
|
Chris@0
|
86 $r = new \ReflectionClass($class);
|
Chris@0
|
87 $container->addResource(new FileResource($r->getFileName()));
|
Chris@0
|
88
|
Chris@0
|
89 if (!method_exists($class, '__construct')) {
|
Chris@0
|
90 return new $class();
|
Chris@0
|
91 }
|
Chris@0
|
92 }
|
Chris@0
|
93 }
|
Chris@0
|
94
|
Chris@0
|
95 final protected function processConfiguration(ConfigurationInterface $configuration, array $configs)
|
Chris@0
|
96 {
|
Chris@0
|
97 $processor = new Processor();
|
Chris@0
|
98
|
Chris@0
|
99 return $processor->processConfiguration($configuration, $configs);
|
Chris@0
|
100 }
|
Chris@0
|
101
|
Chris@0
|
102 /**
|
Chris@0
|
103 * @param ContainerBuilder $container
|
Chris@0
|
104 * @param array $config
|
Chris@0
|
105 *
|
Chris@0
|
106 * @return bool Whether the configuration is enabled
|
Chris@0
|
107 *
|
Chris@0
|
108 * @throws InvalidArgumentException When the config is not enableable
|
Chris@0
|
109 */
|
Chris@0
|
110 protected function isConfigEnabled(ContainerBuilder $container, array $config)
|
Chris@0
|
111 {
|
Chris@0
|
112 if (!array_key_exists('enabled', $config)) {
|
Chris@0
|
113 throw new InvalidArgumentException("The config array has no 'enabled' key.");
|
Chris@0
|
114 }
|
Chris@0
|
115
|
Chris@0
|
116 return (bool) $container->getParameterBag()->resolveValue($config['enabled']);
|
Chris@0
|
117 }
|
Chris@0
|
118 }
|