AbstractFileDriver.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. if(!isset($result[$className])){
  111. throw MappingException::invalidMappingFile($className, str_replace('\\', '.', $className) . $this->_fileExtension);
  112. }
  113. return $result[$className];
  114. }
  115. /**
  116. * Whether the class with the specified name should have its metadata loaded.
  117. * This is only the case if it is either mapped as an Entity or a
  118. * MappedSuperclass.
  119. *
  120. * @param string $className
  121. * @return boolean
  122. */
  123. public function isTransient($className)
  124. {
  125. $fileName = str_replace('\\', '.', $className) . $this->_fileExtension;
  126. // Check whether file exists
  127. foreach ((array) $this->_paths as $path) {
  128. if (file_exists($path . DIRECTORY_SEPARATOR . $fileName)) {
  129. return false;
  130. }
  131. }
  132. return true;
  133. }
  134. /**
  135. * Gets the names of all mapped classes known to this driver.
  136. *
  137. * @return array The names of all mapped classes known to this driver.
  138. */
  139. public function getAllClassNames()
  140. {
  141. $classes = array();
  142. if ($this->_paths) {
  143. foreach ((array) $this->_paths as $path) {
  144. if ( ! is_dir($path)) {
  145. throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
  146. }
  147. $iterator = new \RecursiveIteratorIterator(
  148. new \RecursiveDirectoryIterator($path),
  149. \RecursiveIteratorIterator::LEAVES_ONLY
  150. );
  151. foreach ($iterator as $file) {
  152. if (($fileName = $file->getBasename($this->_fileExtension)) == $file->getBasename()) {
  153. continue;
  154. }
  155. // NOTE: All files found here means classes are not transient!
  156. $classes[] = str_replace('.', '\\', $fileName);
  157. }
  158. }
  159. }
  160. return $classes;
  161. }
  162. /**
  163. * Finds the mapping file for the class with the given name by searching
  164. * through the configured paths.
  165. *
  166. * @param $className
  167. * @return string The (absolute) file name.
  168. * @throws MappingException
  169. */
  170. protected function _findMappingFile($className)
  171. {
  172. $fileName = str_replace('\\', '.', $className) . $this->_fileExtension;
  173. // Check whether file exists
  174. foreach ((array) $this->_paths as $path) {
  175. if (file_exists($path . DIRECTORY_SEPARATOR . $fileName)) {
  176. return $path . DIRECTORY_SEPARATOR . $fileName;
  177. }
  178. }
  179. throw MappingException::mappingFileNotFound($className, $fileName);
  180. }
  181. /**
  182. * Loads a mapping file with the given name and returns a map
  183. * from class/entity names to their corresponding elements.
  184. *
  185. * @param string $file The mapping file to load.
  186. * @return array
  187. */
  188. abstract protected function _loadMappingFile($file);
  189. }