/** * Asks for the prefix and sets it on the InputInterface as the 'prefix' option, if this option is not set yet. * Will set the default to a snake_cased namespace when the namespace has been set on the InputInterface. * * @param array $text What you want printed before the prefix is asked. If null is provided it'll write a default text. * @param string $namespace An optional namespace. If this is set it'll create the default based on this prefix. * If it's not provided it'll check if the InputInterface already has the namespace option. * * @return string The prefix. But it's also been set on the InputInterface. */ public function askForPrefix(array $text = null, $namespace = null) { $prefix = $this->input->hasOption('prefix') ? $this->input->getOption('prefix') : null; if (is_null($text)) { $text = array('', 'You can add a prefix to the table names of the generated entities for example: <comment>projectname_bundlename_</comment>', 'Enter an underscore \'_\' if you don\'t want a prefix.', ''); } if (is_null($prefix)) { if (count($text) > 0) { $this->output->writeln($text); } if (is_null($namespace) || empty($namespace)) { $namespace = $this->input->hasOption('namespace') ? $this->input->getOption('namespace') : null; } else { $namespace = $this->fixNamespace($namespace); } $defaultPrefix = GeneratorUtils::cleanPrefix($this->convertNamespaceToSnakeCase($namespace)); $prefix = $this->dialog->ask($this->output, $this->dialog->getQuestion('Tablename prefix', $defaultPrefix), $defaultPrefix); $prefix = GeneratorUtils::cleanPrefix($prefix); if ($this->input->hasOption('prefix')) { $this->input->setOption('prefix', $prefix); } } return $prefix; }
public function addActions(InputInterface $input, OutputInterface $output, DialogHelper $dialog) { $output->writeln(array('', 'Instead of starting with a blank controller, you can add some actions now. An action', 'is a PHP function or method that executes, for example, when a given route is matched.', 'Actions should be suffixed by <comment>Action</comment>.', '')); $templateNameValidator = function ($name) { if ('default' == $name) { return $name; } if (2 != substr_count($name, ':')) { throw new \InvalidArgumentException(sprintf('Template name "%s" does not have 2 colons', $name)); } return $name; }; $actions = $this->parseActions($input->getOption('actions')); while (true) { // name $output->writeln(''); $actionName = $dialog->askAndValidate($output, $dialog->getQuestion('New action name (press <return> to stop adding actions)', null), function ($name) use($actions) { if (null == $name) { return $name; } if (isset($actions[$name])) { throw new \InvalidArgumentException(sprintf('Action "%s" is already defined', $name)); } if ('Action' != substr($name, -6)) { throw new \InvalidArgumentException(sprintf('Name "%s" is not suffixed by Action', $name)); } return $name; }); if (!$actionName) { break; } // route $route = $dialog->ask($output, $dialog->getQuestion('Action route', '/' . substr($actionName, 0, -6)), '/' . substr($actionName, 0, -6)); $placeholders = $this->getPlaceholdersFromRoute($route); // template $defaultTemplate = $input->getOption('controller') . ':' . substr($actionName, 0, -6) . '.html.' . $input->getOption('template-format'); $template = $dialog->askAndValidate($output, $dialog->getQuestion('Templatename (optional)', $defaultTemplate), $templateNameValidator, false, 'default'); // adding action $actions[$actionName] = array('name' => $actionName, 'route' => $route, 'placeholders' => $placeholders, 'template' => $template); } return $actions; }
protected function generateAdmin(DialogHelper $dialog, InputInterface $input, OutputInterface $output, BundleInterface $bundle, $metadata, $entity, $filename) { $auto = true; if ($input->isInteractive()) { $auto = $dialog->askConfirmation($output, $dialog->getQuestion('Confirm automatic generation of the Admin service', 'yes', '?'), true); } if ($auto) { $group = ucfirst(str_replace('_', ' ', Container::underscore(substr($bundle->getName(), 0, -6)))); $label = $entity; $translationDomain = 'Sonata'; $group = $dialog->ask($output, $dialog->getQuestion('Group for the admin service', $group), $group); $label = $dialog->ask($output, $dialog->getQuestion('Label for the admin service', $label), $label); $translationDomain = $dialog->ask($output, $dialog->getQuestion('Translation domain for the admin service', $translationDomain), $translationDomain); $output->write('Creating the service: '); $this->getContainer()->get('filesystem')->mkdir($bundle->getPath() . '/Resources/config/'); $admin = new AdminManipulator($bundle->getPath() . '/Resources/config/' . $filename); try { $ret = $auto ? $admin->addResource($bundle, $entity, $group, $label, $translationDomain) : false; } catch (\RuntimeException $exc) { $ret = false; } if ($ret) { $output->write('Creating the Admin class: '); $generator = $this->getGenerator($bundle); $generator->generateAdminClass($metadata[0], $input->getOption('overwrite')); } } }
/** * Update the main routing file by including our two custom routing files. * * @param DialogHelper $dialog * @param InputInterface $input * @param OutputInterface $output * @param $bundle * @param $format * @param $app * * @return array */ protected function _updateRouting(DialogHelper $dialog, InputInterface $input, OutputInterface $output, $bundle, $format, $app) { $auto = true; if ($input->isInteractive()) { $auto = $dialog->askConfirmation($output, $dialog->getQuestion('Confirm automatic update of the Routing for the ' . $app . ' app', 'yes', '?'), true); } $prefix = null; if ($input->isInteractive()) { $prefix = $dialog->ask($output, $dialog->getQuestion('Prefix for the ' . $app . ' app ' . ($app == 'backend' ? '(ex: /admin/news)' : '(leave blank for none)'), null), null); } $output->write('Importing the bundle routing resource: '); $routing = new RoutingManipulator($this->getContainer()->getParameter('kernel.root_dir') . '/config/routing.yml'); try { $ret = $auto ? $routing->addResource($bundle, $format, $prefix, 'routing_' . $app, $app) : false; if (!$ret) { if ('annotation' === $format) { $help = sprintf(" <comment>resource: \"@%s/Controller/\"</comment>\n <comment>type: annotation</comment>\n", $bundle); } else { $help = sprintf(" <comment>resource: \"@%s/Resources/config/routing_%s.%s\"</comment>\n", $app, $bundle, $format); } if ($prefix) { $help .= sprintf(" <comment>prefix: %s</comment>\n", $prefix); } return array('- Import the bundle\'s routing resource in the app main routing file:', '', sprintf(' <comment>%s:</comment>', $bundle), $help, ''); } } catch (\RuntimeException $e) { return array(sprintf('Bundle <comment>%s</comment> is already imported.', $bundle), ''); } }