classgrapher.drush.inc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * @file
  4. * Drush commands for Class Grapher
  5. *
  6. * - hier: plot class inheritance on a site
  7. *
  8. * - Idea: use Javascript Infovis Toolkit instead of GraphViz for nicer
  9. * graphs, less server load, and user interaction.
  10. */
  11. use \OSInet\Class_Grapher\Logger;
  12. use \OSInet\Class_Grapher\Graph;
  13. /**
  14. * Implement hook_drush_command().
  15. *
  16. * Declare classgraph (hier) command.
  17. */
  18. function classgrapher_drush_command() {
  19. $items = array();
  20. $items['classgraph'] = array(
  21. 'description' => 'Graph PHP entities',
  22. 'aliases' => array('hier'),
  23. 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
  24. // libraries_load() does not exist on 6.x. PSR0 is bundled in 8.x.
  25. 'core' => array('7'),
  26. 'drupal dependencies' => array('libraries'),
  27. 'arguments' => array(
  28. 'base' => 'A space-separated list of base directories below which to parse source files.',
  29. ),
  30. 'options' => array(
  31. 'cgdebug' => array(
  32. 'description' => 'Debug level: 0 to 7. Default to 6.',
  33. ),
  34. 'cgimager' => array(
  35. 'description' => 'Imager to use. Default to "dot". Use "dump" for a debug dump without GraphViz generation. Alternate imagers like circo, (s)fdp or twopi are converted to dot.',
  36. 'example-value' => 'dump,dot,neato',
  37. ),
  38. 'cgformat' => array(
  39. 'description' => 'Image format. Default to "svg". Not available with the dump imager.',
  40. 'example-value' => implode(',', _drush_classgrapher_get_formats()),
  41. ),
  42. ),
  43. );
  44. return $items;
  45. }
  46. /**
  47. * Helper to enumerate GraphViz format filters on the current system.
  48. *
  49. * @return array
  50. * An array of format names.
  51. */
  52. function _drush_classgrapher_get_formats() {
  53. $dotCommand = 'dot -Tinvalid';
  54. $descriptorSpec = array(
  55. 0 => array('pipe', 'r'),
  56. 1 => array('pipe', 'w'),
  57. 2 => array('pipe', 'w'),
  58. );
  59. $process = proc_open($dotCommand, $descriptorSpec, $pipes, NULL, NULL);
  60. if (!is_resource($process)) {
  61. drush_set_error('classgrapher', 'GraphViz not found.');
  62. }
  63. fclose($pipes[0]);
  64. fclose($pipes[1]);
  65. $stderr = stream_get_contents($pipes[2]);
  66. proc_close($process);
  67. $sts = preg_match('/(.+):( .* )*/', $stderr, $matches);
  68. if (!$sts || count($matches) != 3) {
  69. drush_set_error('classgrapher', 'GraphViz did not return a usable formats list.');
  70. }
  71. $formats = explode(' ', trim($matches[2]));
  72. return $formats;
  73. }
  74. /**
  75. * Validation callback for classgraph.
  76. */
  77. function drush_classgrapher_classgraph_validate() {
  78. // Libraries 7.x-1.x does not include this function, and Drush
  79. // 'drupal dependencies' can not require a specific branch.
  80. if (function_exists('libraries_load')) {
  81. $info = libraries_load('grammar_parser');
  82. }
  83. else {
  84. drush_set_error('classgrapher', 'Libraries 7.x-2.x needed.');
  85. }
  86. // Add this library's path to the include path to use PSR0 autoloading.
  87. $path = explode(':', ini_get('include_path'));
  88. $path[] = realpath(__DIR__ . '/../..');
  89. ini_set('include_path', implode(':', $path));
  90. // Drupal 7 does not include a PSR0 autoloader: use ours.
  91. include 'misc/psr0.php';
  92. spl_autoload_register('psr0_autoload');
  93. // PEAR classes can be included by any PSR0 autoloader, but error messages are
  94. // less clear, as the autoloader cannot know this is a PEAR class, and the
  95. // autoloading must be performed from non-namespaced code, or from a dual-
  96. // compatible autoloader (PSR0 + PEAR), so this is more reliable.
  97. if (!@include_once 'Image/GraphViz.php') {
  98. drush_set_error('classgrapher', 'PEAR Image_Graphviz not found.');
  99. }
  100. }
  101. /**
  102. * Command callback for classgraph.
  103. */
  104. function drush_classgrapher_classgraph() {
  105. $optionDefaults = array(
  106. 'imager' => 'dot',
  107. 'debug' => WATCHDOG_INFO,
  108. 'format' => 'svg',
  109. );
  110. $base = func_get_args();
  111. foreach ($optionDefaults as $name => $default) {
  112. $$name = drush_get_option("cg$name", $default);
  113. }
  114. $graph = new Graph($base, new Logger($debug));
  115. echo $graph->build($imager, $format);
  116. }