File.php 3.2 KB

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