vendor/symfony/src/Symfony/Bridge/Doctrine/Mapping/Driver/XmlDriver.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * This file is part of the Symfony package.
       
     5  *
       
     6  * (c) Fabien Potencier <fabien@symfony.com>
       
     7  *
       
     8  * For the full copyright and license information, please view the LICENSE
       
     9  * file that was distributed with this source code.
       
    10  */
       
    11 
       
    12 namespace Symfony\Bridge\Doctrine\Mapping\Driver;
       
    13 
       
    14 use Doctrine\ORM\Mapping\MappingException;
       
    15 use Doctrine\ORM\Mapping\Driver\XmlDriver as BaseXmlDriver;
       
    16 
       
    17 /**
       
    18  * XmlDriver that additionally looks for mapping information in a global file.
       
    19  *
       
    20  * @author Fabien Potencier <fabien@symfony.com>
       
    21  */
       
    22 class XmlDriver extends BaseXmlDriver
       
    23 {
       
    24     protected $_prefixes = array();
       
    25     protected $_globalBasename;
       
    26     protected $_classCache;
       
    27     protected $_fileExtension = '.orm.xml';
       
    28 
       
    29     public function setGlobalBasename($file)
       
    30     {
       
    31         $this->_globalBasename = $file;
       
    32     }
       
    33 
       
    34     public function getGlobalBasename()
       
    35     {
       
    36         return $this->_globalBasename;
       
    37     }
       
    38 
       
    39     public function setNamespacePrefixes($prefixes)
       
    40     {
       
    41         $this->_prefixes = $prefixes;
       
    42     }
       
    43 
       
    44     public function getNamespacePrefixes()
       
    45     {
       
    46         return $this->_prefixes;
       
    47     }
       
    48 
       
    49     public function isTransient($className)
       
    50     {
       
    51         if (null === $this->_classCache) {
       
    52             $this->initialize();
       
    53         }
       
    54 
       
    55         // The mapping is defined in the global mapping file
       
    56         if (isset($this->_classCache[$className])) {
       
    57             return false;
       
    58         }
       
    59 
       
    60         try {
       
    61             $this->_findMappingFile($className);
       
    62 
       
    63             return false;
       
    64         } catch (MappingException $e) {
       
    65             return true;
       
    66         }
       
    67     }
       
    68 
       
    69     public function getAllClassNames()
       
    70     {
       
    71         if (null === $this->_classCache) {
       
    72             $this->initialize();
       
    73         }
       
    74 
       
    75         $classes = array();
       
    76 
       
    77         if ($this->_paths) {
       
    78             foreach ((array) $this->_paths as $path) {
       
    79                 if (!is_dir($path)) {
       
    80                     throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
       
    81                 }
       
    82 
       
    83                 $iterator = new \RecursiveIteratorIterator(
       
    84                     new \RecursiveDirectoryIterator($path),
       
    85                     \RecursiveIteratorIterator::LEAVES_ONLY
       
    86                 );
       
    87 
       
    88                 foreach ($iterator as $file) {
       
    89                     $fileName = $file->getBasename($this->_fileExtension);
       
    90 
       
    91                     if ($fileName == $file->getBasename() || $fileName == $this->_globalBasename) {
       
    92                         continue;
       
    93                     }
       
    94 
       
    95                     // NOTE: All files found here means classes are not transient!
       
    96                     if (isset($this->_prefixes[$path])) {
       
    97                         $classes[] = $this->_prefixes[$path].'\\'.str_replace('.', '\\', $fileName);
       
    98                     } else {
       
    99                         $classes[] = str_replace('.', '\\', $fileName);
       
   100                     }
       
   101                 }
       
   102             }
       
   103         }
       
   104 
       
   105         return array_merge($classes, array_keys($this->_classCache));
       
   106     }
       
   107 
       
   108     public function getElement($className)
       
   109     {
       
   110         if (null === $this->_classCache) {
       
   111             $this->initialize();
       
   112         }
       
   113 
       
   114         if (!isset($this->_classCache[$className])) {
       
   115             $this->_classCache[$className] = parent::getElement($className);
       
   116         }
       
   117 
       
   118         return $this->_classCache[$className];
       
   119     }
       
   120 
       
   121     protected function initialize()
       
   122     {
       
   123         $this->_classCache = array();
       
   124         if (null !== $this->_globalBasename) {
       
   125             foreach ($this->_paths as $path) {
       
   126                 if (file_exists($file = $path.'/'.$this->_globalBasename.$this->_fileExtension)) {
       
   127                     $this->_classCache = array_merge($this->_classCache, $this->_loadMappingFile($file));
       
   128                 }
       
   129             }
       
   130         }
       
   131     }
       
   132 
       
   133     protected function _findMappingFile($className)
       
   134     {
       
   135         $defaultFileName = str_replace('\\', '.', $className).$this->_fileExtension;
       
   136         foreach ($this->_paths as $path) {
       
   137             if (!isset($this->_prefixes[$path])) {
       
   138                 if (file_exists($path.DIRECTORY_SEPARATOR.$defaultFileName)) {
       
   139                     return $path.DIRECTORY_SEPARATOR.$defaultFileName;
       
   140                 }
       
   141 
       
   142                 continue;
       
   143             }
       
   144 
       
   145             $prefix = $this->_prefixes[$path];
       
   146 
       
   147             if (0 !== strpos($className, $prefix.'\\')) {
       
   148                 continue;
       
   149             }
       
   150 
       
   151             $filename = $path.'/'.strtr(substr($className, strlen($prefix)+1), '\\', '.').$this->_fileExtension;
       
   152             if (file_exists($filename)) {
       
   153                 return $filename;
       
   154             }
       
   155 
       
   156             throw MappingException::mappingFileNotFound($className, $filename);
       
   157         }
       
   158 
       
   159         throw MappingException::mappingFileNotFound($className, substr($className, strrpos($className, '\\') + 1).$this->_fileExtension);
       
   160     }
       
   161 }