Completed
Pull Request — master (#49)
by
unknown
06:05
created

ServiceProvider::boot()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 34
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 10
Bugs 3 Features 2
Metric Value
c 10
b 3
f 2
dl 0
loc 34
rs 8.8571
cc 2
eloc 18
nc 2
nop 0
1
<?php
2
3
namespace Arrilot\Widgets;
4
5
use Arrilot\Widgets\Console\WidgetMakeCommand;
6
use Arrilot\Widgets\Factories\AsyncWidgetFactory;
7
use Arrilot\Widgets\Factories\WidgetFactory;
8
use Arrilot\Widgets\Misc\LaravelApplicationWrapper;
9
use Illuminate\Console\AppNamespaceDetectorTrait;
10
use Illuminate\Support\Facades\Blade;
11
12
class ServiceProvider extends \Illuminate\Support\ServiceProvider
13
{
14
    use AppNamespaceDetectorTrait;
15
16
    /**
17
     * Register the service provider.
18
     *
19
     * @return void
20
     */
21
    public function register()
22
    {
23
        $this->mergeConfigFrom(
24
            __DIR__.'/config/config.php', 'laravel-widgets'
25
        );
26
27
        $this->app->bind('arrilot.widget', function () {
28
            return new WidgetFactory(new LaravelApplicationWrapper());
29
        });
30
31
        $this->app->bind('arrilot.async-widget', function () {
32
            return new AsyncWidgetFactory(new LaravelApplicationWrapper());
33
        });
34
35
        $this->app->singleton('arrilot.widget-group-collection', function () {
36
            return new WidgetGroupCollection(new LaravelApplicationWrapper());
37
        });
38
39
        $this->app->singleton('command.widget.make', function ($app) {
40
            return new WidgetMakeCommand($app['files']);
41
        });
42
43
        $this->commands('command.widget.make');
44
45
        $this->app->alias('arrilot.widget', 'Arrilot\Widgets\Factories\WidgetFactory');
46
        $this->app->alias('arrilot.async-widget', 'Arrilot\Widgets\Factories\AsyncWidgetFactory');
47
        $this->app->alias('arrilot.widget-group-collection', 'Arrilot\Widgets\WidgetGroupCollection');
48
    }
49
50
    /**
51
     * Bootstrap the application events.
52
     *
53
     * @return void
54
     */
55
    public function boot()
56
    {
57
        $this->publishes([
58
            __DIR__.'/config/config.php' => config_path('laravel-widgets.php'),
59
        ]);
60
61
        $routeConfig = [
62
            'namespace'  => 'Arrilot\Widgets\Controllers',
63
            'prefix'     => 'arrilot',
64
            'middleware' => $this->app['config']->get('laravel-widgets.route_middleware', []),
65
        ];
66
67
        if (!$this->app->routesAreCached()) {
0 ignored issues
show
Bug introduced by
The method routesAreCached() does not seem to exist on object<Illuminate\Contra...Foundation\Application>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
68
            $this->app['router']->group($routeConfig, function ($router) {
69
                $router->get('load-widget', 'WidgetController@showWidget');
70
            });
71
        }
72
73
        Blade::directive('widget', function($expression){
74
            return "<?php echo app('arrilot.widget')->run{$expression}; ?>";
75
        });
76
77
        Blade::directive('async-widget', function($expression){
78
            return "<?php echo app('arrilot.async-widget')->run{$expression}; ?>";
79
        });
80
81
        Blade::directive('asyncWidget', function($expression){
82
            return "<?php echo app('arrilot.async-widget')->run{$expression}; ?>";
83
        });
84
85
        Blade::directive('asyncWidget', function($expression){
86
            return "<?php echo app('arrilot.arrilot.widget-group-collection')->group{$expression}->display(); ?>";
87
        });
88
    }
89
90
    /**
91
     * Get the services provided by the provider.
92
     *
93
     * @return array
94
     */
95
    public function provides()
96
    {
97
        return ['arrilot.widget', 'arrilot.async-widget'];
98
    }
99
100
    /**
101
     * Register a blade directive.
102
     *
103
     * @param $name
104
     * @param $expression
105
     */
106
    protected function registerBladeDirective($name, $expression)
107
    {
108
        Blade::extend(function ($view) use ($name, $expression) {
109
            $pattern = $this->createMatcher($name);
110
111
            return preg_replace($pattern, $expression, $view);
112
        });
113
    }
114
115
    /**
116
     * Substitution for $compiler->createMatcher().
117
     *
118
     * Get the regular expression for a generic Blade function.
119
     *
120
     * @param string $function
121
     *
122
     * @return string
123
     */
124
    protected function createMatcher($function)
125
    {
126
        return '/(?<!\w)(\s*)@'.$function.'(\s*\(.*\))/';
127
    }
128
}
129