Extension.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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\DependencyInjection\Container;
  12. use Symfony\Component\DependencyInjection\Exception\BadMethodCallException;
  13. use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
  14. use Symfony\Component\DependencyInjection\ContainerBuilder;
  15. use Symfony\Component\Config\Definition\Processor;
  16. use Symfony\Component\Config\Definition\ConfigurationInterface;
  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 = array();
  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 = get_class($this);
  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 = get_class($this);
  74. $class = substr_replace($class, '\Configuration', strrpos($class, '\\'));
  75. $class = $container->getReflectionClass($class);
  76. $constructor = $class ? $class->getConstructor() : null;
  77. if ($class && (!$constructor || !$constructor->getNumberOfRequiredParameters())) {
  78. return $class->newInstance();
  79. }
  80. }
  81. final protected function processConfiguration(ConfigurationInterface $configuration, array $configs)
  82. {
  83. $processor = new Processor();
  84. return $this->processedConfigs[] = $processor->processConfiguration($configuration, $configs);
  85. }
  86. /**
  87. * @internal
  88. */
  89. final public function getProcessedConfigs()
  90. {
  91. try {
  92. return $this->processedConfigs;
  93. } finally {
  94. $this->processedConfigs = array();
  95. }
  96. }
  97. /**
  98. * @return bool Whether the configuration is enabled
  99. *
  100. * @throws InvalidArgumentException When the config is not enableable
  101. */
  102. protected function isConfigEnabled(ContainerBuilder $container, array $config)
  103. {
  104. if (!array_key_exists('enabled', $config)) {
  105. throw new InvalidArgumentException("The config array has no 'enabled' key.");
  106. }
  107. return (bool) $container->getParameterBag()->resolveValue($config['enabled']);
  108. }
  109. }