Extension.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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\Config\Resource\FileResource;
  15. use Symfony\Component\DependencyInjection\ContainerBuilder;
  16. use Symfony\Component\Config\Definition\Processor;
  17. use Symfony\Component\Config\Definition\ConfigurationInterface;
  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. /**
  26. * Returns the base path for the XSD files.
  27. *
  28. * @return string The XSD base path
  29. */
  30. public function getXsdValidationBasePath()
  31. {
  32. return false;
  33. }
  34. /**
  35. * Returns the namespace to be used for this extension (XML namespace).
  36. *
  37. * @return string The XML namespace
  38. */
  39. public function getNamespace()
  40. {
  41. return 'http://example.org/schema/dic/'.$this->getAlias();
  42. }
  43. /**
  44. * Returns the recommended alias to use in XML.
  45. *
  46. * This alias is also the mandatory prefix to use when using YAML.
  47. *
  48. * This convention is to remove the "Extension" postfix from the class
  49. * name and then lowercase and underscore the result. So:
  50. *
  51. * AcmeHelloExtension
  52. *
  53. * becomes
  54. *
  55. * acme_hello
  56. *
  57. * This can be overridden in a sub-class to specify the alias manually.
  58. *
  59. * @return string The alias
  60. *
  61. * @throws BadMethodCallException When the extension name does not follow conventions
  62. */
  63. public function getAlias()
  64. {
  65. $className = get_class($this);
  66. if (substr($className, -9) != 'Extension') {
  67. throw new BadMethodCallException('This extension does not follow the naming convention; you must overwrite the getAlias() method.');
  68. }
  69. $classBaseName = substr(strrchr($className, '\\'), 1, -9);
  70. return Container::underscore($classBaseName);
  71. }
  72. /**
  73. * {@inheritDoc}
  74. */
  75. public function getConfiguration(array $config, ContainerBuilder $container)
  76. {
  77. $reflected = new \ReflectionClass($this);
  78. $namespace = $reflected->getNamespaceName();
  79. $class = $namespace.'\\Configuration';
  80. if (class_exists($class)) {
  81. $r = new \ReflectionClass($class);
  82. $container->addResource(new FileResource($r->getFileName()));
  83. if (!method_exists($class, '__construct')) {
  84. $configuration = new $class();
  85. return $configuration;
  86. }
  87. }
  88. return null;
  89. }
  90. final protected function processConfiguration(ConfigurationInterface $configuration, array $configs)
  91. {
  92. $processor = new Processor();
  93. return $processor->processConfiguration($configuration, $configs);
  94. }
  95. /**
  96. * @param ContainerBuilder $container
  97. * @param array $config
  98. *
  99. * @return Boolean Whether the configuration is enabled
  100. *
  101. * @throws InvalidArgumentException When the config is not enableable
  102. */
  103. protected function isConfigEnabled(ContainerBuilder $container, array $config)
  104. {
  105. if (!array_key_exists('enabled', $config)) {
  106. throw new InvalidArgumentException("The config array has no 'enabled' key.");
  107. }
  108. return (Boolean) $container->getParameterBag()->resolveValue($config['enabled']);
  109. }
  110. }