classgrapher.drush.inc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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' => 'The base directory 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. if (function_exists('libraries_load')) {
  79. $info = libraries_load('grammar_parser');
  80. }
  81. else {
  82. drush_set_error('classgrapher', 'Libraries 7.x-2.x needed.');
  83. }
  84. // Add this library's path to the include path to use PSR0 autoloading.
  85. $path = explode(':', ini_get('include_path'));
  86. $path[] = realpath(__DIR__ . '/../..');
  87. ini_set('include_path', implode(':', $path));
  88. // Drupal 7 does not include a PSR0 autoloader: use ours.
  89. include 'misc/psr0.php';
  90. spl_autoload_register('psr0_autoload');
  91. // PEAR classes can be included by any PSR0 autoloader, but error messages are
  92. // less clear, as the autoloader cannot know this is a PEAR class, and the
  93. // autoloading must be performed from non-namespaced code, or from a dual-
  94. // compatible autoloader (PSR0 + PEAR), so this is more reliable.
  95. if (!@include_once 'Image/GraphViz.php') {
  96. drush_set_error('classgrapher', 'PEAR Image_Graphviz not found.');
  97. }
  98. }
  99. /**
  100. * Command callback for classgraph.
  101. */
  102. function drush_classgrapher_classgraph($base) {
  103. $optionDefaults = array(
  104. 'imager' => 'dot',
  105. 'debug' => WATCHDOG_INFO,
  106. 'format' => 'svg',
  107. );
  108. foreach ($optionDefaults as $name => $default) {
  109. $$name = drush_get_option("cg$name", $default);
  110. }
  111. $graph = new Graph($base, new Logger($debug));
  112. echo $graph->build($imager, $format);
  113. }