Extension.php 4.2 KB

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