Projet

Général

Profil

Paste
Télécharger (4,01 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / colorbox / drush / colorbox.drush.inc @ b433176d

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
 * Implements 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
 * @codingStandardsIgnoreLine
27
 * @return array
28
 *   An associative array describing your command(s).
29
 */
30
function colorbox_drush_command() {
31
  $items = array();
32

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

    
45
  return $items;
46
}
47

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

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

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

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

    
97
  // Download colorbox plugin only if path is writable.
98
  if (is_writable($path)) {
99
    // Set the directory to the download location.
100
    $olddir = getcwd();
101
    chdir($path);
102

    
103
    // Download the zip archive.
104
    if ($filepath = drush_download_file(COLORBOX_DOWNLOAD_URI)) {
105
      $filename = basename($filepath);
106
      $dirname = COLORBOX_DOWNLOAD_PREFIX . basename($filepath, '.zip');
107

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

    
115
      // Decompress the zip archive.
116
      drush_tarball_extract($filename);
117

    
118
      // Change the directory name to "colorbox" if needed.
119
      if ($dirname != 'colorbox') {
120
        drush_move_dir($dirname, 'colorbox', TRUE);
121
        $dirname = 'colorbox';
122
      }
123
    }
124

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

    
132
    // Set working directory back to the previous working directory.
133
    chdir($olddir);
134
  }
135
  else {
136
    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');
137
  }
138
}