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