Completed
Push — master ( ca354f...7077f3 )
by Michal
09:04
created

TemplateRouter::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 11

Duplication

Lines 18
Ratio 100 %

Importance

Changes 0
Metric Value
dl 18
loc 18
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 4
nop 3
1
<?php
2
declare(strict_types=1);
3
4
use Nette\Application\Routers;
5
use \Nette\Bridges\ApplicationLatte\ILatteFactory;
6
7
8
/**
9
 * Micro-framework router for templates using {url} macro.
10
 */
11 View Code Duplication
class TemplateRouter extends Routers\RouteList
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
12
{
13
    /** @var ILatteFactory  */
14
    private $latteFactory;
15
16
	public function __construct($path, $cachePath, ILatteFactory $latteFactory)
17
	{
18
	    parent::__construct();
19
	    $this->latteFactory = $latteFactory;
20
21
		if (is_file($cacheFile = $cachePath . '/routes.php')) {
22
			$routes = require $cacheFile;
23
		} else {
24
			$routes = $this->scanRoutes($path);
25
			file_put_contents($cacheFile, '<?php return ' . var_export($routes, true) . ';');
26
		}
27
28
		foreach ($routes as $mask => $file) {
29
			$this[] = new Routers\Route($mask, function ($presenter) use ($file, $cachePath, $latteFactory) {
0 ignored issues
show
Documentation introduced by
function ($presenter) us...e'))->setFile($file); } is of type object<Closure>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
30
				return $presenter->createTemplate(null, [$this, 'createLatte'])->setFile($file);
31
			});
32
		}
33
	}
34
35
36
	public function scanRoutes($path)
37
	{
38
		$routes = [];
39
40
        $latte = $this->createLatte();
41
        $macroSet = new Latte\Macros\MacroSet($latte->getCompiler());
42
		$macroSet->addMacro('url', function ($node) use (&$routes, &$file) {
43
			$routes[$node->args] = (string) $file;
44
		}, null, null, $macroSet::ALLOWED_IN_HEAD);
45
46
		foreach (Nette\Utils\Finder::findFiles('*.latte')->from($path) as $file) {
47
			$latte->compile($file);
48
		}
49
50
		return $routes;
51
	}
52
53
	public function createLatte() {
54
        $latte = $this->latteFactory->create();
55
        $macroSet = new Latte\Macros\MacroSet($latte->getCompiler());
56
        $macroSet->addMacro('url', function () {}, null, null, $macroSet::ALLOWED_IN_HEAD); // ignore
57
        return $latte;
58
    }
59
}
60