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