1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the IrishDan\ResponsiveImageBundle package. |
4
|
|
|
* |
5
|
|
|
* (c) Daniel Byrne <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE file that was distributed with this source |
8
|
|
|
* code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace IrishDan\ResponsiveImageBundle\Url; |
12
|
|
|
|
13
|
|
|
use IrishDan\ResponsiveImageBundle\FileSystem\PrimaryFileSystemWrapper; |
14
|
|
|
use League\Flysystem\AdapterInterface; |
15
|
|
|
use Psr\Log\InvalidArgumentException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class UrlBuilder |
19
|
|
|
* |
20
|
|
|
* @package IrishDan\ResponsiveImageBundle\Url |
21
|
|
|
*/ |
22
|
|
|
class UrlBuilder |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var \League\Flysystem\FilesystemInterface|null |
26
|
|
|
*/ |
27
|
|
|
private $fileSystem; |
28
|
|
|
/** |
29
|
|
|
* @var array|null |
30
|
|
|
*/ |
31
|
|
|
private $config; |
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
private $adapterUrlEncoders = []; |
36
|
|
|
/** |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
private $adapterUrlMappings = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $adapterType |
43
|
|
|
* |
44
|
|
|
* @return array |
45
|
|
|
*/ |
46
|
|
|
public function getAdapterUrlMappings($adapterType = '') |
47
|
|
|
{ |
48
|
|
|
if (empty($adapterType)) { |
49
|
|
|
return $this->adapterUrlMappings; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return empty($this->adapterUrlMappings[$adapterType]) ? null : $this->adapterUrlMappings[$adapterType]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function setAdapterUrlMappings($adapterType, $data) |
56
|
|
|
{ |
57
|
|
|
if (!is_string($adapterType)) { |
58
|
|
|
throw new InvalidArgumentException( |
59
|
|
|
'Adapter type must be a string' |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$this->adapterUrlMappings[$adapterType] = $data; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* UrlBuilder constructor. |
68
|
|
|
* |
69
|
|
|
* @param PrimaryFileSystemWrapper|null $PrimaryFileSystemWrapper |
70
|
|
|
* @param array|null $config |
71
|
|
|
*/ |
72
|
|
|
public function __construct(PrimaryFileSystemWrapper $PrimaryFileSystemWrapper = null, array $config = null) |
73
|
|
|
{ |
74
|
|
|
if (!empty($PrimaryFileSystemWrapper)) { |
75
|
|
|
$this->fileSystem = $PrimaryFileSystemWrapper->getFileSystem(); |
76
|
|
|
} |
77
|
|
|
$this->config = $config; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param $key |
82
|
|
|
* @param UrlEncoderInterface $encoder |
83
|
|
|
*/ |
84
|
|
|
public function setAdapterUrlEncoder($key, UrlEncoderInterface $encoder) |
85
|
|
|
{ |
86
|
|
|
$this->adapterUrlEncoders[$key] = $encoder; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param $relativeFilePath |
91
|
|
|
* @param array $urlData |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
* @internal param string $adapterUrlData |
95
|
|
|
*/ |
96
|
|
|
public function filePublicUrl($relativeFilePath, array $urlData = []) |
97
|
|
|
{ |
98
|
|
|
// @TODO: $urlData could be collected when |
99
|
|
|
// Either the data needed to build the url is passed in adapterUrlData |
100
|
|
|
// Or it should be derived from the Adapter |
101
|
|
|
|
102
|
|
|
// Build path from the provided data if it exists |
103
|
|
|
if (!empty($urlData)) { |
104
|
|
|
return $this->formatAsUrl($urlData, $relativeFilePath); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
// Build the url from any mappings provided. |
108
|
|
|
$adapterType = $this->getAdapterTypeFromFilesystem(); |
109
|
|
|
if (!empty($this->getAdapterUrlMappings($adapterType))) { |
110
|
|
|
return $this->formatAsUrl($urlData, $relativeFilePath); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// Build the path from adapter. |
114
|
|
|
// Most adaptors don't have any direct method to do this. |
115
|
|
|
$urlData = $this->getUrlFromFileSystem(); |
116
|
|
|
if (!empty($urlData)) { |
117
|
|
|
return $this->formatAsUrl($urlData, $relativeFilePath); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
// Use a fallback from config. |
121
|
|
|
if (!empty($this->config['default_url'])) { |
122
|
|
|
return $this->formatAsUrl($this->config['default_url'], $relativeFilePath); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
// If all of the above methods fail just return the path. |
126
|
|
|
// perhaps it being generated elsewhere!! |
127
|
|
|
|
128
|
|
|
return $relativeFilePath; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
private function getAdapterTypeFromFilesystem() |
132
|
|
|
{ |
133
|
|
|
$adapter = $this->fileSystem->getAdapter(); |
134
|
|
|
|
135
|
|
|
return $this->getAdapterType($adapter); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param $base |
140
|
|
|
* @param $path |
141
|
|
|
* |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
|
|
protected function formatAsUrl($base, $path) |
145
|
|
|
{ |
146
|
|
|
// @TODO: $base could also be an array. |
147
|
|
|
|
148
|
|
|
$url = $base . '/' . trim($path, '/'); |
149
|
|
|
|
150
|
|
|
// Check it the protocol is included. |
151
|
|
|
$urlBits = explode('://', $url); |
152
|
|
|
if ($urlBits[0] == 'http' || $urlBits[0] == 'https') { |
153
|
|
|
$urlPrefix = $urlBits[0] . '://'; |
154
|
|
|
$urlPath = $urlBits[1]; |
155
|
|
|
} |
156
|
|
|
else { |
157
|
|
|
$urlPrefix = '/'; |
158
|
|
|
$urlPath = $url; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
// Format the url part to ensure its correct. |
162
|
|
|
$urlParts = explode('/', $urlPath); |
163
|
|
|
foreach ($urlParts as $index => $part) { |
164
|
|
|
$part = trim($part); |
165
|
|
|
if (empty($part)) { |
166
|
|
|
unset($urlParts[$index]); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
$urlPath = implode('/', $urlParts); |
170
|
|
|
|
171
|
|
|
return $urlPrefix . $urlPath; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @return string |
176
|
|
|
* @internal param array $data |
177
|
|
|
* |
178
|
|
|
*/ |
179
|
|
|
protected function getUrlFromFileSystem() |
180
|
|
|
{ |
181
|
|
|
$path = false; |
182
|
|
|
$adapter = $this->fileSystem->getAdapter(); |
183
|
|
|
$adapterType = $this->getAdapterType($adapter); |
184
|
|
|
|
185
|
|
|
if (!empty($this->adapterUrlEncoders[$adapterType])) { |
186
|
|
|
$encoder = $this->adapterUrlEncoders[$adapterType]; |
187
|
|
|
$path = $encoder->getUrl($adapter, $this->config); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return $path; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @param AdapterInterface $adapter |
195
|
|
|
* |
196
|
|
|
* @return mixed |
197
|
|
|
*/ |
198
|
|
View Code Duplication |
protected function getAdapterType(AdapterInterface $adapter) |
|
|
|
|
199
|
|
|
{ |
200
|
|
|
$class = get_class($adapter); |
201
|
|
|
$namespaceArray = explode("\\", $class); |
202
|
|
|
|
203
|
|
|
return array_pop($namespaceArray); |
204
|
|
|
} |
205
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.