4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DependencyInjection\Extension;
14 use Symfony\Component\DependencyInjection\Container;
15 use Symfony\Component\DependencyInjection\Exception\BadMethodCallException;
16 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
17 use Symfony\Component\Config\Resource\FileResource;
18 use Symfony\Component\DependencyInjection\ContainerBuilder;
19 use Symfony\Component\Config\Definition\Processor;
20 use Symfony\Component\Config\Definition\ConfigurationInterface;
23 * Provides useful features shared by many extensions.
25 * @author Fabien Potencier <fabien@symfony.com>
27 abstract class Extension implements ExtensionInterface, ConfigurationExtensionInterface
32 public function getXsdValidationBasePath()
40 public function getNamespace()
42 return 'http://example.org/schema/dic/'.$this->getAlias();
46 * Returns the recommended alias to use in XML.
48 * This alias is also the mandatory prefix to use when using YAML.
50 * This convention is to remove the "Extension" postfix from the class
51 * name and then lowercase and underscore the result. So:
59 * This can be overridden in a sub-class to specify the alias manually.
61 * @return string The alias
63 * @throws BadMethodCallException When the extension name does not follow conventions
65 public function getAlias()
67 $className = get_class($this);
68 if (substr($className, -9) != 'Extension') {
69 throw new BadMethodCallException('This extension does not follow the naming convention; you must overwrite the getAlias() method.');
71 $classBaseName = substr(strrchr($className, '\\'), 1, -9);
73 return Container::underscore($classBaseName);
79 public function getConfiguration(array $config, ContainerBuilder $container)
81 $reflected = new \ReflectionClass($this);
82 $namespace = $reflected->getNamespaceName();
84 $class = $namespace.'\\Configuration';
85 if (class_exists($class)) {
86 $r = new \ReflectionClass($class);
87 $container->addResource(new FileResource($r->getFileName()));
89 if (!method_exists($class, '__construct')) {
95 final protected function processConfiguration(ConfigurationInterface $configuration, array $configs)
97 $processor = new Processor();
99 return $processor->processConfiguration($configuration, $configs);
103 * @param ContainerBuilder $container
104 * @param array $config
106 * @return bool Whether the configuration is enabled
108 * @throws InvalidArgumentException When the config is not enableable
110 protected function isConfigEnabled(ContainerBuilder $container, array $config)
112 if (!array_key_exists('enabled', $config)) {
113 throw new InvalidArgumentException("The config array has no 'enabled' key.");
116 return (bool) $container->getParameterBag()->resolveValue($config['enabled']);