vendor/symfony/dependency-injection/Extension/Extension.php line 105

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\DependencyInjection\Extension;
  11. use Symfony\Component\Config\Definition\ConfigurationInterface;
  12. use Symfony\Component\Config\Definition\Processor;
  13. use Symfony\Component\DependencyInjection\Container;
  14. use Symfony\Component\DependencyInjection\ContainerBuilder;
  15. use Symfony\Component\DependencyInjection\Exception\BadMethodCallException;
  16. use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
  17. use Symfony\Component\DependencyInjection\Exception\LogicException;
  18. /**
  19.  * Provides useful features shared by many extensions.
  20.  *
  21.  * @author Fabien Potencier <fabien@symfony.com>
  22.  */
  23. abstract class Extension implements ExtensionInterfaceConfigurationExtensionInterface
  24. {
  25.     private array $processedConfigs = [];
  26.     /**
  27.      * {@inheritdoc}
  28.      */
  29.     public function getXsdValidationBasePath()
  30.     {
  31.         return false;
  32.     }
  33.     /**
  34.      * {@inheritdoc}
  35.      */
  36.     public function getNamespace()
  37.     {
  38.         return 'http://example.org/schema/dic/'.$this->getAlias();
  39.     }
  40.     /**
  41.      * Returns the recommended alias to use in XML.
  42.      *
  43.      * This alias is also the mandatory prefix to use when using YAML.
  44.      *
  45.      * This convention is to remove the "Extension" postfix from the class
  46.      * name and then lowercase and underscore the result. So:
  47.      *
  48.      *     AcmeHelloExtension
  49.      *
  50.      * becomes
  51.      *
  52.      *     acme_hello
  53.      *
  54.      * This can be overridden in a sub-class to specify the alias manually.
  55.      *
  56.      * @throws BadMethodCallException When the extension name does not follow conventions
  57.      */
  58.     public function getAlias(): string
  59.     {
  60.         $className = static::class;
  61.         if (!str_ends_with($className'Extension')) {
  62.             throw new BadMethodCallException('This extension does not follow the naming convention; you must overwrite the getAlias() method.');
  63.         }
  64.         $classBaseName substr(strrchr($className'\\'), 1, -9);
  65.         return Container::underscore($classBaseName);
  66.     }
  67.     /**
  68.      * {@inheritdoc}
  69.      */
  70.     public function getConfiguration(array $configContainerBuilder $container)
  71.     {
  72.         $class = static::class;
  73.         if (str_contains($class"\0")) {
  74.             return null// ignore anonymous classes
  75.         }
  76.         $class substr_replace($class'\Configuration'strrpos($class'\\'));
  77.         $class $container->getReflectionClass($class);
  78.         if (!$class) {
  79.             return null;
  80.         }
  81.         if (!$class->implementsInterface(ConfigurationInterface::class)) {
  82.             throw new LogicException(sprintf('The extension configuration class "%s" must implement "%s".'$class->getName(), ConfigurationInterface::class));
  83.         }
  84.         if (!($constructor $class->getConstructor()) || !$constructor->getNumberOfRequiredParameters()) {
  85.             return $class->newInstance();
  86.         }
  87.         return null;
  88.     }
  89.     final protected function processConfiguration(ConfigurationInterface $configuration, array $configs): array
  90.     {
  91.         $processor = new Processor();
  92.         return $this->processedConfigs[] = $processor->processConfiguration($configuration$configs);
  93.     }
  94.     /**
  95.      * @internal
  96.      */
  97.     final public function getProcessedConfigs(): array
  98.     {
  99.         try {
  100.             return $this->processedConfigs;
  101.         } finally {
  102.             $this->processedConfigs = [];
  103.         }
  104.     }
  105.     /**
  106.      * @throws InvalidArgumentException When the config is not enableable
  107.      */
  108.     protected function isConfigEnabled(ContainerBuilder $container, array $config): bool
  109.     {
  110.         if (!\array_key_exists('enabled'$config)) {
  111.             throw new InvalidArgumentException("The config array has no 'enabled' key.");
  112.         }
  113.         return (bool) $container->getParameterBag()->resolveValue($config['enabled']);
  114.     }
  115. }