-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fluent mapping path loader #48
base: 1.1
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
language: php | ||
|
||
php: | ||
- 5.5 | ||
- 5.6 | ||
- 7.0 | ||
- 7.1 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,9 +6,15 @@ | |
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver; | ||
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder; | ||
use Doctrine\ORM\Mapping\MappingException; | ||
use FilesystemIterator; | ||
use InvalidArgumentException; | ||
use LaravelDoctrine\Fluent\Builders\Builder; | ||
use LaravelDoctrine\Fluent\Mappers\MapperSet; | ||
use RecursiveDirectoryIterator; | ||
use RecursiveIteratorIterator; | ||
use RecursiveRegexIterator; | ||
use ReflectionClass; | ||
use RegexIterator; | ||
|
||
class FluentDriver implements MappingDriver | ||
{ | ||
|
@@ -22,20 +28,37 @@ class FluentDriver implements MappingDriver | |
*/ | ||
protected $fluentFactory; | ||
|
||
/** | ||
* The file extension of mapping documents. | ||
* | ||
* @var string | ||
*/ | ||
protected $fileExtension = '.php'; | ||
|
||
/** | ||
* Initializes a new FileDriver that looks in the given path(s) for mapping | ||
* documents and operates in the specified operating mode. | ||
* | ||
* @param string[] $mappings | ||
* @param array $paths | ||
* | ||
* @throws MappingException | ||
*/ | ||
public function __construct(array $mappings = []) | ||
public function __construct(array $mappings = [], array $paths = []) | ||
{ | ||
$this->fluentFactory = function (ClassMetadata $metadata) { | ||
return new Builder(new ClassMetadataBuilder($metadata)); | ||
}; | ||
|
||
$this->mappers = new MapperSet(); | ||
$this->addMappings($mappings); | ||
|
||
if (!empty($paths)) { | ||
$this->loadPaths($paths); | ||
} | ||
|
||
if (!empty($mappings)) { | ||
$this->addMappings($mappings); | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -78,8 +101,74 @@ public function isTransient($className) | |
$this->mappers->getMapperFor($className)->isTransient(); | ||
} | ||
|
||
/** | ||
* @param array $paths | ||
* | ||
* @throws MappingException | ||
*/ | ||
public function loadPaths(array $paths) | ||
{ | ||
$includedFiles = []; | ||
|
||
foreach ($paths as $path) { | ||
if (!is_dir($path)) { | ||
throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path); | ||
} | ||
|
||
$iterator = new RegexIterator( | ||
new RecursiveIteratorIterator( | ||
new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS), | ||
RecursiveIteratorIterator::LEAVES_ONLY | ||
), | ||
'/^.+'.preg_quote($this->fileExtension).'$/i', | ||
RecursiveRegexIterator::GET_MATCH | ||
); | ||
|
||
foreach ($iterator as $file) { | ||
$sourceFile = $file[0]; | ||
|
||
if (!preg_match('(^phar:)i', $sourceFile)) { | ||
$sourceFile = realpath($sourceFile); | ||
} | ||
|
||
require_once $sourceFile; | ||
|
||
$includedFiles[] = $sourceFile; | ||
} | ||
|
||
$declared = get_declared_classes(); | ||
|
||
foreach ($declared as $className) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @patrickbrouwers this list could be huge. We could use some convention to skip building a ReflectionClass for each loaded class in the system... Or maybe parse filename and class name and only reflect on basename matches. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But that would require people to actually have a Like previous comment, this is also the way the AnnotationsDriver does things. It's cached in production anyway. |
||
$rc = new ReflectionClass($className); | ||
$sourceFile = $rc->getFileName(); | ||
|
||
if (!in_array($sourceFile, $includedFiles)) { | ||
continue; | ||
} | ||
|
||
if ($rc->isAbstract() || $rc->isInterface()) { | ||
continue; | ||
} | ||
|
||
if (!$rc->implementsInterface(Mapping::class)) { | ||
continue; | ||
} | ||
|
||
if ($this->isTransient($className)) { | ||
|
||
/** @var Mapping $mapping */ | ||
$mapping = $rc->newInstanceWithoutConstructor(); | ||
|
||
$this->addMapping($mapping); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param string[] $mappings | ||
* | ||
* @throws InvalidArgumentException | ||
*/ | ||
public function addMappings(array $mappings = []) | ||
{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@patrickbrouwers I don't like this strategy, because it depends on those files not being previously loaded. I don't know what would happen with opcache, for example.
I think we talked about this in the previous PR.
I'd agree to merge as-is, but I wouldn't trust this strategy for my projects.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is actually copy pasted from the AnnotationsDriver.