mailpoet
/
vendor-prefixed
/
doctrine
/
persistence
/
src
/
Persistence
/
Mapping
/
Driver
/
ColocatedMappingDriver.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
ColocatedMappingDriver.php
100 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 FilesystemIterator; |
| 7 | use RecursiveDirectoryIterator; |
| 8 | use RecursiveIteratorIterator; |
| 9 | use RecursiveRegexIterator; |
| 10 | use ReflectionClass; |
| 11 | use RegexIterator; |
| 12 | use function array_merge; |
| 13 | use function array_unique; |
| 14 | use function assert; |
| 15 | use function get_declared_classes; |
| 16 | use function in_array; |
| 17 | use function is_dir; |
| 18 | use function preg_match; |
| 19 | use function preg_quote; |
| 20 | use function realpath; |
| 21 | use function str_replace; |
| 22 | use function strpos; |
| 23 | trait ColocatedMappingDriver |
| 24 | { |
| 25 | protected $paths = []; |
| 26 | protected $excludePaths = []; |
| 27 | protected $fileExtension = '.php'; |
| 28 | protected $classNames; |
| 29 | public function addPaths(array $paths) |
| 30 | { |
| 31 | $this->paths = array_unique(array_merge($this->paths, $paths)); |
| 32 | } |
| 33 | public function getPaths() |
| 34 | { |
| 35 | return $this->paths; |
| 36 | } |
| 37 | public function addExcludePaths(array $paths) |
| 38 | { |
| 39 | $this->excludePaths = array_unique(array_merge($this->excludePaths, $paths)); |
| 40 | } |
| 41 | public function getExcludePaths() |
| 42 | { |
| 43 | return $this->excludePaths; |
| 44 | } |
| 45 | public function getFileExtension() |
| 46 | { |
| 47 | return $this->fileExtension; |
| 48 | } |
| 49 | public function setFileExtension(string $fileExtension) |
| 50 | { |
| 51 | $this->fileExtension = $fileExtension; |
| 52 | } |
| 53 | public abstract function isTransient(string $className); |
| 54 | public function getAllClassNames() |
| 55 | { |
| 56 | if ($this->classNames !== null) { |
| 57 | return $this->classNames; |
| 58 | } |
| 59 | if ($this->paths === []) { |
| 60 | throw MappingException::pathRequiredForDriver(static::class); |
| 61 | } |
| 62 | $classes = []; |
| 63 | $includedFiles = []; |
| 64 | foreach ($this->paths as $path) { |
| 65 | if (!is_dir($path)) { |
| 66 | throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path); |
| 67 | } |
| 68 | $iterator = new RegexIterator(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::LEAVES_ONLY), '/^.+' . preg_quote($this->fileExtension) . '$/i', RecursiveRegexIterator::GET_MATCH); |
| 69 | foreach ($iterator as $file) { |
| 70 | $sourceFile = $file[0]; |
| 71 | if (preg_match('(^phar:)i', $sourceFile) === 0) { |
| 72 | $sourceFile = realpath($sourceFile); |
| 73 | } |
| 74 | foreach ($this->excludePaths as $excludePath) { |
| 75 | $realExcludePath = realpath($excludePath); |
| 76 | assert($realExcludePath !== \false); |
| 77 | $exclude = str_replace('\\', '/', $realExcludePath); |
| 78 | $current = str_replace('\\', '/', $sourceFile); |
| 79 | if (strpos($current, $exclude) !== \false) { |
| 80 | continue 2; |
| 81 | } |
| 82 | } |
| 83 | require_once $sourceFile; |
| 84 | $includedFiles[] = $sourceFile; |
| 85 | } |
| 86 | } |
| 87 | $declared = get_declared_classes(); |
| 88 | foreach ($declared as $className) { |
| 89 | $rc = new ReflectionClass($className); |
| 90 | $sourceFile = $rc->getFileName(); |
| 91 | if (!in_array($sourceFile, $includedFiles, \true) || $this->isTransient($className)) { |
| 92 | continue; |
| 93 | } |
| 94 | $classes[] = $className; |
| 95 | } |
| 96 | $this->classNames = $classes; |
| 97 | return $classes; |
| 98 | } |
| 99 | } |
| 100 |