StaticPHPDriver.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\ORM\Mapping\Driver;
  20. use Doctrine\ORM\Mapping\ClassMetadataInfo,
  21. Doctrine\ORM\Mapping\MappingException;
  22. /**
  23. * The StaticPHPDriver calls a static loadMetadata() method on your entity
  24. * classes where you can manually populate the ClassMetadata instance.
  25. *
  26. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  27. * @link www.doctrine-project.org
  28. * @since 2.0
  29. * @author Benjamin Eberlei <kontakt@beberlei.de>
  30. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  31. * @author Jonathan H. Wage <jonwage@gmail.com>
  32. * @author Roman Borschel <roman@code-factory.org>
  33. */
  34. class StaticPHPDriver implements Driver
  35. {
  36. /**
  37. * Paths of entity directories.
  38. *
  39. * @var array
  40. */
  41. private $_paths = array();
  42. /**
  43. * Map of all class names.
  44. *
  45. * @var array
  46. */
  47. private $_classNames;
  48. /**
  49. * The file extension of mapping documents.
  50. *
  51. * @var string
  52. */
  53. private $_fileExtension = '.php';
  54. public function __construct($paths)
  55. {
  56. $this->addPaths((array) $paths);
  57. }
  58. public function addPaths(array $paths)
  59. {
  60. $this->_paths = array_unique(array_merge($this->_paths, $paths));
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function loadMetadataForClass($className, ClassMetadataInfo $metadata)
  66. {
  67. call_user_func_array(array($className, 'loadMetadata'), array($metadata));
  68. }
  69. /**
  70. * {@inheritDoc}
  71. * @todo Same code exists in AnnotationDriver, should we re-use it somehow or not worry about it?
  72. */
  73. public function getAllClassNames()
  74. {
  75. if ($this->_classNames !== null) {
  76. return $this->_classNames;
  77. }
  78. if (!$this->_paths) {
  79. throw MappingException::pathRequired();
  80. }
  81. $classes = array();
  82. $includedFiles = array();
  83. foreach ($this->_paths as $path) {
  84. if (!is_dir($path)) {
  85. throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
  86. }
  87. $iterator = new \RecursiveIteratorIterator(
  88. new \RecursiveDirectoryIterator($path),
  89. \RecursiveIteratorIterator::LEAVES_ONLY
  90. );
  91. foreach ($iterator as $file) {
  92. if (($fileName = $file->getBasename($this->_fileExtension)) == $file->getBasename()) {
  93. continue;
  94. }
  95. $sourceFile = realpath($file->getPathName());
  96. require_once $sourceFile;
  97. $includedFiles[] = $sourceFile;
  98. }
  99. }
  100. $declared = get_declared_classes();
  101. foreach ($declared as $className) {
  102. $rc = new \ReflectionClass($className);
  103. $sourceFile = $rc->getFileName();
  104. if (in_array($sourceFile, $includedFiles) && !$this->isTransient($className)) {
  105. $classes[] = $className;
  106. }
  107. }
  108. $this->_classNames = $classes;
  109. return $classes;
  110. }
  111. /**
  112. * {@inheritdoc}
  113. */
  114. public function isTransient($className)
  115. {
  116. return method_exists($className, 'loadMetadata') ? false : true;
  117. }
  118. }