buildDocs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/usr/bin/env php
  2. <?php
  3. /*
  4. * This file is part of php-restcord.
  5. *
  6. * (c) Aaron Scherer <aequasi@gmail.com>
  7. *
  8. * This source file is subject to the license that is bundled
  9. * with this source code in the file LICENSE
  10. */
  11. require __DIR__.'/../vendor/autoload.php';
  12. use Symfony\Component\Console\Application;
  13. use Symfony\Component\Console\Input\InputArgument;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. $loader = new Twig_Loader_Filesystem(__DIR__.'/../src/Resources/');
  17. $twig = new Twig_Environment($loader, ['debug' => true]);
  18. $twig->addExtension(new Twig_Extension_Debug());
  19. function recursiveRemoveDirectory($directory)
  20. {
  21. foreach (glob("{$directory}/*") as $file) {
  22. if (is_dir($file)) {
  23. recursiveRemoveDirectory($file);
  24. } else {
  25. unlink($file);
  26. }
  27. }
  28. rmdir($directory);
  29. }
  30. /** @noinspection PhpUnhandledExceptionInspection */
  31. (new Application('Build Docs', '1.0.0'))
  32. ->register('buildDocs')
  33. ->addArgument('version', InputArgument::REQUIRED, 'Version to build')
  34. ->setCode(
  35. function (InputInterface $input, OutputInterface $output) use ($twig) {
  36. $style = new \Symfony\Component\Console\Style\SymfonyStyle($input, $output);
  37. $style->title("Building Docs for: ".$input->getArgument('version'));
  38. $path = __DIR__.'/../docs/_docs';
  39. recursiveRemoveDirectory($path);
  40. mkdir($path, 02775, true);
  41. $path = realpath($path);
  42. $definition = \GuzzleHttp\json_decode(
  43. file_get_contents(
  44. __DIR__.'/../src/Resources/service_description-v'.$input->getArgument('version').'.json'
  45. ),
  46. true
  47. );
  48. foreach ($definition['operations'] as $category => $operations) {
  49. $i = 1;
  50. foreach ($operations as $operation => $config) {
  51. $filePath = $path.'/'.ucwords($category).'/';
  52. if (!file_exists($filePath)) {
  53. mkdir($filePath, 02775, true);
  54. }
  55. try {
  56. $markdown = $twig->render(
  57. 'operation.md.twig',
  58. [
  59. 'category' => $category,
  60. 'operation' => $operation,
  61. 'config' => $config,
  62. 'order' => $i
  63. ]
  64. );
  65. } catch (\Exception $e) {
  66. throw $e;
  67. }
  68. file_put_contents($filePath.str_replace('/', ' or ', $config['name']).'.md', $markdown);
  69. $i++;
  70. }
  71. }
  72. $style->success('Finished. Docs built in: '.realpath($path));
  73. }
  74. )
  75. ->getApplication()
  76. ->setDefaultCommand('buildDocs', true)
  77. ->run();