1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright 2017 Vladimir Jimenez |
5
|
|
|
* @license https://github.com/allejo/stakx/blob/master/LICENSE.md MIT |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace allejo\stakx\Manager; |
9
|
|
|
|
10
|
|
|
use allejo\stakx\Configuration; |
11
|
|
|
use allejo\stakx\Document\ContentItem; |
12
|
|
|
use allejo\stakx\Document\JailedDocument; |
13
|
|
|
use allejo\stakx\Exception\TrackedItemNotFoundException; |
14
|
|
|
use allejo\stakx\Filesystem\File; |
15
|
|
|
use allejo\stakx\Filesystem\FilesystemLoader as fs; |
16
|
|
|
use allejo\stakx\MarkupEngine\MarkupEngineManager; |
17
|
|
|
use Psr\Log\LoggerInterface; |
18
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The class that reads and saves information about all of the collections. |
22
|
|
|
*/ |
23
|
|
|
class CollectionManager extends TrackingManager |
24
|
|
|
{ |
25
|
|
|
/** @var string[][] A copy of the collection definitions to be available for later usage. */ |
26
|
|
|
private $collectionDefinitions; |
27
|
|
|
private $markupEngineManager; |
28
|
|
|
private $configuration; |
29
|
|
|
private $eventDispatcher; |
30
|
|
|
private $logger; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* CollectionManager constructor. |
34
|
|
|
*/ |
35
|
|
|
public function __construct(MarkupEngineManager $markupEngineManager, Configuration $configuration, EventDispatcherInterface $eventDispatcher, LoggerInterface $logger) |
36
|
|
|
{ |
37
|
|
|
$this->markupEngineManager = $markupEngineManager; |
38
|
|
|
$this->configuration = $configuration; |
39
|
|
|
$this->eventDispatcher = $eventDispatcher; |
40
|
|
|
$this->logger = $logger; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
|
|
public function compileManager() |
47
|
|
|
{ |
48
|
|
|
if (!$this->configuration->hasCollections()) |
49
|
|
|
{ |
50
|
|
|
$this->logger->notice('No Collections defined... Ignoring'); |
51
|
|
|
return; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$this->parseCollections($this->configuration->getCollectionsFolders()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get all of the ContentItems grouped by Collection name. |
59
|
|
|
* |
60
|
|
|
* ```php |
61
|
|
|
* [ |
62
|
|
|
* 'collection name' => [ |
63
|
|
|
* new ContentItem(), |
64
|
|
|
* new ContentItem(), |
65
|
|
|
* ] |
66
|
|
|
* ] |
67
|
|
|
* ``` |
68
|
|
|
* |
69
|
|
|
* @return ContentItem[][] |
70
|
|
|
*/ |
71
|
|
|
public function &getCollections() |
72
|
|
|
{ |
73
|
|
|
return $this->trackedItems; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get a ContentItem from a Collection passed on it's path. |
78
|
|
|
* |
79
|
|
|
* @param string $filePath |
80
|
|
|
* |
81
|
|
|
* @throws TrackedItemNotFoundException |
82
|
|
|
* |
83
|
|
|
* @return ContentItem |
84
|
|
|
*/ |
85
|
|
|
public function &getContentItem($filePath) |
86
|
|
|
{ |
87
|
|
|
if (!isset($this->trackedItemsFlattened[$filePath])) |
88
|
|
|
{ |
89
|
|
|
throw new TrackedItemNotFoundException("The ContentItem at '$filePath' was not found."); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $this->trackedItemsFlattened[$filePath]; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* A jailed representation of CollectionManager::getCollections(). |
97
|
|
|
* |
98
|
|
|
* @return JailedDocument[][] |
|
|
|
|
99
|
|
|
*/ |
100
|
|
|
public function getJailedCollections() |
101
|
|
|
{ |
102
|
|
|
return self::getJailedTrackedItems($this->trackedItemsFlattened); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Parse every collection and store them in the manager. |
107
|
|
|
* |
108
|
|
|
* @param string[][] $collections An array of definitions for collections |
109
|
|
|
*/ |
110
|
|
|
public function parseCollections($collections) |
111
|
|
|
{ |
112
|
|
|
if ($collections == null || empty($collections)) |
113
|
|
|
{ |
114
|
|
|
$this->logger->debug('No collections found, nothing to parse.'); |
115
|
|
|
|
116
|
|
|
return; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$this->collectionDefinitions = $collections; |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* The information which each collection has taken from the configuration file. |
123
|
|
|
* |
124
|
|
|
* $collection['name'] string The name of the collection |
125
|
|
|
* ['folder'] string The folder where this collection has its ContentItems |
126
|
|
|
* |
127
|
|
|
* @var array |
128
|
|
|
*/ |
129
|
|
|
foreach ($collections as $collection) |
130
|
|
|
{ |
131
|
|
|
$this->logger->notice('Loading "{name}" collection...', [ |
132
|
|
|
'name' => $collection['name'], |
133
|
|
|
]); |
134
|
|
|
|
135
|
|
|
$collectionFolder = fs::absolutePath($collection['folder']); |
136
|
|
|
|
137
|
|
|
if (!fs::exists($collectionFolder)) |
138
|
|
|
{ |
139
|
|
|
$this->logger->warning('The folder "{folder}" could not be found for the "{name}" collection', [ |
140
|
|
|
'folder' => $collection['folder'], |
141
|
|
|
'name' => $collection['name'], |
142
|
|
|
]); |
143
|
|
|
|
144
|
|
|
continue; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$this->saveFolderDefinition($collection['folder'], $collection); |
148
|
|
|
$this->scanTrackableItems($collectionFolder, [ |
149
|
|
|
'namespace' => $collection['name'], |
150
|
|
|
]); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* {@inheritdoc} |
156
|
|
|
*/ |
157
|
|
|
public function createNewItem($filePath) |
158
|
|
|
{ |
159
|
|
|
$collection = $this->getCollectionNameFromPath($filePath); |
160
|
|
|
|
161
|
|
|
return $this->handleTrackableItem($filePath, [ |
162
|
|
|
'namespace' => $collection, |
163
|
|
|
]); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* {@inheritdoc} |
168
|
|
|
*/ |
169
|
|
|
public function refreshItem($filePath) |
170
|
|
|
{ |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* {@inheritdoc} |
175
|
|
|
*/ |
176
|
|
|
protected function handleTrackableItem(File $filePath, array $options = []) |
177
|
|
|
{ |
178
|
|
|
$collectionName = $options['namespace']; |
179
|
|
|
|
180
|
|
|
$contentItem = new ContentItem($filePath); |
181
|
|
|
$contentItem->setMarkupEngine($this->markupEngineManager); |
182
|
|
|
$contentItem->setNamespace($collectionName); |
183
|
|
|
$contentItem->evaluateFrontMatter([], [ |
184
|
|
|
'site' => $this->configuration->getConfiguration(), |
185
|
|
|
]); |
186
|
|
|
|
187
|
|
|
$this->addObjectToTracker($contentItem, $collectionName); |
188
|
|
|
|
189
|
|
|
$this->logger->info('Loading ContentItem into "{name}" collection: {path}', [ |
190
|
|
|
'name' => $collectionName, |
191
|
|
|
'path' => fs::getRelativePath($filePath), |
192
|
|
|
]); |
193
|
|
|
|
194
|
|
|
return $contentItem; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Get the name of the Collection this ContentItem belongs to based on its location. |
199
|
|
|
* |
200
|
|
|
* @param File $file |
201
|
|
|
* |
202
|
|
|
* @return string |
203
|
|
|
*/ |
204
|
|
|
private function getCollectionNameFromPath(File $file) |
205
|
|
|
{ |
206
|
|
|
$folders = array_column($this->collectionDefinitions, 'folder'); |
207
|
|
|
$index = array_search($file->getRelativeParentFolder(), $folders); |
208
|
|
|
|
209
|
|
|
if (isset($this->collectionDefinitions[$index]['name'])) |
210
|
|
|
{ |
211
|
|
|
return $this->collectionDefinitions[$index]['name']; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
return ''; |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.