Browse Source

Clean up Drush plugin.

- Explicit dependency on libraries 7.x-2.x.
- Validation and autoloading moved to command validation hook.
- Improved the autoloading process to make it more resilient.
Frederic G. MARAND 12 years ago
parent
commit
c7c165c871
1 changed files with 35 additions and 17 deletions
  1. 35 17
      apps/class_grapher/classgrapher.drush.inc

+ 35 - 17
apps/class_grapher/classgrapher.drush.inc

@@ -9,6 +9,9 @@
  *   graphs, less server load, and user interaction.
  */
 
+use \OSInet\Class_Grapher\Logger;
+use \OSInet\Class_Grapher\Graph;
+
 /**
  * Implement hook_drush_command().
  *
@@ -21,8 +24,9 @@ function classgrapher_drush_command() {
     'description' => 'Graph PHP entities',
     'aliases' => array('hier'),
     'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
-    'core' => array('7+'),
-    'drupal dependencies' => array(),
+    // libraries_load() does not exist on 6.x. PSR0 is bundled in 8.x.
+    'core' => array('7'),
+    'drupal dependencies' => array('libraries'),
     'arguments' => array(
       'base' => 'The base directory below which to parse source files.',
     ),
@@ -80,6 +84,26 @@ function _drush_classgrapher_get_formats() {
  * Validation callback for classgraph.
  */
 function drush_classgrapher_classgraph_validate() {
+  if (function_exists('libraries_load')) {
+    $info = libraries_load('grammar_parser');
+  }
+  else {
+    drush_set_error('classgrapher', 'Libraries 7.x-2.x needed.');
+  }
+
+  // Add this library's path to the include path to use PSR0 autoloading.
+  $path = explode(':', ini_get('include_path'));
+  $path[] = realpath(__DIR__ . '/../..');
+  ini_set('include_path', implode(':', $path));
+
+  // Drupal 7 does not include a PSR0 autoloader: use ours.
+  include 'misc/psr0.php';
+  spl_autoload_register('psr0_autoload');
+
+  // PEAR classes can be included by any PSR0 autoloader, but error messages are
+  // less clear, as the autoloader cannot know this is a PEAR class, and the
+  // autoloading must be performed from non-namespaced code, or from a dual-
+  // compatible autoloader (PSR0 + PEAR), so this is more reliable.
   if (!@include_once 'Image/GraphViz.php') {
     drush_set_error('classgrapher', 'PEAR Image_Graphviz not found.');
   }
@@ -89,22 +113,16 @@ function drush_classgrapher_classgraph_validate() {
  * Command callback for classgraph.
  */
 function drush_classgrapher_classgraph($base) {
-  $logger = new ClassGrapherLogger();
-
-  $imager = drush_get_option('cgimager');
-  if (!isset($imager) || $imager === FALSE) {
-    $debug = 'dot';
-  }
+  $optionDefaults = array(
+    'imager' => 'dot',
+    'debug' => WATCHDOG_INFO,
+    'format' => 'svg',
+  );
 
-  $debug = drush_get_option('cgdebug');
-  if (!isset($debug) || $debug === FALSE) {
-    $this->debugLevel = WATCHDOG_INFO;
-  }
-  else {
-    $this->debugLevel = (int) $debug;
+  foreach ($optionDefaults as $name => $default) {
+    $$name = drush_get_option("cg$name", $default);
   }
 
-  $info = libraries_load('grammar_parser');
-  $graph = new ClassGrapherGraph($base, $logger);
-  echo $graph->build($imager);
+  $graph = new Graph($base, new Logger($debug));
+  echo $graph->build($imager, $format);
 }