Extension.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. * {@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 (substr($className, -9) != 'Extension') {
  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. $reflected = new \ReflectionClass($this);
  74. $namespace = $reflected->getNamespaceName();
  75. $class = $namespace.'\\Configuration';
  76. if (class_exists($class)) {
  77. $r = new \ReflectionClass($class);
  78. $container->addResource(new FileResource($r->getFileName()));
  79. if (!method_exists($class, '__construct')) {
  80. return new $class();
  81. }
  82. }
  83. }
  84. final protected function processConfiguration(ConfigurationInterface $configuration, array $configs)
  85. {
  86. $processor = new Processor();
  87. return $processor->processConfiguration($configuration, $configs);
  88. }
  89. /**
  90. * @param ContainerBuilder $container
  91. * @param array $config
  92. *
  93. * @return bool Whether the configuration is enabled
  94. *
  95. * @throws InvalidArgumentException When the config is not enableable
  96. */
  97. protected function isConfigEnabled(ContainerBuilder $container, array $config)
  98. {
  99. if (!array_key_exists('enabled', $config)) {
  100. throw new InvalidArgumentException("The config array has no 'enabled' key.");
  101. }
  102. return (bool) $container->getParameterBag()->resolveValue($config['enabled']);
  103. }
  104. }