File.php 2.1 KB

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