1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright 2018 Vladimir Jimenez |
5
|
|
|
* @license https://github.com/stakx-io/stakx/blob/master/LICENSE.md MIT |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace allejo\stakx\Manager; |
9
|
|
|
|
10
|
|
|
use allejo\stakx\Configuration; |
11
|
|
|
use allejo\stakx\DataTransformer\DataTransformerManager; |
12
|
|
|
use allejo\stakx\Document\DataItem; |
13
|
|
|
use allejo\stakx\Event\DataItemAdded; |
14
|
|
|
use allejo\stakx\Event\DataItemFolderAdded; |
15
|
|
|
use allejo\stakx\Event\DatasetDefinitionAdded; |
16
|
|
|
use allejo\stakx\Exception\DependencyMissingException; |
17
|
|
|
use allejo\stakx\Exception\UnsupportedDataTypeException; |
18
|
|
|
use allejo\stakx\Filesystem\File; |
19
|
|
|
use allejo\stakx\Filesystem\FilesystemLoader as fs; |
20
|
|
|
use Psr\Log\LoggerInterface; |
21
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* This class handles everything in regards to DataItems and DataSets. |
25
|
|
|
*/ |
26
|
|
|
class DataManager extends TrackingManager |
27
|
|
|
{ |
28
|
|
|
private $dataTransformerManager; |
29
|
|
|
private $configuration; |
30
|
|
|
private $eventDispatcher; |
31
|
|
|
private $logger; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* DataManager constructor. |
35
|
|
|
*/ |
36
|
2 |
|
public function __construct(DataTransformerManager $dataTransformerManager, Configuration $configuration, EventDispatcherInterface $eventDispatcher, LoggerInterface $logger) |
37
|
|
|
{ |
38
|
2 |
|
$this->dataTransformerManager = $dataTransformerManager; |
39
|
2 |
|
$this->configuration = $configuration; |
40
|
2 |
|
$this->eventDispatcher = $eventDispatcher; |
41
|
2 |
|
$this->logger = $logger; |
42
|
2 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
public function compileManager() |
48
|
|
|
{ |
49
|
|
|
if (!$this->configuration->hasDataItems()) |
50
|
|
|
{ |
51
|
|
|
$this->logger->notice('No DataItems or Datasets detected... Ignoring.'); |
52
|
|
|
|
53
|
|
|
return; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$this->parseDataItems($this->configuration->getDataFolders()); |
57
|
|
|
$this->parseDataSets($this->configuration->getDataSets()); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get all of the DataItems and DataSets in this manager. |
62
|
|
|
* |
63
|
|
|
* @return array |
64
|
|
|
*/ |
65
|
2 |
|
public function &getDataItems() |
66
|
|
|
{ |
67
|
2 |
|
return $this->trackedItems; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
|
|
public function getJailedDataItems() |
74
|
|
|
{ |
75
|
2 |
|
return self::getJailedTrackedItems($this->trackedItemsFlattened, function (DataItem $dataItem) { |
76
|
2 |
|
return $dataItem->getBasename(); |
77
|
2 |
|
}); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Loop through all of the DataItems specified in `$folders`. Each folder will have contain just DataItems. |
82
|
|
|
* |
83
|
|
|
* For each folder, supported file type is read, parsed, and made available through `$this->getDataItems()` |
84
|
|
|
* |
85
|
|
|
* @param string[] $folders An array of folders to be searched for to contain DataItems |
86
|
|
|
*/ |
87
|
1 |
|
public function parseDataItems($folders) |
88
|
|
|
{ |
89
|
1 |
|
if ($folders === null) |
90
|
1 |
|
{ |
91
|
|
|
return; |
92
|
|
|
} |
93
|
|
|
|
94
|
1 |
|
foreach ($folders as $folder) |
95
|
|
|
{ |
96
|
1 |
|
$event = new DataItemFolderAdded($folder); |
97
|
1 |
|
$this->eventDispatcher->dispatch(DataItemFolderAdded::NAME, $event); |
98
|
|
|
|
99
|
1 |
|
$this->saveFolderDefinition($folder); |
100
|
1 |
|
$this->scanTrackableItems(fs::absolutePath($folder)); |
|
|
|
|
101
|
1 |
|
} |
102
|
1 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Loop through all of the DataSets specified in `$dataSets`. Each DataSet contains a name and a folder location. |
106
|
|
|
* |
107
|
|
|
* For each folder, supported file type is read, parsed, and made available through `$this->getDataItems()` |
108
|
|
|
* |
109
|
|
|
* @param string[] $dataSets An array of DataSets |
110
|
|
|
*/ |
111
|
1 |
|
public function parseDataSets($dataSets) |
112
|
|
|
{ |
113
|
1 |
|
if ($dataSets === null) |
114
|
1 |
|
{ |
115
|
|
|
return; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* The information which each DataSet has from the configuration file. |
120
|
|
|
* |
121
|
|
|
* @var array $dataSet = [ |
122
|
|
|
* 'name' => '(string) The name of the collection', |
123
|
|
|
* 'folder' => '(string) The folder where this collection has its ContentItems' |
124
|
|
|
* ] |
125
|
|
|
*/ |
126
|
1 |
|
foreach ($dataSets as $dataSet) |
127
|
|
|
{ |
128
|
1 |
|
$event = new DatasetDefinitionAdded($dataSet['name'], $dataSet['folder']); |
129
|
1 |
|
$this->eventDispatcher->dispatch(DatasetDefinitionAdded::NAME, $event); |
130
|
|
|
|
131
|
1 |
|
$this->saveFolderDefinition($dataSet['folder'], [ |
132
|
1 |
|
'namespace' => $dataSet['name'], |
133
|
1 |
|
]); |
134
|
1 |
|
$this->scanTrackableItems( |
135
|
1 |
|
fs::absolutePath($dataSet['folder']), |
|
|
|
|
136
|
1 |
|
['namespace' => $dataSet['name']] |
137
|
1 |
|
); |
138
|
1 |
|
} |
139
|
1 |
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* {@inheritdoc} |
143
|
|
|
*/ |
144
|
2 |
|
protected function handleTrackableItem(File $filePath, array $options = []) |
145
|
|
|
{ |
146
|
|
|
try |
147
|
|
|
{ |
148
|
2 |
|
$namespace = (isset($options['namespace'])) ? $options['namespace'] : null; |
149
|
|
|
|
150
|
2 |
|
$dataItem = new DataItem($filePath); |
151
|
2 |
|
$dataItem->setDataTransformer($this->dataTransformerManager); |
152
|
2 |
|
$dataItem->setNamespace($namespace); |
153
|
|
|
|
154
|
2 |
|
$event = new DataItemAdded($dataItem); |
155
|
2 |
|
$this->eventDispatcher->dispatch(DataItemAdded::NAME, $event); |
156
|
|
|
|
157
|
2 |
|
$this->addObjectToTracker($dataItem, $namespace); |
158
|
2 |
|
$this->saveTrackerOptions($dataItem->getRelativeFilePath(), $options); |
159
|
|
|
|
160
|
2 |
|
return $dataItem->getRelativeFilePath(); |
161
|
|
|
} |
162
|
|
|
catch (DependencyMissingException $e) |
163
|
|
|
{ |
164
|
|
|
if ($e->getDependency() === 'XML') |
165
|
|
|
{ |
166
|
|
|
$this->logger->critical('XML support is not available in your PHP installation. For XML support, please install the appropriate package for your system:'); |
167
|
|
|
$this->logger->critical(' e.g. php7.0-xml'); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
catch (UnsupportedDataTypeException $e) |
171
|
|
|
{ |
172
|
|
|
$this->logger->warning('There is no function to handle {ext} file format.', [ |
173
|
|
|
'ext' => $e->getDataType(), |
174
|
|
|
]); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
return $filePath->getBasename(); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: