Extension.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace Symfony\Component\HttpKernel\DependencyInjection;
  3. use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
  4. use Symfony\Component\DependencyInjection\ContainerBuilder;
  5. use Symfony\Component\DependencyInjection\Container;
  6. /*
  7. * This file is part of the Symfony framework.
  8. *
  9. * (c) Fabien Potencier <fabien@symfony.com>
  10. *
  11. * This source file is subject to the MIT license that is bundled
  12. * with this source code in the file LICENSE.
  13. */
  14. /**
  15. * Provides useful features shared by many extensions.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. */
  19. abstract class Extension implements ExtensionInterface
  20. {
  21. private $classes = array();
  22. /**
  23. * Gets the classes to cache.
  24. *
  25. * @return array An array of classes
  26. */
  27. public function getClassesToCompile()
  28. {
  29. return $this->classes;
  30. }
  31. /**
  32. * Adds classes to the class cache.
  33. *
  34. * @param array $classes An array of classes
  35. */
  36. public function addClassesToCompile(array $classes)
  37. {
  38. $this->classes = array_merge($this->classes, $classes);
  39. }
  40. /**
  41. * Returns the base path for the XSD files.
  42. *
  43. * @return string The XSD base path
  44. */
  45. public function getXsdValidationBasePath()
  46. {
  47. return false;
  48. }
  49. /**
  50. * Returns the namespace to be used for this extension (XML namespace).
  51. *
  52. * @return string The XML namespace
  53. */
  54. public function getNamespace()
  55. {
  56. return 'http://example.org/schema/dic/'.$this->getAlias();
  57. }
  58. /**
  59. * Returns the recommended alias to use in XML.
  60. *
  61. * This alias is also the mandatory prefix to use when using YAML.
  62. *
  63. * This convention is to remove the "Extension" postfix from the class
  64. * name and then lowercase and underscore the result. So:
  65. *
  66. * AcmeHelloExtension
  67. *
  68. * becomes
  69. *
  70. * acme_hello
  71. *
  72. * This can be overridden in a sub-class to specify the alias manually.
  73. *
  74. * @return string The alias
  75. */
  76. public function getAlias()
  77. {
  78. $className = get_class($this);
  79. if (substr($className, -9) != 'Extension') {
  80. throw new \BadMethodCallException('This extension does not follow the naming convention; you must overwrite the getAlias() method.');
  81. }
  82. $classBaseName = substr(strrchr($className, '\\'), 1, -9);
  83. return Container::underscore($classBaseName);
  84. }
  85. }