1 | <?php |
||
11 | final class Compiler implements CompilerInterface |
||
12 | { |
||
13 | /** |
||
14 | * @var string |
||
15 | */ |
||
16 | public $classDir; |
||
17 | |||
18 | /** |
||
19 | * @var CodeGenInterface |
||
20 | */ |
||
21 | private $codeGen; |
||
22 | |||
23 | 24 | public function __construct(string $classDir) |
|
35 | |||
36 | 1 | public function __sleep() |
|
37 | { |
||
38 | 1 | return ['classDir']; |
|
39 | } |
||
40 | |||
41 | 1 | public function __wakeup() |
|
42 | { |
||
43 | 1 | $this->__construct($this->classDir); |
|
44 | 1 | } |
|
45 | |||
46 | /** |
||
47 | * {@inheritdoc} |
||
48 | */ |
||
49 | 10 | public function newInstance($class, array $args, BindInterface $bind) |
|
50 | { |
||
51 | 10 | $compiledClass = $this->compile($class, $bind); |
|
52 | 10 | $instance = (new ReflectionClass($compiledClass))->newInstanceArgs($args); |
|
53 | 10 | $instance->bindings = $bind->getBindings(); |
|
54 | |||
55 | 10 | return $instance; |
|
56 | } |
||
57 | |||
58 | /** |
||
59 | * {@inheritdoc} |
||
60 | */ |
||
61 | 18 | public function compile($class, BindInterface $bind) : string |
|
62 | { |
||
63 | 18 | if ($this->hasNoBinding($class, $bind)) { |
|
64 | 1 | return $class; |
|
65 | } |
||
66 | 17 | $newClass = $this->getNewClassName($class, $bind); |
|
67 | 17 | if (class_exists($newClass)) { |
|
68 | 7 | return $newClass; |
|
69 | } |
||
70 | 10 | $file = "{$this->classDir}/{$newClass}.php"; |
|
71 | 10 | if (file_exists($file)) { |
|
72 | /** @noinspection UntrustedInclusionInspection */ |
||
73 | /** @noinspection PhpIncludeInspection */ |
||
74 | 1 | include $file; |
|
75 | |||
76 | 1 | return $newClass; |
|
77 | } |
||
78 | 9 | $this->includeGeneratedCode($newClass, new ReflectionClass($class), $file, $bind); |
|
79 | |||
80 | 9 | return $newClass; |
|
81 | } |
||
82 | |||
83 | 18 | private function hasNoBinding($class, BindInterface $bind) : bool |
|
89 | |||
90 | 17 | private function getNewClassName($class, BindInterface $bind) : string |
|
94 | |||
95 | 18 | private function hasBoundMethod(string $class, BindInterface $bind) : bool |
|
107 | |||
108 | 9 | private function includeGeneratedCode($newClass, \ReflectionClass $sourceClass, string $file, BindInterface $bind) |
|
115 | } |
||
116 |