File.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. /**
  8. * The mapping FileDriver abstract class, defines the
  9. * metadata extraction function common among
  10. * all drivers used on these extensions by file based
  11. * drivers.
  12. *
  13. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  14. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  15. */
  16. abstract class File implements Driver
  17. {
  18. /**
  19. * @var FileLocator
  20. */
  21. protected $locator;
  22. /**
  23. * File extension, must be set in child class
  24. * @var string
  25. */
  26. protected $_extension;
  27. /**
  28. * original driver if it is available
  29. */
  30. protected $_originalDriver = null;
  31. public function setLocator(FileLocator $locator)
  32. {
  33. $this->locator = $locator;
  34. }
  35. /**
  36. * Set the paths for file lookup
  37. *
  38. * @param array $paths
  39. * @return void
  40. */
  41. public function setPaths($paths)
  42. {
  43. $this->_paths = (array)$paths;
  44. }
  45. /**
  46. * Set the file extension
  47. *
  48. * @param string $extension
  49. * @return void
  50. */
  51. public function setExtension($extension)
  52. {
  53. $this->_extension = $extension;
  54. }
  55. /**
  56. * Loads a mapping file with the given name and returns a map
  57. * from class/entity names to their corresponding elements.
  58. *
  59. * @param string $file The mapping file to load.
  60. * @return array
  61. */
  62. abstract protected function _loadMappingFile($file);
  63. /**
  64. * Tries to get a mapping for a given class
  65. *
  66. * @param $className
  67. * @return null|array|object
  68. */
  69. protected function _getMapping($className)
  70. {
  71. //try loading mapping from original driver first
  72. $mapping = null;
  73. if (!is_null($this->_originalDriver)) {
  74. if ($this->_originalDriver instanceof FileDriver || $this->_originalDriver instanceof AbstractFileDriver) {
  75. $mapping = $this->_originalDriver->getElement($className);
  76. }
  77. }
  78. //if no mapping found try to load mapping file again
  79. if (is_null($mapping)) {
  80. $yaml = $this->_loadMappingFile($this->locator->findMappingFile($className));
  81. $mapping = $yaml[$className];
  82. }
  83. return $mapping;
  84. }
  85. /**
  86. * Passes in the mapping read by original driver
  87. *
  88. * @param $driver
  89. * @return void
  90. */
  91. public function setOriginalDriver($driver)
  92. {
  93. $this->_originalDriver = $driver;
  94. }
  95. }