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\Providers; |
||
10 | |||
11 | use Ghaskell\Scaffold\CodeCompilerEngine; |
||
12 | use Ghaskell\Scaffold\VibroCompiler; |
||
13 | use Illuminate\Support\ServiceProvider; |
||
0 ignored issues
–
show
|
|||
14 | use Illuminate\View\Engines\EngineResolver; |
||
15 | use Illuminate\View\Factory; |
||
16 | use Illuminate\View\FileViewFinder; |
||
17 | |||
18 | class CodeServiceProvider extends ServiceProvider |
||
19 | { |
||
20 | const CONFIG_PATH = __DIR__ . './../../config/scaffold.php'; |
||
21 | |||
22 | /** |
||
23 | * Register the service provider. |
||
24 | * |
||
25 | * @return void |
||
26 | */ |
||
27 | public function register() |
||
28 | { |
||
29 | $this->registerFactory(); |
||
30 | |||
31 | $this->registerViewFinder(); |
||
32 | |||
33 | $this->registerEngineResolver(); |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * Register the view environment. |
||
38 | * |
||
39 | * @return void |
||
40 | */ |
||
41 | public function registerFactory() |
||
42 | { |
||
43 | $this->app->singleton('code', function ($app) { |
||
44 | // Next we need to grab the engine resolver instance that will be used by the |
||
45 | // environment. The resolver will be used by an environment to get each of |
||
46 | // the various engine implementations such as plain PHP or Blade engine. |
||
47 | $resolver = $app['code.engine.resolver']; |
||
48 | |||
49 | $finder = $app['code.finder']; |
||
50 | |||
51 | $factory = $this->createFactory($resolver, $finder, $app['events']); |
||
52 | |||
53 | // We will also set the container instance on this view environment since the |
||
54 | // view composers may be classes registered in the container, which allows |
||
55 | // for great testable, flexible composers for the application developer. |
||
56 | $factory->setContainer($app); |
||
57 | |||
58 | $factory->share('app', $app); |
||
59 | |||
60 | return $factory; |
||
61 | }); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Create a new Factory Instance. |
||
66 | * |
||
67 | * @param \Illuminate\View\Engines\EngineResolver $resolver |
||
68 | * @param \Illuminate\View\ViewFinderInterface $finder |
||
69 | * @param \Illuminate\Contracts\Events\Dispatcher $events |
||
70 | * @return \Illuminate\View\Factory |
||
71 | */ |
||
72 | protected function createFactory($resolver, $finder, $events) |
||
73 | { |
||
74 | return new Factory($resolver, $finder, $events); |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Register the view finder implementation. |
||
79 | * |
||
80 | * @return void |
||
81 | */ |
||
82 | public function registerViewFinder() |
||
83 | { |
||
84 | $this->app->bind('code.finder', function ($app) { |
||
85 | return new FileViewFinder($app['files'], $app['config']['view.paths']); |
||
86 | }); |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Register the engine resolver instance. |
||
91 | * |
||
92 | * @return void |
||
93 | */ |
||
94 | public function registerEngineResolver() |
||
95 | { |
||
96 | $this->app->singleton('code.engine.resolver', function () { |
||
97 | $resolver = new EngineResolver; |
||
98 | |||
99 | // Next, we will register the various view engines with the resolver so that the |
||
100 | // environment will resolve the engines needed for various views based on the |
||
101 | // extension of view file. We call a method for each of the view's engines. |
||
102 | $this->registerVibroEngine($resolver); |
||
103 | |||
104 | return $resolver; |
||
105 | }); |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Register the Blade engine implementation. |
||
110 | * |
||
111 | * @param \Illuminate\View\Engines\EngineResolver $resolver |
||
112 | * @return void |
||
113 | */ |
||
114 | public function registerVibroEngine($resolver) |
||
115 | { |
||
116 | // The Compiler engine requires an instance of the CompilerInterface, which in |
||
117 | // this case will be the Blade compiler, so we'll first create the compiler |
||
118 | // instance to pass into the engine so it can compile the views properly. |
||
119 | $this->app->singleton('vibro.compiler', function () { |
||
120 | return new VibroCompiler( |
||
121 | $this->app['files'], $this->app['config']['view.compiled'] |
||
122 | ); |
||
123 | }); |
||
124 | |||
125 | $resolver->register('vibro', function () { |
||
126 | return new CodeCompilerEngine($this->app['vibro.compiler']); |
||
127 | }); |
||
128 | } |
||
129 | } |
||
130 | |||
131 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/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 beforeOtherDir/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: