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\FilesystemLoader as fs; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The class that reads and saves information about all of the collections. |
18
|
|
|
*/ |
19
|
|
|
class CollectionManager extends TrackingManager |
20
|
|
|
{ |
21
|
|
|
/** @var string[][] A copy of the collection definitions to be available for later usage. */ |
22
|
|
|
private $collectionDefinitions; |
23
|
|
|
private $configuration; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* CollectionManager constructor. |
27
|
|
|
*/ |
28
|
37 |
|
public function __construct(Configuration $configuration) |
29
|
|
|
{ |
30
|
37 |
|
parent::__construct(); |
31
|
|
|
|
32
|
37 |
|
$this->configuration = $configuration; |
33
|
37 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
5 |
|
public function compileManager() |
39
|
|
|
{ |
40
|
5 |
|
if (!$this->configuration->hasCollections()) |
41
|
|
|
{ |
42
|
|
|
$this->output->notice('No Collections defined... Ignoring'); |
43
|
|
|
return; |
44
|
|
|
} |
45
|
|
|
|
46
|
5 |
|
$this->parseCollections($this->configuration->getCollectionsFolders()); |
47
|
5 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get all of the ContentItems grouped by Collection name. |
51
|
|
|
* |
52
|
|
|
* @return ContentItem[][] |
|
|
|
|
53
|
|
|
*/ |
54
|
16 |
|
public function &getCollections() |
55
|
|
|
{ |
56
|
16 |
|
return $this->trackedItems; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get a ContentItem from a Collection pased on it's path. |
61
|
|
|
* |
62
|
|
|
* @param string $filePath |
63
|
|
|
* |
64
|
|
|
* @throws TrackedItemNotFoundException |
65
|
|
|
* |
66
|
|
|
* @return ContentItem |
67
|
|
|
*/ |
68
|
1 |
|
public function &getContentItem($filePath) |
69
|
|
|
{ |
70
|
1 |
|
if (!isset($this->trackedItemsFlattened[$filePath])) |
71
|
|
|
{ |
72
|
|
|
throw new TrackedItemNotFoundException("The ContentItem at '$filePath' was not found."); |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
return $this->trackedItemsFlattened[$filePath]; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* A jailed representation of CollectionManager::getCollections(). |
80
|
|
|
* |
81
|
|
|
* @return JailedDocument[][] |
|
|
|
|
82
|
|
|
*/ |
83
|
19 |
|
public function getJailedCollections() |
84
|
|
|
{ |
85
|
19 |
|
return self::getJailedTrackedItems($this->trackedItemsFlattened); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Parse every collection and store them in the manager. |
90
|
|
|
* |
91
|
|
|
* @param string[][] $collections An array of definitions for collections |
92
|
|
|
*/ |
93
|
37 |
|
public function parseCollections($collections) |
94
|
|
|
{ |
95
|
37 |
|
if ($collections === null) |
96
|
|
|
{ |
97
|
|
|
$this->output->debug('No collections found, nothing to parse.'); |
98
|
|
|
|
99
|
|
|
return; |
100
|
|
|
} |
101
|
|
|
|
102
|
37 |
|
$this->collectionDefinitions = $collections; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* The information which each collection has taken from the configuration file. |
106
|
|
|
* |
107
|
|
|
* $collection['name'] string The name of the collection |
108
|
|
|
* ['folder'] string The folder where this collection has its ContentItems |
109
|
|
|
* |
110
|
|
|
* @var array |
111
|
|
|
*/ |
112
|
37 |
|
foreach ($collections as $collection) |
113
|
|
|
{ |
114
|
37 |
|
$this->output->notice("Loading '{name}' collection...", [ |
115
|
37 |
|
'name' => $collection['name'], |
116
|
|
|
]); |
117
|
|
|
|
118
|
37 |
|
$collectionFolder = fs::absolutePath($collection['folder']); |
119
|
|
|
|
120
|
37 |
|
if (!fs::exists($collectionFolder)) |
121
|
|
|
{ |
122
|
|
|
$this->output->warning("The folder '{folder}' could not be found for the '{name}' collection", [ |
123
|
|
|
'folder' => $collection['folder'], |
124
|
|
|
'name' => $collection['name'], |
125
|
|
|
]); |
126
|
|
|
continue; |
127
|
|
|
} |
128
|
|
|
|
129
|
37 |
|
$this->saveFolderDefinition($collection['folder'], $collection); |
130
|
37 |
|
$this->scanTrackableItems($collectionFolder, array( |
131
|
37 |
|
'namespace' => $collection['name'], |
132
|
|
|
)); |
133
|
|
|
} |
134
|
37 |
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* {@inheritdoc} |
138
|
|
|
*/ |
139
|
|
|
public function createNewItem($filePath) |
140
|
|
|
{ |
141
|
|
|
$collection = $this->getTentativeCollectionName($filePath); |
142
|
|
|
|
143
|
|
|
return $this->handleTrackableItem($filePath, array( |
144
|
|
|
'namespace' => $collection, |
145
|
|
|
)); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* {@inheritdoc} |
150
|
|
|
*/ |
151
|
|
|
public function refreshItem($filePath) |
152
|
|
|
{ |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* {@inheritdoc} |
157
|
|
|
*/ |
158
|
37 |
|
protected function handleTrackableItem($filePath, array $options = array()) |
159
|
|
|
{ |
160
|
37 |
|
$collectionName = $options['namespace']; |
161
|
|
|
|
162
|
37 |
|
$contentItem = new ContentItem($filePath); |
163
|
37 |
|
$contentItem->setNamespace($collectionName); |
164
|
37 |
|
$contentItem->evaluateFrontMatter([], [ |
165
|
37 |
|
'site' => $this->configuration->getConfiguration(), |
166
|
|
|
]); |
167
|
|
|
|
168
|
37 |
|
$this->addObjectToTracker($contentItem, $collectionName); |
169
|
|
|
|
170
|
37 |
|
$this->output->info("Loading ContentItem into '{name}' collection: {path}", [ |
171
|
37 |
|
'name' => $collectionName, |
172
|
37 |
|
'path' => fs::getRelativePath($filePath), |
173
|
|
|
]); |
174
|
|
|
|
175
|
37 |
|
return $contentItem; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Get the name of the Collection this Content Item belongs to. |
180
|
|
|
* |
181
|
|
|
* @param string $filePath |
182
|
|
|
* |
183
|
|
|
* @return string |
184
|
|
|
*/ |
185
|
|
|
private function getTentativeCollectionName($filePath) |
186
|
|
|
{ |
187
|
|
|
foreach ($this->collectionDefinitions as $collection) |
188
|
|
|
{ |
189
|
|
|
if (strpos($filePath, $collection['folder']) === 0) |
190
|
|
|
{ |
191
|
|
|
return $collection['name']; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
return ''; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
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.