1 | <?php |
||
11 | class GenFormGenerator extends DoctrineFormGenerator |
||
12 | { |
||
13 | private $className; |
||
|
|||
14 | private $classPath; |
||
15 | |||
16 | /** |
||
17 | * Constructor. |
||
18 | * |
||
19 | * @param Filesystem $filesystem A Filesystem instance |
||
20 | */ |
||
21 | public function __construct(Filesystem $filesystem) |
||
25 | |||
26 | public function getClassName() |
||
30 | |||
31 | public function getClassPath() |
||
35 | |||
36 | public function getFieldFormatter() |
||
40 | |||
41 | /** |
||
42 | * {@inheritdoc} |
||
43 | */ |
||
44 | public function generate(BundleInterface $bundle, $entity, ClassMetadataInfo $metadata, $forceOverwrite = false) |
||
45 | { |
||
46 | $parts = explode('\\', $entity); |
||
47 | $entityClass = array_pop($parts); |
||
48 | |||
49 | $this->className = $entityClass.'Type'; |
||
50 | $dirPath = $bundle->getPath().'/Form'; |
||
51 | $this->classPath = $dirPath.'/'.str_replace('\\', '/', $entity).'Type.php'; |
||
52 | |||
53 | if (!$forceOverwrite && file_exists($this->classPath)) { |
||
54 | throw new \RuntimeException(sprintf('Unable to generate the %s form class as it already exists under the %s file', $this->className, $this->classPath)); |
||
55 | } |
||
56 | |||
57 | if (count($metadata->identifier) > 1) { |
||
58 | throw new \RuntimeException('The form generator does not support entity classes with multiple primary keys.'); |
||
59 | } |
||
60 | |||
61 | $parts = explode('\\', $entity); |
||
62 | array_pop($parts); |
||
63 | |||
64 | $formatter = $this->getFieldFormatter(); |
||
65 | $mappings = $formatter->formatMappings($metadata->fieldMappings); |
||
66 | |||
67 | $this->renderFile('form/FormType.php.twig', $this->classPath, array( |
||
68 | 'fields' => $this->getFieldsFromMetadata($metadata), |
||
69 | 'fields_mapping' => $mappings, |
||
70 | 'metadata' => $metadata->fieldMappings, |
||
71 | 'namespace' => $bundle->getNamespace(), |
||
72 | 'entity_namespace' => implode('\\', $parts), |
||
73 | 'entity_class' => $entityClass, |
||
74 | 'bundle' => $bundle->getName(), |
||
75 | 'form_class' => $this->className, |
||
76 | 'form_type_name' => strtolower(str_replace('\\', '_', $bundle->getNamespace()).($parts ? '_' : '').implode('_', $parts).'_'.substr($this->className, 0, -4)), |
||
77 | |||
78 | // Add 'setDefaultOptions' method with deprecated type hint, if the new 'configureOptions' isn't available. |
||
79 | // Required as long as Symfony 2.6 is supported. |
||
80 | 'configure_options_available' => method_exists('Symfony\Component\Form\AbstractType', 'configureOptions'), |
||
81 | 'get_name_required' => !method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix'), |
||
82 | )); |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * {@inheritdoc} |
||
87 | */ |
||
88 | private function getFieldsFromMetadata(ClassMetadataInfo $metadata) |
||
105 | } |
||
106 |