Completed
Push — master ( c4a686...2e0334 )
by Arjay
11:39
created

AssetServiceProvider::CssDirective()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 9.4285
1
<?php
2
3
namespace Yajra\CMS\Providers;
4
5
use Illuminate\Support\ServiceProvider;
6
use Illuminate\Database\QueryException;
7
use Yajra\CMS\Entities\FileAsset;
8
use Yajra\CMS\Repositories\FileAsset\FileAssetCacheRepository;
9
use Yajra\CMS\Repositories\FileAsset\FileAssetEloquentRepository;
10
use Yajra\CMS\Repositories\FileAsset\FileAssetRepository;
11
use Illuminate\Support\Facades\Blade;
12
13
/**
14
 * Class AssetServiceProvider
15
 *
16
 * @package Yajra\CMS\Providers
17
 */
18
class AssetServiceProvider extends ServiceProvider
19
{
20
    /**
21
     * Bootstrap the application services.
22
     */
23
    public function boot()
24
    {
25
        try {
26
            $this->addAssets('css');
27
            $this->addAssets('js');
28
            $this->requireAdminDefaultAssets();
29
            $this->JsDirective();
30
            $this->CssDirective();
31
        } catch (QueryException $e) {
32
            // \\_(",)_//
33
        }
34
35
        FileAsset::saved(function () {
36
            $this->app['cache.store']->forget('fileAssets.all');
37
        });
38
        FileAsset::deleted(function () {
39
            $this->app['cache.store']->forget('fileAssets.all');
40
        });
41
    }
42
43
    /**
44
     * Register the application services.
45
     *
46
     * @return void
47
     */
48
    public function register()
49
    {
50
        $this->app->singleton(FileAssetRepository::class, function () {
51
            return new FileAssetCacheRepository(
52
                new FileAssetEloquentRepository,
53
                $this->app['cache.store']
54
            );
55
        });
56
    }
57
58
    /**
59
     * Load require admin default assets.
60
     */
61
    protected function requireAdminDefaultAssets()
62
    {
63
        $this->app->make(FileAssetRepository::class)->registerAdminRequireAssets();
64
    }
65
66
    /**
67
     * Register javascript blade directive.
68
     */
69
    protected function JsDirective()
70
    {
71
        Blade::directive('js', function ($expression) {
72
            return "<?php echo app(\\Yajra\\CMS\\View\\Directives\\AssetJsDirective::class)->handle({$expression}) ?>";
73
        });
74
    }
75
76
    /**
77
     * Register css blade directive.
78
     *
79
     * @return string
80
     */
81
    protected function CssDirective()
82
    {
83
        Blade::directive('css', function ($expression) {
84
            return "<?php echo app(\\Yajra\\CMS\\View\\Directives\\AssetCssDirective::class)->handle({$expression}) ?>";
85
        });
86
    }
87
88
    /**
89
     * Add site assets.
90
     *
91
     * @param string $type
92
     */
93
    protected function addAssets($type)
94
    {
95
        return $this->app->make(FileAssetRepository::class)->addAsset($type);
96
    }
97
}
98