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:crud27') ; } /** * @see Command * */ protected function execute(InputInterface $input, OutputInterface $output) { $dialog = $this->getHelperSet()->get('dialog'); $filesystem = $this->getContainer()->get('filesystem'); $kernel = $this->getContainer()->get('kernel'); $output->writeln(array('Welcome to the CRUD generation!', '')); $output->writeln( array( '', '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, sprintf('Bundle namespace [%s]: ', $input->getOption('namespace')), array('Sensio\Bundle\GeneratorBundle\Command\Validators', 'validateBundleNamespace'), false, $input->getOption('namespace') ); $output->writeln( array( '', '', 'You can find the bundle name in the file app/config/routing.yml', 'Example: AcmeMyBundle', '', ) ); $bundle = $dialog->askAndValidate( $output, sprintf('Bundle name [%s]: ', $input->getOption('bundle')), array('Sensio\Bundle\GeneratorBundle\Command\Validators', 'validateBundleName'), false, $input->getOption('bundle') ); $namespace = trim($namespace, '\\'); $input->setOption('bundle', $bundle); $input->setOption('namespace', $namespace); $kernel->getBundle($bundle); $namespaceExplode = explode('\\', $namespace); /* ------------------------------------------ */ // target dir $path = dirname($this->getContainer()->getParameter('kernel.root_dir')).'/src'.DIRECTORY_SEPARATOR; $path .= implode(DIRECTORY_SEPARATOR, $namespaceExplode); if (!$filesystem->exists($path)) { $output->writeln(array( '', sprintf('Command aborted. The directory %s does not exist.', $path), '', )); return 1; } $output->writeln(array('', 'Step 1: Generate CrudConfiguration', '')); $model = str_replace($namespaceExplode[0], '', $bundle); $model = str_replace('Bundle', '', $model); $model = $dialog->askAndValidate( $output, sprintf('Model [%s]: ', $model), function ($anwser) use ($namespace) { $className = $namespace.'\\Model\\'.$anwser; if (!class_exists($className)) { throw new \RunTimeException(sprintf('Invalid model %s', $className)); } return $anwser; }, false, $model ); $classNameConfiguration = $model.'CrudConfiguration'; $nameSpaceConfiguration = $namespace.'\Configuration'; $modelPeer = $model.'Peer'; $modelQuery = $model.'Query'; $routePrefix = $bundle.$model.'Admin'; $formNamespace = ''; $modelNamespace = ''; $class = ''; foreach ($namespaceExplode 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, sprintf('Max per page [%d]: ', 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, sprintf('Rankable? [No]: '), false ); $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.")"; if ($rankable === 'true') { $contentConfiguration .= " ->setSort('orderByRank', \\Criteria::ASC) "; } $contentConfiguration .= " ->setListTitle('".$model."') ->setNewTitle('New ".strtolower($model)."') ->setEditTitle('Edition of \"%id%\"') // Listing: // ->setFieldTemplate('visible', 'TrinityAdminBundle:BaseAdmin:bool.html.twig') // ->setDisplayFields(array( // // )) // ->setFieldname('foo', 'bar') ; } } "; $output->writeln($contentConfiguration); if (!$dialog->askConfirmation($output, 'Do you agree? [Yes]: ', true)) { $output->writeln(['', '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(['', '$e']); return 1; } if ($filesystem->exists($configurationFile)) { $output->writeln(['', 'The file '.$configurationFile.' already exists.']); if ($dialog->askConfirmation($output, 'Do you want to override? [No]: ', false)) { try { $filesystem->remove($configurationFile); file_put_contents($configurationFile, $contentConfiguration); } catch (\IOException $e) { $output->writeln(['', '$e']); return 1; } } } else { try { file_put_contents($configurationFile, $contentConfiguration); } catch (\IOException $e) { $output->writeln(['', '$e']); return 1; } } $output->writeln(array('', 'Step 2: Generate Controller', '')); $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 (!$dialog->askConfirmation($output, 'Do you agree? [Yes]: ', true)) { $output->writeln(['', 'Command aborted']); return 1; } $pathController = $path.DIRECTORY_SEPARATOR.'Controller'; $controllerFile = $pathController.DIRECTORY_SEPARATOR.$classNameController.'.php'; try { $filesystem->mkdir($pathController, 0777); } catch (\IOException $e) { $output->writeln(['', '$e']); return 1; } if ($filesystem->exists($controllerFile)) { $output->writeln(['', 'The file '.$controllerFile.' already exists.']); if ($dialog->askConfirmation($output, 'Do you want to override? [No]: ', false)) { try { $filesystem->remove($controllerFile); file_put_contents($controllerFile, $contentController); } catch (\IOException $e) { $output->writeln(['', '$e']); return 1; } } } else { try { $filesystem->touch($controllerFile, 0777); file_put_contents($controllerFile, $contentController); } catch (\IOException $e) { $output->writeln(['', '$e']); return 1; } } $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(['', '$e']); return 1; } foreach (['index', 'edit', 'new'] as $action) { $file = $pathViews.DIRECTORY_SEPARATOR.$action.'.html.twig'; if ($filesystem->exists($file)) { $output->writeln(['', 'The file '.$file.' already exists.']); if ($dialog->askConfirmation($output, 'Do you want to override? [No]: ', false)) { try { $filesystem->touch($file, 0777); file_put_contents($file, '{% extends "TrinityAdminBundle:BaseAdmin:'.$action.'.html.twig" %}'); } catch (\IOException $e) { $output->writeln(['', '$e']); return 1; } } } else { try { $filesystem->touch($file, 0777); file_put_contents($file, '{% extends "TrinityAdminBundle:BaseAdmin:'.$action.'.html.twig" %}'); } catch (\IOException $e) { $output->writeln(['', '$e']); return 1; } } } $output->writeln(['', 'Finished!']); } }