Completed
Push — master ( 12e65e...324689 )
by Arjay
19:32 queued 04:38
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->customJsPlugin();
27
            $this->customCssPlugin();
28
            $this->addAssetAfter();
29
            $this->addAssetBefore();
30
        } catch (QueryException $e) {
31
            // \\_(",)_//
32
        }
33
34
        FileAsset::saved(function () {
35
            $this->app['cache.store']->forget('fileAssets.all');
36
        });
37
        FileAsset::deleted(function () {
38
            $this->app['cache.store']->forget('fileAssets.all');
39
        });
40
    }
41
42
    /**
43
     * Register the application services.
44
     *
45
     * @return void
46
     */
47
    public function register()
48
    {
49
        $this->app->singleton(FileAssetRepository::class, function () {
50
            return new FileAssetCacheRepository(
51
                new FileAssetEloquentRepository,
52
                $this->app['cache.store']
53
            );
54
        });
55
    }
56
57
    /**
58
     * Register custom javascript plugin.
59
     */
60
    protected function customJsPlugin()
61
    {
62
        Blade::directive('js', function ($expression) {
63
            return "<?php echo app(\\Yajra\\CMS\\View\\Directives\\AssetJsDirective::class)->handle{$expression} ?>";
64
        });
65
    }
66
67
    /**
68
     * Register custom css plugin.
69
     *
70
     * @return string
71
     */
72
    protected function customCssPlugin()
73
    {
74
        Blade::directive('css', function ($expression) {
75
            return "<?php echo app(\\Yajra\\CMS\\View\\Directives\\AssetCssDirective::class)->handle{$expression} ?>";
76
        });
77
    }
78
79
    /**
80
     * Add a css after the selected file.
81
     *
82
     * @return string
83
     */
84
    protected function addAssetAfter()
85
    {
86
        Blade::directive('assetAfter', function ($expression) {
87
            return "<?php echo app(\\Yajra\\CMS\\View\\Directives\\AssetAddAfterDirective::class)->handle{$expression} ?>";
88
        });
89
    }
90
91
    /**
92
     * Add a css before the selected file.
93
     *
94
     * @return string
95
     */
96
    protected function addAssetBefore()
97
    {
98
        Blade::directive('assetBefore', function ($expression) {
99
            return "<?php echo app(\\Yajra\\CMS\\View\\Directives\\AssetAddBeforeDirective::class)->handle{$expression} ?>";
100
        });
101
    }
102
}
103