Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\DependencyInjection\Extension; Chris@0: Chris@17: use Symfony\Component\Config\Definition\ConfigurationInterface; Chris@17: use Symfony\Component\Config\Definition\Processor; Chris@0: use Symfony\Component\DependencyInjection\Container; Chris@17: use Symfony\Component\DependencyInjection\ContainerBuilder; Chris@0: use Symfony\Component\DependencyInjection\Exception\BadMethodCallException; Chris@0: use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; Chris@0: Chris@0: /** Chris@0: * Provides useful features shared by many extensions. Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: */ Chris@0: abstract class Extension implements ExtensionInterface, ConfigurationExtensionInterface Chris@0: { Chris@17: private $processedConfigs = []; Chris@14: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getXsdValidationBasePath() Chris@0: { Chris@0: return false; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getNamespace() Chris@0: { Chris@0: return 'http://example.org/schema/dic/'.$this->getAlias(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the recommended alias to use in XML. Chris@0: * Chris@0: * This alias is also the mandatory prefix to use when using YAML. Chris@0: * Chris@0: * This convention is to remove the "Extension" postfix from the class Chris@0: * name and then lowercase and underscore the result. So: Chris@0: * Chris@0: * AcmeHelloExtension Chris@0: * Chris@0: * becomes Chris@0: * Chris@0: * acme_hello Chris@0: * Chris@0: * This can be overridden in a sub-class to specify the alias manually. Chris@0: * Chris@0: * @return string The alias Chris@0: * Chris@0: * @throws BadMethodCallException When the extension name does not follow conventions Chris@0: */ Chris@0: public function getAlias() Chris@0: { Chris@17: $className = \get_class($this); Chris@14: if ('Extension' != substr($className, -9)) { Chris@0: throw new BadMethodCallException('This extension does not follow the naming convention; you must overwrite the getAlias() method.'); Chris@0: } Chris@0: $classBaseName = substr(strrchr($className, '\\'), 1, -9); Chris@0: Chris@0: return Container::underscore($classBaseName); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getConfiguration(array $config, ContainerBuilder $container) Chris@0: { Chris@17: $class = \get_class($this); Chris@14: $class = substr_replace($class, '\Configuration', strrpos($class, '\\')); Chris@14: $class = $container->getReflectionClass($class); Chris@14: $constructor = $class ? $class->getConstructor() : null; Chris@0: Chris@14: if ($class && (!$constructor || !$constructor->getNumberOfRequiredParameters())) { Chris@14: return $class->newInstance(); Chris@0: } Chris@0: } Chris@0: Chris@0: final protected function processConfiguration(ConfigurationInterface $configuration, array $configs) Chris@0: { Chris@0: $processor = new Processor(); Chris@0: Chris@14: return $this->processedConfigs[] = $processor->processConfiguration($configuration, $configs); Chris@0: } Chris@0: Chris@0: /** Chris@14: * @internal Chris@14: */ Chris@14: final public function getProcessedConfigs() Chris@14: { Chris@14: try { Chris@14: return $this->processedConfigs; Chris@14: } finally { Chris@17: $this->processedConfigs = []; Chris@14: } Chris@14: } Chris@14: Chris@14: /** Chris@0: * @return bool Whether the configuration is enabled Chris@0: * Chris@0: * @throws InvalidArgumentException When the config is not enableable Chris@0: */ Chris@0: protected function isConfigEnabled(ContainerBuilder $container, array $config) Chris@0: { Chris@18: if (!\array_key_exists('enabled', $config)) { Chris@0: throw new InvalidArgumentException("The config array has no 'enabled' key."); Chris@0: } Chris@0: Chris@0: return (bool) $container->getParameterBag()->resolveValue($config['enabled']); Chris@0: } Chris@0: }