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
|
16 |
|
public function &getCollections() |
34
|
|
|
{ |
35
|
16 |
|
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
|
1 |
|
{ |
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
|
19 |
|
public function getJailedCollections() |
63
|
|
|
{ |
64
|
19 |
|
$jailItems = array(); |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var string $key |
68
|
|
|
* @var ContentItem $item |
69
|
|
|
*/ |
70
|
19 |
|
foreach ($this->trackedItemsFlattened as &$item) |
71
|
|
|
{ |
72
|
19 |
|
if (!Service::getParameter(BuildableCommand::USE_DRAFTS) && $item['draft']) { continue; } |
73
|
|
|
|
74
|
19 |
|
$jailItems[$item->getNamespace()][$item->getName()] = $item->createJail(); |
75
|
19 |
|
} |
76
|
|
|
|
77
|
19 |
|
return $jailItems; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Parse every collection and store them in the manager. |
82
|
|
|
* |
83
|
|
|
* @param string[][] $collections An array of definitions for collections |
84
|
|
|
*/ |
85
|
37 |
|
public function parseCollections($collections) |
86
|
|
|
{ |
87
|
37 |
|
if ($collections === null) |
88
|
37 |
|
{ |
89
|
|
|
$this->output->debug('No collections found, nothing to parse.'); |
90
|
|
|
|
91
|
|
|
return; |
92
|
|
|
} |
93
|
|
|
|
94
|
37 |
|
$this->collectionDefinitions = $collections; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* The information which each collection has taken from the configuration file. |
98
|
|
|
* |
99
|
|
|
* $collection['name'] string The name of the collection |
100
|
|
|
* ['folder'] string The folder where this collection has its ContentItems |
101
|
|
|
* |
102
|
|
|
* @var array |
103
|
|
|
*/ |
104
|
37 |
|
foreach ($collections as $collection) |
105
|
|
|
{ |
106
|
37 |
|
$this->output->notice("Loading '{$collection['name']}' collection..."); |
|
|
|
|
107
|
|
|
|
108
|
37 |
|
$collectionFolder = $this->fs->absolutePath($collection['folder']); |
109
|
|
|
|
110
|
37 |
|
if (!$this->fs->exists($collectionFolder)) |
111
|
37 |
|
{ |
112
|
|
|
$this->output->warning("The folder '{$collection['folder']}' could not be found for the '{$collection['name']}' collection"); |
|
|
|
|
113
|
|
|
continue; |
114
|
|
|
} |
115
|
|
|
|
116
|
37 |
|
$this->saveFolderDefinition($collection['folder'], $collection); |
117
|
37 |
|
$this->scanTrackableItems($collectionFolder, array( |
118
|
37 |
|
'namespace' => $collection['name'], |
119
|
37 |
|
)); |
120
|
37 |
|
} |
121
|
37 |
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
*/ |
126
|
|
|
public function createNewItem($filePath) |
127
|
|
|
{ |
128
|
|
|
$collection = $this->getTentativeCollectionName($filePath); |
129
|
|
|
|
130
|
|
|
return $this->handleTrackableItem($filePath, array( |
131
|
|
|
'namespace' => $collection, |
132
|
|
|
)); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* {@inheritdoc} |
137
|
|
|
*/ |
138
|
|
|
public function refreshItem($filePath) |
139
|
|
|
{ |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritdoc} |
144
|
|
|
*/ |
145
|
37 |
|
protected function handleTrackableItem($filePath, $options = array()) |
146
|
|
|
{ |
147
|
37 |
|
$collectionName = $options['namespace']; |
148
|
|
|
|
149
|
37 |
|
$contentItem = new ContentItem($filePath); |
150
|
37 |
|
$contentItem->setNamespace($collectionName); |
151
|
|
|
|
152
|
37 |
|
$this->addObjectToTracker($contentItem, $contentItem->getName(), $collectionName); |
153
|
|
|
|
154
|
37 |
|
$this->output->info(sprintf( |
155
|
37 |
|
"Loading ContentItem into '%s' collection: %s", |
156
|
37 |
|
$collectionName, |
157
|
37 |
|
$this->fs->getRelativePath($filePath) |
158
|
37 |
|
)); |
159
|
|
|
|
160
|
37 |
|
return $contentItem; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Get the name of the Collection this Content Item belongs to. |
165
|
|
|
* |
166
|
|
|
* @param string $filePath |
167
|
|
|
* |
168
|
|
|
* @return string |
169
|
|
|
*/ |
170
|
|
|
private function getTentativeCollectionName($filePath) |
171
|
|
|
{ |
172
|
|
|
foreach ($this->collectionDefinitions as $collection) |
173
|
|
|
{ |
174
|
|
|
if (strpos($filePath, $collection['folder']) === 0) |
175
|
|
|
{ |
176
|
|
|
return $collection['name']; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return ''; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.