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