Completed
Push — master ( 2adaf5...166020 )
by George
05:35 queued 03:32
created

CodeServiceProvider   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
dl 0
loc 157
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A registerFactory() 0 20 1
A register() 0 7 1
A registerVibroEngine() 0 13 1
A registerFileEngine() 0 4 1
A registerPhpEngine() 0 4 1
A registerEngineResolver() 0 13 2
A registerBladeEngine() 0 13 1
A createFactory() 0 3 1
A registerViewFinder() 0 4 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: georg
5
 * Date: 5/28/2018
6
 * Time: 1:09 PM
7
 */
8
9
namespace Ghaskell\Scaffold;
10
11
use Ghaskell\Scaffold\Facades\Vibro;
12
use Illuminate\View\Engines\PhpEngine;
13
use Illuminate\Support\ServiceProvider;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Ghaskell\Scaffold\ServiceProvider. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
14
use Illuminate\View\Engines\FileEngine;
15
use Illuminate\View\Engines\CompilerEngine;
16
use Illuminate\View\Engines\EngineResolver;
17
use Illuminate\View\Compilers\BladeCompiler;
18
use Illuminate\View\FileViewFinder;
19
use Illuminate\View\Factory;
20
21
class CodeServiceProvider extends ServiceProvider
22
{
23
    /**
24
     * Register the service provider.
25
     *
26
     * @return void
27
     */
28
    public function register()
29
    {
30
        $this->registerFactory();
31
32
        $this->registerViewFinder();
33
34
        $this->registerEngineResolver();
35
    }
36
37
    /**
38
     * Register the view environment.
39
     *
40
     * @return void
41
     */
42
    public function registerFactory()
43
    {
44
        $this->app->singleton('view', function ($app) {
45
            // Next we need to grab the engine resolver instance that will be used by the
46
            // environment. The resolver will be used by an environment to get each of
47
            // the various engine implementations such as plain PHP or Blade engine.
48
            $resolver = $app['view.engine.resolver'];
49
50
            $finder = $app['view.finder'];
51
52
            $factory = $this->createFactory($resolver, $finder, $app['events']);
53
54
            // We will also set the container instance on this view environment since the
55
            // view composers may be classes registered in the container, which allows
56
            // for great testable, flexible composers for the application developer.
57
            $factory->setContainer($app);
58
59
            $factory->share('app', $app);
60
61
            return $factory;
62
        });
63
    }
64
65
    /**
66
     * Create a new Factory Instance.
67
     *
68
     * @param  \Illuminate\View\Engines\EngineResolver  $resolver
69
     * @param  \Illuminate\View\ViewFinderInterface  $finder
70
     * @param  \Illuminate\Contracts\Events\Dispatcher  $events
71
     * @return \Illuminate\View\Factory
72
     */
73
    protected function createFactory($resolver, $finder, $events)
74
    {
75
        return new Factory($resolver, $finder, $events);
76
    }
77
78
    /**
79
     * Register the view finder implementation.
80
     *
81
     * @return void
82
     */
83
    public function registerViewFinder()
84
    {
85
        $this->app->bind('view.finder', function ($app) {
86
            return new FileViewFinder($app['files'], $app['config']['view.paths']);
87
        });
88
    }
89
90
    /**
91
     * Register the engine resolver instance.
92
     *
93
     * @return void
94
     */
95
    public function registerEngineResolver()
96
    {
97
        $this->app->singleton('view.engine.resolver', function () {
98
            $resolver = new EngineResolver;
99
100
            // Next, we will register the various view engines with the resolver so that the
101
            // environment will resolve the engines needed for various views based on the
102
            // extension of view file. We call a method for each of the view's engines.
103
            foreach (['file', 'php', 'blade', 'vibro'] as $engine) {
104
                $this->{'register'.ucfirst($engine).'Engine'}($resolver);
105
            }
106
107
            return $resolver;
108
        });
109
    }
110
111
    /**
112
     * Register the file engine implementation.
113
     *
114
     * @param  \Illuminate\View\Engines\EngineResolver  $resolver
115
     * @return void
116
     */
117
    public function registerFileEngine($resolver)
118
    {
119
        $resolver->register('file', function () {
120
            return new FileEngine;
121
        });
122
    }
123
124
    /**
125
     * Register the PHP engine implementation.
126
     *
127
     * @param  \Illuminate\View\Engines\EngineResolver  $resolver
128
     * @return void
129
     */
130
    public function registerPhpEngine($resolver)
131
    {
132
        $resolver->register('php', function () {
133
            return new PhpEngine;
134
        });
135
    }
136
137
    /**
138
     * Register the Blade engine implementation.
139
     *
140
     * @param  \Illuminate\View\Engines\EngineResolver  $resolver
141
     * @return void
142
     */
143
    public function registerBladeEngine($resolver)
144
    {
145
        // The Compiler engine requires an instance of the CompilerInterface, which in
146
        // this case will be the Blade compiler, so we'll first create the compiler
147
        // instance to pass into the engine so it can compile the views properly.
148
        $this->app->singleton('blade.compiler', function () {
149
            return new VibroCompiler(
150
                $this->app['files'], $this->app['config']['view.compiled']
151
            );
152
        });
153
154
        $resolver->register('blade', function () {
155
            return new CompilerEngine($this->app['blade.compiler']);
156
        });
157
    }
158
159
    /**
160
     * Register the Blade engine implementation.
161
     *
162
     * @param  \Illuminate\View\Engines\EngineResolver  $resolver
163
     * @return void
164
     */
165
    public function registerVibroEngine($resolver)
166
    {
167
        // The Compiler engine requires an instance of the CompilerInterface, which in
168
        // this case will be the Blade compiler, so we'll first create the compiler
169
        // instance to pass into the engine so it can compile the views properly.
170
        $this->app->singleton('vibro.compiler', function () {
171
            return new VibroCompiler(
172
                $this->app['files'], $this->app['config']['view.compiled']
173
            );
174
        });
175
176
        $resolver->register('vibro', function () {
177
            return new CompilerEngine($this->app['vibro.compiler']);
178
        });
179
    }
180
}
181