Drush: drush.php Source File

Drush
Go to the documentation of this file.
1 #!/usr/bin/env php
2 <?php
3 
4 /**
5  * @file
6  * drush is a PHP script implementing a command line shell for Drupal.
7  *
8  * @requires PHP CLI 5.3.0, or newer.
9  */
10 
11 require dirname(__FILE__) . '/includes/preflight.inc';
12 
13 if (drush_preflight_prepare() === FALSE) {
14  exit(1);
15 }
16 exit(drush_main());
17 
18 /**
19  * The main Drush function.
20  *
21  * - Runs "early" option code, if set (see global options).
22  * - Parses the command line arguments, configuration files and environment.
23  * - Prepares and executes a Drupal bootstrap, if possible,
24  * - Dispatches the given command.
25  *
26  * function_exists('drush_main') may be used by modules to detect whether
27  * they are being called from Drush. See http://drupal.org/node/1181308
29  *
30  * @return mixed
31  * Whatever the given command returns.
32  */
33 function drush_main() {
34  $return = '';
35  // Start code coverage collection.
36  if ($coverage_file = drush_get_option('drush-coverage', FALSE)) {
37  drush_set_context('DRUSH_CODE_COVERAGE', $coverage_file);
38  xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE);
39  register_shutdown_function('drush_coverage_shutdown');
40  }
41 
42  /* Set up bootstrap object, so that
43  * - 'early' files can bootstrap when needed.
44  * - bootstrap constants are available.
45  */
46  $bootstrap_class = drush_get_option('bootstrap_class', 'Drush\Boot\DrupalBoot');
47  $bootstrap = new $bootstrap_class;
48  drush_set_context('DRUSH_BOOTSTRAP_OBJECT', $bootstrap);
49  $bootstrap->preflight();
50 
51  // Process initial global options such as --debug.
53 
54  $return = '';
56  if (!drush_get_error()) {
57  if ($file = drush_get_option('early', FALSE)) {
58  require_once $file;
59  $function = 'drush_early_' . basename($file, '.inc');
60  if (function_exists($function)) {
61  if ($return = $function()) {
62  // If the function returns FALSE, we continue and attempt to bootstrap
63  // as normal. Otherwise, we exit early with the returned output.
64  if ($return === TRUE) {
65  $return = '';
66  }
67  }
68  }
69  }
70  else {
71  // Do any necessary preprocessing operations on the command,
72  // perhaps handling immediately.
73  $command_handled = drush_preflight_command_dispatch();
74  if (!$command_handled) {
75  $bootstrap = drush_get_context('DRUSH_BOOTSTRAP_OBJECT');
76  $return = $bootstrap->bootstrap_and_dispatch();
77  }
78  }
79  }
81 
82  // After this point the drush_shutdown function will run,
83  // exiting with the correct exit code.
84  return $return;
85 }

File

doxygen/html/drush_8php_source.html
View source
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<title>Drush: drush.php Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { searchBox.OnSelectItem(0); });
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td style="padding-left: 0.5em;">
   <div id="projectname">Drush
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.1.2 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li><a href="modules.html"><span>Modules</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
      <li><a href="globals.html"><span>File&#160;Members</span></a></li>
    </ul>
  </div>
