Пример #1
0
 /**
  * Generate the CRUD controller.
  *
  * @param BundleInterface   $bundle           A bundle object
  * @param string            $entity           The entity relative class name
  * @param ClassMetadataInfo $metadata         The entity class metadata
  * @param string            $format           The configuration format (xml, yaml, annotation)
  * @param string            $routePrefix      The route name prefix
  * @param array             $needWriteActions Wether or not to generate write actions
  *
  * @throws \RuntimeException
  */
 public function generate(BundleInterface $bundle, $entity, ClassMetadataInfo $metadata, $format, $routePrefix, $needWriteActions, $forceOverwrite)
 {
     $this->namingArray = Helper\NamingHelper::getNamingArray($bundle);
     $this->routePrefix = $routePrefix;
     $this->routeNamePrefix = $this->namingArray['vendor'] . '_' . $this->namingArray['bundle'] . '_' . strtolower($entity) . '_';
     $this->actions = $needWriteActions ? array('home', 'add', 'modify', 'see', 'delete') : array('home', 'see');
     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.');
     }
     $this->entity = $entity;
     $this->bundle = $bundle;
     $this->metadata = $metadata;
     $this->format = $format;
     $this->generateControllerClass($forceOverwrite);
     $dir = sprintf('%s/Resources/views/%s', $this->bundle->getPath(), str_replace('\\', '/', $this->entity));
     if (!file_exists($dir)) {
         $this->filesystem->mkdir($dir, 0777);
     }
     $this->generateHomeView($dir);
     $this->generateSeeView($dir);
     $this->generateAddView($dir);
     $this->generateModifyView($dir);
     $this->generateSeeEmbedView($dir);
     $this->generateTestClass();
     $this->generateMenuBuilderClass();
     //        $this->generateConfiguration();
 }
 public function generate(BundleInterface $bundle, $entity, $format, array $fields, $withRepository)
 {
     // configure the bundle (needed if the bundle does not contain any Entities yet)
     $config = $this->registry->getManager(null)->getConfiguration();
     $config->setEntityNamespaces(array_merge(array($bundle->getName() => $bundle->getNamespace() . '\\Entity'), $config->getEntityNamespaces()));
     $entityClass = $this->registry->getAliasNamespace($bundle->getName()) . '\\' . $entity;
     $entityPath = $bundle->getPath() . '/Entity/' . str_replace('\\', '/', $entity) . '.php';
     if (file_exists($entityPath)) {
         throw new \RuntimeException(sprintf('Entity "%s" already exists.', $entityClass));
     }
     $class = new ClassMetadataInfo($entityClass);
     $namingArray = Helper\NamingHelper::getNamingArray($bundle);
     $class->table['name'] = $namingArray['vendor'] . '__' . $namingArray['bundle'] . '__' . strtolower($entity);
     if ($withRepository) {
         $class->customRepositoryClassName = $entityClass . 'Repository';
     }
     $class->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true));
     $class->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO);
     foreach ($fields as $field) {
         $class->mapField($field);
     }
     $entityGenerator = $this->getEntityGenerator();
     if ('annotation' === $format) {
         $entityGenerator->setGenerateAnnotations(true);
         $entityCode = $entityGenerator->generateEntityClass($class);
         $mappingPath = $mappingCode = false;
     } else {
         $cme = new ClassMetadataExporter();
         $exporter = $cme->getExporter('yml' == $format ? 'yaml' : $format);
         $mappingPath = $bundle->getPath() . '/Resources/config/doctrine/' . str_replace('\\', '.', $entity) . '.orm.' . $format;
         if (file_exists($mappingPath)) {
             throw new \RuntimeException(sprintf('Cannot generate entity when mapping "%s" already exists.', $mappingPath));
         }
         $mappingCode = $exporter->exportClassMetadata($class);
         $entityGenerator->setGenerateAnnotations(false);
         $entityCode = $entityGenerator->generateEntityClass($class);
     }
     $this->filesystem->mkdir(dirname($entityPath));
     file_put_contents($entityPath, $entityCode);
     if ($mappingPath) {
         $this->filesystem->mkdir(dirname($mappingPath));
         file_put_contents($mappingPath, $mappingCode);
     }
     if ($withRepository) {
         $path = $bundle->getPath() . str_repeat('/..', substr_count(get_class($bundle), '\\'));
         $this->getRepositoryGenerator()->writeEntityRepositoryClass($class->customRepositoryClassName, $path);
     }
 }