PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/protected/extensions/doctrine/vendors/Doctrine/Common/Persistence/Mapping/Driver/StaticPHPDriver.php

https://bitbucket.org/NordLabs/yiidoctrine
PHP | 131 lines | 63 code | 19 blank | 49 comment | 7 complexity | ddd84eb9dae888907dfd42517020bc12 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\Common\Persistence\Mapping\Driver;
  20. use Doctrine\Common\Persistence\Mapping\ClassMetadata;
  21. use Doctrine\Common\Persistence\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.2
  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 MappingDriver
  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. public function __construct($paths)
  49. {
  50. $this->addPaths((array) $paths);
  51. }
  52. public function addPaths(array $paths)
  53. {
  54. $this->paths = array_unique(array_merge($this->paths, $paths));
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function loadMetadataForClass($className, ClassMetadata $metadata)
  60. {
  61. $className::loadMetadata($metadata);
  62. }
  63. /**
  64. * {@inheritDoc}
  65. * @todo Same code exists in AnnotationDriver, should we re-use it somehow or not worry about it?
  66. */
  67. public function getAllClassNames()
  68. {
  69. if ($this->classNames !== null) {
  70. return $this->classNames;
  71. }
  72. if (!$this->paths) {
  73. throw MappingException::pathRequired();
  74. }
  75. $classes = array();
  76. $includedFiles = array();
  77. foreach ($this->paths as $path) {
  78. if (!is_dir($path)) {
  79. throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
  80. }
  81. $iterator = new \RecursiveIteratorIterator(
  82. new \RecursiveDirectoryIterator($path),
  83. \RecursiveIteratorIterator::LEAVES_ONLY
  84. );
  85. foreach ($iterator as $file) {
  86. if ($file->getBasename('.php') == $file->getBasename()) {
  87. continue;
  88. }
  89. $sourceFile = realpath($file->getPathName());
  90. require_once $sourceFile;
  91. $includedFiles[] = $sourceFile;
  92. }
  93. }
  94. $declared = get_declared_classes();
  95. foreach ($declared as $className) {
  96. $rc = new \ReflectionClass($className);
  97. $sourceFile = $rc->getFileName();
  98. if (in_array($sourceFile, $includedFiles) && !$this->isTransient($className)) {
  99. $classes[] = $className;
  100. }
  101. }
  102. $this->classNames = $classes;
  103. return $classes;
  104. }
  105. /**
  106. * {@inheritdoc}
  107. */
  108. public function isTransient($className)
  109. {
  110. return ! method_exists($className, 'loadMetadata');
  111. }
  112. }