</div><!-- top -->
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Groups</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Pages</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">drush.php</div>  </div>
</div><!--header-->
<div class="contents">
<a href="drush_8php.html">Go to the documentation of this file.</a><div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;<span class="preprocessor">#!/usr/bin/env php</span></div>
<div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;<span class="preprocessor"></span>&lt;?php</div>
<div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160;<span class="comment"></span></div>
<div class="line"><a name="l00004"></a><span class="lineno">    4</span>&#160;<span class="comment">/**</span></div>
<div class="line"><a name="l00005"></a><span class="lineno">    5</span>&#160;<span class="comment"> * @file</span></div>
<div class="line"><a name="l00006"></a><span class="lineno">    6</span>&#160;<span class="comment"> * drush is a PHP script implementing a command line shell for Drupal.</span></div>
<div class="line"><a name="l00007"></a><span class="lineno">    7</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00008"></a><span class="lineno">    8</span>&#160;<span class="comment"> * @requires PHP CLI 5.3.0, or newer.</span></div>
<div class="line"><a name="l00009"></a><span class="lineno">    9</span>&#160;<span class="comment"> */</span></div>
<div class="line"><a name="l00010"></a><span class="lineno">   10</span>&#160;</div>
<div class="line"><a name="l00011"></a><span class="lineno">   11</span>&#160;require dirname(__FILE__) . <span class="stringliteral">&#39;/includes/preflight.inc&#39;</span>;</div>
<div class="line"><a name="l00012"></a><span class="lineno">   12</span>&#160;</div>
<div class="line"><a name="l00013"></a><span class="lineno">   13</span>&#160;<span class="keywordflow">if</span> (<a class="code" href="preflight_8inc.html#a36dd6e0458ff1c29b7e644ebd641c610">drush_preflight_prepare</a>() === FALSE) {</div>
<div class="line"><a name="l00014"></a><span class="lineno">   14</span>&#160;  exit(1);</div>
<div class="line"><a name="l00015"></a><span class="lineno">   15</span>&#160;}</div>
<div class="line"><a name="l00016"></a><span class="lineno">   16</span>&#160;exit(<a class="code" href="drush_8php.html#a2b642d2bbf815b6d80c315fd66b37381">drush_main</a>());</div>
<div class="line"><a name="l00017"></a><span class="lineno">   17</span>&#160;<span class="comment"></span></div>
<div class="line"><a name="l00018"></a><span class="lineno">   18</span>&#160;<span class="comment">/**</span></div>
<div class="line"><a name="l00019"></a><span class="lineno">   19</span>&#160;<span class="comment"> * The main Drush function.</span></div>
<div class="line"><a name="l00020"></a><span class="lineno">   20</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00021"></a><span class="lineno">   21</span>&#160;<span class="comment"> * - Runs &quot;early&quot; option code, if set (see global options).</span></div>
<div class="line"><a name="l00022"></a><span class="lineno">   22</span>&#160;<span class="comment"> * - Parses the command line arguments, configuration files and environment.</span></div>
<div class="line"><a name="l00023"></a><span class="lineno">   23</span>&#160;<span class="comment"> * - Prepares and executes a Drupal bootstrap, if possible,</span></div>
<div class="line"><a name="l00024"></a><span class="lineno">   24</span>&#160;<span class="comment"> * - Dispatches the given command.</span></div>
<div class="line"><a name="l00025"></a><span class="lineno">   25</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00026"></a><span class="lineno">   26</span>&#160;<span class="comment"> * function_exists(&#39;drush_main&#39;) may be used by modules to detect whether</span></div>
<div class="line"><a name="l00027"></a><span class="lineno">   27</span>&#160;<span class="comment"> * they are being called from Drush.  See http://drupal.org/node/1181308</span></div>
<div class="line"><a name="l00028"></a><span class="lineno">   28</span>&#160;<span class="comment"> * and http://drupal.org/node/827478</span></div>
<div class="line"><a name="l00029"></a><span class="lineno">   29</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00030"></a><span class="lineno">   30</span>&#160;<span class="comment"> * @return mixed</span></div>
<div class="line"><a name="l00031"></a><span class="lineno">   31</span>&#160;<span class="comment"> *   Whatever the given command returns.</span></div>
<div class="line"><a name="l00032"></a><span class="lineno">   32</span>&#160;<span class="comment"> */</span></div>
<div class="line"><a name="l00033"></a><span class="lineno"><a class="code" href="drush_8php.html#a2b642d2bbf815b6d80c315fd66b37381">   33</a></span>&#160;<span class="keyword">function</span> <a class="code" href="drush_8php.html#a2b642d2bbf815b6d80c315fd66b37381">drush_main</a>() {</div>
<div class="line"><a name="l00034"></a><span class="lineno">   34</span>&#160;  $return = <span class="stringliteral">&#39;&#39;</span>;</div>
<div class="line"><a name="l00035"></a><span class="lineno">   35</span>&#160;  <span class="comment">// Start code coverage collection.</span></div>
<div class="line"><a name="l00036"></a><span class="lineno">   36</span>&#160;  <span class="keywordflow">if</span> ($coverage_file = <a class="code" href="context_8inc.html#afe91fb3e5219ecb2208c3b036b1f0cb8">drush_get_option</a>(<span class="stringliteral">&#39;drush-coverage&#39;</span>, FALSE)) {</div>
<div class="line"><a name="l00037"></a><span class="lineno">   37</span>&#160;    <a class="code" href="context_8inc.html#af983acfe90e7d7bbdbd67c57f93708ba">drush_set_context</a>(<span class="stringliteral">&#39;DRUSH_CODE_COVERAGE&#39;</span>, $coverage_file);</div>
<div class="line"><a name="l00038"></a><span class="lineno">   38</span>&#160;    xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE);</div>
<div class="line"><a name="l00039"></a><span class="lineno">   39</span>&#160;    register_shutdown_function(<span class="stringliteral">&#39;drush_coverage_shutdown&#39;</span>);</div>
<div class="line"><a name="l00040"></a><span class="lineno">   40</span>&#160;  }</div>
<div class="line"><a name="l00041"></a><span class="lineno">   41</span>&#160;</div>
<div class="line"><a name="l00042"></a><span class="lineno">   42</span>&#160;  <span class="comment">/* Set up bootstrap object, so that</span></div>
<div class="line"><a name="l00043"></a><span class="lineno">   43</span>&#160;<span class="comment">   * - &#39;early&#39; files can bootstrap when needed.</span></div>
<div class="line"><a name="l00044"></a><span class="lineno">   44</span>&#160;<span class="comment">   * - bootstrap constants are available.</span></div>
<div class="line"><a name="l00045"></a><span class="lineno">   45</span>&#160;<span class="comment">   */</span></div>
<div class="line"><a name="l00046"></a><span class="lineno">   46</span>&#160;  $bootstrap_class = <a class="code" href="context_8inc.html#afe91fb3e5219ecb2208c3b036b1f0cb8">drush_get_option</a>(<span class="stringliteral">&#39;bootstrap_class&#39;</span>, <span class="stringliteral">&#39;Drush\Boot\DrupalBoot&#39;</span>);</div>
<div class="line"><a name="l00047"></a><span class="lineno">   47</span>&#160;  $bootstrap = <span class="keyword">new</span> $bootstrap_class;</div>
<div class="line"><a name="l00048"></a><span class="lineno">   48</span>&#160;  <a class="code" href="context_8inc.html#af983acfe90e7d7bbdbd67c57f93708ba">drush_set_context</a>(<span class="stringliteral">&#39;DRUSH_BOOTSTRAP_OBJECT&#39;</span>, $bootstrap);</div>
<div class="line"><a name="l00049"></a><span class="lineno">   49</span>&#160;  $bootstrap-&gt;preflight();</div>
<div class="line"><a name="l00050"></a><span class="lineno">   50</span>&#160;</div>
<div class="line"><a name="l00051"></a><span class="lineno">   51</span>&#160;  <span class="comment">// Process initial global options such as --debug.</span></div>
<div class="line"><a name="l00052"></a><span class="lineno">   52</span>&#160;  _drush_preflight_global_options();</div>
<div class="line"><a name="l00053"></a><span class="lineno">   53</span>&#160;</div>
<div class="line"><a name="l00054"></a><span class="lineno">   54</span>&#160;  $return = <span class="stringliteral">&#39;&#39;</span>;</div>
<div class="line"><a name="l00055"></a><span class="lineno">   55</span>&#160;  <a class="code" href="preflight_8inc.html#a17ac1cc81941cb760a5101343e963f74">drush_preflight</a>();</div>
<div class="line"><a name="l00056"></a><span class="lineno">   56</span>&#160;  <span class="keywordflow">if</span> (!<a class="code" href="group__errorhandling.html#ga33fe8456b3dc49a0e28e7f63692a0d44">drush_get_error</a>()) {</div>
<div class="line"><a name="l00057"></a><span class="lineno">   57</span>&#160;    <span class="keywordflow">if</span> ($file = <a class="code" href="context_8inc.html#afe91fb3e5219ecb2208c3b036b1f0cb8">drush_get_option</a>(<span class="stringliteral">&#39;early&#39;</span>, FALSE)) {</div>
<div class="line"><a name="l00058"></a><span class="lineno">   58</span>&#160;      require_once $file;</div>
<div class="line"><a name="l00059"></a><span class="lineno">   59</span>&#160;      $function = <span class="stringliteral">&#39;drush_early_&#39;</span> . basename($file, <span class="stringliteral">&#39;.inc&#39;</span>);</div>
<div class="line"><a name="l00060"></a><span class="lineno">   60</span>&#160;      <span class="keywordflow">if</span> (function_exists($function)) {</div>
<div class="line"><a name="l00061"></a><span class="lineno">   61</span>&#160;        <span class="keywordflow">if</span> ($return = $function()) {</div>
<div class="line"><a name="l00062"></a><span class="lineno">   62</span>&#160;          <span class="comment">// If the function returns FALSE, we continue and attempt to bootstrap</span></div>
<div class="line"><a name="l00063"></a><span class="lineno">   63</span>&#160;          <span class="comment">// as normal. Otherwise, we exit early with the returned output.</span></div>
<div class="line"><a name="l00064"></a><span class="lineno">   64</span>&#160;          <span class="keywordflow">if</span> ($return === TRUE) {</div>
<div class="line"><a name="l00065"></a><span class="lineno">   65</span>&#160;            $return = <span class="stringliteral">&#39;&#39;</span>;</div>
<div class="line"><a name="l00066"></a><span class="lineno">   66</span>&#160;          }</div>
<div class="line"><a name="l00067"></a><span class="lineno">   67</span>&#160;        }</div>
<div class="line"><a name="l00068"></a><span class="lineno">   68</span>&#160;      }</div>
<div class="line"><a name="l00069"></a><span class="lineno">   69</span>&#160;    }</div>
<div class="line"><a name="l00070"></a><span class="lineno">   70</span>&#160;    <span class="keywordflow">else</span> {</div>
<div class="line"><a name="l00071"></a><span class="lineno">   71</span>&#160;      <span class="comment">// Do any necessary preprocessing operations on the command,</span></div>
<div class="line"><a name="l00072"></a><span class="lineno">   72</span>&#160;      <span class="comment">// perhaps handling immediately.</span></div>
<div class="line"><a name="l00073"></a><span class="lineno">   73</span>&#160;      $command_handled = <a class="code" href="group__commandprocessing.html#gac5fea44d3fe064240192925f864d57a6">drush_preflight_command_dispatch</a>();</div>
<div class="line"><a name="l00074"></a><span class="lineno">   74</span>&#160;      <span class="keywordflow">if</span> (!$command_handled) {</div>
<div class="line"><a name="l00075"></a><span class="lineno">   75</span>&#160;        $bootstrap = <a class="code" href="context_8inc.html#ad25559f8d7d753f636fbeac347c29fc5">drush_get_context</a>(<span class="stringliteral">&#39;DRUSH_BOOTSTRAP_OBJECT&#39;</span>);</div>
<div class="line"><a name="l00076"></a><span class="lineno">   76</span>&#160;        $return = $bootstrap-&gt;bootstrap_and_dispatch();</div>
<div class="line"><a name="l00077"></a><span class="lineno">   77</span>&#160;      }</div>
<div class="line"><a name="l00078"></a><span class="lineno">   78</span>&#160;    }</div>
<div class="line"><a name="l00079"></a><span class="lineno">   79</span>&#160;  }</div>
<div class="line"><a name="l00080"></a><span class="lineno">   80</span>&#160;  <a class="code" href="preflight_8inc.html#a155fabb9f2865199084830b9efc2e5bd">drush_postflight</a>();</div>
<div class="line"><a name="l00081"></a><span class="lineno">   81</span>&#160;</div>
<div class="line"><a name="l00082"></a><span class="lineno">   82</span>&#160;  <span class="comment">// After this point the drush_shutdown function will run,</span></div>
<div class="line"><a name="l00083"></a><span class="lineno">   83</span>&#160;  <span class="comment">// exiting with the correct exit code.</span></div>
<div class="line"><a name="l00084"></a><span class="lineno">   84</span>&#160;  <span class="keywordflow">return</span> $return;</div>
<div class="line"><a name="l00085"></a><span class="lineno">   85</span>&#160;}</div>
</div><!-- fragment --></div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Wed Oct 8 2014 09:04:54 for Drush by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.1.2
</small></address>
</body>
</html>