Completed
Push — master ( 97098d...aeb3c3 )
by
unknown
08:22
created

ViewConfig::addPaths()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Charcoal\View;
4
5
use InvalidArgumentException;
6
7
// From 'charcoal-config'
8
use Charcoal\Config\AbstractConfig;
9
10
// From 'charcoal-view'
11
use Charcoal\View\Mustache\MustacheEngine;
12
use Charcoal\View\Twig\TwigEngine;
13
14
/**
15
 * View configuration.
16
 */
17
class ViewConfig extends AbstractConfig
18
{
19
    /**
20
     * @var array $paths
21
     */
22
    private $paths = [];
23
24
    /**
25
     * @var array $engines
26
     */
27
    private $engines = [];
28
29
    /**
30
     * @var string $defaultEngine
31
     */
32
    private $defaultEngine;
33
34
    /**
35
     * @return array
36
     */
37
    public function defaults()
38
    {
39
        return [
40
            'paths' => [],
41
            'engines' => [
42
                'mustache'      => [
43
                    'cache' => MustacheEngine::DEFAULT_CACHE_PATH
44
                ],
45
                'php'           => [],
46
                'php-mustache'  => [],
47
                'twig'          => [
48
                    'cache' => TwigEngine::DEFAULT_CACHE_PATH
49
                ]
50
            ],
51
            'default_engine' => 'mustache'
52
        ];
53
    }
54
55
    /**
56
     * @param array $paths The paths to search into.
57
     * @return ViewConfig Chainable
58
     */
59
    public function setPaths(array $paths)
60
    {
61
        $this->paths = [];
62
        foreach ($paths as $p) {
63
            $this->addPath($p);
64
        }
65
        return $this;
66
    }
67
68
    /**
69
     * @param  string[] $paths One or more search paths.
70
     * @return self
71
     */
72
    public function addPaths(array $paths)
73
    {
74
        foreach ($paths as $path) {
75
            $this->addPath($path);
76
        }
77
        return $this;
78
    }
79
80
    /**
81
     * @param string $path A path to add to the paths list.
82
     * @throws InvalidArgumentException If the path is not a string.
83
     * @return ViewConfig Chainable
84
     */
85
    public function addPath($path)
86
    {
87
        if (!is_string($path)) {
88
            throw new InvalidArgumentException(
89
                'Template path must be a string'
90
            );
91
        }
92
        $this->paths[] = $path;
93
        return $this;
94
    }
95
96
    /**
97
     * @return array
98
     */
99
    public function paths()
100
    {
101
        return $this->paths;
102
    }
103
104
    /**
105
     * @param array $engines The various engines configuration.
106
     * @return ViewConfig Chainable
107
     */
108
    public function setEngines(array $engines)
109
    {
110
        $this->engines = [];
111
        foreach ($engines as $engineIdent => $engineConfig) {
112
            $this->addEngine($engineIdent, $engineConfig);
113
        }
114
        return $this;
115
    }
116
117
    /**
118
     * @param string $engineIdent  The engine identifier.
119
     * @param array  $engineConfig The engine configuration data.
120
     * @throws InvalidArgumentException If the engine ident is not a string.
121
     * @return ViewConfig Chainable
122
     */
123
    public function addEngine($engineIdent, array $engineConfig)
124
    {
125
        if (!is_string($engineIdent)) {
126
            throw new InvalidArgumentException(
127
                'Can not add engine to view config: engine identifier must be a string.'
128
            );
129
        }
130
        $this->engines[$engineIdent] = $engineConfig;
131
        return $this;
132
    }
133
134
    /**
135
     * @return array
136
     */
137
    public function engines()
138
    {
139
        return $this->engines;
140
    }
141
142
    /**
143
     * Get an engine's configuration.
144
     *
145
     * @param string|null $engineIdent The engine identifier to get the configuration of.
146
     * @throws InvalidArgumentException If the engine ident is not a string or does not match any engines.
147
     * @return array
148
     */
149
    public function engine($engineIdent = null)
150
    {
151
        if ($engineIdent === null) {
152
            $engineIdent = $this->defaultEngine();
153
        }
154
        if (!is_string($engineIdent)) {
155
            throw new InvalidArgumentException(
156
                'Invalid engine identifier (must be a string)'
157
            );
158
        }
159
160
        if (!isset($this->engines[$engineIdent])) {
161
            throw new InvalidArgumentException(
162
                sprintf('No configured engines matching "%s"', $engineIdent)
163
            );
164
        }
165
        return $this->engines[$engineIdent];
166
    }
167
168
    /**
169
     * @param string $engineIdent The default engine (identifier).
170
     * @throws InvalidArgumentException If the engine ident is not a string.
171
     * @return ViewConfig Chainable
172
     */
173
    public function setDefaultEngine($engineIdent)
174
    {
175
        if (!is_string($engineIdent)) {
176
            throw new InvalidArgumentException(
177
                'Default engine must be a string'
178
            );
179
        }
180
        $this->defaultEngine = $engineIdent;
181
        return $this;
182
    }
183
184
    /**
185
     * @return string
186
     */
187
    public function defaultEngine()
188
    {
189
        return $this->defaultEngine;
190
    }
191
}
192