1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of EC-CUBE |
5
|
|
|
* |
6
|
|
|
* Copyright(c) LOCKON CO.,LTD. All Rights Reserved. |
7
|
|
|
* |
8
|
|
|
* http://www.lockon.co.jp/ |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Eccube\Form\Extension; |
15
|
|
|
|
16
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
17
|
|
|
use Doctrine\Common\Annotations\Reader; |
18
|
|
|
use Doctrine\ORM\EntityManager; |
19
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
20
|
|
|
use Eccube\Annotation\FormAppend; |
21
|
|
|
use Eccube\Annotation\FormExtension; |
22
|
|
|
use Symfony\Component\Form\AbstractTypeExtension; |
23
|
|
|
use Symfony\Component\Form\Extension\Core\Type\FormType; |
24
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
25
|
|
|
use Symfony\Component\Form\FormEvent; |
26
|
|
|
use Symfony\Component\Form\FormEvents; |
27
|
|
|
use Symfony\Component\Form\FormInterface; |
28
|
|
|
use Symfony\Component\Form\FormView; |
29
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @FormExtension |
33
|
|
|
*/ |
34
|
|
|
class DoctrineOrmExtension extends AbstractTypeExtension |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* @var EntityManager |
38
|
|
|
*/ |
39
|
|
|
protected $em; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var AnnotationReader |
43
|
|
|
*/ |
44
|
|
|
protected $reader; |
45
|
|
|
|
46
|
674 |
|
public function __construct(EntityManagerInterface $em, Reader $reader) |
47
|
|
|
{ |
48
|
674 |
|
$this->em = $em; |
|
|
|
|
49
|
674 |
|
$this->reader = $reader; |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
674 |
|
public function buildForm(FormBuilderInterface $builder, array $options) |
56
|
|
|
{ |
57
|
674 |
|
$builder->addEventListener( |
58
|
674 |
|
FormEvents::PRE_SET_DATA, |
59
|
674 |
|
function (FormEvent $event) { |
60
|
673 |
|
$form = $event->getForm(); |
61
|
673 |
|
$config = $form->getConfig(); |
62
|
|
|
// data_classオプションが必要 |
63
|
673 |
|
$class = $config->getDataClass(); |
64
|
673 |
|
if (is_null($class)) { |
65
|
673 |
|
return; |
66
|
|
|
} |
67
|
|
|
// メタデータの取得 |
68
|
|
|
try { |
69
|
404 |
|
$meta = $this->em->getClassMetadata($class); |
70
|
44 |
|
} catch (\Exception $e) { |
71
|
44 |
|
return; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** @var \ReflectionProperty[] $props */ |
75
|
384 |
|
$props = $meta->getReflectionProperties(); |
76
|
384 |
|
foreach ($props as $prop) { |
77
|
384 |
|
$anno = $this->reader->getPropertyAnnotation($prop, FormAppend::class); |
|
|
|
|
78
|
384 |
|
if ($anno) { |
79
|
|
|
$options = is_null($anno->options) ? [] : $anno->options; |
80
|
|
|
$options['eccube_form_options'] = [ |
81
|
|
|
'auto_render' => (true === $anno->auto_render), |
82
|
|
|
'form_theme' => $anno->form_theme, |
83
|
|
|
]; |
84
|
|
|
if (!isset($form[$prop->getName()])) { |
85
|
384 |
|
$form->add($prop->getName(), $anno->type, $options); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
674 |
|
} |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
261 |
|
public function buildView(FormView $view, FormInterface $form, array $options) |
94
|
|
|
{ |
95
|
261 |
|
$options = $form->getConfig()->getOption('eccube_form_options'); |
96
|
|
|
|
97
|
261 |
|
if (!array_key_exists('auto_render', $options)) { |
98
|
|
|
$options['auto_render'] = false; |
99
|
|
|
} |
100
|
|
|
|
101
|
261 |
|
if (!array_key_exists('form_theme', $options)) { |
102
|
|
|
$options['form_theme'] = null; |
103
|
|
|
} |
104
|
|
|
|
105
|
261 |
|
$view->vars['eccube_form_options'] = $options; |
106
|
|
|
} |
107
|
|
|
|
108
|
674 |
|
public function configureOptions(OptionsResolver $resolver) |
109
|
|
|
{ |
110
|
674 |
|
$resolver->setDefault( |
111
|
674 |
|
'eccube_form_options', |
112
|
|
|
[ |
113
|
674 |
|
'auto_render' => false, |
114
|
|
|
'form_theme' => null, |
115
|
|
|
] |
116
|
|
|
); |
117
|
|
|
} |
118
|
|
|
|
119
|
675 |
|
public function getExtendedType() |
120
|
|
|
{ |
121
|
675 |
|
return FormType::class; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.