Skip to content
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

[FEATURE] Allow loading mappings from paths #41

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 60 additions & 10 deletions src/FluentDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,34 @@ class FluentDriver implements MappingDriver
protected $mappers;

/**
* @type callable
* @var callable
*/
protected $fluentFactory;

/**
* 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 string[]|null $mappings
* @param string[]|null $paths
*
* @throws \Doctrine\ORM\Mapping\MappingException
*/
public function __construct(array $mappings = [])
public function __construct($mappings = null, $paths = null)
{
$this->fluentFactory = function (ClassMetadata $metadata) {
return new Builder(new ClassMetadataBuilder($metadata));
};

$this->mappers = new MapperSet();
$this->addMappings($mappings);

if ($mappings !== null) {
$this->addMappings($mappings);
}

if ($paths !== null) {
$this->addPaths($paths);
}
}

/**
Expand All @@ -55,7 +65,8 @@ public function loadMetadataForClass($className, ClassMetadata $metadata)
* Gets the names of all mapped classes known to this driver.
*
* @throws MappingException
* @return string[] The names of all mapped classes known to this driver.
*
* @return string[] The names of all mapped classes known to this driver.
*/
public function getAllClassNames()
{
Expand All @@ -73,7 +84,7 @@ public function getAllClassNames()
public function isTransient($className)
{
return
! $this->mappers->hasMapperFor($className) ||
!$this->mappers->hasMapperFor($className) ||
$this->mappers->getMapperFor($className)->isTransient();
}

Expand All @@ -87,10 +98,10 @@ public function addMappings(array $mappings = [])
throw new InvalidArgumentException("Mapping class [{$class}] does not exist");
}

$mapping = new $class;
$mapping = new $class();

if (!$mapping instanceof Mapping) {
throw new InvalidArgumentException("Mapping class [{$class}] should implement " . Mapping::class);
throw new InvalidArgumentException("Mapping class [{$class}] should implement ".Mapping::class);
}

$this->addMapping($mapping);
Expand All @@ -101,7 +112,6 @@ public function addMappings(array $mappings = [])
* @param Mapping $mapping
*
* @throws MappingException
* @return void
*/
public function addMapping(Mapping $mapping)
{
Expand All @@ -116,6 +126,45 @@ public function getMappers()
return $this->mappers;
}

/**
* Add mappings from an array of folders.
*
* @param string[] $paths
*
* @throws MappingException
*/
public function addPaths($paths)
{
$includedFiles = [];
foreach ($paths as $path) {
if (!is_dir($path)) {
throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
}

$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path), \RecursiveIteratorIterator::LEAVES_ONLY);

foreach ($iterator as $file) {
if ($file->getBasename('.php') == $file->getBasename()) {
continue;
}

$sourceFile = realpath($file->getPathName());
require_once $sourceFile;
Copy link
Member Author

@FoxxMD FoxxMD Oct 24, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guiwoda This is how doctrine finds classes for the StaticPHPDriver

$includedFiles[] = $sourceFile;
}
}

$declared = get_declared_classes();

foreach ($declared as $className) {
$rc = new \ReflectionClass($className);
$sourceFile = $rc->getFileName();
if (in_array($sourceFile, $includedFiles) && !$this->mappers->hasMapperFor($className)) {
$this->addMapping(new $className());
}
}
}

/**
* Override the default Fluent factory method with a custom one.
* Use this to implement your own Fluent builder.
Expand All @@ -129,7 +178,8 @@ public function setFluentFactory(callable $factory)
}

/**
* @param ClassMetadata $metadata
* @param ClassMetadata $metadata
*
* @return Fluent
*/
protected function getFluent(ClassMetadata $metadata)
Expand Down