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