[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/module_builder/includes/ -> common.inc (source)

   1  <?php
   2  
   3  /**
   4   * @file common.inc
   5   * Stuff needed both by module and drush command.
   6   *
   7   * IMPORTANT: This file should be identical across all versions of Drupal
   8   * since Drush uses it.
   9   *
  10   */
  11  
  12  /**
  13   * Safe version of drush_print that can be called without worrying about 
  14   * where we are.
  15   */
  16  function _module_builder_drush_print($message, $indent, $type = 'message') {
  17    if (MODULE_BUILDER_ENV == 'drush') {
  18      // Running in a Drush CLI.  
  19      drush_print($message, $indent);    
  20    }
  21    else {
  22      // Running in Drupal UI. 
  23      // @TODO?
  24    }
  25  }
  26  
  27  /**
  28   * Get a path to a resource that is safe to use either on Drupal or Drush.
  29   *
  30   * @param $subpath
  31   *    The subpath inside the module_builder folder. Eg, 'templates'.
  32   */
  33  function module_builder_get_path($subpath) {
  34    $mb_path = drupal_get_path('module', 'module_builder');
  35    if (!$mb_path) {
  36      $mb_path = dirname(__FILE__) . '/..';
  37    }
  38    
  39    $path = $mb_path . '/' . $subpath;
  40    
  41    //print "path: >>$path<<";
  42    
  43    return $path;
  44  }
  45  
  46  /**
  47   * Include a version-specific file whether we're on drush or drupal. 
  48   * That is, we first try to include a file called NAME_X.inc where X is a 
  49   * Drupal major version number before falling back to NAME.inc.
  50   *
  51   * Files are included from the 'includes' folder inside module_builder.
  52   *
  53   * On Drush, this is a wrapper for drush_include().
  54   * On Drupal, this just goes straight for the current version.
  55   *
  56   * @param $name
  57   *  The filename, eg 'update'.
  58   * @param $extension
  59   *  The file extension.
  60   */
  61  function module_builder_include($name, $extension = 'inc') {
  62    $path = module_builder_get_path('includes');
  63    if (MODULE_BUILDER_ENV == 'drush') {
  64      // In Drush.
  65      // the NULL means drush_include will try to find the version.
  66      drush_include($path, $name, NULL, $extension);
  67    }
  68    else {
  69      // In Drupal GUI.
  70      // Try the versioned file first.
  71      $file = sprintf("%s/%s_%s.%s", $path, $name, _module_builder_drupal_major_version(), $extension);
  72      //dsm($file);
  73      if (file_exists($file)) {
  74        require_once($file);
  75        return;
  76      }
  77      // Fall back to the regular file.
  78      $file = sprintf("%s/%s.%s", $path, $name, $extension);
  79      require_once($file); 
  80    }
  81  }
  82  
  83  /**
  84   * Returns the Drupal major version number (5, 6, 7 ...)
  85   *
  86   * Helper function for module_builder_include. Cribbed and hacked from drush.
  87   */
  88  function _module_builder_drupal_major_version() {
  89    list($major_version) = explode('.', VERSION);
  90    return $major_version;
  91  }
  92  
  93  
  94  /**
  95   * Create a directory to store hook files if it does not exist.
  96   *
  97   * IMPORTANT: this function should only be used on Drupal 5 and 6.
  98   *
  99   * This logic blatantly ripped off from image.module -- thanks James! :)
 100   // somewhat obsolete.
 101   */
 102  function _module_builder_check_settings($directory = NULL) {
 103    if ($directory) {
 104      // on drush
 105      if (!is_dir($directory)) {
 106        mkdir($directory); 
 107      }
 108    }
 109    else {
 110      // on module
 111      // sanity check. need to verify /files exists before we do anything. see http://drupal.org/node/367138
 112      $files = file_create_path();
 113      file_check_directory($files, FILE_CREATE_DIRECTORY);
 114      // check hooks directory exists or create it
 115      $hooks_path = file_create_path(variable_get('module_builder_hooks_directory', 'hooks'));
 116      file_check_directory($hooks_path, FILE_CREATE_DIRECTORY, 'module_builder_hooks_directory');
 117    }
 118  }
 119  
 120  /**
 121   * Check hook data is available in storage file.
 122   *
 123   * This allows us to check things are okay at an early stage.
 124   *
 125   * @param $directory
 126   *  (optional) The directory to look in for processed data.
 127   * @return
 128   *  TRUE is file exists, FALSE if not.
 129   */
 130  function _module_builder_check_hook_data($directory = NULL) {
 131    if (!isset($directory)) {
 132      //$directory = file_create_path(variable_get('module_builder_hooks_directory', 'hooks'));
 133      $directory = _module_builder_get_hooks_directory();
 134    }
 135    
 136    return file_exists("$directory/hooks_processed.php");
 137  }
 138  
 139  /**
 140   * Update hook files and process them to our data file.
 141   *
 142   * This is the master function to call from either UI, drush or drupal.
 143   */
 144  function module_builder_update_data() {
 145    // Update the hook documentation.
 146    module_builder_include('update');
 147    $hook_files = module_builder_update_documentation();
 148    
 149    // Process the hook files.
 150    module_builder_include('process');
 151    module_builder_process_hook_data($hook_files);
 152      
 153    return TRUE; // FTW!  
 154  }
 155  
 156  /**
 157   * Get a directory to save or read hook data files.
 158   *
 159   * This is either the variable from Drupal, or the --data option.
 160   * Use of the --data option allows a central store of hook data that needs only
 161   * be downloaded once for all Drupal sites. 
 162   * Subdirectories are made for each version.
 163   *
 164   * This needs to be safe to use at any bootstrap level.
 165   * 
 166   * @return
 167   *   A directory path either relative to Drupal root or absolute.
 168   */
 169  function _module_builder_get_hooks_directory() {
 170    $common = FALSE;
 171    // Figure out the directory we should be using.
 172    if (MODULE_BUILDER_ENV == 'drupal') {
 173      // Running in a Drupal UI: directory is either 'hooks' or whatever the 
 174      // variable is set to.
 175      $directory = variable_get('module_builder_hooks_directory', 'hooks');
 176    }
 177    else {
 178      // TODO: TIDY UP!
 179      
 180      // Running under Drush.
 181      // The order is:
 182      // - command --data option
 183      // - local Drupal variable
 184      // - default Drupal location in files/hooks
 185      if (drush_get_option('data')) {
 186        $directory = drush_get_option('data');
 187        $common = TRUE;
 188      }
 189      if (!$directory) {
 190        if (function_exists('variable_get')) {
 191          // We're in a loaded Drupal, but MB might not be installed here.
 192          $directory = variable_get('module_builder_hooks_directory', 'hooks');
 193          // No variable: but could still be running MB, so we want files/hooks
 194          if (!$directory) {
 195            if (module_exists('module_builder')) {
 196              $directory = 'hooks';
 197            }
 198          }
 199        }
 200      }
 201    }
 202    
 203    if (!$directory) {
 204       // @todo: error! 
 205    }
 206    
 207    // Check and create the directory if necessary: version-specific code.
 208    module_builder_include('common_version');
 209    module_builder_create_directory($directory, $common);
 210      
 211    return $directory;
 212  }
 213  
 214  /**
 215   * Helper function to get all the code files for a given module
 216   * TODO: does drush have this?
 217   *
 218   * @param $module_root_name
 219   *  The root name of a module, eg 'node', 'taxonomy'.
 220   *
 221   * @return
 222   *  A flat array of filenames.  
 223   */
 224  function module_builder_get_module_files($module_root_name) {
 225    $filepath = drupal_get_path('module', $module_root_name);
 226  
 227    //$old_dir = getcwd();
 228    //chdir($filepath);
 229    $files = scandir($filepath);
 230  
 231    foreach ($files as $filename) {
 232      $ext = substr(strrchr($filename, '.'), 1);
 233      if (in_array($ext, array('module', 'install', 'inc'))) {
 234        $module_files[] = $filepath . '/' . $filename;
 235      }
 236    }
 237    
 238    return $module_files;
 239  }
 240  
 241  /**
 242   * Helper function to get all function names from a file.
 243   *
 244   * @param $file
 245   *  A complete filename from the Drupal root, eg 'modules/user/user.module'. 
 246   */
 247  function module_builder_get_functions($file) {
 248    $code = file_get_contents($file);
 249    //drush_print($code);
 250    
 251    $matches = array();
 252    $pattern = "/^function (\w+)/m";
 253    preg_match_all($pattern, $code, $matches); 
 254    
 255    return $matches[1];
 256  } 
 257  
 258  
 259  /**
 260   * Helper function to invoke hook_module_builder_info() in all modules.
 261   *
 262   * The tricky part is that we want to include ourselves, but module_builder
 263   * might not be installed (or even present) in Drupal if we are on Drush.
 264   */
 265  function _module_builder_invoke_hook() {
 266    // TODO: just get ours if no bootstrap?
 267    module_builder_include('common_version');
 268    $mb_files = module_builder_system_listing('\.module_builder.inc$', 'modules');
 269    //print_r($mb_files);
 270    
 271    $module_data = array();
 272    
 273    foreach ($mb_files as $file) {
 274      // Our system listing wrapper ensured that there is a uri property on all versions.
 275      include_once($file->uri);
 276      // Use a property of the (badly-documented!) $file object that is common to both D6 and D7.
 277      $module = str_replace('.module_builder', '', $file->name);
 278      // Note that bad data got back from the hook breaks things.
 279      if ($result = module_invoke($module, 'module_builder_info')) {
 280        $module_data = array_merge($module_data, $result);
 281      }
 282    }
 283    
 284    //print_r($module_data);
 285    
 286    // If we are running as Drush command, we're not an installed module.
 287    if (!module_exists('module_builder')) {
 288      include_once(dirname(__FILE__) . '/../module_builder.module_builder.inc');
 289      $data = array_merge($module_data, module_builder_module_builder_info());
 290    }  
 291    else {
 292      $data = $module_data;
 293      // Yeah we switch names so the merging above isn't affected by an empty array.
 294      // Gah PHP. Am probably doin it wrong.
 295    }
 296    
 297    //print_r($data);
 298    return $data;
 299  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7