PageRenderTime 68ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

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