File.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace Gedmo\Mapping\Driver;
  3. use Gedmo\Mapping\Driver;
  4. /**
  5. * The mapping FileDriver abstract class, defines the
  6. * metadata extraction function common among
  7. * all drivers used on these extensions by file based
  8. * drivers.
  9. *
  10. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  11. * @package Gedmo.Common.Mapping
  12. * @subpackage FileDriver
  13. * @link http://www.gediminasm.org
  14. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  15. */
  16. abstract class File implements Driver
  17. {
  18. /**
  19. * File extension, must be set in child class
  20. * @var string
  21. */
  22. protected $_extension;
  23. /**
  24. * List of paths for file search
  25. * @var array
  26. */
  27. private $_paths = array();
  28. /**
  29. * Set the paths for file lookup
  30. *
  31. * @param array $paths
  32. * @return void
  33. */
  34. public function setPaths($paths)
  35. {
  36. $this->_paths = (array)$paths;
  37. }
  38. /**
  39. * Set the file extension
  40. *
  41. * @param string $extension
  42. * @return void
  43. */
  44. public function setExtension($extension)
  45. {
  46. $this->_extension = $extension;
  47. }
  48. /**
  49. * Loads a mapping file with the given name and returns a map
  50. * from class/entity names to their corresponding elements.
  51. *
  52. * @param string $file The mapping file to load.
  53. * @return array
  54. */
  55. abstract protected function _loadMappingFile($file);
  56. /**
  57. * Finds the mapping file for the class with the given name by searching
  58. * through the configured paths.
  59. *
  60. * @param $className
  61. * @return string The (absolute) file name.
  62. * @throws RuntimeException if not found
  63. */
  64. protected function _findMappingFile($className)
  65. {
  66. $fileName = str_replace('\\', '.', $className) . $this->_extension;
  67. // Check whether file exists
  68. foreach ((array) $this->_paths as $path) {
  69. if (file_exists($path . DIRECTORY_SEPARATOR . $fileName)) {
  70. return $path . DIRECTORY_SEPARATOR . $fileName;
  71. }
  72. }
  73. throw new \Gedmo\Exception\UnexpectedValueException("No mapping file found named '$fileName' for class '$className'.");
  74. }
  75. }