1 | <?php |
||
20 | final class Compiler |
||
21 | { |
||
22 | /** |
||
23 | * Compile application |
||
24 | * |
||
25 | * @param string $appName application name "MyVendor|MyProject" |
||
26 | * @param string $context application context "prod-app" |
||
27 | * @param string $appDir application path |
||
28 | */ |
||
29 | 1 | public function __invoke($appName, $context, $appDir) : string |
|
30 | { |
||
31 | 1 | $appMeta = new AppMeta($appName, $context, $appDir); |
|
32 | 1 | $injector = new AppInjector($appName, $context); |
|
33 | 1 | $cache = $injector->getInstance(Cache::class); |
|
34 | 1 | $reader = $injector->getInstance(AnnotationReader::class); |
|
35 | /* @var $reader \Doctrine\Common\Annotations\Reader */ |
||
36 | 1 | $namedParams = $injector->getInstance(NamedParameterInterface::class); |
|
37 | /* @var $namedParams NamedParameterInterface */ |
||
38 | |||
39 | // create DI factory class and AOP compiled class for all resources and save $app cache. |
||
40 | 1 | (new Bootstrap)->newApp($appMeta, $context, $cache); |
|
41 | |||
42 | // check resource injection and create annotation cache |
||
43 | 1 | foreach ($appMeta->getResourceListGenerator() as list($className)) { |
|
44 | 1 | $this->scanClass($injector, $reader, $namedParams, $className); |
|
45 | } |
||
46 | 1 | $logFile = $appMeta->logDir . '/compile.log'; |
|
47 | 1 | $this->saveCompileLog($appMeta, $context, $logFile); |
|
48 | |||
49 | 1 | return $logFile; |
|
50 | } |
||
51 | |||
52 | 1 | private function scanClass(InjectorInterface $injector, Reader $reader, NamedParameterInterface $namedParams, string $className) |
|
53 | { |
||
54 | 1 | $instance = $injector->getInstance($className); |
|
55 | 1 | $class = new \ReflectionClass($className); |
|
56 | 1 | $reader->getClassAnnotations($class); |
|
57 | 1 | $methods = $class->getMethods(); |
|
58 | 1 | foreach ($methods as $method) { |
|
59 | 1 | $methodName = $method->getName(); |
|
|
|||
60 | 1 | if ($this->isMagicMethod($methodName)) { |
|
61 | 1 | continue; |
|
62 | } |
||
63 | 1 | $this->saveNamedParam($namedParams, $instance, $methodName); |
|
64 | // method annotation |
||
65 | 1 | $reader->getMethodAnnotations($method); |
|
66 | } |
||
67 | 1 | } |
|
68 | |||
69 | 1 | private function isMagicMethod($method) : bool |
|
73 | |||
74 | 1 | private function saveNamedParam(NamedParameterInterface $namedParameter, $instance, string $method) |
|
86 | |||
87 | 1 | private function saveCompileLog(AbstractAppMeta $appMeta, string $context, string $logFile) |
|
97 | } |
||
98 |