1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Spiral Framework. |
5
|
|
|
* |
6
|
|
|
* @license MIT |
7
|
|
|
* @author Anton Titov (Wolfy-J) |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Spiral\Views\Config; |
13
|
|
|
|
14
|
|
|
use Spiral\Core\Container\Autowire; |
15
|
|
|
use Spiral\Core\InjectableConfig; |
16
|
|
|
use Spiral\Views\Engine\Native\NativeEngine; |
17
|
|
|
use Spiral\Views\Exception\ConfigException; |
18
|
|
|
|
19
|
|
|
final class ViewsConfig extends InjectableConfig |
20
|
|
|
{ |
21
|
|
|
public const CONFIG = 'views'; |
22
|
|
|
|
23
|
|
|
/** @var array */ |
24
|
|
|
protected $config = [ |
25
|
|
|
'cache' => [ |
26
|
|
|
'enable' => false, |
27
|
|
|
'directory' => '/tmp' |
28
|
|
|
], |
29
|
|
|
'namespaces' => [], |
30
|
|
|
'dependencies' => [], |
31
|
|
|
'engines' => [ |
32
|
|
|
NativeEngine::class |
33
|
|
|
] |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return bool |
38
|
|
|
*/ |
39
|
|
|
public function isCacheEnabled(): bool |
40
|
|
|
{ |
41
|
|
|
return !empty($this->config['cache']['enable']) || !empty($this->config['cache']['enabled']); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
public function getCacheDirectory(): string |
48
|
|
|
{ |
49
|
|
|
return rtrim($this->config['cache']['directory'], '/') . '/'; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Return all namespaces and their associated directories. |
54
|
|
|
* |
55
|
|
|
* @return array |
56
|
|
|
*/ |
57
|
|
|
public function getNamespaces(): array |
58
|
|
|
{ |
59
|
|
|
return $this->config['namespaces']; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Class names of all view dependencies. |
64
|
|
|
* |
65
|
|
|
* @return Autowire[] |
66
|
|
|
* |
67
|
|
|
* @throws ConfigException |
68
|
|
|
*/ |
69
|
|
|
public function getDependencies(): array |
70
|
|
|
{ |
71
|
|
|
$dependencies = []; |
72
|
|
|
foreach ($this->config['dependencies'] as $dependency) { |
73
|
|
|
$dependencies[] = $this->wire($dependency); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $dependencies; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get all the engines associated with view component. |
81
|
|
|
* |
82
|
|
|
* @return Autowire[] |
83
|
|
|
* |
84
|
|
|
* @throws ConfigException |
85
|
|
|
*/ |
86
|
|
|
public function getEngines(): array |
87
|
|
|
{ |
88
|
|
|
$engines = []; |
89
|
|
|
foreach ($this->config['engines'] as $engine) { |
90
|
|
|
$engines[] = $this->wire($engine); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $engines; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param mixed $item |
98
|
|
|
* @return Autowire |
99
|
|
|
* |
100
|
|
|
* @throws ConfigException |
101
|
|
|
*/ |
102
|
|
|
private function wire($item): Autowire |
103
|
|
|
{ |
104
|
|
|
if ($item instanceof Autowire) { |
105
|
|
|
return $item; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if (is_string($item)) { |
109
|
|
|
return new Autowire($item); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
throw new ConfigException('Invalid class reference in view config.'); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|