|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* WebHemi. |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 7.1 |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright 2012 - 2017 Gixx-web (http://www.gixx-web.com) |
|
8
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
|
9
|
|
|
* |
|
10
|
|
|
* @link http://www.gixx-web.com |
|
11
|
|
|
*/ |
|
12
|
|
|
declare(strict_types = 1); |
|
13
|
|
|
|
|
14
|
|
|
namespace WebHemi\Data\Storage\Filesystem; |
|
15
|
|
|
|
|
16
|
|
|
use RuntimeException; |
|
17
|
|
|
use WebHemi\DateTime; |
|
18
|
|
|
use WebHemi\Data\ConnectorInterface; |
|
19
|
|
|
use WebHemi\Data\EntityInterface; |
|
20
|
|
|
use WebHemi\Data\Storage\AbstractStorage; |
|
21
|
|
|
use WebHemi\Data\Entity\Filesystem\FilesystemDirectoryEntity; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Class FilesystemDirectoryStorage. |
|
25
|
|
|
*/ |
|
26
|
|
|
class FilesystemDirectoryStorage extends AbstractStorage |
|
27
|
|
|
{ |
|
28
|
|
|
/** @var string */ |
|
29
|
|
|
protected $dataGroup = 'webhemi_filesystem_directory'; |
|
30
|
|
|
/** @var string */ |
|
31
|
|
|
protected $idKey = 'id_filesystem_directory'; |
|
32
|
|
|
/** @var string */ |
|
33
|
|
|
private $description = 'description'; |
|
34
|
|
|
/** @var string */ |
|
35
|
|
|
private $directoryType = 'directory_type'; |
|
36
|
|
|
/** @var string */ |
|
37
|
|
|
private $proxy = 'proxy'; |
|
38
|
|
|
/** @var string */ |
|
39
|
|
|
private $isAutoIndex = 'is_autoindex'; |
|
40
|
|
|
/** @var string */ |
|
41
|
|
|
private $dateCreated = 'date_created'; |
|
42
|
|
|
/** @var string */ |
|
43
|
|
|
private $dateModified = 'date_modified'; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Populates an entity with storage data. |
|
47
|
|
|
* |
|
48
|
|
|
* @param EntityInterface $dataEntity |
|
49
|
|
|
* @param array $data |
|
50
|
|
|
* @return void |
|
51
|
|
|
*/ |
|
52
|
|
|
protected function populateEntity(EntityInterface&$dataEntity, array $data) : void |
|
53
|
|
|
{ |
|
54
|
|
|
/* @var FilesystemDirectoryEntity $dataEntity */ |
|
55
|
|
|
$dataEntity->setFilesystemDirectoryId((int) $data[$this->idKey]) |
|
56
|
|
|
->setDescription($data[$this->description]) |
|
57
|
|
|
->setDirectoryType($data[$this->directoryType]) |
|
58
|
|
|
->setProxy($data[$this->proxy]) |
|
59
|
|
|
->setAutoIndex((bool) $data[$this->isAutoIndex]) |
|
60
|
|
|
->setDateCreated(new DateTime($data[$this->dateCreated] ?? 'now')) |
|
61
|
|
|
->setDateModified(new DateTime($data[$this->dateModified] ?? 'now')); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Get data from an entity. |
|
66
|
|
|
* |
|
67
|
|
|
* @param EntityInterface $dataEntity |
|
68
|
|
|
* @return array |
|
69
|
|
|
*/ |
|
70
|
|
|
protected function getEntityData(EntityInterface $dataEntity) : array |
|
71
|
|
|
{ |
|
72
|
|
|
/** @var FilesystemDirectoryEntity $dataEntity */ |
|
73
|
|
|
$dateCreated = $dataEntity->getDateCreated(); |
|
74
|
|
|
$dateModified = $dataEntity->getDateModified(); |
|
75
|
|
|
|
|
76
|
|
|
return [ |
|
77
|
|
|
$this->idKey => $dataEntity->getKeyData(), |
|
78
|
|
|
$this->description => $dataEntity->getDescription(), |
|
79
|
|
|
$this->directoryType => $dataEntity->getDirectoryType(), |
|
80
|
|
|
$this->proxy => $dataEntity->getProxy(), |
|
81
|
|
|
$this->isAutoIndex => (int) $dataEntity->getAutoIndex(), |
|
82
|
|
|
$this->dateCreated => $dateCreated instanceof DateTime ? $dateCreated->format('Y-m-d H:i:s') : null, |
|
83
|
|
|
$this->dateModified => $dateModified instanceof DateTime ? $dateModified->format('Y-m-d H:i:s') : null |
|
84
|
|
|
]; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Gets the filesystem directory entity by the identifier. |
|
89
|
|
|
* |
|
90
|
|
|
* @param int $identifier |
|
91
|
|
|
* @return null|FilesystemDirectoryEntity |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getFilesystemDirectoryById(int $identifier) : ? FilesystemDirectoryEntity |
|
94
|
|
|
{ |
|
95
|
|
|
/** @var null|FilesystemDirectoryEntity $dataEntity */ |
|
96
|
|
|
$dataEntity = $this->getDataEntity([$this->idKey => $identifier]); |
|
97
|
|
|
|
|
98
|
|
|
return $dataEntity; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Gets the filesystem directory entity by the proxy. |
|
103
|
|
|
* From named proxies only one exists form each type. |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $proxy |
|
106
|
|
|
* @return null|FilesystemDirectoryEntity |
|
107
|
|
|
*/ |
|
108
|
|
|
public function getFilesystemDirectoryByProxy(string $proxy) : ? FilesystemDirectoryEntity |
|
109
|
|
|
{ |
|
110
|
|
|
/** @var null|FilesystemDirectoryEntity $dataEntity */ |
|
111
|
|
|
$dataEntity = $this->getDataEntity([$this->proxy => $proxy]); |
|
112
|
|
|
|
|
113
|
|
|
return $dataEntity; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Collects complex information about |
|
118
|
|
|
* |
|
119
|
|
|
* @param int $applicationId |
|
120
|
|
|
* @param string $proxy |
|
121
|
|
|
* @throws RuntimeException |
|
122
|
|
|
* @return array |
|
123
|
|
|
*/ |
|
124
|
|
|
public function getDirectoryDataByApplicationAndProxy(int $applicationId, string $proxy) : array |
|
125
|
|
|
{ |
|
126
|
|
|
/** @var FilesystemDirectoryEntity $directoryEntity */ |
|
127
|
|
|
$directoryEntity = $this->getFilesystemDirectoryByProxy($proxy); |
|
128
|
|
|
|
|
129
|
|
|
if (!$directoryEntity instanceof FilesystemDirectoryEntity) { |
|
130
|
|
|
throw new RuntimeException(sprintf('Unknown proxy given: %s', $proxy), 1000); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
// MUST exist |
|
134
|
|
|
$filesystemData = $this->getFilesystemData($applicationId, $directoryEntity->getFilesystemDirectoryId()); |
|
135
|
|
|
$uri = $filesystemData['path'].'/'.$filesystemData['basename']; |
|
136
|
|
|
|
|
137
|
|
|
if (strpos($uri, '//') !== false) { |
|
138
|
|
|
$uri = str_replace('//', '/', $uri); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
return [ |
|
142
|
|
|
'id_filesystem_direcory' => $directoryEntity->getFilesystemDirectoryId(), |
|
143
|
|
|
'description' => $directoryEntity->getDescription(), |
|
144
|
|
|
'type' => $directoryEntity->getDirectoryType(), |
|
145
|
|
|
'is_autoindex' => $directoryEntity->getAutoIndex(), |
|
146
|
|
|
'id_application' => $applicationId, |
|
147
|
|
|
'id_filesystem' => $filesystemData['id_filesystem'], |
|
148
|
|
|
'path' => $filesystemData['path'], |
|
149
|
|
|
'basename' => $filesystemData['basename'], |
|
150
|
|
|
'uri' => $uri, |
|
151
|
|
|
'title' => $filesystemData['title'] |
|
152
|
|
|
]; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @param int $applicationId |
|
157
|
|
|
* @param int $directoryId |
|
158
|
|
|
* @return array |
|
159
|
|
|
*/ |
|
160
|
|
|
private function getFilesystemData(int $applicationId, int $directoryId) : array |
|
161
|
|
|
{ |
|
162
|
|
|
/** @var ConnectorInterface $connector */ |
|
163
|
|
|
$connector = $this->getConnector(); |
|
164
|
|
|
|
|
165
|
|
|
// Switch to another data group (DO NOT FORGET TO SET IT BACK!!) |
|
166
|
|
|
$connector->setDataGroup('webhemi_filesystem') |
|
167
|
|
|
->setIdKey('id_filesystem'); |
|
168
|
|
|
|
|
169
|
|
|
$filesystemRecord = $connector->getDataSet( |
|
170
|
|
|
['fk_application' => $applicationId, 'fk_filesystem_directory' => $directoryId], |
|
171
|
|
|
[ConnectorInterface::OPTION_LIMIT => 1] |
|
172
|
|
|
); |
|
173
|
|
|
|
|
174
|
|
|
// switch back to the original data group |
|
175
|
|
|
$connector->setDataGroup($this->dataGroup) |
|
176
|
|
|
->setIdKey($this->idKey); |
|
177
|
|
|
|
|
178
|
|
|
return $filesystemRecord[0] ?? []; |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|