PageRenderTime 38ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Doctrine/ODM/MongoDB/Mapping/Driver/PHPDriver.php

https://github.com/weaverryan/mongodb-odm
PHP | 116 lines | 61 code | 18 blank | 37 comment | 7 complexity | d69796fd696217652bbd7b62d19c9607 MD5 | raw file
  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\ODM\MongoDB\Mapping\Driver;
  20. use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
  21. /**
  22. * The PHPDriver invokes a static PHP function on the document class itself passing
  23. * a ClassMetadata instance for you to manually populate with mapping information.
  24. *
  25. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  26. * @link www.doctrine-project.org
  27. * @since 1.0
  28. * @author Jonathan H. Wage <jonwage@gmail.com>
  29. * @author Roman Borschel <roman@code-factory.org>
  30. */
  31. class PHPDriver implements Driver
  32. {
  33. private $paths = array();
  34. public function __construct($paths)
  35. {
  36. $this->addPaths((array) $paths);
  37. }
  38. public function addPaths(array $paths)
  39. {
  40. $this->paths = array_unique(array_merge($this->paths, $paths));
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function loadMetadataForClass($className, ClassMetadata $metadata)
  46. {
  47. call_user_func_array(array($className, 'loadMetadata'), array($metadata));
  48. }
  49. /**
  50. * {@inheritDoc}
  51. * @todo Same code exists in AnnotationDriver, should we re-use it somehow or not worry about it?
  52. */
  53. public function getAllClassNames()
  54. {
  55. if ($this->classNames !== null) {
  56. return $this->classNames;
  57. }
  58. if (!$this->paths) {
  59. throw MongoDBException::pathRequired();
  60. }
  61. $classes = array();
  62. $includedFiles = array();
  63. foreach ($this->paths as $path) {
  64. if ( ! is_dir($path)) {
  65. throw MongoDBException::fileMappingDriversRequireConfiguredDirectoryPath();
  66. }
  67. $iterator = new \RecursiveIteratorIterator(
  68. new \RecursiveDirectoryIterator($path),
  69. \RecursiveIteratorIterator::LEAVES_ONLY
  70. );
  71. foreach ($iterator as $file) {
  72. if (($fileName = $file->getBasename($this->fileExtension)) == $file->getBasename()) {
  73. continue;
  74. }
  75. $sourceFile = realpath($file->getPathName());
  76. require_once $sourceFile;
  77. $includedFiles[] = $sourceFile;
  78. }
  79. }
  80. $declared = get_declared_classes();
  81. foreach ($declared as $className) {
  82. $rc = new \ReflectionClass($className);
  83. $sourceFile = $rc->getFileName();
  84. if (in_array($sourceFile, $includedFiles) && ! $this->isTransient($className)) {
  85. $classes[] = $className;
  86. }
  87. }
  88. $this->classNames = $classes;
  89. return $classes;
  90. }
  91. /**
  92. * {@inheritdoc}
  93. */
  94. public function isTransient($className)
  95. {
  96. return method_exists($className, 'loadMetadata') ? false : true;
  97. }
  98. }