classgrapher.drush.inc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. /**
  12. * Implement hook_drush_command().
  13. *
  14. * Declare classgraph (hier) command.
  15. */
  16. function classgrapher_drush_command() {
  17. $items = array();
  18. $items['classgraph'] = array(
  19. 'description' => 'Graph PHP entities',
  20. 'aliases' => array('hier'),
  21. 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
  22. 'core' => array('7+'),
  23. 'drupal dependencies' => array(),
  24. 'arguments' => array(
  25. 'base' => 'The base directory below which to parse source files.',
  26. ),
  27. 'options' => array(
  28. 'cgdebug' => array(
  29. 'description' => 'Debug level: 0 to 7. Default to 6.',
  30. ),
  31. 'cgimager' => array(
  32. '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.',
  33. 'example-value' => 'dump,dot,neato',
  34. ),
  35. 'cgformat' => array(
  36. 'description' => 'Image format. Default to "svg". Not available with the dump imager.',
  37. 'example-value' => implode(',', _drush_classgrapher_get_formats()),
  38. ),
  39. ),
  40. );
  41. return $items;
  42. }
  43. /**
  44. * Helper to enumerate GraphViz format filters on the current system.
  45. *
  46. * @return array
  47. * An array of format names.
  48. */
  49. function _drush_classgrapher_get_formats() {
  50. $dotCommand = 'dot -Tinvalid';
  51. $descriptorSpec = array(
  52. 0 => array('pipe', 'r'),
  53. 1 => array('pipe', 'w'),
  54. 2 => array('pipe', 'w'),
  55. );
  56. $process = proc_open($dotCommand, $descriptorSpec, $pipes, NULL, NULL);
  57. if (!is_resource($process)) {
  58. drush_set_error('classgrapher', 'GraphViz not found.');
  59. }
  60. fclose($pipes[0]);
  61. fclose($pipes[1]);
  62. $stderr = stream_get_contents($pipes[2]);
  63. proc_close($process);
  64. $sts = preg_match('/(.+):( .* )*/', $stderr, $matches);
  65. if (!$sts || count($matches) != 3) {
  66. drush_set_error('classgrapher', 'GraphViz did not return a usable formats list.');
  67. }
  68. $formats = explode(' ', trim($matches[2]));
  69. return $formats;
  70. }
  71. /**
  72. * Validation callback for classgraph.
  73. */
  74. function drush_classgrapher_classgraph_validate() {
  75. if (!@include_once 'Image/GraphViz.php') {
  76. drush_set_error('classgrapher', 'PEAR Image_Graphviz not found.');
  77. }
  78. }
  79. /**
  80. * Command callback for classgraph.
  81. */
  82. function drush_classgrapher_classgraph($base) {
  83. $logger = new ClassGrapherLogger();
  84. $imager = drush_get_option('cgimager');
  85. if (!isset($imager) || $imager === FALSE) {
  86. $debug = 'dot';
  87. }
  88. $debug = drush_get_option('cgdebug');
  89. if (!isset($debug) || $debug === FALSE) {
  90. $this->debugLevel = WATCHDOG_INFO;
  91. }
  92. else {
  93. $this->debugLevel = (int) $debug;
  94. }
  95. $info = libraries_load('grammar_parser');
  96. $graph = new ClassGrapherGraph($base, $logger);
  97. echo $graph->build($imager);
  98. }