mailpoet
/
vendor-prefixed
/
doctrine
/
persistence
/
src
/
Persistence
/
Mapping
/
Driver
/
FileDriver.php
mailpoet
/
vendor-prefixed
/
doctrine
/
persistence
/
src
/
Persistence
/
Mapping
/
Driver
Last commit date
ColocatedMappingDriver.php
9 months ago
DefaultFileLocator.php
9 months ago
FileDriver.php
9 months ago
FileLocator.php
9 months ago
MappingDriver.php
9 months ago
MappingDriverChain.php
9 months ago
PHPDriver.php
9 months ago
StaticPHPDriver.php
9 months ago
SymfonyFileLocator.php
9 months ago
index.php
3 years ago
FileDriver.php
94 lines
| 1 | <?php |
| 2 | declare (strict_types=1); |
| 3 | namespace MailPoetVendor\Doctrine\Persistence\Mapping\Driver; |
| 4 | if (!defined('ABSPATH')) exit; |
| 5 | use MailPoetVendor\Doctrine\Persistence\Mapping\MappingException; |
| 6 | use function array_keys; |
| 7 | use function array_merge; |
| 8 | use function array_unique; |
| 9 | use function array_values; |
| 10 | use function is_file; |
| 11 | use function str_replace; |
| 12 | abstract class FileDriver implements MappingDriver |
| 13 | { |
| 14 | protected $locator; |
| 15 | protected $classCache; |
| 16 | protected $globalBasename = ''; |
| 17 | public function __construct($locator, ?string $fileExtension = null) |
| 18 | { |
| 19 | if ($locator instanceof FileLocator) { |
| 20 | $this->locator = $locator; |
| 21 | } else { |
| 22 | $this->locator = new DefaultFileLocator((array) $locator, $fileExtension); |
| 23 | } |
| 24 | } |
| 25 | public function setGlobalBasename(string $file) |
| 26 | { |
| 27 | $this->globalBasename = $file; |
| 28 | } |
| 29 | public function getGlobalBasename() |
| 30 | { |
| 31 | return $this->globalBasename; |
| 32 | } |
| 33 | public function getElement(string $className) |
| 34 | { |
| 35 | if ($this->classCache === null) { |
| 36 | $this->initialize(); |
| 37 | } |
| 38 | if (isset($this->classCache[$className])) { |
| 39 | return $this->classCache[$className]; |
| 40 | } |
| 41 | $result = $this->loadMappingFile($this->locator->findMappingFile($className)); |
| 42 | if (!isset($result[$className])) { |
| 43 | throw MappingException::invalidMappingFile($className, str_replace('\\', '.', $className) . $this->locator->getFileExtension()); |
| 44 | } |
| 45 | $this->classCache[$className] = $result[$className]; |
| 46 | return $result[$className]; |
| 47 | } |
| 48 | public function isTransient(string $className) |
| 49 | { |
| 50 | if ($this->classCache === null) { |
| 51 | $this->initialize(); |
| 52 | } |
| 53 | if (isset($this->classCache[$className])) { |
| 54 | return \false; |
| 55 | } |
| 56 | return !$this->locator->fileExists($className); |
| 57 | } |
| 58 | public function getAllClassNames() |
| 59 | { |
| 60 | if ($this->classCache === null) { |
| 61 | $this->initialize(); |
| 62 | } |
| 63 | if ($this->classCache === []) { |
| 64 | return $this->locator->getAllClassNames($this->globalBasename); |
| 65 | } |
| 66 | $classCache = $this->classCache; |
| 67 | $keys = array_keys($classCache); |
| 68 | return array_values(array_unique(array_merge($keys, $this->locator->getAllClassNames($this->globalBasename)))); |
| 69 | } |
| 70 | protected abstract function loadMappingFile(string $file); |
| 71 | protected function initialize() |
| 72 | { |
| 73 | $this->classCache = []; |
| 74 | if ($this->globalBasename === '') { |
| 75 | return; |
| 76 | } |
| 77 | foreach ($this->locator->getPaths() as $path) { |
| 78 | $file = $path . '/' . $this->globalBasename . $this->locator->getFileExtension(); |
| 79 | if (!is_file($file)) { |
| 80 | continue; |
| 81 | } |
| 82 | $this->classCache = array_merge($this->classCache, $this->loadMappingFile($file)); |
| 83 | } |
| 84 | } |
| 85 | public function getLocator() |
| 86 | { |
| 87 | return $this->locator; |
| 88 | } |
| 89 | public function setLocator(FileLocator $locator) |
| 90 | { |
| 91 | $this->locator = $locator; |
| 92 | } |
| 93 | } |
| 94 |