Skip to content

Commit 3a65046

Browse files
committed
ApplicationExtension: can use external RobotLoader [Closes #227]
1 parent 03e9428 commit 3a65046

File tree

3 files changed

+61
-4
lines changed

3 files changed

+61
-4
lines changed

src/Bridges/ApplicationDI/ApplicationExtension.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,22 @@ final class ApplicationExtension extends Nette\DI\CompilerExtension
2828
/** @var array */
2929
private $scanDirs;
3030

31+
/** @var Nette\Loaders\RobotLoader|null */
32+
private $robotLoader;
33+
3134
/** @var int */
3235
private $invalidLinkMode;
3336

3437
/** @var string|null */
3538
private $tempDir;
3639

3740

38-
public function __construct(bool $debugMode = false, array $scanDirs = null, string $tempDir = null)
41+
public function __construct(bool $debugMode = false, array $scanDirs = null, string $tempDir = null, Nette\Loaders\RobotLoader $robotLoader = null)
3942
{
4043
$this->debugMode = $debugMode;
4144
$this->scanDirs = (array) $scanDirs;
4245
$this->tempDir = $tempDir;
46+
$this->robotLoader = $robotLoader;
4347
}
4448

4549

@@ -78,7 +82,7 @@ public function loadConfiguration()
7882
}
7983
$this->compiler->addExportedType(Nette\Application\Application::class);
8084

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

138-
if ($config->scanDirs) {
141+
if ($this->robotLoader) {
142+
if ($config->scanDirs && $config->scanDirs !== $this->scanDirs) {
143+
trigger_error("Option 'scanDir' has no effect, global RobotLoader is used.", E_USER_DEPRECATED);
144+
}
145+
$robot = $this->robotLoader;
146+
$robot->refresh();
147+
148+
} elseif ($config->scanDirs) {
139149
if (!class_exists(Nette\Loaders\RobotLoader::class)) {
140150
throw new Nette\NotSupportedException("RobotLoader is required to find presenters, install package `nette/robot-loader` or disable option {$this->prefix('scanDirs')}: false");
141151
}
@@ -148,6 +158,10 @@ private function findPresenters(): array
148158
} else {
149159
$robot->rebuild();
150160
}
161+
}
162+
163+
$classes = [];
164+
if (isset($robot)) {
151165
$classes = array_keys($robot->getIndexedClasses());
152166
$this->getContainerBuilder()->addDependency($this->tempDir . '/touch');
153167
}

tests/Bridges.DI/ApplicationExtension.scan.phpt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,25 @@ test(function () {
7878
$name = $container->findByType(Presenter1::class)[0];
7979
Assert::same('test', $container->createService($name)->getView());
8080
});
81+
82+
83+
test(function () {
84+
$robot = new Nette\Loaders\RobotLoader;
85+
$robot->addDirectory(__DIR__ . '/files');
86+
$robot->setTempDirectory(getTempDir());
87+
88+
$compiler = new DI\Compiler;
89+
$compiler->addExtension('application', new ApplicationExtension(false, null, null, $robot));
90+
91+
$builder = $compiler->getContainerBuilder();
92+
$builder->addDefinition('myRouter')->setFactory(Nette\Application\Routers\SimpleRouter::class);
93+
$builder->addDefinition('myHttpRequest')->setFactory(Nette\Http\Request::class, [new DI\Statement(Nette\Http\UrlScript::class)]);
94+
$builder->addDefinition('myHttpResponse')->setFactory(Nette\Http\Response::class);
95+
$code = $compiler->setClassName('Container4')->compile();
96+
eval($code);
97+
98+
$container = new Container2;
99+
Assert::count(3, $container->findByType(BasePresenter::class));
100+
Assert::count(1, $container->findByType(Presenter1::class));
101+
Assert::count(1, $container->findByType(Presenter2::class));
102+
});

tests/bootstrap.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,27 @@
2222
}, ob_get_level());
2323

2424

25+
function getTempDir(): string
26+
{
27+
$dir = __DIR__ . '/tmp/' . getmypid();
28+
29+
if (empty($GLOBALS['\\lock'])) {
30+
// garbage collector
31+
$GLOBALS['\\lock'] = $lock = fopen(__DIR__ . '/lock', 'w');
32+
if (rand(0, 100)) {
33+
flock($lock, LOCK_SH);
34+
@mkdir(dirname($dir));
35+
} elseif (flock($lock, LOCK_EX)) {
36+
Tester\Helpers::purge(dirname($dir));
37+
}
38+
39+
@mkdir($dir);
40+
}
41+
42+
return $dir;
43+
}
44+
45+
2546
function test(\Closure $function): void
2647
{
2748
$function();

0 commit comments

Comments
 (0)