ds.drush.inc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. /**
  3. * @file
  4. * Display Suite drush integration.
  5. */
  6. /**
  7. * Implements hook_drush_command().
  8. */
  9. function ds_drush_command() {
  10. $items = array();
  11. $items['ds-build'] = array(
  12. 'description' => 'Create a basic template and configuration file for a new Display Suite layout.',
  13. 'arguments' => array(
  14. 'name' => 'Name for your layout.',
  15. ),
  16. 'options' => array(
  17. 'name' => 'Name for your layout.',
  18. 'regions' => 'Regions for your layout, comma-separated.',
  19. 'css' => 'Set this to true if you want to include a CSS file for your layout.',
  20. 'image' => 'Set this to true if you want to include a preview image for your layout.',
  21. ),
  22. 'examples' => array(
  23. 'drush ds-build "My layout name"' => 'Create a layout with a few example regions.',
  24. 'drush ds-build "My layout name" --regions="Region 1, Region 2"' => 'Create a layout with custom regions.',
  25. 'drush ds-build "My layout name" --css' => 'Create a layout with an included CSS file.',
  26. ),
  27. );
  28. return $items;
  29. }
  30. /**
  31. * Create a basic template and configuration file for a new Display Suite layout.
  32. */
  33. function drush_ds_build($name = NULL) {
  34. // Determine the layout name.
  35. if (!isset($name)) {
  36. $name = drush_get_option('name');
  37. }
  38. if (!$name) {
  39. drush_die(dt('You need to set a name for your layout. Type "drush help ds-build" for help.'));
  40. }
  41. // Determine the machine name.
  42. $machine_name = ds_prepare_machine_name($name);
  43. // Determine the path to our example layout templates.
  44. $ds_layout_path = dirname(__FILE__) . '/example_layout';
  45. // We create files in the current working directory.
  46. $layout_path = drush_cwd() . '/' . $machine_name;
  47. drush_op('mkdir', $layout_path);
  48. // Determine regions.
  49. $regions = drush_get_option('regions');
  50. if ($regions) {
  51. $regions = preg_split('/,(\ )?/', $regions);
  52. }
  53. // Copy the example templates.
  54. $tpl_machine_name = strtr($machine_name, '_', '-');
  55. drush_op('copy', $ds_layout_path . '/example-layout.tpl.php', $layout_path . "/$tpl_machine_name.tpl.php");
  56. drush_op('copy', $ds_layout_path . '/example_layout.inc', $layout_path . "/$machine_name.inc");
  57. // Prepare an array of things that need to be rewritten in our templates.
  58. $find = array();
  59. $replace = array();
  60. // Replace example name.
  61. $find[] = '/example layout/i';
  62. $replace[] = $name;
  63. $find[] = '/example_layout/';
  64. $replace[] = $machine_name;
  65. // Include a CSS file for this layout.
  66. $css = drush_get_option('css');
  67. if (isset($css)) {
  68. drush_op('copy', $ds_layout_path . '/example_layout.css', $layout_path . "/$machine_name.css");
  69. // Replace example styling if we have custom regions.
  70. if ($regions) {
  71. // Separate variables so this won't mess up our other templates.
  72. $css_find = $find;
  73. $css_replace = $replace;
  74. $css_find[] = "/(\*\/\n\n).+(\n)$/s";
  75. $css_replace[] = '$1' . ds_prepare_regions_css($regions) . '$2';
  76. drush_op('ds_file_preg_replace', array($layout_path . "/$machine_name.css"), $css_find, $css_replace);
  77. }
  78. // Uncomment the CSS rule in our configuration.
  79. $find[] = "/\/\/ ('css' => TRUE,)/";
  80. $replace[] = '$1';
  81. }
  82. // Check on form option.
  83. $image = drush_get_option('image');
  84. if (isset($image)) {
  85. // Uncomment the Form rule in our configuration.
  86. $find[] = "/\/\/ ('image' => TRUE,)/";
  87. $replace[] = '$1';
  88. }
  89. // Replace example region PHP/HTML code.
  90. if ($regions) {
  91. $find[] = '/ <!-- regions -->.+<!-- \/regions -->/s';
  92. $replace[] = ds_prepare_regions_html($regions);
  93. $find[] = "/( \* Regions:\n).+(\n \*\/)/s";
  94. $replace[] = '$1' . ds_prepare_regions_variable_documentation($regions) . '$2';
  95. $find[] = "/( 'regions' => array\(\n).+(\n \),)/s";
  96. $replace[] = '$1' . ds_prepare_regions_configuration($regions) . '$2';
  97. }
  98. // Rewrite templates.
  99. drush_op('ds_file_preg_replace', array($layout_path . "/$tpl_machine_name.tpl.php", $layout_path . "/$machine_name.inc"), $find, $replace);
  100. // Notify user of the newly created templates.
  101. drush_print(dt('Templates for "!name" created in: !path', array('!name' => $name, '!path' => $layout_path)));
  102. }
  103. /**
  104. * Prepare a string for use as a valid machine name.
  105. */
  106. function ds_prepare_machine_name($string) {
  107. $machine_name = str_replace(' ', '_', drupal_strtolower($string));
  108. // Remove characters not valid in function names.
  109. $machine_name = preg_replace('/[^a-z0-9_]/', '', $machine_name);
  110. return $machine_name;
  111. }
  112. /**
  113. * Perform preg_replace() on the contents of an array of files.
  114. */
  115. function ds_file_preg_replace($file_paths, $find, $replace) {
  116. foreach ($file_paths as $path) {
  117. $file_contents = file_get_contents($path);
  118. $file_contents = preg_replace($find, $replace, $file_contents);
  119. file_put_contents($path, $file_contents);
  120. }
  121. }
  122. /**
  123. * Prepare HTML structure for an array of regions.
  124. */
  125. function ds_prepare_regions_html($region_names) {
  126. $output = array();
  127. foreach ($region_names as $name) {
  128. $machine_name = ds_prepare_machine_name($name);
  129. $html_class = drupal_html_class($name);
  130. $output[] = <<<END
  131. <<?php print \${$machine_name}_wrapper; ?> class="ds-$html_class<?php print \${$machine_name}_classes; ?>">
  132. <?php print \$$machine_name; ?>
  133. </<?php print \${$machine_name}_wrapper; ?>>
  134. END;
  135. }
  136. return implode("\n\n", $output);
  137. }
  138. /**
  139. * Prepare variable documentation for an array of regions.
  140. */
  141. function ds_prepare_regions_variable_documentation($region_names) {
  142. $output = array();
  143. foreach ($region_names as $name) {
  144. $machine_name = ds_prepare_machine_name($name);
  145. $output[] = <<<END
  146. *
  147. * - \$$machine_name: Rendered content for the "$name" region.
  148. * - \${$machine_name}_classes: String of classes that can be used to style the "$name" region.
  149. END;
  150. }
  151. return implode("\n", $output);
  152. }
  153. /**
  154. * Prepare configuration for an array of regions.
  155. */
  156. function ds_prepare_regions_configuration($region_names) {
  157. $output = array();
  158. foreach ($region_names as $name) {
  159. $machine_name = ds_prepare_machine_name($name);
  160. $output[] = " '$machine_name' => t('$name'),";
  161. }
  162. return implode("\n", $output);
  163. }
  164. /**
  165. * Prepare styling for an array of regions.
  166. */
  167. function ds_prepare_regions_css($region_names) {
  168. $output = array();
  169. foreach ($region_names as $name) {
  170. $machine_name = ds_prepare_machine_name($name);
  171. $html_class = drupal_html_class($name);
  172. $output[] = <<<END
  173. .ds-$html_class {
  174. /* Styles for the "$html_class" region go here */
  175. }
  176. END;
  177. }
  178. return implode("\n\n", $output);
  179. }