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\Configuration; |
11
|
|
|
use allejo\stakx\DataTransformer\DataTransformerManager; |
12
|
|
|
use allejo\stakx\Exception\DependencyMissingException; |
13
|
|
|
use allejo\stakx\Document\DataItem; |
14
|
|
|
use allejo\stakx\Exception\UnsupportedDataTypeException; |
15
|
|
|
use allejo\stakx\Filesystem\File; |
16
|
|
|
use allejo\stakx\Filesystem\FilesystemLoader as fs; |
17
|
|
|
use Psr\Log\LoggerInterface; |
18
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* This class handles everything in regards to DataItems and DataSets. |
22
|
|
|
*/ |
23
|
|
|
class DataManager extends TrackingManager |
24
|
|
|
{ |
25
|
|
|
private $dataTransformerManager; |
26
|
|
|
private $configuration; |
27
|
|
|
private $eventDispatcher; |
28
|
|
|
private $logger; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* DataManager constructor. |
32
|
|
|
*/ |
33
|
2 |
|
public function __construct(DataTransformerManager $dataTransformerManager, Configuration $configuration, EventDispatcherInterface $eventDispatcher, LoggerInterface $logger) |
|
|
|
|
34
|
|
|
{ |
35
|
2 |
|
$this->dataTransformerManager = $dataTransformerManager; |
36
|
2 |
|
$this->configuration = $configuration; |
37
|
2 |
|
$this->eventDispatcher = $eventDispatcher; |
38
|
2 |
|
$this->logger = $logger; |
39
|
2 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
public function compileManager() |
45
|
|
|
{ |
46
|
|
|
if (!$this->configuration->hasDataItems()) |
47
|
|
|
{ |
48
|
|
|
$this->logger->notice('No DataItems or Datasets detected... Ignoring.'); |
49
|
|
|
return; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$this->parseDataItems($this->configuration->getDataFolders()); |
53
|
|
|
$this->parseDataSets($this->configuration->getDataSets()); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get all of the DataItems and DataSets in this manager. |
58
|
|
|
* |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
2 |
|
public function &getDataItems() |
62
|
|
|
{ |
63
|
2 |
|
return $this->trackedItems; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
|
public function getJailedDataItems() |
70
|
|
|
{ |
71
|
2 |
|
return self::getJailedTrackedItems($this->trackedItemsFlattened, function (DataItem $dataItem) { |
72
|
2 |
|
return $dataItem->getBasename(); |
73
|
2 |
|
}); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Loop through all of the DataItems specified in `$folders`. Each folder will have contain just DataItems. |
78
|
|
|
* |
79
|
|
|
* For each folder, supported file type is read, parsed, and made available through `$this->getDataItems()` |
80
|
|
|
* |
81
|
|
|
* @param string[] $folders An array of folders to be searched for to contain DataItems |
82
|
|
|
*/ |
83
|
1 |
|
public function parseDataItems($folders) |
84
|
|
|
{ |
85
|
1 |
|
if ($folders === null) |
86
|
1 |
|
{ |
87
|
|
|
return; |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
foreach ($folders as $folder) |
91
|
|
|
{ |
92
|
1 |
|
$this->saveFolderDefinition($folder); |
93
|
1 |
|
$this->scanTrackableItems(fs::absolutePath($folder)); |
94
|
1 |
|
} |
95
|
1 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Loop through all of the DataSets specified in `$dataSets`. Each DataSet contains a name and a folder location. |
99
|
|
|
* |
100
|
|
|
* For each folder, supported file type is read, parsed, and made available through `$this->getDataItems()` |
101
|
|
|
* |
102
|
|
|
* @param string[] $dataSets An array of DataSets |
103
|
|
|
*/ |
104
|
1 |
|
public function parseDataSets($dataSets) |
105
|
|
|
{ |
106
|
1 |
|
if ($dataSets === null) |
107
|
1 |
|
{ |
108
|
|
|
return; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* The information which each DataSet has from the configuration file. |
113
|
|
|
* |
114
|
|
|
* $dataSet['name'] string The name of the collection |
115
|
|
|
* ['folder'] string The folder where this collection has its ContentItems |
116
|
|
|
* |
117
|
|
|
* @var array $dataSet |
118
|
|
|
*/ |
119
|
1 |
|
foreach ($dataSets as $dataSet) |
120
|
|
|
{ |
121
|
1 |
|
$this->saveFolderDefinition($dataSet['folder'], array( |
122
|
1 |
|
'namespace' => $dataSet['name'], |
123
|
1 |
|
)); |
124
|
1 |
|
$this->scanTrackableItems( |
125
|
1 |
|
fs::absolutePath($dataSet['folder']), |
126
|
1 |
|
['namespace' => $dataSet['name']] |
127
|
1 |
|
); |
128
|
1 |
|
} |
129
|
1 |
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritdoc} |
133
|
|
|
*/ |
134
|
2 |
|
protected function handleTrackableItem(File $filePath, array $options = []) |
135
|
|
|
{ |
136
|
|
|
try |
137
|
|
|
{ |
138
|
2 |
|
$namespace = (isset($options['namespace'])) ? $options['namespace'] : null; |
139
|
|
|
|
140
|
2 |
|
$dataItem = new DataItem($filePath); |
141
|
2 |
|
$dataItem->setDataTransformer($this->dataTransformerManager); |
142
|
2 |
|
$dataItem->setNamespace($namespace); |
143
|
|
|
|
144
|
2 |
|
$this->addObjectToTracker($dataItem, $namespace); |
145
|
2 |
|
$this->saveTrackerOptions($dataItem->getRelativeFilePath(), $options); |
146
|
|
|
|
147
|
2 |
|
return $dataItem->getRelativeFilePath(); |
148
|
|
|
} |
149
|
|
|
catch (DependencyMissingException $e) |
150
|
|
|
{ |
151
|
|
|
if ($e->getDependency() === 'XML') |
152
|
|
|
{ |
153
|
|
|
$this->logger->critical('XML support is not available in your PHP installation. For XML support, please install the appropriate package for your system:'); |
154
|
|
|
$this->logger->critical(' e.g. php7.0-xml'); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
catch (UnsupportedDataTypeException $e) |
158
|
|
|
{ |
159
|
|
|
$this->logger->warning('There is no function to handle {ext} file format.', [ |
160
|
|
|
'ext' => $e->getDataType(), |
161
|
|
|
]); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return fs::getBaseName($filePath); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.