/src/Symfony/Bundle/DoctrinePHPCRBundle/Mapping/Driver/XmlDriver.php

https://github.com/simensen/symfony-cmf · PHP · 137 lines · 104 code · 27 blank · 6 comment · 17 complexity · 151a92e4c35f1203912c1cb8299f5ca7 MD5 · raw file

  1. <?php
  2. namespace Symfony\Bundle\DoctrinePHPCRBundle\Mapping\Driver;
  3. use Doctrine\ODM\PHPCR\Mapping\MappingException;
  4. use Doctrine\ODM\PHPCR\Mapping\Driver\XmlDriver as BaseXmlDriver;
  5. /**
  6. * XmlDriver that additionally looks for mapping information in a global file.
  7. *
  8. * @author Fabien Potencier <fabien@symfony.com>
  9. */
  10. class XmlDriver extends BaseXmlDriver
  11. {
  12. protected $prefixes = array();
  13. protected $globalBasename;
  14. protected $classCache;
  15. protected $fileExtension = '.phpcr.xml';
  16. public function setGlobalBasename($file)
  17. {
  18. $this->globalBasename = $file;
  19. }
  20. public function getGlobalBasename()
  21. {
  22. return $this->globalBasename;
  23. }
  24. public function setNamespacePrefixes($prefixes)
  25. {
  26. $this->prefixes = $prefixes;
  27. }
  28. public function getNamespacePrefixes()
  29. {
  30. return $this->prefixes;
  31. }
  32. public function isTransient($className)
  33. {
  34. return !in_array($className, $this->getAllClassNames());
  35. }
  36. public function getAllClassNames()
  37. {
  38. if (null === $this->classCache) {
  39. $this->initialize();
  40. }
  41. $classes = array();
  42. if ($this->paths) {
  43. foreach ((array) $this->paths as $path) {
  44. if (!is_dir($path)) {
  45. throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
  46. }
  47. $iterator = new \RecursiveIteratorIterator(
  48. new \RecursiveDirectoryIterator($path),
  49. \RecursiveIteratorIterator::LEAVES_ONLY
  50. );
  51. foreach ($iterator as $file) {
  52. $fileName = $file->getBasename($this->fileExtension);
  53. if ($fileName == $file->getBasename() || $fileName == $this->globalBasename) {
  54. continue;
  55. }
  56. // NOTE: All files found here means classes are not transient!
  57. if (isset($this->prefixes[$path])) {
  58. $classes[] = $this->prefixes[$path].'\\'.str_replace('.', '\\', $fileName);
  59. } else {
  60. $classes[] = str_replace('.', '\\', $fileName);
  61. }
  62. }
  63. }
  64. }
  65. return array_merge($classes, array_keys($this->classCache));
  66. }
  67. public function getElement($className)
  68. {
  69. if (null === $this->classCache) {
  70. $this->initialize();
  71. }
  72. if (!isset($this->classCache[$className])) {
  73. $this->classCache[$className] = parent::getElement($className);
  74. }
  75. return $this->classCache[$className];
  76. }
  77. protected function initialize()
  78. {
  79. $this->classCache = array();
  80. if (null !== $this->globalBasename) {
  81. foreach ($this->paths as $path) {
  82. if (file_exists($file = $path.'/'.$this->globalBasename.$this->fileExtension)) {
  83. $this->classCache = array_merge($this->classCache, $this->loadMappingFile($file));
  84. }
  85. }
  86. }
  87. }
  88. protected function findMappingFile($className)
  89. {
  90. $defaultFileName = str_replace('\\', '.', $className) . $this->fileExtension;
  91. foreach ($this->paths as $path) {
  92. if (!isset($this->prefixes[$path])) {
  93. if (file_exists($path . DIRECTORY_SEPARATOR . $defaultFileName)) {
  94. return $path . DIRECTORY_SEPARATOR . $defaultFileName;
  95. }
  96. continue;
  97. }
  98. $prefix = $this->prefixes[$path];
  99. if (0 !== strpos($className, $prefix.'\\')) {
  100. continue;
  101. }
  102. $filename = $path.'/'.strtr(substr($className, strlen($prefix)+1), '\\', '.').$this->fileExtension;
  103. if (file_exists($filename)) {
  104. return $filename;
  105. }
  106. throw MappingException::mappingFileNotFound($className, $filename);
  107. }
  108. throw MappingException::mappingFileNotFound($className, substr($className, strrpos($className, '\\') + 1).$this->fileExtension);
  109. }
  110. }