1
|
|
|
<?php namespace Onigoetz\Imagecache\Support\Laravel; |
2
|
|
|
|
3
|
|
|
use Illuminate\Support\ServiceProvider; |
4
|
|
|
use Onigoetz\Imagecache\Exceptions\InvalidPresetException; |
5
|
|
|
use Onigoetz\Imagecache\Exceptions\NotFoundException; |
6
|
|
|
use Onigoetz\Imagecache\Manager; |
7
|
|
|
use Onigoetz\Imagecache\Transfer; |
8
|
|
|
|
9
|
|
|
class ImagecacheServiceProvider extends ServiceProvider |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Add the namespace to config |
13
|
|
|
*/ |
14
|
15 |
|
public function registerConfiguration() |
15
|
|
|
{ |
16
|
15 |
|
$this->publishes( |
17
|
|
|
[ |
18
|
15 |
|
__DIR__ . '/../../config/imagecache.php' => config_path('imagecache.php'), |
19
|
|
|
] |
20
|
5 |
|
); |
21
|
15 |
|
} |
22
|
|
|
|
23
|
15 |
|
public function getConfiguration() |
24
|
|
|
{ |
25
|
15 |
|
return $this->app['config']->get('imagecache'); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Register imagecache |
30
|
|
|
*/ |
31
|
15 |
|
public function registerManager() |
32
|
|
|
{ |
33
|
|
|
$this->app->singleton('imagecache', function () { |
34
|
15 |
|
$config = $this->getConfiguration(); |
35
|
|
|
|
36
|
15 |
|
return new Manager($config); |
37
|
15 |
|
}); |
38
|
15 |
|
} |
39
|
|
|
|
40
|
15 |
|
public function registerRoute() |
41
|
|
|
{ |
42
|
15 |
|
$config = $this->getConfiguration(); |
43
|
|
|
|
44
|
15 |
|
$url = "{$config['path_web']}/{$config['path_cache']}/{preset}/{file}"; |
45
|
|
|
|
46
|
15 |
|
$this->app['router']->get( |
47
|
15 |
|
$url, |
48
|
|
|
function ($preset, $file) { |
49
|
|
|
try { |
50
|
12 |
|
$final_file = $this->app['imagecache']->handleRequest($preset, $file); |
51
|
10 |
|
} catch (InvalidPresetException $e) { |
52
|
3 |
|
return \Response::make('Invalid preset', 404); |
53
|
6 |
|
} catch (NotFoundException $e) { |
54
|
3 |
|
return \Response::make('File not found', 404); |
55
|
3 |
|
} catch (\RuntimeException $e) { |
56
|
3 |
|
return \Response::make($e->getMessage(), 500); |
57
|
|
|
} |
58
|
|
|
|
59
|
3 |
|
$transfer = new Transfer($final_file); |
60
|
|
|
|
61
|
3 |
|
$callback = function () use ($transfer) { |
62
|
3 |
|
$transfer->stream(); |
63
|
3 |
|
}; |
64
|
|
|
|
65
|
3 |
|
return \Response::stream($callback, $transfer->getStatus(), $transfer->getHeaders()); |
66
|
10 |
|
} |
67
|
15 |
|
)->where('file', '.*'); |
68
|
15 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Register the service provider. |
72
|
|
|
* |
73
|
|
|
* @return void |
74
|
|
|
*/ |
75
|
15 |
|
public function register() |
76
|
|
|
{ |
77
|
15 |
|
$this->registerConfiguration(); |
78
|
|
|
|
79
|
15 |
|
$this->registerManager(); |
80
|
|
|
|
81
|
15 |
|
$this->registerRoute(); |
82
|
15 |
|
} |
83
|
|
|
} |
84
|
|
|
|