ImageUploadServiceProvider::boot()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 2
eloc 6
nc 2
nop 0
1
<?php
2
3
namespace AnkitPokhrel\LaravelImage;
4
5
use Illuminate\Contracts\Filesystem\Filesystem;
6
use Illuminate\Support\ServiceProvider;
7
use League\Flysystem\Adapter\Local;
8
use League\Flysystem\Filesystem as LeagueFilesystem;
9
use League\Glide\Responses\LaravelResponseFactory;
10
use League\Glide\ServerFactory;
11
12
class ImageUploadServiceProvider extends ServiceProvider
13
{
14
    /**
15
     * Bootstrap the application services.
16
     */
17
    public function boot()
18
    {
19
        if ( ! $this->app->routesAreCached()) {
20
            require __DIR__ . '/routes.php';
21
        }
22
23
        $this->publishes([
24
            __DIR__ . '/../config/config.php' => config_path('laravelimage.php'),
25
        ]);
26
27
        $this->registerBladeExtensions();
28
    }
29
30
    /**
31
     * Register the application services.
32
     */
33
    public function register()
34
    {
35
        $this->app->bind('\AnkitPokhrel\LaravelImage\ImageUploadService');
36
37
        $this->app->singleton('laravelImage', function () {
38
            return $this->app->make('\AnkitPokhrel\LaravelImage\ImageHelper');
39
        });
40
41
        $this->registerGlide();
42
    }
43
44
    /**
45
     * Register glide.
46
     */
47
    protected function registerGlide()
48
    {
49
        $this->app->singleton('\League\Glide\Server', function ($app) {
50
            $fileSystem = $app->make(Filesystem::class);
51
52
            $uploadDir = config('laravelimage.uploadDir');
53
            // Set source filesystem
54
            $source = new LeagueFilesystem(
55
                new Local($uploadDir)
56
            );
57
58
            // Set cache filesystem
59
            $cache = new LeagueFilesystem(
60
                new Local($fileSystem->getDriver()->getAdapter()->getPathPrefix() . '/laravel-image-cache')
61
            );
62
63
            // Setup glide server
64
            return ServerFactory::create([
65
                'source'   => $source,
66
                'cache'    => $cache,
67
                'base_url' => config('laravelimage.routePath') . '/' . basename($uploadDir),
68
                'response' => new LaravelResponseFactory(),
69
            ]);
70
        });
71
    }
72
73
    /**
74
     * Register blade templates.
75
     */
76
    protected function registerBladeExtensions()
77
    {
78
        $blade = $this->app['view']->getEngineResolver()->resolve('blade')->getCompiler();
79
80
        $blade->directive('laravelImage', function ($options) {
81
            return "<?php echo \\AnkitPokhrel\\LaravelImage\\LaravelImageFacade::image($options);?>";
82
        });
83
    }
84
}
85