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

  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.     public function getXsdValidationBasePath()
  27.     {
  28.         return false;
  29.     }
  30.     public function getNamespace()
  31.     {
  32.         return 'http://example.org/schema/dic/'.$this->getAlias();
  33.     }
  34.     /**
  35.      * Returns the recommended alias to use in XML.
  36.      *
  37.      * This alias is also the mandatory prefix to use when using YAML.
  38.      *
  39.      * This convention is to remove the "Extension" postfix from the class
  40.      * name and then lowercase and underscore the result. So:
  41.      *
  42.      *     AcmeHelloExtension
  43.      *
  44.      * becomes
  45.      *
  46.      *     acme_hello
  47.      *
  48.      * This can be overridden in a sub-class to specify the alias manually.
  49.      *
  50.      * @throws BadMethodCallException When the extension name does not follow conventions
  51.      */
  52.     public function getAlias(): string
  53.     {
  54.         $className = static::class;
  55.         if (!str_ends_with($className'Extension')) {
  56.             throw new BadMethodCallException('This extension does not follow the naming convention; you must overwrite the getAlias() method.');
  57.         }
  58.         $classBaseName substr(strrchr($className'\\'), 1, -9);
  59.         return Container::underscore($classBaseName);
  60.     }
  61.     public function getConfiguration(array $configContainerBuilder $container)
  62.     {
  63.         $class = static::class;
  64.         if (str_contains($class"\0")) {
  65.             return null// ignore anonymous classes
  66.         }
  67.         $class substr_replace($class'\Configuration'strrpos($class'\\'));
  68.         $class $container->getReflectionClass($class);
  69.         if (!$class) {
  70.             return null;
  71.         }
  72.         if (!$class->implementsInterface(ConfigurationInterface::class)) {
  73.             throw new LogicException(sprintf('The extension configuration class "%s" must implement "%s".'$class->getName(), ConfigurationInterface::class));
  74.         }
  75.         if (!($constructor $class->getConstructor()) || !$constructor->getNumberOfRequiredParameters()) {
  76.             return $class->newInstance();
  77.         }
  78.         return null;
  79.     }
  80.     final protected function processConfiguration(ConfigurationInterface $configuration, array $configs): array
  81.     {
  82.         $processor = new Processor();
  83.         return $this->processedConfigs[] = $processor->processConfiguration($configuration$configs);
  84.     }
  85.     /**
  86.      * @internal
  87.      */
  88.     final public function getProcessedConfigs(): array
  89.     {
  90.         try {
  91.             return $this->processedConfigs;
  92.         } finally {
  93.             $this->processedConfigs = [];
  94.         }
  95.     }
  96.     /**
  97.      * @throws InvalidArgumentException When the config is not enableable
  98.      */
  99.     protected function isConfigEnabled(ContainerBuilder $container, array $config): bool
  100.     {
  101.         if (!\array_key_exists('enabled'$config)) {
  102.             throw new InvalidArgumentException("The config array has no 'enabled' key.");
  103.         }
  104.         return (bool) $container->getParameterBag()->resolveValue($config['enabled']);
  105.     }
  106. }