Skip to content

Commit

Permalink
Loader: add includes recursive limitation [Closes #164][Closes #162]
Browse files Browse the repository at this point in the history
  • Loading branch information
trejjam authored and dg committed Mar 26, 2018
1 parent 8eb9072 commit 2d0da64
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/DI/Config/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class Loader

private $dependencies = [];

private $loadedFiles = [];


/**
* Reads configuration from file.
Expand All @@ -39,6 +41,12 @@ public function load(string $file): array
if (!is_file($file) || !is_readable($file)) {
throw new Nette\FileNotFoundException("File '$file' is missing or is not readable.");
}

if (isset($this->loadedFiles[$file])) {
throw new Nette\InvalidStateException("Recursive included file '$file'");
}
$this->loadedFiles[$file] = true;

$this->dependencies[] = $file;
$data = $this->getAdapter($file)->load($file);

Expand All @@ -53,6 +61,7 @@ public function load(string $file): array
}
}
unset($data[self::INCLUDES_KEY]);
unset($this->loadedFiles[$file]);

return Helpers::merge($data, $merged);
}
Expand Down
19 changes: 19 additions & 0 deletions tests/DI/Loader.recursiveInclude.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/**
* Test: Nette\DI\Config\Loader max includes nesting.
*/

declare(strict_types=1);

use Nette\DI;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';

$compiler = new DI\Config\Loader();

Assert::exception(function () use ($compiler) {
$compiler->load('files/loader.recursiveInclude.neon');
}, \Nette\InvalidStateException::class, "Recursive included file 'files/loader.recursiveInclude.neon'");
2 changes: 2 additions & 0 deletions tests/DI/files/loader.recursiveInclude.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
includes:
- loader.recursiveInclude.neon

0 comments on commit 2d0da64

Please sign in to comment.