Completed
Push — experimental/3.1 ( 3ded70...5f55e6 )
by chihiro
18:23 queued 18:05
created

ComponentScanner::generate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 18
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
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
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
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
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$reader" missing
Loading history...
introduced by
Doc comment for parameter "$refClass" missing
Loading history...
50
     * @param $reader AnnotationReader
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
51
     * @param $refClass \ReflectionClass
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
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
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$anno" missing
Loading history...
introduced by
Doc comment for parameter "$refClass" missing
Loading history...
68
     * @param $anno Component
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
69
     * @param $refClass \ReflectionClass
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
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
0 ignored issues
show
introduced by
Expected 13 spaces after parameter type; 1 found
Loading history...
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)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
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
}