setDefinition(array( new InputOption('namespace', '', InputOption::VALUE_REQUIRED, 'The namespace of the bundle to create'), new InputOption('bundle', '', InputOption::VALUE_REQUIRED, 'The bundle to generate model classes from'), )) ->setDescription('Generate basics files for the CRUD') ->setHelp(<<setName('generate:crud') ; } /** * @see Command * */ protected function execute(InputInterface $input, OutputInterface $output) { $dialog = $this->getDialogHelper(); $filesystem = $this->getContainer()->get('filesystem'); if ($input->isInteractive()) { if (!$dialog->askConfirmation($output, $dialog->getQuestion('Do you confirm generation', 'yes', '?'), true)) { $output->writeln( array( '', 'Command aborted', '' ) ); return 1; } } $dialog->writeSection($output, 'Welcome to the CRUD generation!'); /* ------------------------------------------ */ $output->writeln( array( '', 'Help:', ' Use \ for the namespace delimiter to avoid any problem.', '', ' You can find the bundle name in the file app/AppKernel.php without ()', '', ) ); $namespace = $dialog->askAndValidate( $output, $dialog->getQuestion('Bundle namespace', $input->getOption('namespace')), array('Sensio\Bundle\GeneratorBundle\Command\Validators', 'validateBundleNamespace'), false, $input->getOption('namespace') ); $input->setOption('namespace', $namespace); $bundle = $dialog->askAndValidate( $output, $dialog->getQuestion('Bundle name', $input->getOption('bundle')), array('Sensio\Bundle\GeneratorBundle\Command\Validators', 'validateBundleName'), false, $input->getOption('bundle') ); $input->setOption('bundle', $bundle); $kernel = $this->getContainer()->get('kernel'); $kernel->getBundle($bundle); $namespace_explode = explode("\\",$namespace); /* ------------------------------------------ */ // target dir $path = dirname($this->getContainer()->getParameter('kernel.root_dir')).'/src'.DIRECTORY_SEPARATOR; $path.= implode(DIRECTORY_SEPARATOR, $namespace_explode); if (!$filesystem->exists($path)) { $output->writeln(array('','Command aborted. The directory '.$path.' doesn\'t exist.','')); return 1; } /* ------------------------------------------ */ $errors = array(); $runner = $dialog->getRunner($output, $errors); /* ------------------------------------------ */ $output->writeln(array('','Step 1: Generate CrudConfiguration','')); $model = str_replace($namespace_explode[0], '', $bundle); $model = str_replace('Bundle','',$model); $model = $dialog->ask($output, $dialog->getQuestion('Model', $model, ':'), $model); $classNameConfiguration = $model.'CrudConfiguration'; $nameSpaceConfiguration = $namespace.'\Configuration'; $modelPeer = $model.'Peer'; $modelQuery = $model.'Query'; $routePrefix = $bundle.$model.'Admin'; $formNamespace = ''; $modelNamespace = ''; $class = ''; foreach ($namespace_explode as $dir) { $formNamespace .= $dir."\\\\"; $modelNamespace .= $dir."\\\\"; $class .= $dir."\\"; } $formNamespace .= 'Form\\Type'; $modelNamespace .= 'Model'; $class .= 'Model\\'.$model; $formEditNew = $model.'Type'; $formFilter = $model.'FilterType'; $maxPerPage = $dialog->askAndValidate( $output, $dialog->getQuestion('Max per page', 20, ':'), function ($answer) { if (!is_numeric($answer)) { throw new \RunTimeException( 'The value must be an integer' ); } return (int) $answer; }, false, 20 ); $rankable = $dialog->askConfirmation( $output, $dialog->getQuestion('Rankable', 'no', ':'), false ); $order = null; if ($rankable) { $order = $dialog->select($output, $dialog->getQuestion('Rank order','ASC'), array('ASC', 'DESC'), 0); } $rankable = !$rankable ? 'false' : 'true'; $contentConfiguration = "setModelNamespace('".$modelNamespace."') ->setModel('".$model."') ->setModelPeer('".$modelPeer."') ->setModelQuery('".$modelQuery."') ->setRoutePrefix('".$routePrefix."') ->setFormNamespace('".$formNamespace."') ->setFormEdit('".$formEditNew."') ->setFormFilter('".$formFilter."') ->setFormNew('".$formEditNew."') ->setFieldsets(array( '' => array( '*', ), )) ->setMaxPerPage(".$maxPerPage.") ->setRankable(".$rankable.")"; $orderRankable = array(0 => 'ASC', 1 => 'DESC'); if ($order) { $contentConfiguration .= " ->setSort('orderByRank', \\Criteria::".$orderRankable[$order].") "; } $contentConfiguration .= " ->setListTitle('".$model."') ->setNewTitle('New ".strtolower($model)."') ->setEditTitle('Edition of \"%id%\"') // Listing: // ->setFieldTemplate('visible', 'TrinityAdminBundle:BaseAdmin:bool.html.twig') // ->setDisplayFields(array( // // )) // ->setFieldname('foo', 'bar') ; } } "; //demande si ok ? $output->writeln($contentConfiguration); if ($input->isInteractive()) { if (!$dialog->askConfirmation($output, $dialog->getQuestion('Do you agree', 'yes', '?'), true)) { $output->writeln( array( '', 'Command aborted', '' ) ); return 1; } } // on regarde si le dossier configuration existe $pathConfiguration = $path.DIRECTORY_SEPARATOR.'Configuration'; $configurationFile = $pathConfiguration.DIRECTORY_SEPARATOR.$classNameConfiguration.'.php'; try { $filesystem->mkdir($pathConfiguration, 0777); } catch (\IOException $e) { $output->writeln( array( '', '$e', '' ) ); return 1; } if ($filesystem->exists($configurationFile)) { $output->writeln( array( '', 'The file '.$configurationFile.' already exists.', '' ) ); if ($input->isInteractive()) { if ($dialog->askConfirmation($output, $dialog->getQuestion('Do you want to replace it', 'no', '?'), false)) { $filesystem->remove($configurationFile); $output->writeln( array( '', 'File '.$configurationFile.' REMOVED', '' ) ); try { $filesystem->touch($configurationFile, 0777); file_put_contents($configurationFile, $contentConfiguration); } catch (\IOException $e) { $output->writeln( array( '', '$e', '' ) ); return 1; } } } } else { try { file_put_contents($configurationFile, $contentConfiguration); } catch (\IOException $e) { $output->writeln( array( '', '$e', '' ) ); return 1; } } $output->writeln(array('', 'Generating the CRUD configuration: OK','')); /* ------------------------------------------ */ $output->writeln(array('','Step 2: Generate Controller','')); if ($input->isInteractive()) { if (!$dialog->askConfirmation($output, $dialog->getQuestion('Continue', 'yes', '?'), true)) { $output->writeln( array( '', 'Command aborted', '' ) ); return 1; } } $classNameController = $model.'AdminController'; $nameSpaceController = $namespace.'\Controller'; $contentController = 'configuration = new CrudConfiguration(); } /** * @Route("/{page}", name="'.$routePrefix.'_index", defaults={"page" = "1"}, requirements={"page" = "\d+"}) * @Template() */ public function indexAction($page, Request $request) { return parent::indexAction($page, $request); } /** * @Route("/new", name="'.$routePrefix.'_new") * @Template() */ public function newAction(Request $request) { return parent::newAction($request); } /** * @Route("/edit/{id}", name="'.$routePrefix.'_edit") * @Template() * @ParamConverter("object", class="'.$class.'") */ public function editAction($object, Request $request) { return parent::editAction($object, $request); } /** * @Route("/remove/{id}/{token}", name="'.$routePrefix.'_remove") * @Template() * @ParamConverter("object", class="'.$class.'") */ public function removeAction($object, $token, Request $request) { return parent::removeAction($object, $token, $request); } /** * @Route("/batch", name="'.$routePrefix.'_batch") * @Template() * @Method({"POST"}) */ public function batchAction(Request $request) { return parent::batchAction($request); } /** * @Route("/filter/clear", name="'.$routePrefix.'_filter_clear") * @Template() */ public function clearFilterAction(Request $request) { return parent::clearFilterAction($request); }'; if ($rankable) { $contentController .= ' /** * @Route("/rank", name="'.$routePrefix.'_rank") * @Template("TrinityAdminBundle:BaseAdmin:rank.html.twig") * @Method({"POST"}) */ public function rankAction(Request $request) { return parent::rankAction($request); }'; } $contentController .= ' } '; $output->writeln($contentController); if ($input->isInteractive()) { if (!$dialog->askConfirmation($output, $dialog->getQuestion('Are you agree', 'yes', '?'), true)) { $output->writeln( array( '', 'Command aborted', '' ) ); return 1; } } // on regarde si le dossier configuration existe $pathController = $path.DIRECTORY_SEPARATOR.'Controller'; $controllerFile = $pathController.DIRECTORY_SEPARATOR.$classNameController.'.php'; try { $filesystem->mkdir($pathController, 0777); } catch (\IOException $e) { $output->writeln( array( '', '$e', '' ) ); return 1; } if ($filesystem->exists($controllerFile)) { $output->writeln( array( '', 'The file '.$controllerFile.' already exists.', '' ) ); if ($input->isInteractive()) { if ($dialog->askConfirmation($output, $dialog->getQuestion('Do you want to replace it', 'no', '?'), false)) { $filesystem->remove($controllerFile); $output->writeln( array( '', 'File '.$controllerFile.' REMOVED', '' ) ); try { $filesystem->touch($controllerFile, 0777); file_put_contents($controllerFile, $contentController); } catch (\IOException $e) { $output->writeln( array( '', '$e', '' ) ); return 1; } } } } else { try { $filesystem->touch($controllerFile, 0777); file_put_contents($controllerFile, $contentController); } catch (\IOException $e) { $output->writeln( array( '', '$e', '' ) ); return 1; } } $output->writeln(array('', 'Generating the file Controller: OK','')); /* ------------------------------------------ */ $output->writeln(array('','Step 3: Generate views','')); $pathViews = $path.DIRECTORY_SEPARATOR.'Resources'.DIRECTORY_SEPARATOR.'views'.DIRECTORY_SEPARATOR.$model.'Admin'; try { $filesystem->mkdir($pathViews, 0777); } catch (\IOException $e) { $output->writeln( array( '', '$e', '' ) ); return 1; } // test des fichiers de vues if ($filesystem->exists($pathViews.DIRECTORY_SEPARATOR.'index.html.twig')) { $output->writeln( array( '', 'The file '.$pathViews.DIRECTORY_SEPARATOR.'index.html.twig already exists.', '' ) ); if ($input->isInteractive()) { if ($dialog->askConfirmation($output, $dialog->getQuestion('Do you want to replace it', 'no', '?'), false)) { $filesystem->remove($pathViews.DIRECTORY_SEPARATOR.'index.html.twig'); $output->writeln( array( '', 'File '.$pathViews.DIRECTORY_SEPARATOR.'index.html.twig REMOVED', '' ) ); try { $filesystem->touch($pathViews.DIRECTORY_SEPARATOR.'index.html.twig', 0777); file_put_contents($pathViews.DIRECTORY_SEPARATOR.'index.html.twig', '{% extends "TrinityAdminBundle:BaseAdmin:index.html.twig" %}'); } catch (\IOException $e) { $output->writeln( array( '', '$e', '' ) ); return 1; } } } } else { try { $filesystem->touch($pathViews.DIRECTORY_SEPARATOR.'index.html.twig', 0777); file_put_contents($pathViews.DIRECTORY_SEPARATOR.'index.html.twig', '{% extends "TrinityAdminBundle:BaseAdmin:index.html.twig" %}'); } catch (\IOException $e) { $output->writeln( array( '', '$e', '' ) ); return 1; } } if ($filesystem->exists($pathViews.DIRECTORY_SEPARATOR.'edit.html.twig')) { $output->writeln( array( '', 'The file '.$pathViews.DIRECTORY_SEPARATOR.'edit.html.twig already exists.', '' ) ); if ($input->isInteractive()) { if ($dialog->askConfirmation($output, $dialog->getQuestion('Do you want to replace it', 'no', '?'), false)) { $filesystem->remove($pathViews.DIRECTORY_SEPARATOR.'edit.html.twig'); $output->writeln( array( '', 'File '.$pathViews.DIRECTORY_SEPARATOR.'edit.html.twig REMOVED', '' ) ); try { $filesystem->touch($pathViews.DIRECTORY_SEPARATOR.'edit.html.twig', 0777); file_put_contents($pathViews.DIRECTORY_SEPARATOR.'edit.html.twig', '{% extends "TrinityAdminBundle:BaseAdmin:edit.html.twig" %}'); } catch (\IOException $e) { $output->writeln( array( '', '$e', '' ) ); return 1; } } } } else { try { $filesystem->touch($pathViews.DIRECTORY_SEPARATOR.'edit.html.twig', 0777); file_put_contents($pathViews.DIRECTORY_SEPARATOR.'edit.html.twig', '{% extends "TrinityAdminBundle:BaseAdmin:edit.html.twig" %}'); } catch (\IOException $e) { $output->writeln( array( '', '$e', '' ) ); return 1; } } if ($filesystem->exists($pathViews.DIRECTORY_SEPARATOR.'new.html.twig')) { $output->writeln( array( '', 'The file '.$pathViews.DIRECTORY_SEPARATOR.'new.html.twig already exists.', '' ) ); if ($input->isInteractive()) { if ($dialog->askConfirmation($output, $dialog->getQuestion('Do you want to replace it', 'no', '?'), false)) { $filesystem->remove($pathViews.DIRECTORY_SEPARATOR.'new.html.twig'); $output->writeln( array( '', 'File '.$pathViews.DIRECTORY_SEPARATOR.'new.html.twig REMOVED', '' ) ); try { $filesystem->touch($pathViews.DIRECTORY_SEPARATOR.'new.html.twig', 0777); file_put_contents($pathViews.DIRECTORY_SEPARATOR.'new.html.twig', '{% extends "TrinityAdminBundle:BaseAdmin:new.html.twig" %}'); } catch (\IOException $e) { $output->writeln( array( '', '$e', '' ) ); return 1; } } } } else { try { $filesystem->touch($pathViews.DIRECTORY_SEPARATOR.'new.html.twig', 0777); file_put_contents($pathViews.DIRECTORY_SEPARATOR.'new.html.twig', '{% extends "TrinityAdminBundle:BaseAdmin:new.html.twig" %}'); } catch (\IOException $e) { $output->writeln( array( '', '$e', '' ) ); return 1; } } $output->writeln(array('', 'Generating views: OK','')); /* ------------------------------------------ */ $dialog->writeGeneratorSummary($output, $errors); } protected function getDialogHelper() { $dialog = $this->getHelperSet()->get('dialog'); if (!$dialog || get_class($dialog) !== 'Sensio\Bundle\GeneratorBundle\Command\Helper\DialogHelper') { $this->getHelperSet()->set($dialog = new DialogHelper()); } return $dialog; } }