5 * Contains \Drupal\Console\Command\Develop\GenerateDocDashCommand.
8 namespace Drupal\Console\Command\Develop;
10 use Symfony\Component\Console\Input\InputInterface;
11 use Symfony\Component\Console\Output\OutputInterface;
12 use Symfony\Component\Console\Input\InputOption;
13 use Symfony\Component\Filesystem\Exception\IOException;
14 use Symfony\Component\Filesystem\Filesystem;
15 use Symfony\Component\Console\Command\Command;
16 use Drupal\Console\Core\Style\DrupalStyle;
17 use Drupal\Console\Core\Command\Shared\CommandTrait;
18 use Drupal\Console\Core\Utils\TwigRenderer;
19 use Drupal\Console\Core\Utils\ConfigurationManager;
21 class GenerateDocDashCommand extends Command
26 * @var TwigRenderer $renderer
29 protected $consoleRoot;
31 * @constant Contents of the plist file required by the docset format.
33 const PLIST = <<<PLIST
34 <?xml version="1.0" encoding="UTF-8"?>
35 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
38 <key>CFBundleIdentifier</key>
39 <string>drupalconsole</string>
40 <key>CFBundleName</key>
41 <string>Drupal Console</string>
42 <key>DocSetPlatformFamily</key>
43 <string>drupalconsole</string>
44 <key>isDashDocset</key>
46 <key>dashIndexFilePath</key>
47 <string>index.html</string>
52 private $single_commands = [
63 * @var SQLite3 Controller for the sqlite db required by the docset format.
68 * @var ConfigurationManager $configurationManager
70 protected $configurationManager;
73 * GenerateDocDashCommand constructor.
78 public function __construct(TwigRenderer $renderer, $consoleRoot)
80 $this->renderer = $renderer;
81 $this->consoleRoot = $consoleRoot;
82 parent::__construct();
90 protected function configure()
93 ->setName('generate:doc:dash')
94 ->setDescription($this->trans('commands.generate.doc.dash.description'))
98 InputOption::VALUE_OPTIONAL,
99 $this->trans('commands.generate.doc.dash.options.path')
107 protected function execute(InputInterface $input, OutputInterface $output)
109 $io = new DrupalStyle($input, $output);
112 if ($input->hasOption('path')) {
113 $path = $input->getOption('path');
118 $this->trans('commands.generate.doc.dash.messages.missing_path')
124 // Setup the docset structure
125 $this->initDocset($path);
127 $application = $this->getApplication();
130 foreach ($this->single_commands as $single_command) {
131 $command = $application->find($single_command);
132 $command_list['none'][] = [
133 'name' => $command->getName(),
134 'description' => $command->getDescription(),
136 $this->renderCommand($command, $path, $this->renderer);
137 $this->registerCommand($command, $path);
140 $namespaces = $application->getNamespaces();
143 $namespaces = array_filter(
144 $namespaces, function ($item) {
145 return (strpos($item, ':') <= 0);
149 foreach ($namespaces as $namespace) {
150 $commands = $application->all($namespace);
153 $commands, function ($cmd1, $cmd2) {
154 return strcmp($cmd1->getName(), $cmd2->getName());
158 foreach ($commands as $command) {
159 if ($command->getModule() == 'Console') {
160 $command_list[$namespace][] = [
161 'name' => $command->getName(),
162 'description' => $command->getDescription(),
164 $this->renderCommand($command, $path, $this->renderer);
165 $this->registerCommand($command, $path);
170 $input = $application->getDefinition();
171 $options = $input->getOptions();
172 $arguments = $input->getArguments();
174 'command_list' => $command_list,
175 'options' => $options,
176 'arguments' => $arguments,
177 'css_path' => 'style.css'
180 // Set the index page
182 'dash/index.html.twig',
183 $path . '/DrupalConsole.docset/Contents/Resources/Documents/index.html',
190 private function renderCommand($command, $path, $renderer)
192 $input = $command->getDefinition();
193 $options = $input->getOptions();
194 $arguments = $input->getArguments();
197 'options' => $options,
198 'arguments' => $arguments,
199 'command' => $command->getName(),
200 'description' => $command->getDescription(),
201 'aliases' => $command->getAliases(),
202 'css_path' => '../style.css'
206 'dash/generate-doc.html.twig',
207 $path . '/DrupalConsole.docset/Contents/Resources/Documents/commands/'
208 . str_replace(':', '-', $command->getName()) . '.html',
215 private function registerCommand($command)
218 $statement = $this->sqlite->prepare('INSERT OR IGNORE INTO searchIndex(name, type, path) VALUES (:name, :type, :path)');
219 $statement->bindValue(':name', $command->getName(), SQLITE3_TEXT);
220 $statement->bindValue(':type', 'Command', SQLITE3_TEXT);
221 $statement->bindValue(
224 . str_replace(':', '-', $command->getName()) . '.html',
227 $statement->execute();
228 } catch (\Exception $e) {
233 private function renderFile(
239 $filesystem = new Filesystem();
241 $filesystem->dumpFile(
243 $renderer->render($template, $parameters)
245 } catch (IOException $e) {
250 private function initDocset($path)
253 $filesystem = new Filesystem();
255 $path . '/DrupalConsole.docset/Contents/Resources/Documents/',
258 $filesystem->dumpFile(
259 $path . '/DrupalConsole.docset/Contents/Info.plist',
264 $this->consoleRoot . '/resources/drupal-console.png',
265 $path . '/DrupalConsole.docset/icon.png'
268 $this->consoleRoot . '/resources/dash.css',
269 $path . '/DrupalConsole.docset/Contents/Resources/Documents/style.css'
271 // create the required sqlite db
272 $this->sqlite = new \SQLite3($path . '/DrupalConsole.docset/Contents/Resources/docSet.dsidx');
273 $this->sqlite->query("CREATE TABLE searchIndex(id INTEGER PRIMARY KEY, name TEXT, type TEXT, path TEXT)");
274 $this->sqlite->query("CREATE UNIQUE INDEX anchor ON searchIndex (name, type, path)");
275 } catch (\Exception $e) {