ViewServiceProvider::boot()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 6
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace ReneDeKat\LaravelViewFileFinder;
4
5
use Illuminate\View\Factory;
6
use Illuminate\View\Engines\PhpEngine;
7
use Illuminate\Support\ServiceProvider;
8
use Illuminate\View\Engines\FileEngine;
9
use Illuminate\View\Engines\CompilerEngine;
10
use Illuminate\View\Engines\EngineResolver;
11
use Illuminate\View\Compilers\BladeCompiler;
12
13
class ViewServiceProvider extends ServiceProvider
14
{
15
    /**
16
     * Bootstrap the application services.
17
     */
18
    public function boot()
19
    {
20
        $this->publishes([
21
            __DIR__.'/../assets/missing.blade.php' => resource_path('/assets/pages/missing.blade.php'),
22
        ], 'assets');
23
    }
24
25
    /**
26
     * Register the service provider.
27
     *
28
     * @return void
29
     */
30
    public function register()
31
    {
32
        $this->registerFactory();
33
34
        $this->registerViewFinder();
35
36
        $this->registerEngineResolver();
37
    }
38
39
    /**
40
     * Register the view environment.
41
     *
42
     * @return void
43
     */
44
    public function registerFactory()
45
    {
46
        $this->app->singleton('view', function ($app) {
47
            // Next we need to grab the engine resolver instance that will be used by the
48
            // environment. The resolver will be used by an environment to get each of
49
            // the various engine implementations such as plain PHP or Blade engine.
50
            $resolver = $app['view.engine.resolver'];
51
52
            $finder = $app['view.finder'];
53
54
            $env = new Factory($resolver, $finder, $app['events']);
55
56
            // We will also set the container instance on this view environment since the
57
            // view composers may be classes registered in the container, which allows
58
            // for great testable, flexible composers for the application developer.
59
            $env->setContainer($app);
60
61
            $env->share('app', $app);
62
63
            return $env;
64
        });
65
    }
66
67
    /**
68
     * Register the view finder implementation.
69
     *
70
     * @return void
71
     */
72
    public function registerViewFinder()
73
    {
74
        $this->app->bind('view.finder', function ($app) {
75
            return new FileViewFinder($app['files'], $app['config']['view.paths']);
76
        });
77
    }
78
79
    /**
80
     * Register the engine resolver instance.
81
     *
82
     * @return void
83
     */
84
    public function registerEngineResolver()
85
    {
86
        $this->app->singleton('view.engine.resolver', function () {
87
            $resolver = new EngineResolver;
88
89
            // Next, we will register the various view engines with the resolver so that the
90
            // environment will resolve the engines needed for various views based on the
91
            // extension of view file. We call a method for each of the view's engines.
92
            foreach (['file', 'php', 'blade'] as $engine) {
93
                $this->{'register'.ucfirst($engine).'Engine'}($resolver);
94
            }
95
96
            return $resolver;
97
        });
98
    }
99
100
    /**
101
     * Register the file engine implementation.
102
     *
103
     * @param  \Illuminate\View\Engines\EngineResolver  $resolver
104
     * @return void
105
     */
106
    public function registerFileEngine($resolver)
107
    {
108
        $resolver->register('file', function () {
109
            return new FileEngine;
110
        });
111
    }
112
113
    /**
114
     * Register the PHP engine implementation.
115
     *
116
     * @param  \Illuminate\View\Engines\EngineResolver  $resolver
117
     * @return void
118
     */
119
    public function registerPhpEngine($resolver)
120
    {
121
        $resolver->register('php', function () {
122
            return new PhpEngine;
123
        });
124
    }
125
126
    /**
127
     * Register the Blade engine implementation.
128
     *
129
     * @param  \Illuminate\View\Engines\EngineResolver  $resolver
130
     * @return void
131
     */
132
    public function registerBladeEngine($resolver)
133
    {
134
        // The Compiler engine requires an instance of the CompilerInterface, which in
135
        // this case will be the Blade compiler, so we'll first create the compiler
136
        // instance to pass into the engine so it can compile the views properly.
137
        $this->app->singleton('blade.compiler', function () {
138
            return new BladeCompiler(
139
                $this->app['files'], $this->app['config']['view.compiled']
140
            );
141
        });
142
143
        $resolver->register('blade', function () {
144
            return new CompilerEngine($this->app['blade.compiler']);
145
        });
146
    }
147
}
148