Skip to content

Commit

Permalink
ApplicationExtension: can use external RobotLoader [Closes #227]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Sep 17, 2019
1 parent d2e8e63 commit 3f29043
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/Bridges/ApplicationDI/ApplicationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,22 @@ final class ApplicationExtension extends Nette\DI\CompilerExtension
/** @var array */
private $scanDirs;

/** @var Nette\Loaders\RobotLoader|null */
private $robotLoader;

/** @var int */
private $invalidLinkMode;

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


public function __construct(bool $debugMode = false, array $scanDirs = null, string $tempDir = null)
public function __construct(bool $debugMode = false, array $scanDirs = null, string $tempDir = null, Nette\Loaders\RobotLoader $robotLoader = null)
{
$this->debugMode = $debugMode;
$this->scanDirs = (array) $scanDirs;
$this->tempDir = $tempDir;
$this->robotLoader = $robotLoader;
}


Expand All @@ -50,7 +54,7 @@ public function getConfigSchema(): Nette\Schema\Schema
'errorPresenter' => Expect::string('Nette:Error')->dynamic(),
'catchExceptions' => Expect::bool(!$this->debugMode)->dynamic(),
'mapping' => Expect::arrayOf('string|array'),
'scanDirs' => Expect::anyOf(Expect::arrayOf('string'), false)->default($this->scanDirs),
'scanDirs' => Expect::anyOf(Expect::arrayOf('string'), false),
'scanComposer' => Expect::bool(class_exists(ClassLoader::class)),
'scanFilter' => Expect::string('Presenter'),
'silentLinks' => Expect::bool(),
Expand Down Expand Up @@ -78,7 +82,7 @@ public function loadConfiguration()
}
$this->compiler->addExportedType(Nette\Application\Application::class);

$touch = $this->debugMode && $config->scanDirs && $this->tempDir ? $this->tempDir . '/touch' : null;
$touch = $this->debugMode && ($config->scanDirs || $this->robotLoader) && $this->tempDir ? $this->tempDir . '/touch' : null;
$presenterFactory = $builder->addDefinition($this->prefix('presenterFactory'))
->setType(Nette\Application\IPresenterFactory::class)
->setFactory(Nette\Application\PresenterFactory::class, [new Definitions\Statement(
Expand Down Expand Up @@ -133,9 +137,15 @@ public function beforeCompile()
private function findPresenters(): array
{
$config = $this->getConfig();
$classes = [];

if ($config->scanDirs) {
if ($this->robotLoader) {
if ($config->scanDirs) {
trigger_error("Option 'scanDir' has no effect, global RobotLoader is used.", E_USER_DEPRECATED);
}
$robot = $this->robotLoader;
$robot->refresh();

} elseif ($config->scanDirs) {
if (!class_exists(Nette\Loaders\RobotLoader::class)) {
throw new Nette\NotSupportedException("RobotLoader is required to find presenters, install package `nette/robot-loader` or disable option {$this->prefix('scanDirs')}: false");
}
Expand All @@ -148,6 +158,10 @@ private function findPresenters(): array
} else {
$robot->rebuild();
}
}

$classes = [];
if (isset($robot)) {
$classes = array_keys($robot->getIndexedClasses());
$this->getContainerBuilder()->addDependency($this->tempDir . '/touch');
}
Expand Down

0 comments on commit 3f29043

Please # to comment.