PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/bin/docbook_skeleton.php

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