|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Brendt\Stitcher\Parser\Site; |
|
4
|
|
|
|
|
5
|
|
|
use Brendt\Html\Meta\Meta; |
|
6
|
|
|
use Brendt\Stitcher\Event\Event; |
|
7
|
|
|
use Brendt\Stitcher\Exception\InvalidSiteException; |
|
8
|
|
|
use Brendt\Stitcher\Exception\TemplateNotFoundException; |
|
9
|
|
|
use Brendt\Stitcher\Site\Http\Htaccess; |
|
10
|
|
|
use Brendt\Stitcher\Site\Page; |
|
11
|
|
|
use Brendt\Stitcher\Site\Site; |
|
12
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
|
13
|
|
|
use Symfony\Component\Finder\Finder; |
|
14
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
|
15
|
|
|
use Symfony\Component\Yaml\Exception\ParseException; |
|
16
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
17
|
|
|
|
|
18
|
|
|
class SiteParser |
|
19
|
|
|
{ |
|
20
|
|
|
const EVENT_PARSER_INIT = 'parser.initialised'; |
|
21
|
|
|
|
|
22
|
|
|
const EVENT_PAGE_PARSING = 'page.parsing'; |
|
23
|
|
|
|
|
24
|
|
|
const EVENT_PAGE_PARSED = 'page.parsed'; |
|
25
|
|
|
|
|
26
|
|
|
const TOKEN_REDIRECT = 'redirect'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
private $filter; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
private $srcDir; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var array |
|
40
|
|
|
*/ |
|
41
|
|
|
private $metaConfig; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var EventDispatcher |
|
45
|
|
|
*/ |
|
46
|
|
|
private $eventDispatcher; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var PageParser |
|
50
|
|
|
*/ |
|
51
|
|
|
private $pageParser; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var Htaccess |
|
55
|
|
|
*/ |
|
56
|
|
|
private $htaccess; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* SiteParser constructor. |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $srcDir |
|
62
|
|
|
* @param EventDispatcher $eventDispatcher |
|
63
|
|
|
* @param PageParser $pageParser |
|
64
|
|
|
* @param Htaccess $htaccess |
|
65
|
|
|
* @param array $metaConfig |
|
66
|
|
|
*/ |
|
67
|
|
|
public function __construct( |
|
68
|
|
|
string $srcDir, |
|
69
|
|
|
EventDispatcher $eventDispatcher, |
|
70
|
|
|
PageParser $pageParser, |
|
71
|
|
|
Htaccess $htaccess, |
|
72
|
|
|
array $metaConfig = [] |
|
73
|
|
|
) { |
|
74
|
|
|
$this->srcDir = $srcDir; |
|
75
|
|
|
$this->eventDispatcher = $eventDispatcher; |
|
76
|
|
|
$this->pageParser = $pageParser; |
|
77
|
|
|
$this->htaccess = $htaccess; |
|
78
|
|
|
$this->metaConfig = $metaConfig; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Load a site from YAML configuration files in the `directories.src`/site directory. |
|
83
|
|
|
* All YAML files are loaded and parsed into Page objects and added to a Site collection. |
|
84
|
|
|
* |
|
85
|
|
|
* @param array $routes |
|
86
|
|
|
* |
|
87
|
|
|
* @return Site |
|
88
|
|
|
* @throws InvalidSiteException |
|
89
|
|
|
* @see \Brendt\Stitcher\Site\Page |
|
90
|
|
|
* @see \Brendt\Stitcher\Site\Site |
|
91
|
|
|
*/ |
|
92
|
|
|
public function loadSite(array $routes = []) : Site { |
|
93
|
|
|
/** @var SplFileInfo[] $files */ |
|
94
|
|
|
$files = Finder::create()->files()->in("{$this->srcDir}/site")->name('*.yml'); |
|
95
|
|
|
$site = new Site(); |
|
96
|
|
|
|
|
97
|
|
|
foreach ($files as $file) { |
|
98
|
|
|
try { |
|
99
|
|
|
$fileContents = (array) Yaml::parse($file->getContents()); |
|
100
|
|
|
} catch (ParseException $e) { |
|
101
|
|
|
throw new InvalidSiteException("{$file->getRelativePathname()}: {$e->getMessage()}"); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
foreach ($fileContents as $route => $data) { |
|
105
|
|
|
if (count($routes) && !in_array($route, $routes)) { |
|
106
|
|
|
continue; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
if (isset($data[self::TOKEN_REDIRECT])) { |
|
110
|
|
|
$this->htaccess->addRedirect($route, $data[self::TOKEN_REDIRECT]); |
|
111
|
|
|
|
|
112
|
|
|
continue; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$page = new Page($route, $data, $this->createMeta()); |
|
116
|
|
|
$site->addPage($page); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return $site; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Parse a path into usable data. |
|
125
|
|
|
* |
|
126
|
|
|
* @param array $routes |
|
127
|
|
|
* @param string $filterValue |
|
128
|
|
|
* |
|
129
|
|
|
* @return array|mixed |
|
130
|
|
|
* @throws TemplateNotFoundException |
|
131
|
|
|
*/ |
|
132
|
|
|
public function parse($routes = [], string $filterValue = null) : array { |
|
133
|
|
|
$blanket = []; |
|
134
|
|
|
|
|
135
|
|
|
$site = $this->loadSite((array) $routes); |
|
136
|
|
|
$this->eventDispatcher->dispatch(self::EVENT_PARSER_INIT, Event::create(['site' => $site])); |
|
137
|
|
|
|
|
138
|
|
|
foreach ($site as $page) { |
|
139
|
|
|
$this->eventDispatcher->dispatch(self::EVENT_PAGE_PARSING, Event::create(['page' => $page])); |
|
140
|
|
|
|
|
141
|
|
|
$this->pageParser->validate($page); |
|
142
|
|
|
$pages = $this->pageParser->parseAdapters($page, $filterValue); |
|
143
|
|
|
|
|
144
|
|
|
/** @var Page $entryPage */ |
|
145
|
|
|
foreach ($pages as $entryPage) { |
|
146
|
|
|
$blanket[$entryPage->getId()] = $this->pageParser->parsePage($entryPage); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
$this->eventDispatcher->dispatch(self::EVENT_PAGE_PARSED, Event::create(['page' => $page])); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
return $blanket; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @param string $filter |
|
157
|
|
|
* |
|
158
|
|
|
* @return SiteParser |
|
159
|
|
|
*/ |
|
160
|
|
|
public function setFilter(string $filter) : SiteParser { |
|
161
|
|
|
$this->filter = $filter; |
|
162
|
|
|
|
|
163
|
|
|
return $this; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @return Meta |
|
168
|
|
|
*/ |
|
169
|
|
|
private function createMeta() : Meta { |
|
170
|
|
|
$meta = new Meta(); |
|
171
|
|
|
|
|
172
|
|
|
foreach ($this->metaConfig as $name => $value) { |
|
173
|
|
|
$meta->name($name, $value); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
return $meta; |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|