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\Config\Definition\ConfigurationInterface;
15 use Symfony\Component\Config\Definition\Processor;
16 use Symfony\Component\DependencyInjection\Container;
17 use Symfony\Component\DependencyInjection\ContainerBuilder;
18 use Symfony\Component\DependencyInjection\Exception\BadMethodCallException;
19 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
22 * Provides useful features shared by many extensions.
24 * @author Fabien Potencier <fabien@symfony.com>
26 abstract class Extension implements ExtensionInterface, ConfigurationExtensionInterface
28 private $processedConfigs = array();
33 public function getXsdValidationBasePath()
41 public function getNamespace()
43 return 'http://example.org/schema/dic/'.$this->getAlias();
47 * Returns the recommended alias to use in XML.
49 * This alias is also the mandatory prefix to use when using YAML.
51 * This convention is to remove the "Extension" postfix from the class
52 * name and then lowercase and underscore the result. So:
60 * This can be overridden in a sub-class to specify the alias manually.
62 * @return string The alias
64 * @throws BadMethodCallException When the extension name does not follow conventions
66 public function getAlias()
68 $className = \get_class($this);
69 if ('Extension' != substr($className, -9)) {
70 throw new BadMethodCallException('This extension does not follow the naming convention; you must overwrite the getAlias() method.');
72 $classBaseName = substr(strrchr($className, '\\'), 1, -9);
74 return Container::underscore($classBaseName);
80 public function getConfiguration(array $config, ContainerBuilder $container)
82 $class = \get_class($this);
83 $class = substr_replace($class, '\Configuration', strrpos($class, '\\'));
84 $class = $container->getReflectionClass($class);
85 $constructor = $class ? $class->getConstructor() : null;
87 if ($class && (!$constructor || !$constructor->getNumberOfRequiredParameters())) {
88 return $class->newInstance();
92 final protected function processConfiguration(ConfigurationInterface $configuration, array $configs)
94 $processor = new Processor();
96 return $this->processedConfigs[] = $processor->processConfiguration($configuration, $configs);
102 final public function getProcessedConfigs()
105 return $this->processedConfigs;
107 $this->processedConfigs = array();
112 * @return bool Whether the configuration is enabled
114 * @throws InvalidArgumentException When the config is not enableable
116 protected function isConfigEnabled(ContainerBuilder $container, array $config)
118 if (!array_key_exists('enabled', $config)) {
119 throw new InvalidArgumentException("The config array has no 'enabled' key.");
122 return (bool) $container->getParameterBag()->resolveValue($config['enabled']);