/** * Generates the entity form class if it does not exist. * * @param BundleInterface $bundle The bundle in which to create the class * @param string $entity The entity relative class name * @param ClassMetadataInfo $metadata The entity metadata class */ public function generate(BundleInterface $bundle, $entity, ClassMetadataInfo $metadata) { if (is_null($this->src)) { $this->src = $bundle->getPath(); } $this->entity = $entity; $dirPath = $this->src . '/Form/Type'; $this->classPath = $dirPath . '/' . str_replace('\\', '/', $entity) . 'FormType.php'; if (count($metadata->identifier) > 1) { throw new \RuntimeException('The form generator does not support entity classes with multiple primary keys.'); } $fields = $this->getFieldsFromMetadata($metadata); $maxColumnNameSize = 0; foreach ($fields as $field) { $maxColumnNameSize = max($field['columnNameSize'] + 2, $maxColumnNameSize); } $options = array('fields' => $fields, 'form_class' => $entity . 'FormType', 'form_label' => $entity, 'max_column_name_size' => $maxColumnNameSize); $this->tplOptions = array_merge($this->tplOptions, $options); $this->generateForm(); $this->generateServices(); if ($this->getContainer()->getParameter('dev_generator_tool.generate_translation')) { $g = new TranslationGenerator($this->filesystem, sprintf('%s/Resources/translations', $this->src), $entity, $fields); $g->generate(); } }
/** * Generate the CRUD controller. * * @param BundleInterface $entityBundle A bundle object * @param string $entity The entity relative class name * @param ClassMetadataInfo $metadata The entity class metadata * @param string $routePrefix The route name prefix * @param array $needWriteActions Wether or not to generate write actions * * @throws \RuntimeException */ public function generate($bundle, $entity, ClassMetadataInfo $metadata, $routePrefix, $needWriteActions) { $this->bundle = $bundle; $this->metadata = $metadata; $this->actions = $needWriteActions ? ['list', 'show', 'new', 'edit', 'delete'] : ['list', 'show']; $this->entity = $entity; $str = preg_replace('/([A-Z])/', ' \\1', $entity); $str = trim($str); $str = strtolower($str); $this->entityName = str_replace(' ', '_', $str); if (count($metadata->identifier) > 1) { throw new \RuntimeException('The CRUD generator does not support entity classes with multiple primary keys.'); } if (!in_array('id', $metadata->identifier)) { throw new \RuntimeException('The CRUD generator expects the entity object has a primary key field named "id" with a getId() method.'); } if (is_null($this->src)) { $this->src = $this->bundle->getPath(); } if (is_null($this->outputBundle)) { $this->outputBundle = $this->bundle->getName(); } $this->initTplOptions($routePrefix); $this->generateControllerClass(); $dirViews = sprintf('%s/Resources/views/%s', $this->src, str_replace('\\', '/', $this->entity)); if (!file_exists($dirViews)) { $this->filesystem->mkdir($dirViews, 0777); } $this->generateIndexView($dirViews); if (in_array('show', $this->actions)) { $this->generateShowView($dirViews); } if (in_array('new', $this->actions)) { $this->generateNewView($dirViews); } if (in_array('edit', $this->actions)) { $this->generateEditView($dirViews); } if ($this->getContainer()->getParameter('dev_generator_tool.generate_translation')) { $fieldMappings = $this->getFieldMappings(); $g = new TranslationGenerator($this->filesystem, sprintf('%s/Resources/translations', $this->src), $entity, $fieldMappings); $g->generate(); } $this->generateConfiguration(); }