public function notify(\Exception $exception) { if (!$exception instanceof ServiceNotFoundException) { return; } $serviceId = $exception->getId(); $guessedFqcn = $this->guessFqcn($serviceId); $definition = new Definition(); $definition->setClass($guessedFqcn); $containerBuilder = new ContainerBuilder(); $containerBuilder->addDefinitions([$serviceId => $definition]); $dumper = new YamlDumper($containerBuilder); $result = $dumper->dump(); $message = sprintf('Service `%s` missing. Define it in your services.yml:', $serviceId); $this->output->writeln('--- ' . $message . PHP_EOL); $this->output->write($result, true); $errorMessages = ['Service ' . $serviceId . ' was not found.']; $formatter = new FormatterHelper(); $formattedBlock = $formatter->formatBlock($errorMessages, 'error', true); $this->output->writeln(''); $this->output->writeln($formattedBlock); $this->output->writeln(''); $question = sprintf('<question>Do you want to create a specification for %s? (Y/n)</question>', $guessedFqcn); $dialog = new DialogHelper(); if ($dialog->askConfirmation($this->output, $question, true)) { $this->specRunner->runDescCommand($guessedFqcn); } }
protected function updateRouting(DialogHelper $dialog, InputInterface $input, OutputInterface $output, $bundle, $format) { $auto = true; if ($input->isInteractive()) { $auto = $dialog->askConfirmation($output, $dialog->getQuestion('Confirm automatic update of the Routing', 'yes', '?'), true); } $output->write('Importing the bundle routing resource: '); $routing = new RoutingManipulator($this->getContainer()->getParameter('kernel.root_dir') . '/config/routing.yml'); $routing->setYamlPrefix($input->getOption('prefix')); try { $ret = $auto ? $routing->addResource($bundle, 'admingenerator') : false; if (!$ret) { $help = sprintf(" <comment>resource: \"@%s/Controller/%s/\"</comment>\n <comment>type: admingenerator</comment>\n", $bundle, ucfirst($input->getOption('prefix'))); $help .= " <comment>prefix: /</comment>\n"; 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), ''); } }
/** * @param DialogHelper $dialog The dialog helper * @param InputInterface $input The command input * @param OutputInterface $output The command output * @param Bundle $bundle The bundle * @param string $entityClass The classname of the entity * * @return void */ protected function updateRouting(DialogHelper $dialog, InputInterface $input, OutputInterface $output, Bundle $bundle, $entityClass) { $auto = true; $multilang = false; if ($input->isInteractive()) { $multilang = $dialog->askConfirmation($output, $dialog->getQuestion('Is it a multilanguage site', 'yes', '?'), true); $auto = $dialog->askConfirmation($output, $dialog->getQuestion('Do you want to update the routing automatically', 'yes', '?'), true); } $prefix = $multilang ? '/{_locale}' : ''; $code = sprintf("%s:\n", strtolower($bundle->getName()) . '_' . strtolower($entityClass) . '_admin_list'); $code .= sprintf(" resource: @%s/Controller/%sAdminListController.php\n", $bundle->getName(), $entityClass); $code .= " type: annotation\n"; $code .= sprintf(" prefix: %s/admin/%s/\n", $prefix, strtolower($entityClass)); if ($multilang) { $code .= " requirements:\n"; $code .= " _locale: %requiredlocales%\n"; } if ($auto) { $file = $bundle->getPath() . '/Resources/config/routing.yml'; $content = ''; if (file_exists($file)) { $content = file_get_contents($file); } elseif (!is_dir($dir = dirname($file))) { mkdir($dir, 0777, true); } $content .= "\n"; $content .= $code; if (false === file_put_contents($file, $content)) { $output->writeln($dialog->getHelperSet()->get('formatter')->formatBlock("Failed adding the content automatically", 'error')); } else { return; } } $output->writeln('Add the following to your routing.yml'); $output->writeln('/*******************************/'); $output->write($code); $output->writeln('/*******************************/'); }
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')); } } }
$lines = []; $tableHelper = new TableHelper(); $tableHelper->setHeaders(['identifier', 'groups']); foreach ($products as $product) { $line = []; $line['identifier'] = (string) $product->getIdentifier(); $line['groups'] = []; foreach ($product->getGroups() as $group) { $line['groups'][] = $group->getCode(); } $lines[] = $line; $tableHelper->addRow([$line['identifier'], implode(', ', $line['groups'])]); } $tableHelper->render($output); $dialogHelper = new DialogHelper(); if ($dialogHelper->askConfirmation($output, 'Would you like to generate a csv file to fix all those products ? (Y,n) ')) { $tmpFolder = sys_get_temp_dir(); $filePath = $tmpFolder . '/invalid_product.csv'; if (!is_writable($tmpFolder)) { throw new \Exception(sprintf('The filepath %s is not writable', $filePath)); } $csv = sprintf("%s;groups\n", $identifierCode); foreach ($lines as $line) { $csv .= sprintf("%s;%s\n", $line['identifier'], implode(',', $line['groups'])); } file_put_contents($filePath, $csv); $output->writeln(sprintf('Generated CSV product import : %s', $filePath)); } } else { $output->writeln('Everything seems fine, no product in multiple variant groups'); }
protected function updateRouting(DialogHelper $dialog, InputInterface $input, OutputInterface $output, BundleInterface $bundle, $format, $entity, $prefix) { $auto = true; if ($input->isInteractive()) { $auto = $dialog->askConfirmation($output, $dialog->getQuestion('Confirm automatic update of the Routing', 'yes', '?'), true); } $output->write('Importing the CRUD routes: '); $this->getContainer()->get('filesystem')->mkdir($bundle->getPath() . '/Resources/config/'); $routing = new RoutingManipulator($bundle->getPath() . '/Resources/config/routing.yml'); try { $ret = $auto ? $routing->addResource($bundle->getName(), $format, '/' . $prefix, 'routing/' . strtolower(str_replace('\\', '_', $entity))) : false; } catch (\RuntimeException $exc) { $ret = false; } if (!$ret) { $help = sprintf(" <comment>resource: \"@%s/Resources/config/routing/%s.%s\"</comment>\n", $bundle->getName(), strtolower(str_replace('\\', '_', $entity)), $format); $help .= sprintf(" <comment>prefix: /%s</comment>\n", $prefix); return array('- Import the bundle\'s routing resource in the bundle routing file', sprintf(' (%s).', $bundle->getPath() . '/Resources/config/routing.yml'), '', sprintf(' <comment>%s:</comment>', $bundle->getName() . ('' !== $prefix ? '_' . str_replace('/', '_', $prefix) : '')), $help, ''); } }
/** * 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), ''); } }