setName('generate:doc:cheatsheet') ->setDescription($this->trans('commands.generate.doc.cheatsheet.description')) ->addOption( 'path', null, InputOption::VALUE_OPTIONAL, $this->trans('commands.generate.doc.cheatsheet.options.path') ) ->addOption( 'wkhtmltopdf', null, InputOption::VALUE_OPTIONAL, $this->trans('commands.generate.doc.cheatsheet.options.wkhtmltopdf') ); ; } /** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $io = new DrupalStyle($input, $output); $path = null; if ($input->hasOption('path')) { $path = $input->getOption('path'); } if (!$path) { $io->error( $this->trans('commands.generate.doc.gitbook.messages.missing_path') ); return 1; } // $wkhtmltopdfPath is overwritable by command option if ($input->getOption('wkhtmltopdf')) { $this->wkhtmltopdfPath = $input->getOption('wkhtmltopdf'); } $application = $this->getApplication(); $command_list = []; foreach ($this->singleCommands as $single_command) { $command = $application->find($single_command); $command_list['none'][] = [ 'name' => $command->getName(), 'description' => $command->getDescription(), ]; } $namespaces = $application->getNamespaces(); sort($namespaces); $namespaces = array_filter( $namespaces, function ($item) { return (strpos($item, ':')<=0); } ); foreach ($namespaces as $namespace) { $commands = $application->all($namespace); usort( $commands, function ($cmd1, $cmd2) { return strcmp($cmd1->getName(), $cmd2->getName()); } ); foreach ($commands as $command) { if ($command->getModule()=='Console') { $command_list[$namespace][] = [ 'name' => $command->getName(), 'description' => $command->getDescription(), ]; } } } if (!empty($command_list)) { $this->prepareHtml($command_list, $path, $io); } } /** * Generates (programatically, not with twig) the HTML to convert to PDF * * @param array $array_content * @param string $path */ protected function prepareHtml($array_content, $path, $io) { $str = ''; $str .= "
Drupal Console cheatsheet
"; // 1st page foreach ($this->orderCommands as $command) { $str .= $this->doTable($command, $array_content[$command]); } // 2nd page $str .= "

"; $str .= "

DrupalConsole Cheatsheet



"; $str .= $this->doTable("generate", $array_content["generate"]); $str .= $this->doTable("miscelaneous", $array_content["none"]); $this->doPdf($str, $path, $io); } /** * Generates the pdf with Snappy * * @param string $content * @param string $path * * @return string */ protected function doPdf($content, $path, $io) { $snappy = new Pdf(); //@TODO: catch exception if binary path doesn't exist! $snappy->setBinary($this->wkhtmltopdfPath); $snappy->setOption('orientation', "Landscape"); $snappy->generateFromHtml($content, "/" .$path . 'dc-cheatsheet.pdf'); $io->success("cheatsheet generated at /" .$path ."/dc-cheatsheet.pdf"); // command execution ends here } /** * Encloses text in tags * * @param string $str * * @return string */ public function td($str, $mode = null) { if ("header" == $mode) { return "" . strtoupper($str) . ""; } else { if ("body" == $mode) { return "". $str. ""; } else { return "" . $str . ""; } } } /** * Encloses text in tags * * @param string $str * @param array $element * * @return string */ public function tr($str) { return "" . $str . ""; } /** * Encloses text in tag * * @param string $key_element - header * @param array $element - command, description * * @return string */ public function doTable($key_element, $element) { $str = "
"; $str .= $this->td($key_element, "header"); foreach ($element as $section) { $str .= $this->tr($this->td($section["name"], "body") . $this->td($section["description"], "body")); } return $str . "
\n\r"; } }