1
|
|
|
<?php namespace Onigoetz\Imagecache\Support\Slim; |
2
|
|
|
|
3
|
|
|
use GuzzleHttp\Psr7\LazyOpenStream; |
4
|
|
|
use Onigoetz\Imagecache\Exceptions\InvalidPresetException; |
5
|
|
|
use Onigoetz\Imagecache\Exceptions\NotFoundException; |
6
|
|
|
use Onigoetz\Imagecache\Manager; |
7
|
|
|
use Onigoetz\Imagecache\Transfer; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
10
|
|
|
use RuntimeException; |
11
|
|
|
use Slim\App; |
12
|
|
|
|
13
|
|
|
class ImagecacheRegister3 |
14
|
|
|
{ |
15
|
|
|
public function request() |
16
|
|
|
{ |
17
|
|
|
return function (ServerRequestInterface $req, ResponseInterface $res, $args) { |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
$preset = $args['preset']; |
21
|
|
|
$file = $args['file']; |
22
|
|
|
|
23
|
|
|
try { |
24
|
|
|
$final_file = $this->imagecache->handleRequest($preset, $file); |
|
|
|
|
25
|
|
|
} catch (InvalidPresetException $e) { |
26
|
|
|
$res->getBody()->write($e->getMessage()); |
27
|
|
|
return $res->withStatus(404); |
28
|
|
|
} catch (NotFoundException $e) { |
29
|
|
|
$res->getBody()->write($e->getMessage()); |
30
|
|
|
return $res->withStatus(404); |
31
|
|
|
} catch (RuntimeException $e) { |
32
|
|
|
$res->getBody()->write($e->getMessage()); |
33
|
|
|
return $res->withStatus(500); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$transfer = new Transfer($final_file); |
37
|
|
|
foreach ($transfer->getHeaders() as $key => $value) { |
38
|
|
|
$res = $res->withHeader($key, $value); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return $res->withStatus($transfer->getStatus())->withBody(new LazyOpenStream($final_file, 'r')); |
42
|
|
|
}; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public static function register(App $app, $config) |
46
|
|
|
{ |
47
|
|
|
$app->getContainer()['imagecache'] = function () use ($config) { |
48
|
|
|
return new Manager($config); |
49
|
|
|
}; |
50
|
|
|
|
51
|
|
|
$app->get( |
52
|
|
|
"/{$config['path_web']}/{$config['path_cache']}/{preset}/{file:.*}", |
53
|
|
|
(new ImagecacheRegister)->request() |
|
|
|
|
54
|
|
|
) |
55
|
|
|
->setName('onigoetz.imagecache'); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: