File.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace Gedmo\Mapping\Driver;
  3. use Gedmo\Mapping\Driver;
  4. use Doctrine\ORM\Mapping\Driver\AbstractFileDriver as ORMAbstractFileDriver;
  5. use Doctrine\ODM\MongoDB\Mapping\Driver\AbstractFileDriver as MongoDBAbstractFileDriver;
  6. /**
  7. * The mapping FileDriver abstract class, defines the
  8. * metadata extraction function common among
  9. * all drivers used on these extensions by file based
  10. * drivers.
  11. *
  12. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  13. * @package Gedmo.Common.Mapping
  14. * @subpackage FileDriver
  15. * @link http://www.gediminasm.org
  16. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  17. */
  18. abstract class File implements Driver
  19. {
  20. /**
  21. * File extension, must be set in child class
  22. * @var string
  23. */
  24. protected $_extension;
  25. /**
  26. * original driver if it is available
  27. */
  28. protected $_originalDriver = null;
  29. /**
  30. * List of paths for file search
  31. * @var array
  32. */
  33. private $_paths = array();
  34. /**
  35. * Set the paths for file lookup
  36. *
  37. * @param array $paths
  38. * @return void
  39. */
  40. public function setPaths($paths)
  41. {
  42. $this->_paths = (array)$paths;
  43. }
  44. /**
  45. * Set the file extension
  46. *
  47. * @param string $extension
  48. * @return void
  49. */
  50. public function setExtension($extension)
  51. {
  52. $this->_extension = $extension;
  53. }
  54. /**
  55. * Loads a mapping file with the given name and returns a map
  56. * from class/entity names to their corresponding elements.
  57. *
  58. * @param string $file The mapping file to load.
  59. * @return array
  60. */
  61. abstract protected function _loadMappingFile($file);
  62. /**
  63. * Finds the mapping file for the class with the given name by searching
  64. * through the configured paths.
  65. *
  66. * @param $className
  67. * @return string The (absolute) file name.
  68. * @throws RuntimeException if not found
  69. */
  70. protected function _findMappingFile($className)
  71. {
  72. $fileName = str_replace('\\', '.', $className) . $this->_extension;
  73. // Check whether file exists
  74. foreach ((array) $this->_paths as $path) {
  75. if (file_exists($path . DIRECTORY_SEPARATOR . $fileName)) {
  76. return $path . DIRECTORY_SEPARATOR . $fileName;
  77. }
  78. }
  79. throw new \Gedmo\Exception\UnexpectedValueException("No mapping file found named '$fileName' for class '$className'.");
  80. }
  81. /**
  82. * Tries to get a mapping for a given class
  83. *
  84. * @param $className
  85. * @return null|array|object
  86. */
  87. protected function _getMapping($className)
  88. {
  89. //try loading mapping from original driver first
  90. $mapping = null;
  91. if (!is_null($this->_originalDriver)) {
  92. if ($this->_originalDriver instanceof ORMAbstractFileDriver || $this->_originalDriver instanceof MongoDBAbstractFileDriver) {
  93. $mapping = $this->_originalDriver->getElement($className);
  94. }
  95. }
  96. //if no mapping found try to load mapping file again
  97. if (is_null($mapping)) {
  98. $yaml = $this->_loadMappingFile($this->_findMappingFile($className));
  99. $mapping = $yaml[$className];
  100. }
  101. return $mapping;
  102. }
  103. /**
  104. * Passes in the mapping read by original driver
  105. *
  106. * @param $driver
  107. * @return void
  108. */
  109. public function setOriginalDriver($driver)
  110. {
  111. $this->_originalDriver = $driver;
  112. }
  113. }