PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/protected/extensions/doctrine/vendors/Doctrine/ORM/Mapping/Driver/StaticPHPDriver.php

https://bitbucket.org/NordLabs/yiidoctrine
PHP | 138 lines | 64 code | 20 blank | 54 comment | 7 complexity | 8bd7a9c37fe40a1bed915605c9ba490c MD5 | raw file
Possible License(s): BSD-2-Clause, LGPL-2.1, BSD-3-Clause
  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. }