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