474ab6e0ca878b5311aa9becdcbd0c5fd2dcb8b9
[yaffs-website] / vendor / drupal / console / src / Command / Develop / GenerateDocCheatsheetCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Develop\GenerateDocCheatsheetCommand.
6  *
7  * @TODO: use twig
8  */
9
10 namespace Drupal\Console\Command\Develop;
11
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;
17 use Knp\Snappy\Pdf;
18 use Drupal\Console\Core\Command\Shared\CommandTrait;
19
20 class GenerateDocCheatsheetCommand extends Command
21 {
22     use CommandTrait;
23
24     private $singleCommands = [
25       'about',
26       'chain',
27       'help',
28       'list',
29       'server'
30     ];
31
32     //exclude: yaml, translation
33     private $orderCommands = [
34       'cache',
35       'chain',
36       'config',
37       'database',
38       'create',
39       'cron',
40       'image',
41       'container',
42       'locale',
43       'migrate',
44       'module',
45       'multisite',
46       'rest',
47       'settings',
48       'views',
49       'router',
50       'state',
51       'user',
52       'site',
53       'update',
54       'theme'
55
56     ];
57
58     private $logoUrl = 'http://drupalconsole.com/themes/custom/drupalconsole/assets/src/images/drupal-console.png';
59
60     private $wkhtmltopdfPath = "/usr/bin/wkhtmltopdf";
61
62     /**
63      * GenerateDocCheatsheetCommand constructor.
64      */
65     public function __construct()
66     {
67         parent::__construct();
68     }
69
70     /**
71      * {@inheritdoc}
72      */
73     protected function configure()
74     {
75         $this
76             ->setName('generate:doc:cheatsheet')
77             ->setDescription($this->trans('commands.generate.doc.cheatsheet.description'))
78             ->addOption(
79                 'path',
80                 null,
81                 InputOption::VALUE_OPTIONAL,
82                 $this->trans('commands.generate.doc.cheatsheet.options.path')
83             )
84             ->addOption(
85                 'wkhtmltopdf',
86                 null,
87                 InputOption::VALUE_OPTIONAL,
88                 $this->trans('commands.generate.doc.cheatsheet.options.wkhtmltopdf')
89             );
90         ;
91     }
92
93     /**
94      * {@inheritdoc}
95      */
96     protected function execute(InputInterface $input, OutputInterface $output)
97     {
98         $io = new DrupalStyle($input, $output);
99
100         $path = null;
101
102         if ($input->hasOption('path')) {
103             $path = $input->getOption('path');
104         }
105
106         if (!$path) {
107             $io->error(
108                 $this->trans('commands.generate.doc.gitbook.messages.missing_path')
109             );
110
111             return 1;
112         }
113
114         // $wkhtmltopdfPath is overwritable by command option
115
116         if ($input->getOption('wkhtmltopdf')) {
117             $this->wkhtmltopdfPath = $input->getOption('wkhtmltopdf');
118         }
119
120         $application = $this->getApplication();
121         $command_list = [];
122
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(),
128             ];
129         }
130
131         $namespaces = $application->getNamespaces();
132         sort($namespaces);
133
134         $namespaces = array_filter(
135             $namespaces, function ($item) {
136                 return (strpos($item, ':')<=0);
137             }
138         );
139
140         foreach ($namespaces as $namespace) {
141             $commands = $application->all($namespace);
142
143             usort(
144                 $commands, function ($cmd1, $cmd2) {
145                     return strcmp($cmd1->getName(), $cmd2->getName());
146                 }
147             );
148
149             foreach ($commands as $command) {
150                 if ($command->getModule()=='Console') {
151                     $command_list[$namespace][] = [
152                         'name' => $command->getName(),
153                         'description' => $command->getDescription(),
154                     ];
155                 }
156             }
157         }
158
159         if (!empty($command_list)) {
160             $this->prepareHtml($command_list, $path, $io);
161         }
162     }
163
164
165     /**
166      * Generates (programatically, not with twig) the HTML to convert to PDF
167      *
168      * @param array  $array_content
169      * @param string $path
170      */
171     protected function prepareHtml($array_content, $path, $io)
172     {
173         $str  = '<meta charset="UTF-8" />';
174         $str .= "<center><div style='font-size: 12px;'>Drupal Console cheatsheet</div></center>";
175
176         // 1st page
177         foreach ($this->orderCommands as $command) {
178             $str .= $this->doTable($command, $array_content[$command]);
179         }
180
181         // 2nd page
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>";
184
185         $str .= "<td style='vertical-align: bottom;'><h1>DrupalConsole Cheatsheet</h1></td></tr></table><br/><br/>";
186
187         $str .= $this->doTable("generate", $array_content["generate"]);
188         $str .= $this->doTable("miscelaneous", $array_content["none"]);
189
190         $this->doPdf($str, $path, $io);
191     }
192
193
194     /**
195      * Generates the pdf with Snappy
196      *
197      * @param string $content
198      * @param string $path
199      *
200      * @return string
201      */
202     protected function doPdf($content, $path, $io)
203     {
204         $snappy = new Pdf();
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");
210
211         // command execution ends here
212     }
213
214     /**
215    * Encloses text in <td> tags
216    *
217    * @param string $str
218    *
219    * @return string
220    */
221     public function td($str, $mode = null)
222     {
223         if ("header" == $mode) {
224             return "<td colspan='2' style='background-color:whitesmoke;font-size: 12px;'><b>" . strtoupper($str) . "</b></td>";
225         } else {
226             if ("body" == $mode) {
227                 return "<td style='font-size: 11px;width=35%'><i>". $str. "</i></td>";
228             } else {
229                 return "<td>" . $str . "</td>";
230             }
231         }
232     }
233
234     /**
235    * Encloses text in <tr> tags
236    *
237    * @param string $str
238    * @param array  $element
239    *
240    * @return string
241    */
242     public function tr($str)
243     {
244         return "<tr>" . $str . "</tr>";
245     }
246
247     /**
248    * Encloses text in <table> tag
249    *
250    * @param string $key_element - header
251    * @param array  $element     - command, description
252    *
253    * @return string
254    */
255     public function doTable($key_element, $element)
256     {
257         $str = "<table cellspacing='0' border='0' style='float:left;width:49%;'>";
258         $str .= $this->td($key_element, "header");
259
260         foreach ($element as $section) {
261             $str .= $this->tr($this->td($section["name"], "body") . $this->td($section["description"], "body"));
262         }
263
264         return $str . "</table>\n\r";
265     }
266 }