Extension.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 ExtensionInterface, ConfigurationExtensionInterface
  24. {
  25. private $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. * @return string The alias
  57. *
  58. * @throws BadMethodCallException When the extension name does not follow conventions
  59. */
  60. public function getAlias()
  61. {
  62. $className = static::class;
  63. if ('Extension' != substr($className, -9)) {
  64. throw new BadMethodCallException('This extension does not follow the naming convention; you must overwrite the getAlias() method.');
  65. }
  66. $classBaseName = substr(strrchr($className, '\\'), 1, -9);
  67. return Container::underscore($classBaseName);
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function getConfiguration(array $config, ContainerBuilder $container)
  73. {
  74. $class = static::class;
  75. if (false !== strpos($class, "\0")) {
  76. return null; // ignore anonymous classes
  77. }
  78. $class = substr_replace($class, '\Configuration', strrpos($class, '\\'));
  79. $class = $container->getReflectionClass($class);
  80. if (!$class) {
  81. return null;
  82. }
  83. if (!$class->implementsInterface(ConfigurationInterface::class)) {
  84. throw new LogicException(sprintf('The extension configuration class "%s" must implement "%s".', $class->getName(), ConfigurationInterface::class));
  85. }
  86. if (!($constructor = $class->getConstructor()) || !$constructor->getNumberOfRequiredParameters()) {
  87. return $class->newInstance();
  88. }
  89. return null;
  90. }
  91. final protected function processConfiguration(ConfigurationInterface $configuration, array $configs): array
  92. {
  93. $processor = new Processor();
  94. return $this->processedConfigs[] = $processor->processConfiguration($configuration, $configs);
  95. }
  96. /**
  97. * @internal
  98. */
  99. final public function getProcessedConfigs(): array
  100. {
  101. try {
  102. return $this->processedConfigs;
  103. } finally {
  104. $this->processedConfigs = [];
  105. }
  106. }
  107. /**
  108. * @return bool Whether the configuration is enabled
  109. *
  110. * @throws InvalidArgumentException When the config is not enableable
  111. */
  112. protected function isConfigEnabled(ContainerBuilder $container, array $config)
  113. {
  114. if (!\array_key_exists('enabled', $config)) {
  115. throw new InvalidArgumentException("The config array has no 'enabled' key.");
  116. }
  117. return (bool) $container->getParameterBag()->resolveValue($config['enabled']);
  118. }
  119. }