FileDriver.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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\MappingException;
  21. /**
  22. * Base driver for file-based metadata drivers.
  23. *
  24. * A file driver operates in a mode where it loads the mapping files of individual
  25. * classes on demand. This requires the user to adhere to the convention of 1 mapping
  26. * file per class and the file names of the mapping files must correspond to the full
  27. * class name, including namespace, with the namespace delimiters '\', replaced by dots '.'.
  28. *
  29. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  30. * @link www.doctrine-project.com
  31. * @since 2.2
  32. * @author Benjamin Eberlei <kontakt@beberlei.de>
  33. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  34. * @author Jonathan H. Wage <jonwage@gmail.com>
  35. * @author Roman Borschel <roman@code-factory.org>
  36. */
  37. abstract class FileDriver implements MappingDriver
  38. {
  39. /**
  40. * @var FileLocator
  41. */
  42. protected $locator;
  43. /**
  44. * @var array
  45. */
  46. protected $classCache;
  47. /**
  48. * @var string
  49. */
  50. protected $globalBasename;
  51. /**
  52. * Initializes a new FileDriver that looks in the given path(s) for mapping
  53. * documents and operates in the specified operating mode.
  54. *
  55. * @param string|array|FileLocator $paths A FileLocator or one/multiple paths where mapping documents can be found.
  56. * @param string $fileExtension
  57. */
  58. public function __construct($locator, $fileExtension = null)
  59. {
  60. if ($locator instanceof FileLocator) {
  61. $this->locator = $locator;
  62. } else {
  63. $this->locator = new DefaultFileLocator((array)$locator, $fileExtension);
  64. }
  65. }
  66. public function setGlobalBasename($file)
  67. {
  68. $this->globalBasename = $file;
  69. }
  70. public function getGlobalBasename()
  71. {
  72. return $this->globalBasename;
  73. }
  74. /**
  75. * Get the element of schema meta data for the class from the mapping file.
  76. * This will lazily load the mapping file if it is not loaded yet
  77. *
  78. * @return array $element The element of schema meta data
  79. */
  80. public function getElement($className)
  81. {
  82. if ($this->classCache === null) {
  83. $this->initialize();
  84. }
  85. if (isset($this->classCache[$className])) {
  86. return $this->classCache[$className];
  87. }
  88. $result = $this->loadMappingFile($this->locator->findMappingFile($className));
  89. return $result[$className];
  90. }
  91. /**
  92. * Whether the class with the specified name should have its metadata loaded.
  93. * This is only the case if it is either mapped as an Entity or a
  94. * MappedSuperclass.
  95. *
  96. * @param string $className
  97. * @return boolean
  98. */
  99. public function isTransient($className)
  100. {
  101. if ($this->classCache === null) {
  102. $this->initialize();
  103. }
  104. if (isset($this->classCache[$className])) {
  105. return false;
  106. }
  107. return !$this->locator->fileExists($className);
  108. }
  109. /**
  110. * Gets the names of all mapped classes known to this driver.
  111. *
  112. * @return array The names of all mapped classes known to this driver.
  113. */
  114. public function getAllClassNames()
  115. {
  116. if ($this->classCache === null) {
  117. $this->initialize();
  118. }
  119. $classNames = (array)$this->locator->getAllClassNames($this->globalBasename);
  120. if ($this->classCache) {
  121. $classNames = array_merge(array_keys($this->classCache), $classNames);
  122. }
  123. return $classNames;
  124. }
  125. /**
  126. * Loads a mapping file with the given name and returns a map
  127. * from class/entity names to their corresponding file driver elements.
  128. *
  129. * @param string $file The mapping file to load.
  130. * @return array
  131. */
  132. abstract protected function loadMappingFile($file);
  133. /**
  134. * Initialize the class cache from all the global files.
  135. *
  136. * Using this feature adds a substantial performance hit to file drivers as
  137. * more metadata has to be loaded into memory than might actually be
  138. * necessary. This may not be relevant to scenarios where caching of
  139. * metadata is in place, however hits very hard in scenarios where no
  140. * caching is used.
  141. *
  142. * @return void
  143. */
  144. protected function initialize()
  145. {
  146. $this->classCache = array();
  147. if (null !== $this->globalBasename) {
  148. foreach ($this->locator->getPaths() as $path) {
  149. $file = $path.'/'.$this->globalBasename.$this->locator->getFileExtension();
  150. if (is_file($file)) {
  151. $this->classCache = array_merge(
  152. $this->classCache,
  153. $this->loadMappingFile($file)
  154. );
  155. }
  156. }
  157. }
  158. }
  159. }