ViewConfig::defaultEngine()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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