File.php 3.3 KB

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