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

SearchExtension - ability to create factory definitions from interfaces #200

Merged
merged 2 commits into from Jul 24, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 40 additions & 10 deletions src/DI/Extensions/SearchExtension.php
Expand Up @@ -23,6 +23,9 @@ final class SearchExtension extends Nette\DI\CompilerExtension
/** @var array */
private $classes = [];

/** @var array */
private $factories = [];

/** @var string */
private $tempDir;

Expand Down Expand Up @@ -64,9 +67,15 @@ public function loadConfiguration()
throw new Nette\DI\InvalidConfigurationException("Option '{$this->name} › {$name} › in' must be valid directory name, '{$batch->in}' given.");
}

foreach ($this->findClasses($batch) as $class) {
[$instantiable, $interface] = $this->findClasses($batch);

foreach ($instantiable as $class) {
$this->classes[$class] = array_merge($this->classes[$class] ?? [], $batch->tags);
}

foreach ($interface as $factory) {
$this->factories[$factory] = array_merge($this->factories[$factory] ?? [], $batch->tags);
}
}
}

Expand All @@ -79,29 +88,38 @@ public function findClasses(\stdClass $config): array
$robot->acceptFiles = $config->files ?: ['*.php'];
$robot->reportParseErrors(false);
$robot->refresh();

$classes = array_unique(array_keys($robot->getIndexedClasses()));
$classes = array_filter($classes, 'class_exists');
$classes = array_filter($classes, static function ($name) { return class_exists($name) || interface_exists($name); });

$exclude = $config->exclude;
$acceptRE = self::buildNameRegexp($config->classes);
$rejectRE = self::buildNameRegexp($exclude->classes);
$acceptParent = array_merge($config->extends, $config->implements);
$rejectParent = array_merge($exclude->extends, $exclude->implements);

$found = [];
$instantiable = [];
$interface = [];
foreach ($classes as $class) {
$rc = new \ReflectionClass($class);

if (
$rc->isInstantiable()
&& (!$acceptRE || preg_match($acceptRE, $rc->getName()))
&& (!$rejectRE || !preg_match($rejectRE, $rc->getName()))
&& (!$acceptParent || Arrays::some($acceptParent, function ($nm) use ($rc) { return $rc->isSubclassOf($nm); }))
&& (!$rejectParent || Arrays::every($rejectParent, function ($nm) use ($rc) { return !$rc->isSubclassOf($nm); }))
($acceptRE && !preg_match($acceptRE, $rc->getName())) ||
($rejectRE && preg_match($rejectRE, $rc->getName())) ||
($acceptParent && Arrays::every($acceptParent, function ($nm) use ($rc) { return !$rc->isSubclassOf($nm); })) ||
($rejectParent && Arrays::some($rejectParent, function ($nm) use ($rc) { return $rc->isSubclassOf($nm); }))
) {
$found[] = $rc->getName();
continue;
}

if ($rc->isInstantiable()) {
$instantiable[] = $rc->getName();
} elseif ($rc->isInterface()) {
$interface[] = $rc->getName();
}
}
return $found;

return [$instantiable, $interface];
}


Expand All @@ -116,6 +134,18 @@ public function beforeCompile()
->setTags(Arrays::normalize($tags, true));
}
}

foreach ($this->factories as $class => $tags) {
if (!$builder->findByType($class)) {
$definition = new Nette\DI\Definitions\FactoryDefinition();
$definition
->setImplement($class)
->setAutowired(true);

$builder->addDefinition(null, $definition)
->setTags(Arrays::normalize($tags, true));
}
}
}


Expand Down