Completed
Branch 09branch (31301e)
by Anton
02:42
created

ModifiableLoader   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 88
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A getSource() 0 17 2
A exists() 0 4 1
A getNamespaces() 0 4 1
A withExtension() 0 7 1
1
<?php
2
/**
3
 * spiral
4
 *
5
 * @author    Wolfy-J
6
 */
7
8
namespace Spiral\Views\Loaders;
9
10
use Spiral\Core\Component;
11
use Spiral\Debug\Traits\BenchmarkTrait;
12
use Spiral\Views\EnvironmentInterface;
13
use Spiral\Views\LoaderInterface;
14
use Spiral\Views\ViewSource;
15
16
/**
17
 * Provides ability to wrap another loader in a given enviroment and process all view sources
18
 * before giving them to engine.
19
 *
20
 * Modifiers executed on template source BEFORE providing it to engine.
21
 */
22
class ModifiableLoader extends Component implements LoaderInterface
23
{
24
    use BenchmarkTrait;
25
26
    /**
27
     * Loaded to be used for file resolution.
28
     *
29
     * @var LoaderInterface
30
     */
31
    private $parent = null;
32
33
    /**
34
     * Required in order to give processors some context.
35
     *
36
     * @var EnvironmentInterface
37
     */
38
    private $environment = null;
39
40
    /**
41
     * @var \Spiral\Views\ProcessorInterface[]
42
     */
43
    protected $modifiers = [];
44
45
    /**
46
     * ProcessableLoader constructor.
47
     *
48
     * @param EnvironmentInterface $environment
49
     * @param LoaderInterface      $loader
50
     * @param array                $modifiers
51
     */
52
    public function __construct(
53
        EnvironmentInterface $environment,
54
        LoaderInterface $loader,
55
        array $modifiers = []
56
    ) {
57
        $this->environment = $environment;
58
        $this->parent = $loader;
59
        $this->modifiers = $modifiers;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function getSource(string $path): ViewSource
66
    {
67
        $source = $this->parent->getSource($path);
68
69
        foreach ($this->modifiers as $modifier) {
70
            $benchmark = $this->benchmark('process', $path . '@' . get_class($modifier));
71
            try {
72
                $source = $source->withCode(
73
                    $modifier->modify($this->environment, $source, $source->getCode())
74
                );
75
            } finally {
76
                $this->benchmark($benchmark);
77
            }
78
        }
79
80
        return $source;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function exists(string $path): bool
87
    {
88
        return $this->parent->exists($path);
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function getNamespaces(): array
95
    {
96
        return $this->parent->getNamespaces();
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function withExtension(string $extension = null): LoaderInterface
103
    {
104
        $wrapper = clone $this;
105
        $wrapper->parent = $wrapper->parent->withExtension($extension);
106
107
        return $wrapper;
108
    }
109
}