Projet

Général

Profil

Paste
Télécharger (3,93 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / colorbox / drush / colorbox.drush.inc @ 73ab1d0a

1
<?php
2

    
3
/**
4
 * @file
5
 *   drush integration for colorbox.
6
 */
7

    
8
/**
9
 * The Colorbox plugin URI.
10
 */
11
define('COLORBOX_DOWNLOAD_URI', 'https://github.com/jackmoore/colorbox/archive/1.x.zip');
12
define('COLORBOX_DOWNLOAD_PREFIX', 'colorbox-');
13

    
14
/**
15
 * Implementation of hook_drush_command().
16
 *
17
 * In this hook, you specify which commands your
18
 * drush module makes available, what it does and
19
 * description.
20
 *
21
 * Notice how this structure closely resembles how
22
 * you define menu hooks.
23
 *
24
 * See `drush topic docs-commands` for a list of recognized keys.
25
 *
26
 * @return array
27
 *   An associative array describing your command(s).
28
 */
29
function colorbox_drush_command() {
30
  $items = array();
31

    
32
  // the key in the $items array is the name of the command.
33
  $items['colorbox-plugin'] = array(
34
    'callback' => 'drush_colorbox_plugin',
35
    'description' => dt('Download and install the Colorbox plugin.'),
36
    'bootstrap' => DRUSH_BOOTSTRAP_DRUSH, // No bootstrap.
37
    'arguments' => array(
38
      'path' => dt('Optional. A path where to install the Colorbox plugin. If omitted Drush will use the default location.'),
39
    ),
40
    'aliases' => array('colorboxplugin'),
41
  );
42

    
43
  return $items;
44
}
45

    
46
/**
47
 * Implementation of hook_drush_help().
48
 *
49
 * This function is called whenever a drush user calls
50
 * 'drush help <name-of-your-command>'
51
 *
52
 * @param string $section
53
 *   A string with the help section (prepend with 'drush:')
54
 *
55
 * @return string
56
 *   A string with the help text for your command.
57
 */
58
function colorbox_drush_help($section) {
59
  switch ($section) {
60
    case 'drush:colorbox-plugin':
61
      return dt('Download and install the Colorbox plugin from jacklmoore.com/colorbox, default location is sites/all/libraries.');
62
  }
63
}
64

    
65
/**
66
 * Implements drush_MODULE_pre_pm_enable().
67
 */
68
function drush_colorbox_pre_pm_enable() {
69
   $modules = drush_get_context('PM_ENABLE_MODULES');
70
   if (in_array('colorbox', $modules) && !drush_get_option('skip')) {
71
     drush_colorbox_plugin();
72
   }
73
}
74

    
75
/**
76
 * Command to download the Colorbox plugin.
77
 */
78
function drush_colorbox_plugin() {
79
  $args = func_get_args();
80
  if (!empty($args[0])) {
81
    $path = $args[0];
82
  }
83
  else {
84
    $path = 'sites/all/libraries';
85
  }
86

    
87
  // Create the path if it does not exist.
88
  if (!is_dir($path)) {
89
    drush_op('mkdir', $path);
90
    drush_log(dt('Directory @path was created', array('@path' => $path)), 'notice');
91
  }
92

    
93
  // Download colorbox plugin only if path is writable.
94
  if (is_writable($path)) {
95
    // Set the directory to the download location.
96
    $olddir = getcwd();
97
    chdir($path);
98

    
99
    // Download the zip archive
100
    if ($filepath = drush_download_file(COLORBOX_DOWNLOAD_URI)) {
101
      $filename = basename($filepath);
102
      $dirname = COLORBOX_DOWNLOAD_PREFIX . basename($filepath, '.zip');
103

    
104
      // Remove any existing Colorbox plugin directory.
105
      if (is_dir($dirname) || is_dir('colorbox')) {
106
        drush_delete_dir($dirname, TRUE);
107
        drush_delete_dir('colorbox', TRUE);
108
        drush_log(dt('A existing Colorbox plugin was deleted from @path', array('@path' => $path)), 'notice');
109
      }
110

    
111
      // Decompress the zip archive
112
      drush_tarball_extract($filename);
113

    
114
      // Change the directory name to "colorbox" if needed.
115
      if ($dirname != 'colorbox') {
116
        drush_move_dir($dirname, 'colorbox', TRUE);
117
        $dirname = 'colorbox';
118
      }
119
    }
120

    
121
    if (is_dir($dirname)) {
122
      drush_log(dt('Colorbox plugin has been installed in @path', array('@path' => $path)), 'success');
123
    }
124
    else {
125
      drush_log(dt('Drush was unable to install the Colorbox plugin to @path', array('@path' => $path)), 'error');
126
    }
127

    
128
    // Set working directory back to the previous working directory.
129
    chdir($olddir);
130
  }
131
  else {
132
    drush_log(dt('Drush was unable to install the Colorbox plugin because @path is not writable. If you enable the colorbox module before you install the plugin library, you may find that colorbox does not work until you reinstall the colorbox module.', array('@path' => $path)), 'warning');
133
  }
134
}