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