mailpoet
/
vendor-prefixed
/
doctrine
/
persistence
/
src
/
Persistence
/
Mapping
/
Driver
/
StaticPHPDriver.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
StaticPHPDriver.php
74 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\ClassMetadata; |
| 6 | use MailPoetVendor\Doctrine\Persistence\Mapping\MappingException; |
| 7 | use RecursiveDirectoryIterator; |
| 8 | use RecursiveIteratorIterator; |
| 9 | use ReflectionClass; |
| 10 | use function array_merge; |
| 11 | use function array_unique; |
| 12 | use function get_declared_classes; |
| 13 | use function in_array; |
| 14 | use function is_dir; |
| 15 | use function method_exists; |
| 16 | use function realpath; |
| 17 | class StaticPHPDriver implements MappingDriver |
| 18 | { |
| 19 | private $paths = []; |
| 20 | private $classNames; |
| 21 | public function __construct($paths) |
| 22 | { |
| 23 | $this->addPaths((array) $paths); |
| 24 | } |
| 25 | public function addPaths(array $paths) |
| 26 | { |
| 27 | $this->paths = array_unique(array_merge($this->paths, $paths)); |
| 28 | } |
| 29 | public function loadMetadataForClass(string $className, ClassMetadata $metadata) |
| 30 | { |
| 31 | $className::loadMetadata($metadata); |
| 32 | } |
| 33 | public function getAllClassNames() |
| 34 | { |
| 35 | if ($this->classNames !== null) { |
| 36 | return $this->classNames; |
| 37 | } |
| 38 | if ($this->paths === []) { |
| 39 | throw MappingException::pathRequiredForDriver(static::class); |
| 40 | } |
| 41 | $classes = []; |
| 42 | $includedFiles = []; |
| 43 | foreach ($this->paths as $path) { |
| 44 | if (!is_dir($path)) { |
| 45 | throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path); |
| 46 | } |
| 47 | $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::LEAVES_ONLY); |
| 48 | foreach ($iterator as $file) { |
| 49 | if ($file->getBasename('.php') === $file->getBasename()) { |
| 50 | continue; |
| 51 | } |
| 52 | $sourceFile = realpath($file->getPathName()); |
| 53 | require_once $sourceFile; |
| 54 | $includedFiles[] = $sourceFile; |
| 55 | } |
| 56 | } |
| 57 | $declared = get_declared_classes(); |
| 58 | foreach ($declared as $className) { |
| 59 | $rc = new ReflectionClass($className); |
| 60 | $sourceFile = $rc->getFileName(); |
| 61 | if (!in_array($sourceFile, $includedFiles, \true) || $this->isTransient($className)) { |
| 62 | continue; |
| 63 | } |
| 64 | $classes[] = $className; |
| 65 | } |
| 66 | $this->classNames = $classes; |
| 67 | return $classes; |
| 68 | } |
| 69 | public function isTransient(string $className) |
| 70 | { |
| 71 | return !method_exists($className, 'loadMetadata'); |
| 72 | } |
| 73 | } |
| 74 |