1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
5
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
6
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
7
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
8
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
9
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
10
|
|
|
* THE SOFTWARE. |
11
|
|
|
* |
12
|
|
|
* This software consists of voluntary contributions made by many individuals |
13
|
|
|
* and is licensed under the MIT license. |
14
|
|
|
* Copyright (c) 2015 Yuuki Takezawa |
15
|
|
|
* |
16
|
|
|
*/ |
17
|
|
|
namespace Ytake\LaravelAspect; |
18
|
|
|
|
19
|
|
|
use Ray\Aop\Compiler; |
20
|
|
|
use Illuminate\Filesystem\Filesystem; |
21
|
|
|
use Illuminate\Contracts\Container\Container; |
22
|
|
|
use Ytake\LaravelAspect\Exception\ClassNotFoundException; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class RayAspectKernel |
26
|
|
|
*/ |
27
|
|
|
class RayAspectKernel implements AspectDriverInterface |
28
|
|
|
{ |
29
|
|
|
/** @var Container */ |
30
|
|
|
protected $app; |
31
|
|
|
|
32
|
|
|
/** @var array */ |
33
|
|
|
protected $configure; |
34
|
|
|
|
35
|
|
|
/** @var Compiler */ |
36
|
|
|
protected $compiler; |
37
|
|
|
|
38
|
|
|
/** @var Filesystem */ |
39
|
|
|
protected $filesystem; |
40
|
|
|
|
41
|
|
|
/** @var bool */ |
42
|
|
|
protected $cacheable = false; |
43
|
|
|
|
44
|
|
|
/** @var \Ytake\LaravelAspect\Modules\AspectModule */ |
45
|
|
|
protected $aspectResolver; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* RayAspectKernel constructor. |
49
|
|
|
* @param Container $app |
50
|
|
|
* @param Filesystem $filesystem |
51
|
|
|
* @param array $configure |
52
|
|
|
*/ |
53
|
|
|
public function __construct(Container $app, Filesystem $filesystem, array $configure) |
54
|
|
|
{ |
55
|
|
|
$this->app = $app; |
56
|
|
|
$this->filesystem = $filesystem; |
57
|
|
|
$this->configure = $configure; |
58
|
|
|
$this->makeCompileDir(); |
59
|
|
|
$this->makeCacheableDir(); |
60
|
|
|
$this->compiler = $this->getCompiler(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param null $module |
65
|
|
|
* |
66
|
|
|
* @throws ClassNotFoundException |
67
|
|
|
*/ |
68
|
|
|
public function register($module = null) |
69
|
|
|
{ |
70
|
|
|
if (!class_exists($module)) { |
71
|
|
|
throw new ClassNotFoundException($module); |
72
|
|
|
} |
73
|
|
|
$this->aspectResolver = (new $module($this->app)); |
74
|
|
|
$this->aspectResolver->attach(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* boot aspect kernel |
79
|
|
|
*/ |
80
|
|
|
public function dispatch() |
81
|
|
|
{ |
82
|
|
|
foreach ($this->aspectResolver->getResolver() as $class => $pointcuts) { |
83
|
|
|
$bind = (new AspectBind($this->filesystem, $this->configure['cache_dir'], $this->cacheable)) |
84
|
|
|
->bind($class, $pointcuts); |
85
|
|
|
$compiledClass = $this->compiler->compile($class, $bind); |
86
|
|
|
$this->app->bind($class, function ($app) use ($bind, $compiledClass) { |
87
|
|
|
$instance = $app->make($compiledClass); |
88
|
|
|
$instance->bindings = $bind->getBindings(); |
89
|
|
|
return $instance; |
90
|
|
|
}); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return Compiler |
96
|
|
|
*/ |
97
|
|
|
protected function getCompiler() |
98
|
|
|
{ |
99
|
|
|
return new Compiler($this->configure['compile_dir']); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* make source compile file directory |
104
|
|
|
* |
105
|
|
|
* @return void |
106
|
|
|
*/ |
107
|
|
|
protected function makeCompileDir() |
108
|
|
|
{ |
109
|
|
|
$this->makeDirectories($this->configure['compile_dir'], 0777); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* make aspect cache directory |
114
|
|
|
* |
115
|
|
|
* @return void |
116
|
|
|
*/ |
117
|
|
|
protected function makeCacheableDir() |
118
|
|
|
{ |
119
|
|
|
if ($this->configure['cache']) { |
120
|
|
|
$this->makeDirectories($this->configure['cache_dir'], 0777); |
121
|
|
|
$this->cacheable = true; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param string $dir |
127
|
|
|
* @param int $mode |
128
|
|
|
*/ |
129
|
|
|
private function makeDirectories($dir, $mode = 0777) |
130
|
|
|
{ |
131
|
|
|
// @codeCoverageIgnoreStart |
132
|
|
|
if (!$this->filesystem->exists($dir)) { |
133
|
|
|
$this->filesystem->makeDirectory($dir, $mode, true); |
134
|
|
|
} |
135
|
|
|
// @@codeCoverageIgnoreEnd |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|