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@0: use Symfony\Component\DependencyInjection\Container; Chris@0: use Symfony\Component\DependencyInjection\Exception\BadMethodCallException; Chris@0: use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; Chris@0: use Symfony\Component\Config\Resource\FileResource; Chris@0: use Symfony\Component\DependencyInjection\ContainerBuilder; Chris@0: use Symfony\Component\Config\Definition\Processor; Chris@0: use Symfony\Component\Config\Definition\ConfigurationInterface; 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@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@0: $className = get_class($this); Chris@0: if (substr($className, -9) != 'Extension') { 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@0: $reflected = new \ReflectionClass($this); Chris@0: $namespace = $reflected->getNamespaceName(); Chris@0: Chris@0: $class = $namespace.'\\Configuration'; Chris@0: if (class_exists($class)) { Chris@0: $r = new \ReflectionClass($class); Chris@0: $container->addResource(new FileResource($r->getFileName())); Chris@0: Chris@0: if (!method_exists($class, '__construct')) { Chris@0: return new $class(); Chris@0: } 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@0: return $processor->processConfiguration($configuration, $configs); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @param ContainerBuilder $container Chris@0: * @param array $config Chris@0: * 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@0: 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: }