Completed
Push — master ( 05dd03...c5e7d0 )
by Nekrasov
10s
created

ServiceProvider::createMatcher()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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 cannot recognize @async-widget, so @async-widget still use the custom matcher.
78
        $this->registerBladeDirective('async-widget', '$1<?php echo app("arrilot.async-widget")->run$2; ?>');
79
80
        Blade::directive('asyncWidget', function ($expression) {
81
            return "<?php echo app('arrilot.async-widget')->run{$expression}; ?>";
82
        });
83
84
        Blade::directive('widgetGroup', function ($expression) {
85
            return "<?php echo app('arrilot.widget-group-collection')->group{$expression}->display(); ?>";
86
        });
87
    }
88
89
    /**
90
     * Get the services provided by the provider.
91
     *
92
     * @return array
93
     */
94
    public function provides()
95
    {
96
        return ['arrilot.widget', 'arrilot.async-widget'];
97
    }
98
99
    /**
100
     * Register a blade directive.
101
     *
102
     * @param $name
103
     * @param $expression
104
     */
105
    protected function registerBladeDirective($name, $expression)
106
    {
107
        Blade::extend(function ($view) use ($name, $expression) {
108
            $pattern = $this->createMatcher($name);
109
110
            return preg_replace($pattern, $expression, $view);
111
        });
112
    }
113
114
    /**
115
     * Substitution for $compiler->createMatcher().
116
     *
117
     * Get the regular expression for a generic Blade function.
118
     *
119
     * @param string $function
120
     *
121
     * @return string
122
     */
123
    protected function createMatcher($function)
124
    {
125
        return '/(?<!\w)(\s*)@'.$function.'(\s*\(.*\))/';
126
    }
127
}
128