AbstractFileDriver.php 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /*
  3. * $Id$
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information, see
  19. * <http://www.doctrine-project.org>.
  20. */
  21. namespace Doctrine\ORM\Mapping\Driver;
  22. use Doctrine\ORM\Mapping\MappingException;
  23. /**
  24. * Base driver for file-based metadata drivers.
  25. *
  26. * A file driver operates in a mode where it loads the mapping files of individual
  27. * classes on demand. This requires the user to adhere to the convention of 1 mapping
  28. * file per class and the file names of the mapping files must correspond to the full
  29. * class name, including namespace, with the namespace delimiters '\', replaced by dots '.'.
  30. *
  31. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  32. * @link www.doctrine-project.com
  33. * @since 2.0
  34. * @version $Revision$
  35. * @author Benjamin Eberlei <kontakt@beberlei.de>
  36. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  37. * @author Jonathan H. Wage <jonwage@gmail.com>
  38. * @author Roman Borschel <roman@code-factory.org>
  39. */
  40. abstract class AbstractFileDriver implements Driver
  41. {
  42. /**
  43. * The paths where to look for mapping files.
  44. *
  45. * @var array
  46. */
  47. protected $_paths = array();
  48. /**
  49. * The file extension of mapping documents.
  50. *
  51. * @var string
  52. */
  53. protected $_fileExtension;
  54. /**
  55. * Initializes a new FileDriver that looks in the given path(s) for mapping
  56. * documents and operates in the specified operating mode.
  57. *
  58. * @param string|array $paths One or multiple paths where mapping documents can be found.
  59. */
  60. public function __construct($paths)
  61. {
  62. $this->addPaths((array) $paths);
  63. }
  64. /**
  65. * Append lookup paths to metadata driver.
  66. *
  67. * @param array $paths
  68. */
  69. public function addPaths(array $paths)
  70. {
  71. $this->_paths = array_unique(array_merge($this->_paths, $paths));
  72. }
  73. /**
  74. * Retrieve the defined metadata lookup paths.
  75. *
  76. * @return array
  77. */
  78. public function getPaths()
  79. {
  80. return $this->_paths;
  81. }
  82. /**
  83. * Get the file extension used to look for mapping files under
  84. *
  85. * @return void
  86. */
  87. public function getFileExtension()
  88. {
  89. return $this->_fileExtension;
  90. }
  91. /**
  92. * Set the file extension used to look for mapping files under
  93. *
  94. * @param string $fileExtension The file extension to set
  95. * @return void
  96. */
  97. public function setFileExtension($fileExtension)
  98. {
  99. $this->_fileExtension = $fileExtension;
  100. }
  101. /**
  102. * Get the element of schema meta data for the class from the mapping file.
  103. * This will lazily load the mapping file if it is not loaded yet
  104. *
  105. * @return array $element The element of schema meta data
  106. */
  107. public function getElement($className)
  108. {
  109. $result = $this->_loadMappingFile($this->_findMappingFile($className));
  110. return $result[$className];
  111. }
  112. /**
  113. * Whether the class with the specified name should have its metadata loaded.
  114. * This is only the case if it is either mapped as an Entity or a
  115. * MappedSuperclass.
  116. *
  117. * @param string $className
  118. * @return boolean
  119. */
  120. public function isTransient($className)
  121. {
  122. $fileName = str_replace('\\', '.', $className) . $this->_fileExtension;
  123. // Check whether file exists
  124. foreach ((array) $this->_paths as $path) {
  125. if (file_exists($path . DIRECTORY_SEPARATOR . $fileName)) {
  126. return false;
  127. }
  128. }
  129. return true;
  130. }
  131. /**
  132. * Gets the names of all mapped classes known to this driver.
  133. *
  134. * @return array The names of all mapped classes known to this driver.
  135. */
  136. public function getAllClassNames()
  137. {
  138. $classes = array();
  139. if ($this->_paths) {
  140. foreach ((array) $this->_paths as $path) {
  141. if ( ! is_dir($path)) {
  142. throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
  143. }
  144. $iterator = new \RecursiveIteratorIterator(
  145. new \RecursiveDirectoryIterator($path),
  146. \RecursiveIteratorIterator::LEAVES_ONLY
  147. );
  148. foreach ($iterator as $file) {
  149. if (($fileName = $file->getBasename($this->_fileExtension)) == $file->getBasename()) {
  150. continue;
  151. }
  152. // NOTE: All files found here means classes are not transient!
  153. $classes[] = str_replace('.', '\\', $fileName);
  154. }
  155. }
  156. }
  157. return $classes;
  158. }
  159. /**
  160. * Finds the mapping file for the class with the given name by searching
  161. * through the configured paths.
  162. *
  163. * @param $className
  164. * @return string The (absolute) file name.
  165. * @throws MappingException
  166. */
  167. protected function _findMappingFile($className)
  168. {
  169. $fileName = str_replace('\\', '.', $className) . $this->_fileExtension;
  170. // Check whether file exists
  171. foreach ((array) $this->_paths as $path) {
  172. if (file_exists($path . DIRECTORY_SEPARATOR . $fileName)) {
  173. return $path . DIRECTORY_SEPARATOR . $fileName;
  174. }
  175. }
  176. throw MappingException::mappingFileNotFound($className, $fileName);
  177. }
  178. /**
  179. * Loads a mapping file with the given name and returns a map
  180. * from class/entity names to their corresponding elements.
  181. *
  182. * @param string $file The mapping file to load.
  183. * @return array
  184. */
  185. abstract protected function _loadMappingFile($file);
  186. }