-
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
Convert paths into Mapping objects #49
base: 1.x
Are you sure you want to change the base?
Changes from 2 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 |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
namespace LaravelDoctrine\Fluent; | ||
|
||
use Doctrine\ORM\Mapping\MappingException; | ||
use FilesystemIterator; | ||
use RecursiveDirectoryIterator; | ||
use RecursiveIteratorIterator; | ||
use RecursiveRegexIterator; | ||
use ReflectionClass; | ||
use RegexIterator; | ||
|
||
/** | ||
* Returns an array of Mapping objects found on the given paths. | ||
* | ||
* @param string[] $paths | ||
* @param string $fileExtension | ||
* | ||
* @throws MappingException | ||
* | ||
* @return Mapping[] | ||
*/ | ||
function mappingsFrom(array $paths, $fileExtension = '.php') | ||
{ | ||
$includedFiles = []; | ||
|
||
foreach ($paths as $path) { | ||
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 I'm pretty sure we should be able to replace this loop with another iterator, so that we iterate recursively over each path, instead of building the full iterator chain for each path in the array. |
||
if (!is_dir($path)) { | ||
throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path); | ||
} | ||
|
||
$iterator = new RegexIterator( | ||
new RecursiveIteratorIterator( | ||
new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS), | ||
RecursiveIteratorIterator::LEAVES_ONLY | ||
), | ||
'/^.+'.preg_quote($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] = true; | ||
} | ||
} | ||
|
||
$mappings = []; | ||
$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 moved inner loop outside the |
||
$rc = new ReflectionClass($className); | ||
$sourceFile = $rc->getFileName(); | ||
if ($sourceFile === false || !array_key_exists($sourceFile, $includedFiles)) { | ||
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 switched to key check instead of |
||
continue; | ||
} | ||
|
||
if ($rc->isAbstract() || $rc->isInterface()) { | ||
continue; | ||
} | ||
|
||
if (!$rc->implementsInterface(Mapping::class)) { | ||
continue; | ||
} | ||
|
||
$mappings[] = $rc->newInstanceWithoutConstructor(); | ||
} | ||
|
||
return $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 there's no other way to load functions, right? I couldn't find documentation of namespaced functions in composer docs.
Oh, this probably breaks in php 5.5, so we should bump anyway.
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.
Commited bump in here so travis doesn't fail on phpunit run.