Completed
Push — master ( 74371a...6da8c0 )
by Vladimir
02:21
created

AbstractFilesystemTwigExtension::__invoke()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
rs 9.4285
cc 3
eloc 10
nc 4
nop 2
1
<?php
2
3
/**
4
 * @copyright 2018 Vladimir Jimenez
5
 * @license   https://github.com/allejo/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\Templating\Twig\Extension;
9
10
use allejo\stakx\Service;
11
use allejo\stakx\Filesystem\FilesystemLoader as fs;
12
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
13
use Twig_Environment;
14
15
abstract class AbstractFilesystemTwigExtension extends AbstractTwigExtension
16
{
17
    protected $globs;
18
    protected $dir;
19
    protected $path;
20
21
    public function __invoke(Twig_Environment $env, $location)
22
    {
23
        $this->globs = $env->getGlobals();
24
        $this->dir = fs::getFolderPath($this->globs['__currentTemplate']);
25
        $this->path = fs::appendPath($this->dir, $location);
26
27
        if (is_file($this->path))
28
        {
29
            $this->path = realpath($this->path);
30
        }
31
32
        if (strpos($this->path, Service::getWorkingDirectory()) !== 0)
33
        {
34
            throw new FileNotFoundException(sprintf(
35
                "The '%s' file could not be found or is outside the website working directory",
36
                $location
37
            ));
38
        }
39
    }
40
}
41