mailpoet
/
vendor-prefixed
/
doctrine
/
persistence
/
src
/
Persistence
/
Mapping
/
Driver
/
SymfonyFileLocator.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
SymfonyFileLocator.php
144 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 InvalidArgumentException; |
| 7 | use RecursiveDirectoryIterator; |
| 8 | use RecursiveIteratorIterator; |
| 9 | use RuntimeException; |
| 10 | use function array_keys; |
| 11 | use function array_merge; |
| 12 | use function assert; |
| 13 | use function is_dir; |
| 14 | use function is_file; |
| 15 | use function is_int; |
| 16 | use function realpath; |
| 17 | use function sprintf; |
| 18 | use function str_replace; |
| 19 | use function strlen; |
| 20 | use function strpos; |
| 21 | use function strrpos; |
| 22 | use function strtr; |
| 23 | use function substr; |
| 24 | use const DIRECTORY_SEPARATOR; |
| 25 | class SymfonyFileLocator implements FileLocator |
| 26 | { |
| 27 | protected $paths = []; |
| 28 | protected $prefixes = []; |
| 29 | protected $fileExtension; |
| 30 | private $nsSeparator; |
| 31 | public function __construct(array $prefixes, string $fileExtension = '', string $nsSeparator = '.') |
| 32 | { |
| 33 | $this->addNamespacePrefixes($prefixes); |
| 34 | $this->fileExtension = $fileExtension; |
| 35 | if ($nsSeparator === '') { |
| 36 | throw new InvalidArgumentException('Namespace separator should not be empty'); |
| 37 | } |
| 38 | $this->nsSeparator = $nsSeparator; |
| 39 | } |
| 40 | public function addNamespacePrefixes(array $prefixes) |
| 41 | { |
| 42 | $this->prefixes = array_merge($this->prefixes, $prefixes); |
| 43 | $this->paths = array_merge($this->paths, array_keys($prefixes)); |
| 44 | } |
| 45 | public function getNamespacePrefixes() |
| 46 | { |
| 47 | return $this->prefixes; |
| 48 | } |
| 49 | public function getPaths() |
| 50 | { |
| 51 | return $this->paths; |
| 52 | } |
| 53 | public function getFileExtension() |
| 54 | { |
| 55 | return $this->fileExtension; |
| 56 | } |
| 57 | public function setFileExtension(string $fileExtension) |
| 58 | { |
| 59 | $this->fileExtension = $fileExtension; |
| 60 | } |
| 61 | public function fileExists(string $className) |
| 62 | { |
| 63 | $defaultFileName = str_replace('\\', $this->nsSeparator, $className) . $this->fileExtension; |
| 64 | foreach ($this->paths as $path) { |
| 65 | if (!isset($this->prefixes[$path])) { |
| 66 | // global namespace class |
| 67 | if (is_file($path . DIRECTORY_SEPARATOR . $defaultFileName)) { |
| 68 | return \true; |
| 69 | } |
| 70 | continue; |
| 71 | } |
| 72 | $prefix = $this->prefixes[$path]; |
| 73 | if (strpos($className, $prefix . '\\') !== 0) { |
| 74 | continue; |
| 75 | } |
| 76 | $filename = $path . '/' . strtr(substr($className, strlen($prefix) + 1), '\\', $this->nsSeparator) . $this->fileExtension; |
| 77 | if (is_file($filename)) { |
| 78 | return \true; |
| 79 | } |
| 80 | } |
| 81 | return \false; |
| 82 | } |
| 83 | public function getAllClassNames(?string $globalBasename = null) |
| 84 | { |
| 85 | if ($this->paths === []) { |
| 86 | return []; |
| 87 | } |
| 88 | $classes = []; |
| 89 | foreach ($this->paths as $path) { |
| 90 | if (!is_dir($path)) { |
| 91 | throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path); |
| 92 | } |
| 93 | $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::LEAVES_ONLY); |
| 94 | foreach ($iterator as $file) { |
| 95 | $fileName = $file->getBasename($this->fileExtension); |
| 96 | if ($fileName === $file->getBasename() || $fileName === $globalBasename) { |
| 97 | continue; |
| 98 | } |
| 99 | // NOTE: All files found here means classes are not transient! |
| 100 | if (isset($this->prefixes[$path])) { |
| 101 | // Calculate namespace suffix for given prefix as a relative path from basepath to file path |
| 102 | $nsSuffix = strtr(substr($this->realpath($file->getPath()), strlen($this->realpath($path))), $this->nsSeparator, '\\'); |
| 103 | $class = $this->prefixes[$path] . str_replace(DIRECTORY_SEPARATOR, '\\', $nsSuffix) . '\\' . str_replace($this->nsSeparator, '\\', $fileName); |
| 104 | } else { |
| 105 | $class = str_replace($this->nsSeparator, '\\', $fileName); |
| 106 | } |
| 107 | $classes[] = $class; |
| 108 | } |
| 109 | } |
| 110 | return $classes; |
| 111 | } |
| 112 | public function findMappingFile(string $className) |
| 113 | { |
| 114 | $defaultFileName = str_replace('\\', $this->nsSeparator, $className) . $this->fileExtension; |
| 115 | foreach ($this->paths as $path) { |
| 116 | if (!isset($this->prefixes[$path])) { |
| 117 | if (is_file($path . DIRECTORY_SEPARATOR . $defaultFileName)) { |
| 118 | return $path . DIRECTORY_SEPARATOR . $defaultFileName; |
| 119 | } |
| 120 | continue; |
| 121 | } |
| 122 | $prefix = $this->prefixes[$path]; |
| 123 | if (strpos($className, $prefix . '\\') !== 0) { |
| 124 | continue; |
| 125 | } |
| 126 | $filename = $path . '/' . strtr(substr($className, strlen($prefix) + 1), '\\', $this->nsSeparator) . $this->fileExtension; |
| 127 | if (is_file($filename)) { |
| 128 | return $filename; |
| 129 | } |
| 130 | } |
| 131 | $pos = strrpos($className, '\\'); |
| 132 | assert(is_int($pos)); |
| 133 | throw MappingException::mappingFileNotFound($className, substr($className, $pos + 1) . $this->fileExtension); |
| 134 | } |
| 135 | private function realpath(string $path) : string |
| 136 | { |
| 137 | $realpath = realpath($path); |
| 138 | if ($realpath === \false) { |
| 139 | throw new RuntimeException(sprintf('Could not get realpath for %s', $path)); |
| 140 | } |
| 141 | return $realpath; |
| 142 | } |
| 143 | } |
| 144 |