mailpoet
/
vendor-prefixed
/
doctrine
/
persistence
/
src
/
Persistence
/
Mapping
/
Driver
/
DefaultFileLocator.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
DefaultFileLocator.php
88 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 RecursiveDirectoryIterator; |
| 7 | use RecursiveIteratorIterator; |
| 8 | use function array_merge; |
| 9 | use function array_unique; |
| 10 | use function assert; |
| 11 | use function is_dir; |
| 12 | use function is_file; |
| 13 | use function is_string; |
| 14 | use function str_replace; |
| 15 | use const DIRECTORY_SEPARATOR; |
| 16 | class DefaultFileLocator implements FileLocator |
| 17 | { |
| 18 | protected $paths = []; |
| 19 | protected $fileExtension; |
| 20 | public function __construct($paths, ?string $fileExtension = null) |
| 21 | { |
| 22 | $this->addPaths((array) $paths); |
| 23 | $this->fileExtension = $fileExtension; |
| 24 | } |
| 25 | public function addPaths(array $paths) |
| 26 | { |
| 27 | $this->paths = array_unique(array_merge($this->paths, $paths)); |
| 28 | } |
| 29 | public function getPaths() |
| 30 | { |
| 31 | return $this->paths; |
| 32 | } |
| 33 | public function getFileExtension() |
| 34 | { |
| 35 | return $this->fileExtension; |
| 36 | } |
| 37 | public function setFileExtension(?string $fileExtension) |
| 38 | { |
| 39 | $this->fileExtension = $fileExtension; |
| 40 | } |
| 41 | public function findMappingFile(string $className) |
| 42 | { |
| 43 | $fileName = str_replace('\\', '.', $className) . $this->fileExtension; |
| 44 | // Check whether file exists |
| 45 | foreach ($this->paths as $path) { |
| 46 | if (is_file($path . DIRECTORY_SEPARATOR . $fileName)) { |
| 47 | return $path . DIRECTORY_SEPARATOR . $fileName; |
| 48 | } |
| 49 | } |
| 50 | throw MappingException::mappingFileNotFound($className, $fileName); |
| 51 | } |
| 52 | public function getAllClassNames(string $globalBasename) |
| 53 | { |
| 54 | if ($this->paths === []) { |
| 55 | return []; |
| 56 | } |
| 57 | $classes = []; |
| 58 | foreach ($this->paths as $path) { |
| 59 | if (!is_dir($path)) { |
| 60 | throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path); |
| 61 | } |
| 62 | $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::LEAVES_ONLY); |
| 63 | foreach ($iterator as $file) { |
| 64 | $fileName = $file->getBasename($this->fileExtension); |
| 65 | if ($fileName === $file->getBasename() || $fileName === $globalBasename) { |
| 66 | continue; |
| 67 | } |
| 68 | // NOTE: All files found here means classes are not transient! |
| 69 | assert(is_string($fileName)); |
| 70 | $class = str_replace('.', '\\', $fileName); |
| 71 | $classes[] = $class; |
| 72 | } |
| 73 | } |
| 74 | return $classes; |
| 75 | } |
| 76 | public function fileExists(string $className) |
| 77 | { |
| 78 | $fileName = str_replace('\\', '.', $className) . $this->fileExtension; |
| 79 | // Check whether file exists |
| 80 | foreach ($this->paths as $path) { |
| 81 | if (is_file($path . DIRECTORY_SEPARATOR . $fileName)) { |
| 82 | return \true; |
| 83 | } |
| 84 | } |
| 85 | return \false; |
| 86 | } |
| 87 | } |
| 88 |