Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.Dcp_Utils_WStartCLI.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 
7 namespace Dcp\Utils;
8 
9 require_once __DIR__ . '/Class.Dcp_Utils_WStart.php';
10 
12 {
13 }
14 
16 {
17  public static function usage($me)
18  {
19  print <<<EOF
20 
21 Usage
22 -----
23 
24  $me [<options>] [--all|<operations>]
25 
26 Options:
27 
28  -v|--verbose Increase verbosity (can be specified multiple times to increase verbosity).
29 
30 Operations:
31 
32  -r|--resetAutoloader Re-generate class autoloader.
33  -l|--links Re-generate Images and Docs symlinks.
34  -c|--clearFile Clear cached content.
35  -u|--upgradeVersion Increment WVERSION.
36  -b|--dbconnect Configure CORE_DBCONNECT method
37  -s|--style Recompute style assets.
38  -m|--unStop Remove maintenance mode.
39 
40 
41 EOF;
42 
43 
44  }
45  /**
46  * Initialize context environment
47  *
48  * @return string
49  * @throws WStartCLIException
50  */
51  private static function bootstrap()
52  {
53  $contextRoot = realpath(__DIR__ . DIRECTORY_SEPARATOR . '..');
54  if ($contextRoot === false) {
55  throw new WStartCLIException(sprintf("Could not get context root directory from directory '%s'!", __DIR__));
56  }
57  if (chdir($contextRoot) === false) {
58  throw new WStartCLIException(sprintf("Could not change current working directory to '%s'!", $contextRoot));
59  }
60  set_include_path($contextRoot . PATH_SEPARATOR . sprintf('%s/WHAT', $contextRoot) . PATH_SEPARATOR . get_include_path());
61  require_once sprintf('%s/WHAT/Lib.Prefix.php', $contextRoot);
62  require_once sprintf('%s/WHAT/Class.Dcp_Utils_WStart.php', $contextRoot);
63  return $contextRoot;
64  }
65  /**
66  * Main CLI interface
67  *
68  * @param $argv
69  * @throws WStartCLIException
70  */
71  public static function run(&$argv)
72  {
73  $me = array_shift($argv);
74  if ($me === null) {
75  throw new WStartCLIException(sprintf("Undefined argument #0!"));
76  }
77  $contextRoot = self::bootstrap();
78  /*
79  * Default builtin getopt() function does not report unknown options, which I find rather annoying.
80  * And it treats short and long options as distinct options, which I also find rather annoying.
81  * So, I'd better craft my own getopt().
82  */
83  $opts = array();
84  while (count($argv) > 0) {
85  $opt = array_shift($argv);
86  if ($opt == '--') {
87  break;
88  }
89  switch ($opt) {
90  case '-h':
91  case '--help':
92  $opts['help'] = true;
93  break;
94 
95  case '-v':
96  case '--verbose':
97  if (!isset($opts['verbose'])) {
98  $opts['verbose'] = 0;
99  }
100  $opts['verbose']++;
101  break;
102 
103  case '-a':
104  case '--all':
105  $opts['all'] = true;
106  break;
107 
108  case '-r':
109  case '--resetAutoloader':
110  $opts['resetAutoloader'] = true;
111  break;
112 
113  case '-l':
114  case '--links':
115  $opts['links'] = true;
116  break;
117 
118  case '-c':
119  case '--clearFile':
120  $opts['clearFile'] = true;
121  break;
122 
123  case '-u':
124  case '--upgradeVersion':
125  $opts['upgradeVersion'] = true;
126  break;
127 
128  case '-b':
129  case '--dbconnect':
130  $opts['dbconnect'] = true;
131  break;
132 
133  case '-s':
134  case '--style':
135  $opts['style'] = true;
136  break;
137 
138  case '-m':
139  case '--unStop':
140  $opts['unStop'] = true;
141  break;
142 
143  default:
144  printf("ERROR: Unknown argument/option '%s'!\n", $opt);
145  self::usage($me);
146  exit(1);
147  break;
148  }
149  }
150  if (isset($opts['help'])) {
151  self::usage($me);
152  exit(0);
153  }
154  /*
155  * Set operations that need to be executed.
156  */
157  $operations = array(
158  'reapplyDatabaseParameters' => true, /* Always reapply database parameters before anything else */
159  'clearAutoloadCache' => false,
160  'imageAndDocsLinks' => false,
161  'clearFileCache' => false,
162  'refreshJsVersion' => false,
163  'configureDbConnect' => false,
164  'style' => false,
165  'unStop' => false
166  );
167  $all = true;
168  if (isset($opts['resetAutoloader'])) {
169  $operations['clearAutoloadCache'] = true;
170  $all = false;
171  }
172  if (isset($opts['links'])) {
173  $operations['imageAndDocsLinks'] = true;
174  $all = false;
175  }
176  if (isset($opts['clearFile'])) {
177  $operations['clearFileCache'] = true;
178  $all = false;
179  }
180  if (isset($opts['upgradeVersion'])) {
181  $operations['refreshJsVersion'] = true;
182  $all = false;
183  }
184  if (isset($opts['dbconnect'])) {
185  $operations['configureDbConnect'] = true;
186  $all = false;
187  }
188  if (isset($opts['style'])) {
189  $operations['style'] = true;
190  $all = false;
191  }
192  if (isset($opts['unStop'])) {
193  $operations['unStop'] = true;
194  $all = false;
195  }
196  /*
197  * If $all remains true, then set all operations for execution
198  */
199  if ($all) {
200  foreach ($operations as $name => & $needExec) {
201  $needExec = true;
202  }
203  unset($needExec);
204  }
205  $wstart = new WStart($contextRoot);
206  $wstart->setStdio(new WStartCLI());
207  if (isset($opts['verbose'])) {
208  $wstart->setVerbose($opts['verbose']);
209  }
210  foreach ($operations as $name => $needExec) {
211  if (!$needExec) {
212  continue;
213  }
214  if (!method_exists($wstart, $name)) {
215  throw new WStartCLIException(sprintf("Unknown operation '%s'!", $name));
216  }
217  if (call_user_func_array(array(
218  $wstart,
219  $name
220  ) , array()) === false) {
221  throw new WStartCLIException(sprintf("Execution of '%s' returned with error!", $name));
222  }
223  }
224  exit(0);
225  }
226  /**
227  * Wstart's stdout I/O interface
228  * @param $msg
229  */
230  public function wstart_stdout($msg)
231  {
232  fputs(STDOUT, $msg);
233  }
234  /**
235  * Wstart's stderr I/O interface
236  * @param $msg
237  */
238  public function wstart_stderr($msg)
239  {
240  if (mb_substr($msg, -1) != "\n") {
241  $msg.= "\n";
242  }
243  fputs(STDERR, $msg);
244  }
245 }
Exception class use exceptionCode to identifiy correctly exception.
Definition: exceptions.php:19
usage()
print
Definition: checklist.php:49
switch($command) exit
Definition: checkVault.php:46
← centre documentaire © anakeen