PageRenderTime 24ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Support/bootstrap.php

http://github.com/psynaptic/php-drupal.tmbundle
PHP | 259 lines | 206 code | 44 blank | 9 comment | 44 complexity | e61e501c01288896cc8cc1107c21ba4b MD5 | raw file
  1. <?php
  2. $basename = 'hook';
  3. if (empty($_SERVER['TM_DRUPAL_VERSION'])) {
  4. $_SERVER['TM_DRUPAL_VERSION'] = '7';
  5. }
  6. if (empty($_SERVER['TM_DRUPAL_API'])) {
  7. $_SERVER['TM_DRUPAL_API'] = 'http://api.drupal.org';
  8. }
  9. function textmate_detect_settings($filepath, $_basename = 'hook', $_version = NULL) {
  10. $settings = array(
  11. 'basename' => array(
  12. 'default' => TRUE,
  13. 'value' => $_basename,
  14. ),
  15. 'version' => array(
  16. 'default' => TRUE,
  17. 'value' => ($_version == NULL) ? $_SERVER['TM_DRUPAL_VERSION'] : $_version,
  18. ),
  19. );
  20. $nomask = array('.', '..', 'CVS');
  21. $path = explode('/', dirname($filepath));
  22. while($path) {
  23. if ($file = textmate_scan_directory(implode('/', $path), "/.*\.info$/", $nomask, 0, FALSE)) {
  24. $file = array_shift($file);
  25. if (isset($file->name) && $settings['basename']['default'] == TRUE) {
  26. $settings['basename']['value'] = $file->name;
  27. $settings['basename']['default'] = FALSE;
  28. }
  29. // search [module].info file
  30. if ($settings['version']['default'] == TRUE) {
  31. $info = textmate_parse_info_file($file->filename);
  32. if (!empty($info['core'])) {
  33. $settings['version']['value'] = (int) $info['core'];
  34. $settings['version']['default'] = FALSE;
  35. }
  36. }
  37. }
  38. // search system.info file in drupal core
  39. if ($settings['version']['default'] == TRUE) {
  40. $info = textmate_parse_info_file(implode('/', $path) . '/modules/system/system.info');
  41. if (!empty($info['core'])) {
  42. $settings['version']['value'] = (int) $info['core'];
  43. $settings['version']['default'] = FALSE;
  44. }
  45. }
  46. if ($settings['basename']['default'] == FALSE && $settings['version']['default'] == FALSE) {
  47. return $settings;
  48. }
  49. array_pop($path);
  50. }
  51. return $settings;
  52. }
  53. function textmate_find_command($name) {
  54. $fallback = NULL;
  55. if (strpos($name, 'theme_') === 0) {
  56. $folder = 'theme/';
  57. $fallback = 'theme';
  58. }
  59. elseif (strpos($name, 'hook_') === 0) {
  60. $folder = 'hooks/';
  61. $fallback = 'hook';
  62. }
  63. elseif (strpos($name, 'drupal_') === 0) {
  64. $folder = 'drupal/';
  65. }
  66. elseif (strpos($name, 'js_') === 0) {
  67. $folder = 'js/';
  68. }
  69. elseif (strpos($name, 'content_') === 0) {
  70. $folder = 'cck/';
  71. }
  72. elseif (strpos($name, 'menu_') === 0) {
  73. $folder = 'menu/';
  74. }
  75. elseif (strpos($name, 'template_preprocess_') === 0) {
  76. $folder = 'preprocess/';
  77. $fallback = 'template';
  78. }
  79. elseif (strpos($name, 'schema_') === 0) {
  80. $folder = 'schema/';
  81. $name = str_replace('schema_', '', $name);
  82. }
  83. elseif (strpos($name, 'unit_test_') === 0 || strpos($name, 'web_test_') === 0) {
  84. $folder = 'simpletest/';
  85. }
  86. elseif (strpos($name, 'element_') === 0) {
  87. $folder = 'fapi/elements/';
  88. $name = str_replace('element_', '', $name);
  89. }
  90. elseif (strpos($name, 'fapi_') === 0) {
  91. $folder = 'fapi/controls/';
  92. $name = str_replace('fapi_', '', $name);
  93. }
  94. else {
  95. $folder = '';
  96. }
  97. $settings = textmate_detect_settings($_SERVER['TM_FILEPATH'], $fallback);
  98. $basename = $GLOBALS['basename'] = $settings['basename']['value'];
  99. $version = $GLOBALS['version'] = $settings['version']['value'];
  100. $files = array(
  101. $_SERVER['TM_BUNDLE_SUPPORT'] . '/commands/overrides/' . $folder . $version . '/' . $name . '.' . $version . '.php',
  102. $_SERVER['TM_BUNDLE_SUPPORT'] . '/commands/generated/' . $folder . $version . '/' . $name . '.' . $version . '.php',
  103. $_SERVER['TM_BUNDLE_SUPPORT'] . '/commands/overrides/' . $folder . '/' . $name . '.php',
  104. $_SERVER['TM_BUNDLE_SUPPORT'] . '/commands/generated/' . $folder . '/' . $name . '.php',
  105. $_SERVER['TM_BUNDLE_SUPPORT'] . '/does_not_exist.php',
  106. );
  107. foreach ($files as $file)
  108. if (file_exists($file))
  109. return $file;
  110. }
  111. function textmate_parse_info_file($filename) {
  112. if (!file_exists($filename)) {
  113. return array();
  114. }
  115. $data = file_get_contents($filename);
  116. return textmate_parse_info_format($data);
  117. }
  118. function textmate_parse_info_format($data) {
  119. $info = array();
  120. $constants = get_defined_constants();
  121. if (preg_match_all('
  122. @^\s* # Start at the beginning of a line, ignoring leading whitespace
  123. ((?:
  124. [^=;\[\]]| # Key names cannot contain equal signs, semi-colons or square brackets,
  125. \[[^\[\]]*\] # unless they are balanced and not nested
  126. )+?)
  127. \s*=\s* # Key/value pairs are separated by equal signs (ignoring white-space)
  128. (?:
  129. ("(?:[^"]|(?<=\\\\)")*")| # Double-quoted string, which may contain slash-escaped quotes/slashes
  130. (\'(?:[^\']|(?<=\\\\)\')*\')| # Single-quoted string, which may contain slash-escaped quotes/slashes
  131. ([^\r\n]*?) # Non-quoted string
  132. )\s*$ # Stop at the next end of a line, ignoring trailing whitespace
  133. @msx', $data, $matches, PREG_SET_ORDER)) {
  134. foreach ($matches as $match) {
  135. // Fetch the key and value string
  136. $i = 0;
  137. foreach (array('key', 'value1', 'value2', 'value3') as $var) {
  138. $$var = isset($match[++$i]) ? $match[$i] : '';
  139. }
  140. $value = stripslashes(substr($value1, 1, -1)) . stripslashes(substr($value2, 1, -1)) . $value3;
  141. // Parse array syntax
  142. $keys = preg_split('/\]?\[/', rtrim($key, ']'));
  143. $last = array_pop($keys);
  144. $parent = &$info;
  145. // Create nested arrays
  146. foreach ($keys as $key) {
  147. if ($key == '') {
  148. $key = count($parent);
  149. }
  150. if (!isset($parent[$key]) || !is_array($parent[$key])) {
  151. $parent[$key] = array();
  152. }
  153. $parent = &$parent[$key];
  154. }
  155. // Handle PHP constants.
  156. if (isset($constants[$value])) {
  157. $value = $constants[$value];
  158. }
  159. // Insert actual value
  160. if ($last == '') {
  161. $last = count($parent);
  162. }
  163. $parent[$last] = $value;
  164. }
  165. }
  166. return $info;
  167. }
  168. function textmate_scan_directory($dir, $mask, $nomask = array('.', '..', 'CVS'), $callback = 0, $recurse_max_depth = TRUE, $key = 'filename', $min_depth = 0, $include_dot_files = FALSE, $depth = 0) {
  169. $key = (in_array($key, array('filename', 'basename', 'name')) ? $key : 'filename');
  170. $files = array();
  171. if (is_dir($dir) && $handle = opendir($dir)) {
  172. while (FALSE !== ($file = readdir($handle))) {
  173. if (!in_array($file, $nomask) && (($include_dot_files && (!preg_match("/\.\+/",$file))) || ($file[0] != '.'))) {
  174. if (is_dir("$dir/$file") && (($recurse_max_depth === TRUE) || ($depth < $recurse_max_depth))) {
  175. // Give priority to files in this folder by merging them in after any subdirectory files.
  176. $files = array_merge(textmate_scan_directory("$dir/$file", $mask, $nomask, $callback, $recurse_max_depth, $key, $min_depth, $include_dot_files, $depth + 1), $files);
  177. }
  178. elseif ($depth >= $min_depth && preg_match($mask, $file)) {
  179. // Always use this match over anything already set in $files with the same $$key.
  180. $filename = "$dir/$file";
  181. $basename = basename($file);
  182. $name = substr($basename, 0, strrpos($basename, '.'));
  183. $files[$$key] = new stdClass();
  184. $files[$$key]->filename = $filename;
  185. $files[$$key]->basename = $basename;
  186. $files[$$key]->name = $name;
  187. if ($callback) {
  188. $callback($filename);
  189. }
  190. }
  191. }
  192. }
  193. closedir($handle);
  194. }
  195. return $files;
  196. }
  197. function textmate_docs_for_word($function) {
  198. $settings = textmate_detect_settings($_SERVER['TM_FILEPATH']);
  199. $basename = $settings['basename']['value'];
  200. $version = $settings['version']['value'];
  201. if (strpos($_SERVER['TM_DRUPAL_API'], 'drupalcontrib.org') !== FALSE) {
  202. $suggestions = array();
  203. $suggestions[] = preg_replace('/\A' . $basename .'_/', 'hook_', $function, 1);
  204. $suggestions[] = preg_replace('/\A' . $basename .'_/', 'theme_', $function, 1);
  205. $suggestions[] = preg_replace('/\A' . $basename .'_/', 'template_', $function, 1);
  206. $suggestions[] = $function;
  207. $suggestions = array_unique($suggestions);
  208. return $_SERVER['TM_DRUPAL_API'] . '/textmate_api/search/' . $version . '/' . implode('/', $suggestions);
  209. }
  210. return $_SERVER['TM_DRUPAL_API'] . '/api/search/' . $version . '/' . $function;
  211. }
  212. function textmate_docs_for_theme_hook($line) {
  213. preg_match("/theme\('([a-z_]+)'/", $line, $matches);
  214. return textmate_docs_for_word('theme_' . $matches[1]);
  215. }