1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Symplify |
7
|
|
|
* Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz). |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Symplify\PHP7_Sculpin\Configuration; |
11
|
|
|
|
12
|
|
|
use SplFileInfo; |
13
|
|
|
use Symplify\PHP7_Sculpin\Configuration\Parser\YamlAndNeonParser; |
14
|
|
|
|
15
|
|
|
final class Configuration |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
const DEFAULT_POST_ROUTE = 'blog/:year/:month/:day/:title'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
private $globalVariables = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var YamlAndNeonParser |
29
|
|
|
*/ |
30
|
|
|
private $yamlAndNeonParser; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $sourceDirectory; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private $outputDirectory; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
private $postRoute = self::DEFAULT_POST_ROUTE; |
46
|
|
|
|
47
|
25 |
|
public function __construct(YamlAndNeonParser $yamlAndNeonParser) |
48
|
|
|
{ |
49
|
25 |
|
$this->yamlAndNeonParser = $yamlAndNeonParser; |
50
|
25 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param SplFileInfo[] $files |
54
|
|
|
*/ |
55
|
2 |
|
public function loadFromFiles(array $files) |
56
|
|
|
{ |
57
|
2 |
|
foreach ($files as $file) { |
58
|
2 |
|
$decodedOptions = $this->yamlAndNeonParser->decodeFromFile($file->getRealPath()); |
59
|
2 |
|
$this->globalVariables += $this->extractPostRoute($decodedOptions); |
60
|
|
|
} |
61
|
2 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $name |
65
|
|
|
* @param string|array $value |
66
|
|
|
*/ |
67
|
3 |
|
public function addGlobalVarialbe(string $name, $value) |
68
|
|
|
{ |
69
|
3 |
|
$this->globalVariables[$name] = $value; |
70
|
3 |
|
} |
71
|
|
|
|
72
|
7 |
|
public function getGlobalVariables() : array |
73
|
|
|
{ |
74
|
7 |
|
return $this->globalVariables; |
75
|
|
|
} |
76
|
|
|
|
77
|
20 |
|
public function setSourceDirectory(string $sourceDirectory) |
78
|
|
|
{ |
79
|
20 |
|
$this->sourceDirectory = $sourceDirectory; |
80
|
20 |
|
} |
81
|
|
|
|
82
|
16 |
|
public function getSourceDirectory() : string |
83
|
|
|
{ |
84
|
16 |
|
return $this->sourceDirectory; |
85
|
|
|
} |
86
|
|
|
|
87
|
9 |
|
public function setOutputDirectory(string $outputDirectory) |
88
|
|
|
{ |
89
|
9 |
|
$this->outputDirectory = $outputDirectory; |
90
|
9 |
|
} |
91
|
|
|
|
92
|
5 |
|
public function getOutputDirectory() : string |
93
|
|
|
{ |
94
|
5 |
|
return $this->outputDirectory; |
95
|
|
|
} |
96
|
|
|
|
97
|
4 |
|
public function setPostRoute(string $postRoute) |
98
|
|
|
{ |
99
|
4 |
|
$this->postRoute = $postRoute; |
100
|
4 |
|
} |
101
|
|
|
|
102
|
3 |
|
public function getPostRoute() : string |
103
|
|
|
{ |
104
|
3 |
|
return $this->postRoute; |
105
|
|
|
} |
106
|
|
|
|
107
|
2 |
|
private function extractPostRoute(array $options) : array |
108
|
|
|
{ |
109
|
2 |
|
if (! isset($options['configuration']['postRoute'])) { |
110
|
2 |
|
return $options; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$this->setPostRoute($options['configuration']['postRoute']); |
114
|
|
|
unset($options['configuration']['postRoute']); |
115
|
|
|
|
116
|
|
|
return $options; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|