|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* apparat-resource |
|
5
|
|
|
* |
|
6
|
|
|
* @category Apparat |
|
7
|
|
|
* @package Apparat\Resource |
|
8
|
|
|
* @subpackage Apparat\Resource\Infrastructure |
|
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
|
10
|
|
|
* @copyright Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
|
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
/*********************************************************************************** |
|
15
|
|
|
* The MIT License (MIT) |
|
16
|
|
|
* |
|
17
|
|
|
* Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
|
18
|
|
|
* |
|
19
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
|
20
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
|
21
|
|
|
* the Software without restriction, including without limitation the rights to |
|
22
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
|
23
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
|
24
|
|
|
* subject to the following conditions: |
|
25
|
|
|
* |
|
26
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
27
|
|
|
* copies or substantial portions of the Software. |
|
28
|
|
|
* |
|
29
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
30
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
|
31
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
|
32
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
|
33
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
|
34
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
35
|
|
|
***********************************************************************************/ |
|
36
|
|
|
|
|
37
|
|
|
namespace Apparat\Resource\Ports; |
|
38
|
|
|
|
|
39
|
|
|
use Apparat\Kernel\Ports\Kernel; |
|
40
|
|
|
use Apparat\Resource\Domain\Contract\ReaderInterface; |
|
41
|
|
|
use Apparat\Resource\Domain\Contract\WriterInterface; |
|
42
|
|
|
use Apparat\Resource\Infrastructure\Io\File\AbstractFileReaderWriter; |
|
43
|
|
|
use Apparat\Resource\Infrastructure\Io\File\Reader as FileReader; |
|
44
|
|
|
use Apparat\Resource\Infrastructure\Io\File\Writer as FileWriter; |
|
45
|
|
|
use Apparat\Resource\Infrastructure\Io\InMemory\AbstractInMemoryReaderWriter; |
|
46
|
|
|
use Apparat\Resource\Infrastructure\Io\InMemory\Reader as InMemoryReader; |
|
47
|
|
|
use Apparat\Resource\Infrastructure\Io\InMemory\Writer as InMemoryWriter; |
|
48
|
|
|
use Apparat\Resource\Infrastructure\Service\Copy; |
|
49
|
|
|
use Apparat\Resource\Infrastructure\Service\Delete; |
|
50
|
|
|
use Apparat\Resource\Infrastructure\Service\Move; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* API tools |
|
54
|
|
|
* |
|
55
|
|
|
* @package Apparat\Resource |
|
56
|
|
|
* @subpackage Apparat\Resource\Infrastructure |
|
57
|
|
|
*/ |
|
58
|
|
|
class Tools |
|
59
|
|
|
{ |
|
60
|
|
|
/** |
|
61
|
|
|
* Reader classes for stream wrappers |
|
62
|
|
|
* |
|
63
|
|
|
* @var array |
|
64
|
|
|
*/ |
|
65
|
|
|
protected static $reader = array( |
|
66
|
|
|
AbstractFileReaderWriter::WRAPPER => FileReader::class, |
|
67
|
|
|
AbstractInMemoryReaderWriter::WRAPPER => InMemoryReader::class, |
|
68
|
|
|
); |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Writer classes for stream wrappers |
|
72
|
|
|
* |
|
73
|
|
|
* @var array |
|
74
|
|
|
*/ |
|
75
|
|
|
protected static $writer = array( |
|
76
|
|
|
AbstractFileReaderWriter::WRAPPER => FileWriter::class, |
|
77
|
|
|
AbstractInMemoryReaderWriter::WRAPPER => InMemoryWriter::class, |
|
78
|
|
|
); |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Find and instantiate a writer for a particular target |
|
82
|
|
|
* |
|
83
|
|
|
* @param string $target Target |
|
84
|
|
|
* @param array $parameters Parameters |
|
85
|
|
|
* @return null|WriterInterface Writer instance |
|
86
|
|
|
*/ |
|
87
|
15 |
|
public static function writer(&$target, array $parameters = array()) |
|
88
|
|
|
{ |
|
89
|
15 |
|
$writer = null; |
|
90
|
|
|
|
|
91
|
|
|
// Run through all registered writer |
|
92
|
15 |
|
foreach (self::$writer as $wrapper => $writerClass) { |
|
93
|
15 |
|
$wrapperLength = strlen($wrapper); |
|
94
|
|
|
|
|
95
|
|
|
// If this wrapper is used: Instantiate the reader and resource |
|
96
|
15 |
|
if ($wrapperLength ? !strncmp($wrapper, $target, $wrapperLength) : !preg_match( |
|
97
|
15 |
|
"%^[a-z0-9\.]+\:\/\/%", |
|
98
|
|
|
$target |
|
99
|
|
|
) |
|
100
|
|
|
) { |
|
101
|
12 |
|
array_unshift($parameters, substr($target, $wrapperLength)); |
|
102
|
12 |
|
$writer = Kernel::create($writerClass, $parameters); |
|
103
|
15 |
|
break; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
15 |
|
return $writer; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Copy a resource |
|
112
|
|
|
* |
|
113
|
|
|
* @param string $src Stream-wrapped source |
|
114
|
|
|
* @param array ...$parameters Reader parameters |
|
115
|
|
|
* @return Copy Copy handler |
|
116
|
|
|
* @throws \Apparat\Resource\Ports\InvalidArgumentException If the reader stream wrapper is invalid |
|
117
|
|
|
* @api |
|
118
|
|
|
*/ |
|
119
|
7 |
|
public static function copy($src, ...$parameters) |
|
120
|
|
|
{ |
|
121
|
7 |
|
$reader = self::reader($src, $parameters); |
|
122
|
7 |
|
if ($reader instanceof ReaderInterface) { |
|
123
|
6 |
|
return Kernel::create(Copy::class, [$reader]); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
1 |
|
throw self::failInvalidReader(); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Find and instantiate a reader for a particular source |
|
131
|
|
|
* |
|
132
|
|
|
* @param string $src Source |
|
133
|
|
|
* @param array $parameters Parameters |
|
134
|
|
|
* @return null|ReaderInterface Reader instance |
|
135
|
|
|
*/ |
|
136
|
28 |
|
public static function reader(&$src, array $parameters = array()) |
|
137
|
|
|
{ |
|
138
|
28 |
|
$reader = null; |
|
139
|
|
|
|
|
140
|
|
|
// Run through all registered readers |
|
141
|
28 |
|
foreach (self::$reader as $wrapper => $readerClass) { |
|
142
|
28 |
|
$wrapperLength = strlen($wrapper); |
|
143
|
|
|
|
|
144
|
|
|
// If this wrapper is used: Instantiate the reader and resource |
|
145
|
28 |
|
if ($wrapperLength ? !strncmp($wrapper, $src, $wrapperLength) : !preg_match("%^[a-z0-9\.]+\:\/\/%", $src)) { |
|
146
|
24 |
|
array_unshift($parameters, substr($src, $wrapperLength)); |
|
147
|
24 |
|
$reader = Kernel::create($readerClass, $parameters); |
|
148
|
28 |
|
break; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
28 |
|
return $reader; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Fail because of an invalid reader stream wrapper |
|
157
|
|
|
* |
|
158
|
|
|
* @return InvalidReaderArgumentException If the reader stream wrapper is invalid |
|
159
|
|
|
*/ |
|
160
|
3 |
|
protected static function failInvalidReader() |
|
161
|
|
|
{ |
|
162
|
3 |
|
return new InvalidReaderArgumentException( |
|
163
|
3 |
|
'Invalid reader stream wrapper', |
|
164
|
3 |
|
InvalidReaderArgumentException::INVALID_READER_STREAM_WRAPPER |
|
165
|
|
|
); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Move / rename a resource |
|
170
|
|
|
* |
|
171
|
|
|
* @param string $src Stream-wrapped source |
|
172
|
|
|
* @param array ...$parameters Reader parameters |
|
173
|
|
|
* @return Move move handler |
|
174
|
|
|
* @throws \Apparat\Resource\Ports\InvalidArgumentException If the reader stream wrapper is invalid |
|
175
|
|
|
* @api |
|
176
|
|
|
*/ |
|
177
|
7 |
|
public static function move($src, ...$parameters) |
|
178
|
|
|
{ |
|
179
|
7 |
|
$reader = self::reader($src, $parameters); |
|
180
|
7 |
|
if ($reader instanceof ReaderInterface) { |
|
181
|
6 |
|
return Kernel::create(Move::class, [$reader]); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
1 |
|
throw self::failInvalidReader(); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Delete a resource |
|
189
|
|
|
* |
|
190
|
|
|
* @param string $src Stream-wrapped source |
|
191
|
|
|
* @param array ...$parameters Reader parameters |
|
192
|
|
|
* @return Move move handler |
|
193
|
|
|
* @throws \Apparat\Resource\Ports\InvalidArgumentException If the reader stream wrapper is invalid |
|
194
|
|
|
* @api |
|
195
|
|
|
*/ |
|
196
|
3 |
|
public static function delete($src, ...$parameters) |
|
197
|
|
|
{ |
|
198
|
3 |
|
$reader = self::reader($src, $parameters); |
|
199
|
3 |
|
if ($reader instanceof ReaderInterface) { |
|
200
|
|
|
/** @var Delete $deleter */ |
|
201
|
2 |
|
$deleter = Kernel::create(Delete::class, [$reader]); |
|
202
|
2 |
|
return $deleter(); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
1 |
|
throw self::failInvalidReader(); |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|