Skip to content

Commit

Permalink
Add path loader to fluent driver
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickbrouwers committed Jul 4, 2017
1 parent 99f1182 commit e2e1441
Showing 1 changed file with 92 additions and 2 deletions.
94 changes: 92 additions & 2 deletions src/FluentDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
}
}

/**
Expand Down Expand Up @@ -78,8 +101,75 @@ public function isTransient($className)
$this->mappers->getMapperFor($className)->isTransient();
}

/**
* @param array $paths
*
* @throws MappingException
*/
public function loadPaths(array $paths)
{
$classes = [];
$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) {
$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 = [])
{
Expand Down

0 comments on commit e2e1441

Please sign in to comment.