Projet

Général

Profil

Paste
Télécharger (6,42 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ds / drush / ds.drush.inc @ ba09eb79

1
<?php
2

    
3
/**
4
 * @file
5
 * Display Suite drush integration.
6
 */
7

    
8
/**
9
 * Implements hook_drush_command().
10
 */
11
function ds_drush_command() {
12
  $items = array();
13

    
14
  $items['ds-build'] = array(
15
    'description' => 'Create a basic template and configuration file for a new Display Suite layout.',
16
    'arguments' => array(
17
      'name'    => 'Name for your layout.',
18
    ),
19
    'options' => array(
20
      'name'    => 'Name for your layout.',
21
      'regions' => 'Regions for your layout, comma-separated.',
22
      'css'     => 'Set this to true if you want to include a CSS file for your layout.',
23
      'image'   => 'Set this to true if you want to include a preview image for your layout.',
24
    ),
25
    'examples' => array(
26
      'drush ds-build "My layout name"' => 'Create a layout with a few example regions.',
27
      'drush ds-build "My layout name" --regions="Region 1, Region 2"' => 'Create a layout with custom regions.',
28
      'drush ds-build "My layout name" --css' => 'Create a layout with an included CSS file.',
29
    ),
30
  );
31

    
32
  return $items;
33
}
34

    
35
/**
36
 * Create a basic template and configuration file for a new Display Suite layout.
37
 */
38
function drush_ds_build($name = NULL) {
39
  // Determine the layout name.
40
  if (!isset($name)) {
41
    $name = drush_get_option('name');
42
  }
43
  if (!$name) {
44
    drush_die(dt('You need to set a name for your layout. Type "drush help ds-build" for help.'));
45
  }
46

    
47
  // Determine the machine name.
48
  $machine_name = ds_prepare_machine_name($name);
49

    
50
  // Determine the path to our example layout templates.
51
  $ds_layout_path = dirname(__FILE__) . '/example_layout';
52

    
53
  // We create files in the current working directory.
54
  $layout_path = drush_cwd() . '/' . $machine_name;
55
  drush_op('mkdir', $layout_path);
56

    
57
  // Determine regions.
58
  $regions = drush_get_option('regions');
59
  if ($regions) {
60
    $regions = preg_split('/,(\ )?/', $regions);
61
  }
62

    
63
  // Copy the example templates.
64
  $tpl_machine_name = strtr($machine_name, '_', '-');
65
  drush_op('copy', $ds_layout_path . '/example-layout.tpl.php', $layout_path . "/$tpl_machine_name.tpl.php");
66
  drush_op('copy', $ds_layout_path . '/example_layout.inc', $layout_path . "/$machine_name.inc");
67

    
68
  // Prepare an array of things that need to be rewritten in our templates.
69
  $find = array();
70
  $replace = array();
71

    
72
  // Replace example name.
73
  $find[] = '/example layout/i';
74
  $replace[] = $name;
75
  $find[] = '/example_layout/';
76
  $replace[] = $machine_name;
77

    
78
  // Include a CSS file for this layout.
79
  $css = drush_get_option('css');
80
  if (isset($css)) {
81
    drush_op('copy', $ds_layout_path . '/example_layout.css', $layout_path . "/$machine_name.css");
82

    
83
    // Replace example styling if we have custom regions.
84
    if ($regions) {
85
      // Separate variables so this won't mess up our other templates.
86
      $css_find = $find;
87
      $css_replace = $replace;
88

    
89
      $css_find[] = "/(\*\/\n\n).+(\n)$/s";
90
      $css_replace[] = '$1' . ds_prepare_regions_css($regions) . '$2';
91

    
92
      drush_op('ds_file_preg_replace', array($layout_path . "/$machine_name.css"), $css_find, $css_replace);
93
    }
94

    
95
    // Uncomment the CSS rule in our configuration.
96
    $find[] = "/\/\/ ('css' => TRUE,)/";
97
    $replace[] = '$1';
98
  }
99

    
100
  // Check on form option.
101
  $image = drush_get_option('image');
102
  if (isset($image)) {
103
    // Uncomment the Form rule in our configuration.
104
    $find[] = "/\/\/ ('image' => TRUE,)/";
105
    $replace[] = '$1';
106
  }
107

    
108
  // Replace example region PHP/HTML code.
109
  if ($regions) {
110
    $find[] = '/  <!-- regions -->.+<!-- \/regions -->/s';
111
    $replace[] = ds_prepare_regions_html($regions);
112
    $find[] = "/( \* Regions:\n).+(\n \*\/)/s";
113
    $replace[] = '$1' . ds_prepare_regions_variable_documentation($regions) . '$2';
114
    $find[] = "/(    'regions' => array\(\n).+(\n    \),)/s";
115
    $replace[] = '$1' . ds_prepare_regions_configuration($regions) . '$2';
116
  }
117

    
118
  // Rewrite templates.
119
  drush_op('ds_file_preg_replace', array($layout_path . "/$tpl_machine_name.tpl.php", $layout_path . "/$machine_name.inc"), $find, $replace);
120

    
121
  // Notify user of the newly created templates.
122
  drush_print(dt('Templates for "!name" created in: !path', array('!name' => $name, '!path' => $layout_path)));
123
}
124

    
125
/**
126
 * Prepare a string for use as a valid machine name.
127
 */
128
function ds_prepare_machine_name($string) {
129
  $machine_name = str_replace(' ', '_', drupal_strtolower($string));
130
  // Remove characters not valid in function names.
131
  $machine_name = preg_replace('/[^a-z0-9_]/', '', $machine_name);
132

    
133
  return $machine_name;
134
}
135

    
136
/**
137
 * Perform preg_replace() on the contents of an array of files.
138
 */
139
function ds_file_preg_replace($file_paths, $find, $replace) {
140
  foreach ($file_paths as $path) {
141
    $file_contents = file_get_contents($path);
142
    $file_contents = preg_replace($find, $replace, $file_contents);
143
    file_put_contents($path, $file_contents);
144
  }
145
}
146

    
147
/**
148
 * Prepare HTML structure for an array of regions.
149
 */
150
function ds_prepare_regions_html($region_names) {
151
  $output = array();
152

    
153
  foreach ($region_names as $name) {
154
    $machine_name = ds_prepare_machine_name($name);
155
    $html_class = drupal_html_class($name);
156

    
157
    $output[] = <<<END
158
    <<?php print \${$machine_name}_wrapper; ?> class="ds-$html_class<?php print \${$machine_name}_classes; ?>">
159
      <?php print \$$machine_name; ?>
160
    </<?php print \${$machine_name}_wrapper; ?>>
161
END;
162
  }
163

    
164
  return implode("\n\n", $output);
165
}
166

    
167
/**
168
 * Prepare variable documentation for an array of regions.
169
 */
170
function ds_prepare_regions_variable_documentation($region_names) {
171
  $output = array();
172

    
173
  foreach ($region_names as $name) {
174
    $machine_name = ds_prepare_machine_name($name);
175

    
176
    $output[] = <<<END
177
 *
178
 * - \$$machine_name: Rendered content for the "$name" region.
179
 * - \${$machine_name}_classes: String of classes that can be used to style the "$name" region.
180
END;
181
  }
182

    
183
  return implode("\n", $output);
184
}
185

    
186
/**
187
 * Prepare configuration for an array of regions.
188
 */
189
function ds_prepare_regions_configuration($region_names) {
190
  $output = array();
191

    
192
  foreach ($region_names as $name) {
193
    $machine_name = ds_prepare_machine_name($name);
194
    $output[] = "      '$machine_name' => t('$name'),";
195
  }
196

    
197
  return implode("\n", $output);
198
}
199

    
200
/**
201
 * Prepare styling for an array of regions.
202
 */
203
function ds_prepare_regions_css($region_names) {
204
  $output = array();
205

    
206
  foreach ($region_names as $name) {
207
    $machine_name = ds_prepare_machine_name($name);
208
    $html_class = drupal_html_class($name);
209

    
210
    $output[] = <<<END
211
.ds-$html_class {
212
  /* Styles for the "$html_class" region go here */
213
}
214
END;
215
  }
216

    
217
  return implode("\n\n", $output);
218
}