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