5 * Contains \Drupal\Console\Command\Develop\GenerateDocCheatsheetCommand.
10 namespace Drupal\Console\Command\Develop;
12 use Symfony\Component\Console\Input\InputInterface;
13 use Symfony\Component\Console\Output\OutputInterface;
14 use Symfony\Component\Console\Input\InputOption;
15 use Symfony\Component\Console\Command\Command;
16 use Drupal\Console\Core\Style\DrupalStyle;
18 use Drupal\Console\Core\Command\Shared\CommandTrait;
20 class GenerateDocCheatsheetCommand extends Command
24 private $singleCommands = [
32 //exclude: yaml, translation
33 private $orderCommands = [
58 private $logoUrl = 'http://drupalconsole.com/themes/custom/drupalconsole/assets/src/images/drupal-console.png';
60 private $wkhtmltopdfPath = "/usr/bin/wkhtmltopdf";
63 * GenerateDocCheatsheetCommand constructor.
65 public function __construct()
67 parent::__construct();
73 protected function configure()
76 ->setName('generate:doc:cheatsheet')
77 ->setDescription($this->trans('commands.generate.doc.cheatsheet.description'))
81 InputOption::VALUE_OPTIONAL,
82 $this->trans('commands.generate.doc.cheatsheet.options.path')
87 InputOption::VALUE_OPTIONAL,
88 $this->trans('commands.generate.doc.cheatsheet.options.wkhtmltopdf')
96 protected function execute(InputInterface $input, OutputInterface $output)
98 $io = new DrupalStyle($input, $output);
102 if ($input->hasOption('path')) {
103 $path = $input->getOption('path');
108 $this->trans('commands.generate.doc.gitbook.messages.missing_path')
114 // $wkhtmltopdfPath is overwritable by command option
116 if ($input->getOption('wkhtmltopdf')) {
117 $this->wkhtmltopdfPath = $input->getOption('wkhtmltopdf');
120 $application = $this->getApplication();
123 foreach ($this->singleCommands as $single_command) {
124 $command = $application->find($single_command);
125 $command_list['none'][] = [
126 'name' => $command->getName(),
127 'description' => $command->getDescription(),
131 $namespaces = $application->getNamespaces();
134 $namespaces = array_filter(
135 $namespaces, function ($item) {
136 return (strpos($item, ':')<=0);
140 foreach ($namespaces as $namespace) {
141 $commands = $application->all($namespace);
144 $commands, function ($cmd1, $cmd2) {
145 return strcmp($cmd1->getName(), $cmd2->getName());
149 foreach ($commands as $command) {
150 if ($command->getModule()=='Console') {
151 $command_list[$namespace][] = [
152 'name' => $command->getName(),
153 'description' => $command->getDescription(),
159 if (!empty($command_list)) {
160 $this->prepareHtml($command_list, $path, $io);
166 * Generates (programatically, not with twig) the HTML to convert to PDF
168 * @param array $array_content
169 * @param string $path
171 protected function prepareHtml($array_content, $path, $io)
173 $str = '<meta charset="UTF-8" />';
174 $str .= "<center><div style='font-size: 12px;'>Drupal Console cheatsheet</div></center>";
177 foreach ($this->orderCommands as $command) {
178 $str .= $this->doTable($command, $array_content[$command]);
182 $str .= "<br/><br/><table style='width:99%;page-break-before:always;padding-top:10%'><tr><td><img src='".
183 $this->logoUrl ."' width='150px' style='float:left'/></td>";
185 $str .= "<td style='vertical-align: bottom;'><h1>DrupalConsole Cheatsheet</h1></td></tr></table><br/><br/>";
187 $str .= $this->doTable("generate", $array_content["generate"]);
188 $str .= $this->doTable("miscelaneous", $array_content["none"]);
190 $this->doPdf($str, $path, $io);
195 * Generates the pdf with Snappy
197 * @param string $content
198 * @param string $path
202 protected function doPdf($content, $path, $io)
205 //@TODO: catch exception if binary path doesn't exist!
206 $snappy->setBinary($this->wkhtmltopdfPath);
207 $snappy->setOption('orientation', "Landscape");
208 $snappy->generateFromHtml($content, "/" .$path . 'dc-cheatsheet.pdf');
209 $io->success("cheatsheet generated at /" .$path ."/dc-cheatsheet.pdf");
211 // command execution ends here
215 * Encloses text in <td> tags
221 public function td($str, $mode = null)
223 if ("header" == $mode) {
224 return "<td colspan='2' style='background-color:whitesmoke;font-size: 12px;'><b>" . strtoupper($str) . "</b></td>";
226 if ("body" == $mode) {
227 return "<td style='font-size: 11px;width=35%'><i>". $str. "</i></td>";
229 return "<td>" . $str . "</td>";
235 * Encloses text in <tr> tags
238 * @param array $element
242 public function tr($str)
244 return "<tr>" . $str . "</tr>";
248 * Encloses text in <table> tag
250 * @param string $key_element - header
251 * @param array $element - command, description
255 public function doTable($key_element, $element)
257 $str = "<table cellspacing='0' border='0' style='float:left;width:49%;'>";
258 $str .= $this->td($key_element, "header");
260 foreach ($element as $section) {
261 $str .= $this->tr($this->td($section["name"], "body") . $this->td($section["description"], "body"));
264 return $str . "</table>\n\r";