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 |
30
|
|
|
*/ |
31
|
|
|
private $config; |
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
private $adapterUrlEncoders = []; |
36
|
|
|
/** |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
private $adapterUrlMappings = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return array |
43
|
|
|
*/ |
44
|
|
|
public function getAdapterUrlMappings($adapterType = '') |
45
|
|
|
{ |
46
|
|
|
if (empty($adapterType)) { |
47
|
|
|
return $this->adapterUrlMappings; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return empty($this->adapterUrlMappings[$adapterType]) ? null : $this->adapterUrlMappings[$adapterType]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function setAdapterUrlMappings($adapterType, $data) |
54
|
|
|
{ |
55
|
|
|
if (!is_string($adapterType)) { |
56
|
|
|
throw new InvalidArgumentException( |
57
|
|
|
'Adapter type must be a string' |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$this->adapterUrlMappings[$adapterType] = $data; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* UrlBuilder constructor. |
66
|
|
|
* |
67
|
|
|
* @param PrimaryFileSystemWrapper|null $PrimaryFileSystemWrapper |
68
|
|
|
* @param array|null $config |
69
|
|
|
*/ |
70
|
|
|
public function __construct(PrimaryFileSystemWrapper $PrimaryFileSystemWrapper = null, array $config = null) |
71
|
|
|
{ |
72
|
|
|
if (!empty($PrimaryFileSystemWrapper)) { |
73
|
|
|
$this->fileSystem = $PrimaryFileSystemWrapper->getFileSystem(); |
74
|
|
|
} |
75
|
|
|
$this->config = $config; |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param $key |
80
|
|
|
* @param UrlEncoderInterface $encoder |
81
|
|
|
*/ |
82
|
|
|
public function setAdapterUrlEncoder($key, UrlEncoderInterface $encoder) |
83
|
|
|
{ |
84
|
|
|
$this->adapterUrlEncoders[$key] = $encoder; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param $relativeFilePath |
89
|
|
|
* @param string $adapterUrlData |
|
|
|
|
90
|
|
|
* |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
public function filePublicUrl($relativeFilePath, array $urlData = []) |
94
|
|
|
{ |
95
|
|
|
// @TODO: $urlData could be collected when |
96
|
|
|
// Either the data needed to build the url is passed in adapterUrlData |
97
|
|
|
// Or it should be derived from the Adapter |
98
|
|
|
|
99
|
|
|
// Build path from the provided data if it exists |
100
|
|
|
if (!empty($urlData)) { |
101
|
|
|
return $this->formatAsUrl($urlData, $relativeFilePath); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
// Build the url from any mappings provided. |
105
|
|
|
$adapterType = $this->getAdapterTypeFromFilesystem(); |
106
|
|
|
if (!empty($this->getAdapterUrlMappings($adapterType))) { |
107
|
|
|
return $this->formatAsUrl($urlData, $relativeFilePath); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
// Build the path from adapter. |
111
|
|
|
// Most adaptors don't have any direct method to do this. |
112
|
|
|
$urlData = $this->getUrlFromFileSystem(); |
113
|
|
|
if (!empty($urlData)) { |
114
|
|
|
return $this->formatAsUrl($urlData, $relativeFilePath); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
// Use a fallback from config. |
118
|
|
|
if (!empty($this->config['default_url'])) { |
119
|
|
|
return $this->formatAsUrl($this->config['default_url'], $relativeFilePath); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
// If all of the above methods fail just return the path. |
123
|
|
|
// perhaps it being generated elsewhere!! |
124
|
|
|
|
125
|
|
|
return $relativeFilePath; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
private function getAdapterTypeFromFilesystem() |
129
|
|
|
{ |
130
|
|
|
$adapter = $this->fileSystem->getAdapter(); |
131
|
|
|
|
132
|
|
|
return $this->getAdapterType($adapter); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param $base |
137
|
|
|
* @param $path |
138
|
|
|
* |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
|
|
protected function formatAsUrl($base, $path) |
142
|
|
|
{ |
143
|
|
|
// @TODO: $base could also be an array. |
144
|
|
|
|
145
|
|
|
$url = $base . '/' . trim($path, '/'); |
146
|
|
|
|
147
|
|
|
// Check it the protocol is included. |
148
|
|
|
$urlBits = explode('://', $url); |
149
|
|
|
if ($urlBits[0] == 'http' || $urlBits[0] == 'https') { |
150
|
|
|
$urlPrefix = $urlBits[0] . '://'; |
151
|
|
|
$urlPath = $urlBits[1]; |
152
|
|
|
} |
153
|
|
|
else { |
154
|
|
|
$urlPrefix = '/'; |
155
|
|
|
$urlPath = $url; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
// Format the url part to ensure its correct. |
159
|
|
|
$urlParts = explode('/', $urlPath); |
160
|
|
|
foreach ($urlParts as $index => $part) { |
161
|
|
|
$part = trim($part); |
162
|
|
|
if (empty($part)) { |
163
|
|
|
unset($urlParts[$index]); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
$urlPath = implode('/', $urlParts); |
167
|
|
|
|
168
|
|
|
return $urlPrefix . $urlPath; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param array $data |
|
|
|
|
173
|
|
|
* |
174
|
|
|
* @return string |
175
|
|
|
*/ |
176
|
|
|
protected function getUrlFromFileSystem() |
177
|
|
|
{ |
178
|
|
|
$path = false; |
179
|
|
|
$adapter = $this->fileSystem->getAdapter(); |
180
|
|
|
$adapterType = $this->getAdapterType($adapter); |
181
|
|
|
|
182
|
|
|
if (!empty($this->adapterUrlEncoders[$adapterType])) { |
183
|
|
|
$encoder = $this->adapterUrlEncoders[$adapterType]; |
184
|
|
|
$path = $encoder->getUrl($adapter, $this->config); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return $path; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @param AdapterInterface $adapter |
192
|
|
|
* |
193
|
|
|
* @return mixed |
194
|
|
|
*/ |
195
|
|
View Code Duplication |
protected function getAdapterType(AdapterInterface $adapter) |
|
|
|
|
196
|
|
|
{ |
197
|
|
|
$class = get_class($adapter); |
198
|
|
|
$namespaceArray = explode("\\", $class); |
199
|
|
|
|
200
|
|
|
return array_pop($namespaceArray); |
201
|
|
|
} |
202
|
|
|
} |
Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.
To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.
The function can be called with either null or an array for the parameter
$needle
but will only accept an array as$haystack
.