2
2
3
3
namespace League \Glide ;
4
4
5
- use InvalidArgumentException ;
6
5
use League \Flysystem \FileExistsException ;
7
6
use League \Flysystem \FilesystemInterface ;
8
7
use League \Glide \Api \ApiInterface ;
9
8
use League \Glide \Filesystem \FilesystemException ;
10
9
use League \Glide \Http \NotFoundException ;
11
- use League \Glide \Http \RequestFactory ;
10
+ use League \Glide \Http \RequestArgumentsResolver ;
12
11
use League \Glide \Http \ResponseFactory ;
13
12
use Symfony \Component \HttpFoundation \Request ;
14
13
use Symfony \Component \HttpFoundation \StreamedResponse ;
@@ -57,7 +56,7 @@ class Server
57
56
* @param FilesystemInterface|null $cache The cache file system.
58
57
* @param ApiInterface $api The image manipulation API.
59
58
*/
60
- public function __construct (FilesystemInterface $ source , $ cache , ApiInterface $ api )
59
+ public function __construct (FilesystemInterface $ source , FilesystemInterface $ cache = null , ApiInterface $ api )
61
60
{
62
61
$ this ->setSource ($ source );
63
62
$ this ->setCache ($ cache );
@@ -108,7 +107,7 @@ public function getSourcePathPrefix()
108
107
*/
109
108
public function getSourcePath ()
110
109
{
111
- $ request = $ this -> resolveRequestObject (func_get_args ());
110
+ $ request = ( new RequestArgumentsResolver ())-> getRequest (func_get_args ());
112
111
113
112
$ path = trim ($ request ->getPathInfo (), '/ ' );
114
113
@@ -134,7 +133,7 @@ public function getSourcePath()
134
133
*/
135
134
public function sourceFileExists ()
136
135
{
137
- $ request = $ this -> resolveRequestObject (func_get_args ());
136
+ $ request = ( new RequestArgumentsResolver ())-> getRequest (func_get_args ());
138
137
139
138
return $ this ->source ->has ($ this ->getSourcePath ($ request ));
140
139
}
@@ -143,7 +142,7 @@ public function sourceFileExists()
143
142
* Set the cache file system.
144
143
* @param FilesystemInterface|null $cache The cache file system.
145
144
*/
146
- public function setCache ($ cache )
145
+ public function setCache (FilesystemInterface $ cache = null )
147
146
{
148
147
$ this ->cache = $ cache ;
149
148
}
@@ -182,7 +181,7 @@ public function getCachePathPrefix()
182
181
*/
183
182
public function getCachePath ()
184
183
{
185
- $ request = $ this -> resolveRequestObject (func_get_args ());
184
+ $ request = ( new RequestArgumentsResolver ())-> getRequest (func_get_args ());
186
185
187
186
$ path = md5 ($ this ->getSourcePath ($ request ).'? ' .http_build_query ($ request ->query ->all ()));
188
187
@@ -204,7 +203,7 @@ public function cacheFileExists()
204
203
return false ;
205
204
}
206
205
207
- $ request = $ this -> resolveRequestObject (func_get_args ());
206
+ $ request = ( new RequestArgumentsResolver ())-> getRequest (func_get_args ());
208
207
209
208
return $ this ->cache ->has ($ this ->getCachePath ($ request ));
210
209
}
@@ -252,7 +251,7 @@ public function getBaseUrl()
252
251
*/
253
252
public function outputImage ()
254
253
{
255
- $ request = $ this -> resolveRequestObject (func_get_args ());
254
+ $ request = ( new RequestArgumentsResolver ())-> getRequest (func_get_args ());
256
255
257
256
$ this ->makeImage ($ request );
258
257
@@ -274,7 +273,7 @@ public function outputImage()
274
273
*/
275
274
public function getImageResponse ()
276
275
{
277
- $ request = $ this -> resolveRequestObject (func_get_args ());
276
+ $ request = ( new RequestArgumentsResolver ())-> getRequest (func_get_args ());
278
277
279
278
$ this ->makeImage ($ request );
280
279
@@ -288,7 +287,7 @@ public function getImageResponse()
288
287
*/
289
288
public function makeImage ()
290
289
{
291
- $ request = $ this -> resolveRequestObject (func_get_args ());
290
+ $ request = ( new RequestArgumentsResolver ())-> getRequest (func_get_args ());
292
291
293
292
if ($ this ->cacheFileExists ($ request ) === true ) {
294
293
return $ request ;
@@ -310,62 +309,36 @@ public function makeImage()
310
309
);
311
310
}
312
311
313
- try {
314
- $ written = $ this ->writeCache ($ request , $ source );
315
- } catch (FileExistsException $ exception ) {
316
- // Cache file failed to write. Fail silently.
317
- return $ request ;
318
- }
319
-
320
- if ($ written === false ) {
321
- throw new FilesystemException (
322
- 'Could not write the image ` ' .$ this ->getCachePath ($ request ).'`. '
323
- );
312
+ if ($ this ->cache ) {
313
+ $ this ->writeCache ($ request , $ source );
324
314
}
325
315
326
316
return $ request ;
327
317
}
328
318
329
- /**
330
- * Resolve request object.
331
- * @param array $args Array of supplied arguments.
332
- * @return Request The request object.
333
- */
334
- protected function resolveRequestObject ($ args )
335
- {
336
- if (isset ($ args [0 ]) and $ args [0 ] instanceof Request) {
337
- return $ args [0 ];
338
- }
339
-
340
- if (isset ($ args [0 ]) and is_string ($ args [0 ])) {
341
- $ path = $ args [0 ];
342
- $ params = [];
343
-
344
- if (isset ($ args [1 ]) and is_array ($ args [1 ])) {
345
- $ params = $ args [1 ];
346
- }
347
-
348
- return RequestFactory::create ($ path , $ params );
349
- }
350
-
351
- throw new InvalidArgumentException ('Not a valid path or Request object. ' );
352
- }
353
-
354
319
/**
355
320
* Write the cache file if caching is enabled.
356
- * @param Request $request
357
- * @param string $source
321
+ * @param Request $request
322
+ * @param string $source
358
323
* @return mixed
359
324
*/
360
- private function writeCache (Request $ request , $ source )
325
+ protected function writeCache (Request $ request , $ source )
361
326
{
362
- if ($ this ->cache === null ) {
363
- return null ;
364
- }
327
+ try {
328
+ $ written = $ this ->cache ->write (
329
+ $ this ->getCachePath ($ request ),
330
+ $ this ->api ->run ($ request , $ source )
331
+ );
365
332
366
- return $ this ->cache ->write (
367
- $ this ->getCachePath ($ request ),
368
- $ this ->api ->run ($ request , $ source )
369
- );
333
+ if ($ written === false ) {
334
+ throw new FilesystemException (
335
+ 'Could not write the image ` ' .$ this ->getCachePath ($ request ).'`. '
336
+ );
337
+ }
338
+ } catch (FileExistsException $ exception ) {
339
+ // This edge case occurs when the target already exists
340
+ // because it's currently be written to disk in another
341
+ // request. It's best to just fail silently.
342
+ }
370
343
}
371
344
}
0 commit comments