1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of EC-CUBE |
4
|
|
|
* |
5
|
|
|
* Copyright(c) 2000-2017 LOCKON CO.,LTD. All Rights Reserved. |
6
|
|
|
* |
7
|
|
|
* http://www.lockon.co.jp/ |
8
|
|
|
* |
9
|
|
|
* This program is free software; you can redistribute it and/or |
10
|
|
|
* modify it under the terms of the GNU General Public License |
11
|
|
|
* as published by the Free Software Foundation; either version 2 |
12
|
|
|
* of the License, or (at your option) any later version. |
13
|
|
|
* |
14
|
|
|
* This program is distributed in the hope that it will be useful, |
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17
|
|
|
* GNU General Public License for more details. |
18
|
|
|
* |
19
|
|
|
* You should have received a copy of the GNU General Public License |
20
|
|
|
* along with this program; if not, write to the Free Software |
21
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
namespace Eccube\Di\Scanner; |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
28
|
|
|
use Doctrine\ORM\Mapping\Annotation; |
29
|
|
|
use Eccube\Annotation\Component; |
30
|
|
|
use Eccube\Di\ComponentDefinition; |
31
|
|
|
use Eccube\Di\Scanner; |
32
|
|
|
|
33
|
|
|
class ComponentScanner implements Scanner |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @var string[] |
37
|
|
|
*/ |
38
|
|
|
private $scanDirs; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* ComponentAutoWiring constructor. |
42
|
|
|
* @param string[] $scanDirs |
43
|
|
|
*/ |
44
|
1140 |
|
public function __construct(array $scanDirs) |
45
|
|
|
{ |
46
|
1140 |
|
$this->scanDirs = $scanDirs; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
|
|
|
|
50
|
|
|
* @param $reader AnnotationReader |
|
|
|
|
51
|
|
|
* @param $refClass \ReflectionClass |
|
|
|
|
52
|
|
|
* @return Annotation |
53
|
|
|
*/ |
54
|
22 |
|
public function findAnnotation($reader, $refClass) |
55
|
|
|
{ |
56
|
22 |
|
return $reader->getClassAnnotation($refClass, $this->getAnnotationClass()); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
16 |
|
public function getAnnotationClass() |
63
|
|
|
{ |
64
|
16 |
|
return Component::class; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
|
|
|
|
68
|
|
|
* @param $anno Component |
|
|
|
|
69
|
|
|
* @param $refClass \ReflectionClass |
|
|
|
|
70
|
|
|
* @return ComponentDefinition |
71
|
|
|
*/ |
72
|
17 |
|
public function createComponentDefinition($anno, $refClass) |
73
|
|
|
{ |
74
|
17 |
|
return new ComponentDefinition($anno->value ?: $refClass->getName(), $refClass); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param \Twig_Environment $twig |
79
|
|
|
* @param array $components |
|
|
|
|
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
21 |
|
public function generate(\Twig_Environment $twig, array $components) |
83
|
|
|
{ |
84
|
21 |
|
return $twig->createTemplate( |
85
|
21 |
|
'{% for component in components -%} |
86
|
|
|
$app["{{ component.id }}"] = function (\Pimple\Container $app) { |
87
|
|
|
$class = new \ReflectionClass(\{{ component.className }}::class); |
88
|
|
|
$instance = $class->newInstanceWithoutConstructor(); |
89
|
|
|
|
90
|
|
|
{% for dependency in component.dependencies -%} |
91
|
|
|
$property = $class->getProperty("{{ dependency.propertyName }}"); |
92
|
|
|
$property->setAccessible(true); |
93
|
|
|
$property->setValue($instance, {% if is_app(dependency.id) %}$app{% else %}$app["{{ dependency.id }}"]{% endif %}); |
94
|
|
|
{% endfor %} |
95
|
|
|
|
96
|
|
|
return $instance; |
97
|
|
|
}; |
98
|
21 |
|
{% endfor %}')->render(['components' => $components]); |
99
|
|
|
} |
100
|
|
|
|
101
|
18 |
|
public function generateExtend(\Twig_Environment $twig, array $components) |
|
|
|
|
102
|
|
|
{ |
103
|
18 |
|
return null; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return string[] |
108
|
|
|
*/ |
109
|
22 |
|
public function getScanDirs() |
110
|
|
|
{ |
111
|
22 |
|
return $this->scanDirs; |
112
|
|
|
} |
113
|
|
|
} |