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 18, 2019
1 parent d2e8e63 commit a9cb581
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
22 changes: 18 additions & 4 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 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 && $config->scanDirs !== $this->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
22 changes: 22 additions & 0 deletions tests/Bridges.DI/ApplicationExtension.scan.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,25 @@ test(function () {
$name = $container->findByType(Presenter1::class)[0];
Assert::same('test', $container->createService($name)->getView());
});


test(function () {
$robot = new Nette\Loaders\RobotLoader;
$robot->addDirectory(__DIR__ . '/files');
$robot->setTempDirectory(getTempDir());

$compiler = new DI\Compiler;
$compiler->addExtension('application', new ApplicationExtension(false, null, null, $robot));

$builder = $compiler->getContainerBuilder();
$builder->addDefinition('myRouter')->setFactory(Nette\Application\Routers\SimpleRouter::class);
$builder->addDefinition('myHttpRequest')->setFactory(Nette\Http\Request::class, [new DI\Statement(Nette\Http\UrlScript::class)]);
$builder->addDefinition('myHttpResponse')->setFactory(Nette\Http\Response::class);
$code = $compiler->setClassName('Container4')->compile();
eval($code);

$container = new Container2;
Assert::count(3, $container->findByType(BasePresenter::class));
Assert::count(1, $container->findByType(Presenter1::class));
Assert::count(1, $container->findByType(Presenter2::class));
});
21 changes: 21 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,27 @@
}, ob_get_level());


function getTempDir(): string
{
$dir = __DIR__ . '/tmp/' . getmypid();

if (empty($GLOBALS['\\lock'])) {
// garbage collector
$GLOBALS['\\lock'] = $lock = fopen(__DIR__ . '/lock', 'w');
if (rand(0, 100)) {
flock($lock, LOCK_SH);
@mkdir(dirname($dir));
} elseif (flock($lock, LOCK_EX)) {
Tester\Helpers::purge(dirname($dir));
}

@mkdir($dir);
}

return $dir;
}


function test(\Closure $function): void
{
$function();
Expand Down

0 comments on commit a9cb581

Please sign in to comment.