PageRenderTime 45ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/bin/docbook_skeleton.php

https://bitbucket.org/aboozar/zf2
PHP | 135 lines | 81 code | 9 blank | 45 comment | 8 complexity | 866f8b29cf3ebc5d16f94312db7f3ef0 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. #!/usr/bin/env php
  2. <?php
  3. /**
  4. * Zend Framework
  5. *
  6. * LICENSE
  7. *
  8. * This source file is subject to the new BSD license that is bundled
  9. * with this package in the file LICENSE.txt.
  10. * It is also available through the world-wide-web at this URL:
  11. * http://framework.zend.com/license/new-bsd
  12. * If you did not receive a copy of the license and are unable to
  13. * obtain it through the world-wide-web, please send an email
  14. * to license@zend.com so we can send you a copy immediately.
  15. *
  16. * @category Zend
  17. * @package Zend_DocBook
  18. * @subpackage Exception
  19. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  20. * @license http://framework.zend.com/license/new-bsd New BSD License
  21. */
  22. use Zend\Console;
  23. use Zend\DocBook\ClassParser;
  24. use Zend\DocBook\SkeletonGenerator;
  25. use Zend\Code\Reflection\ClassReflection as ReflectionClass;
  26. use Zend\Loader\StandardAutoloader;
  27. /**
  28. * Generate Docbook XML skeleton for a given class as a documentation stub.
  29. *
  30. * Additionally, it parses all public methods of the class and creates stub
  31. * documentation for each in the "Available Methods" section of the class
  32. * documentation.
  33. *
  34. * Usage:
  35. * --help|-h Get usage message
  36. * --class|-c [ <string> ] Class name for which to provide documentation
  37. * --output|-o [ <string> ] Where to write generated documentation. By default,
  38. * assumes documentation/manual/en/module_specs, in a
  39. * file named after the provided class (in the form of
  40. * "zend.component.class-name.xml")
  41. */
  42. $libPath = getenv('LIB_PATH') ? getenv('LIB_PATH') : __DIR__ . '/../library';
  43. if (!is_dir($libPath)) {
  44. // Try to load StandardAutoloader from include_path
  45. if (false === include('Zend/Loader/StandardAutoloader.php')) {
  46. echo "Unable to locate autoloader via include_path; aborting" . PHP_EOL;
  47. exit(2);
  48. }
  49. } else {
  50. // Try to load StandardAutoloader from library
  51. if (false === include($libPath . '/Zend/Loader/StandardAutoloader.php')) {
  52. echo "Unable to locate autoloader via library; aborting" . PHP_EOL;
  53. exit(2);
  54. }
  55. }
  56. // Setup autoloading
  57. $loader = new StandardAutoloader(array('autoregister_zf' => true));
  58. $loader->register();
  59. $rules = array(
  60. 'help|h' => 'Get usage message',
  61. 'class|c=s' => 'Class for which to create a docbook skeleton',
  62. 'output|o-s' => 'Where to write skeleton file; if not provided, assumes documentation/manual/en/module_specs/<class id>.xml"',
  63. );
  64. $docbookPath = __DIR__ . '/../documentation/manual/en/module_specs';
  65. $docbookFile = false;
  66. // Parse options
  67. try {
  68. $opts = new Zend\Console\Getopt($rules);
  69. $opts->parse();
  70. } catch (Console\Exception\RuntimeException $e) {
  71. // Error creating or parsing options; show usage message
  72. echo $e->getUsageMessage();
  73. exit(2);
  74. }
  75. // Help requested
  76. if ($opts->getOption('h')) {
  77. echo $opts->getUsageMessage();
  78. exit();
  79. }
  80. // Were we provided a class name?
  81. if (!isset($opts->c)) {
  82. // No class name == no execution
  83. echo "Must provide a class via the -c or --class option\n\n";
  84. echo $opts->getUsageMessage();
  85. exit(2);
  86. }
  87. // Valid class name?
  88. $class = $opts->c;
  89. if (!class_exists($class) && !interface_exists($class)) {
  90. // Invalid class name == no execution
  91. printf("Invalid class '%s' provided' class not found\n\n", $class);
  92. echo $opts->getUsageMessage();
  93. exit(2);
  94. }
  95. // Was a specific filename provided for the generated output?
  96. if (isset($opts->o)) {
  97. $docbookPath = dirname($opts->o);
  98. $docbookFile = basename($opts->o);
  99. }
  100. $parser = new ClassParser(new ReflectionClass($class));
  101. $generator = new SkeletonGenerator($parser);
  102. $xml = $generator->generate();
  103. // Normalize per CS
  104. $xml = strtr($xml, array(
  105. ' ' => ' ', // 4 space tabs
  106. '</info>' => "</info>\n", // Extra newline between blocks
  107. '</section>' => "</section>\n",
  108. '</term>' => "</term>\n",
  109. '</varlistentry>' => "</varlistentry>\n",
  110. ));
  111. // Strip extra whitespace at end of document
  112. $xml = str_replace("</section>\n\n</section>\n", "</section>\n</section>", $xml);
  113. // Write file
  114. if (!$docbookFile) {
  115. // Auto-generate filename based on class ID
  116. $docbookFile = $parser->getId() . '.xml';
  117. }
  118. $path = $docbookPath . DIRECTORY_SEPARATOR . $docbookFile;
  119. file_put_contents($path, $xml);
  120. echo "[DONE] Wrote Docbook XML skeleton to $path\n";