0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
zf.php
Go to the documentation of this file.
1 <?php
2 /*
3  * Zend Framework
4  *
5  * LICENSE
6  *
7  * This source file is subject to the new BSD license that is bundled
8  * with this package in the file LICENSE.txt.
9  * It is also available through the world-wide-web at this URL:
10  * http://framework.zend.com/license/new-bsd
11  * If you did not receive a copy of the license and are unable to
12  * obtain it through the world-wide-web, please send an email
13  * to license@zend.com so we can send you a copy immediately.
14  *
15  * @category Zend
16  * @package Zend_Tool
17  * @subpackage Framework
18  * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
19  * @license http://framework.zend.com/license/new-bsd New BSD License
20  * @version $Id$
21  */
22 
23 /*
24  * ZF
25  *
26  * @category Zend
27  * @package Zend_Tool
28  * @subpackage Framework
29  * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
30  * @license http://framework.zend.com/license/new-bsd New BSD License
31  */
32 class ZF
33 {
34 
35  protected $_clientLoaded = false;
36 
37  protected $_mode = 'runTool';
38 
39  protected $_messages = array();
40 
41  protected $_homeDirectory = null;
42 
43  protected $_storageDirectory = null;
44 
45  protected $_configFile = null;
46 
47  public static function main()
48  {
49  $zf = new self();
50  $zf->bootstrap();
51  $zf->run();
52  }
53 
59  public function bootstrap()
60  {
61  // detect settings
62  $this->_mode = $this->_detectMode();
63  $this->_homeDirectory = $this->_detectHomeDirectory();
64  $this->_storageDirectory = $this->_detectStorageDirectory();
65  $this->_configFile = $this->_detectConfigFile();
66 
67  // setup
68  $this->_setupPHPRuntime();
69  $this->_setupToolRuntime();
70  }
71 
77  public function run()
78  {
79  switch ($this->_mode) {
80  case 'runError':
81  $this->_runError();
82  $this->_runInfo();
83  break;
84  case 'runSetup':
85  if ($this->_runSetup() === false) {
86  $this->_runInfo();
87  }
88  break;
89  case 'runInfo':
90  $this->_runInfo();
91  break;
92  case 'runTool':
93  default:
94  $this->_runTool();
95  break;
96  }
97 
98  return $this;
99  }
100 
106  protected function _detectMode()
107  {
108  $arguments = $_SERVER['argv'];
109 
110  $mode = 'runTool';
111 
112  if (!isset($arguments[0])) {
113  return $mode;
114  }
115 
116  if ($arguments[0] == $_SERVER['PHP_SELF']) {
117  $this->_executable = array_shift($arguments);
118  }
119 
120  if (!isset($arguments[0])) {
121  return $mode;
122  }
123 
124  if ($arguments[0] == '--setup') {
125  $mode = 'runSetup';
126  } elseif ($arguments[0] == '--info') {
127  $mode = 'runInfo';
128  }
129 
130  return $mode;
131  }
132 
133 
141  protected function _detectHomeDirectory($mustExist = true, $returnMessages = true)
142  {
143  $homeDirectory = null;
144 
145  $homeDirectory = getenv('ZF_HOME'); // check env var ZF_HOME
146  if ($homeDirectory) {
147  $this->_logMessage('Home directory found in environment variable ZF_HOME with value ' . $homeDirectory, $returnMessages);
148  if (!$mustExist || ($mustExist && file_exists($homeDirectory))) {
149  return $homeDirectory;
150  } else {
151  $this->_logMessage('Home directory does not exist at ' . $homeDirectory, $returnMessages);
152  }
153  }
154 
155  $homeDirectory = getenv('HOME'); // HOME environment variable
156 
157  if ($homeDirectory) {
158  $this->_logMessage('Home directory found in environment variable HOME with value ' . $homeDirectory, $returnMessages);
159  if (!$mustExist || ($mustExist && file_exists($homeDirectory))) {
160  return $homeDirectory;
161  } else {
162  $this->_logMessage('Home directory does not exist at ' . $homeDirectory, $returnMessages);
163  }
164 
165  }
166 
167  $homeDirectory = getenv('HOMEPATH');
168 
169  if ($homeDirectory) {
170  $this->_logMessage('Home directory found in environment variable HOMEPATH with value ' . $homeDirectory, $returnMessages);
171  if (!$mustExist || ($mustExist && file_exists($homeDirectory))) {
172  return $homeDirectory;
173  } else {
174  $this->_logMessage('Home directory does not exist at ' . $homeDirectory, $returnMessages);
175  }
176  }
177 
178  $homeDirectory = getenv('USERPROFILE');
179 
180  if ($homeDirectory) {
181  $this->_logMessage('Home directory found in environment variable USERPROFILE with value ' . $homeDirectory, $returnMessages);
182  if (!$mustExist || ($mustExist && file_exists($homeDirectory))) {
183  return $homeDirectory;
184  } else {
185  $this->_logMessage('Home directory does not exist at ' . $homeDirectory, $returnMessages);
186  }
187  }
188 
189  return false;
190  }
191 
199  protected function _detectStorageDirectory($mustExist = true, $returnMessages = true)
200  {
201  $storageDirectory = false;
202 
203  $storageDirectory = getenv('ZF_STORAGE_DIR');
204  if ($storageDirectory) {
205  $this->_logMessage('Storage directory path found in environment variable ZF_STORAGE_DIR with value ' . $storageDirectory, $returnMessages);
206  if (!$mustExist || ($mustExist && file_exists($storageDirectory))) {
207  return $storageDirectory;
208  } else {
209  $this->_logMessage('Storage directory does not exist at ' . $storageDirectory, $returnMessages);
210  }
211  }
212 
213  $homeDirectory = ($this->_homeDirectory) ? $this->_homeDirectory : $this->_detectHomeDirectory(true, false);
214 
215  if ($homeDirectory) {
216  $storageDirectory = $homeDirectory . '/.zf/';
217  $this->_logMessage('Storage directory assumed in home directory at location ' . $storageDirectory, $returnMessages);
218  if (!$mustExist || ($mustExist && file_exists($storageDirectory))) {
219  return $storageDirectory;
220  } else {
221  $this->_logMessage('Storage directory does not exist at ' . $storageDirectory, $returnMessages);
222  }
223  }
224 
225  return false;
226  }
227 
235  protected function _detectConfigFile($mustExist = true, $returnMessages = true)
236  {
237  $configFile = null;
238 
239  $configFile = getenv('ZF_CONFIG_FILE');
240  if ($configFile) {
241  $this->_logMessage('Config file found environment variable ZF_CONFIG_FILE at ' . $configFile, $returnMessages);
242  if (!$mustExist || ($mustExist && file_exists($configFile))) {
243  return $configFile;
244  } else {
245  $this->_logMessage('Config file does not exist at ' . $configFile, $returnMessages);
246  }
247  }
248 
249  $homeDirectory = ($this->_homeDirectory) ? $this->_homeDirectory : $this->_detectHomeDirectory(true, false);
250  if ($homeDirectory) {
251  $configFile = $homeDirectory . '/.zf.ini';
252  $this->_logMessage('Config file assumed in home directory at location ' . $configFile, $returnMessages);
253  if (!$mustExist || ($mustExist && file_exists($configFile))) {
254  return $configFile;
255  } else {
256  $this->_logMessage('Config file does not exist at ' . $configFile, $returnMessages);
257  }
258  }
259 
260  $storageDirectory = ($this->_storageDirectory) ? $this->_storageDirectory : $this->_detectStorageDirectory(true, false);
261  if ($storageDirectory) {
262  $configFile = $storageDirectory . '/zf.ini';
263  $this->_logMessage('Config file assumed in storage directory at location ' . $configFile, $returnMessages);
264  if (!$mustExist || ($mustExist && file_exists($configFile))) {
265  return $configFile;
266  } else {
267  $this->_logMessage('Config file does not exist at ' . $configFile, $returnMessages);
268  }
269  }
270 
271  return false;
272  }
273 
274 
280  protected function _setupPHPRuntime()
281  {
282  // set php runtime settings
283  ini_set('display_errors', true);
284 
285  // support the changing of the current working directory, necessary for some providers
286  $cwd = getenv('ZEND_TOOL_CURRENT_WORKING_DIRECTORY');
287  if ($cwd != '' && realpath($cwd)) {
288  chdir($cwd);
289  }
290 
291  if (!$this->_configFile) {
292  return;
293  }
294  $zfINISettings = parse_ini_file($this->_configFile);
295  $phpINISettings = ini_get_all();
296  foreach ($zfINISettings as $zfINIKey => $zfINIValue) {
297  if (substr($zfINIKey, 0, 4) === 'php.') {
298  $phpINIKey = substr($zfINIKey, 4);
299  if (array_key_exists($phpINIKey, $phpINISettings)) {
300  ini_set($phpINIKey, $zfINIValue);
301  }
302  }
303  }
304  }
305 
312  protected function _setupToolRuntime()
313  {
314 
315  $includePathPrepend = getenv('ZEND_TOOL_INCLUDE_PATH_PREPEND');
316  $includePathFull = getenv('ZEND_TOOL_INCLUDE_PATH');
317 
318  // check if the user has not provided anything
319  if (!($includePathPrepend || $includePathFull)) {
320  if ($this->_tryClientLoad()) {
321  return;
322  }
323  }
324 
325  // if ZF is not in the include_path, but relative to this file, put it in the include_path
326  if ($includePathPrepend || $includePathFull) {
327  if (isset($includePathPrepend) && ($includePathPrepend !== false)) {
328  set_include_path($includePathPrepend . PATH_SEPARATOR . get_include_path());
329  } elseif (isset($includePathFull) && ($includePathFull !== false)) {
330  set_include_path($includePathFull);
331  }
332  }
333 
334  if ($this->_tryClientLoad()) {
335  return;
336  }
337 
338  $zfIncludePath['relativePath'] = dirname(__FILE__) . '/../library/';
339  if (file_exists($zfIncludePath['relativePath'] . 'Zend/Tool/Framework/Client/Console.php')) {
340  set_include_path(realpath($zfIncludePath['relativePath']) . PATH_SEPARATOR . get_include_path());
341  }
342 
343  if (!$this->_tryClientLoad()) {
344  $this->_mode = 'runError';
345  return;
346  }
347  }
348 
357  protected function _tryClientLoad()
358  {
359  $this->_clientLoaded = false;
360  $fh = @fopen('Zend/Tool/Framework/Client/Console.php', 'r', true);
361  if (!$fh) {
362  return $this->_clientLoaded; // false
363  } else {
364  fclose($fh);
365  unset($fh);
366  include 'Zend/Tool/Framework/Client/Console.php';
367  $this->_clientLoaded = class_exists('Zend_Tool_Framework_Client_Console');
368  }
369 
370  return $this->_clientLoaded;
371  }
372 
379  protected function _runError()
380  {
381 
382  echo <<<EOS
383 
384 ***************************** ZF ERROR ********************************
385 In order to run the zf command, you need to ensure that Zend Framework
386 is inside your include_path. There are a variety of ways that you can
387 ensure that this zf command line tool knows where the Zend Framework
388 library is on your system, but not all of them can be described here.
389 
390 The easiest way to get the zf command running is to give it the include
391 path via an environment variable ZEND_TOOL_INCLUDE_PATH or
392 ZEND_TOOL_INCLUDE_PATH_PREPEND with the proper include path to use,
393 then run the command "zf --setup". This command is designed to create
394 a storage location for your user, as well as create the zf.ini file
395 that the zf command will consult in order to run properly on your
396 system.
397 
398 Example you would run:
399 
400 $ ZEND_TOOL_INCLUDE_PATH=/path/to/library zf --setup
401 
402 Your are encourged to read more in the link that follows.
403 
404 EOS;
405 
406  }
407 
414  protected function _runInfo()
415  {
416  echo 'Zend_Tool & CLI Setup Information' . PHP_EOL
417  . '(available via the command line "zf --info")'
418  . PHP_EOL;
419 
420  echo ' * ' . implode(PHP_EOL . ' * ', $this->_messages) . PHP_EOL;
421 
422  echo PHP_EOL;
423 
424  echo 'To change the setup of this tool, run: "zf --setup"';
425 
426  echo PHP_EOL;
427 
428  }
429 
435  protected function _runSetup()
436  {
437  $setupCommand = (isset($_SERVER['argv'][2])) ? $_SERVER['argv'][2] : null;
438 
439  switch ($setupCommand) {
440  case 'storage-directory':
441  $this->_runSetupStorageDirectory();
442  break;
443  case 'config-file':
444  $this->_runSetupConfigFile();
445  break;
446  default:
447  $this->_runSetupMoreInfo();
448  break;
449  }
450  }
451 
457  protected function _runSetupStorageDirectory()
458  {
459  $storageDirectory = $this->_detectStorageDirectory(false, false);
460 
461  if (file_exists($storageDirectory)) {
462  echo 'Directory already exists at ' . $storageDirectory . PHP_EOL
463  . 'Cannot create storage directory.';
464  return;
465  }
466 
467  mkdir($storageDirectory);
468 
469  echo 'Storage directory created at ' . $storageDirectory . PHP_EOL;
470  }
471 
477  protected function _runSetupConfigFile()
478  {
479  $configFile = $this->_detectConfigFile(false, false);
480 
481  if (file_exists($configFile)) {
482  echo 'File already exists at ' . $configFile . PHP_EOL
483  . 'Cannot write new config file.';
484  return;
485  }
486 
487  $includePath = get_include_path();
488 
489  $contents = 'php.include_path = "' . $includePath . '"';
490 
491  file_put_contents($configFile, $contents);
492 
493  $iniValues = ini_get_all();
494  if ($iniValues['include_path']['global_value'] != $iniValues['include_path']['local_value']) {
495  echo 'NOTE: the php include_path to be used with the tool has been written' . PHP_EOL
496  . 'to the config file, using ZEND_TOOL_INCLUDE_PATH (or other include_path setters)' . PHP_EOL
497  . 'is no longer necessary.' . PHP_EOL . PHP_EOL;
498  }
499 
500  echo 'Config file written to ' . $configFile . PHP_EOL;
501  }
502 
508  protected function _runSetupMoreInfo()
509  {
510  $homeDirectory = $this->_detectHomeDirectory(false, false);
511  $storageDirectory = $this->_detectStorageDirectory(false, false);
512  $configFile = $this->_detectConfigFile(false, false);
513 
514  echo <<<EOS
515 
516 ZF Command Line Tool - Setup
517 ----------------------------
518 
519 Current Paths (Existing or not):
520  Home Directory: {$homeDirectory}
521  Storage Directory: {$storageDirectory}
522  Config File: {$configFile}
523 
524 Important Environment Variables:
525  ZF_HOME
526  - the directory this tool will look for a home directory
527  - directory must exist
528  ZF_STORAGE_DIR
529  - where this tool will look for a storage directory
530  - directory must exist
531  ZF_CONFIG_FILE
532  - where this tool will look for a configuration file
533  ZF_TOOL_INCLUDE_PATH
534  - set the include_path for this tool to use this value
535  ZF_TOOL_INCLUDE_PATH_PREPEND
536  - prepend the current php.ini include_path with this value
537 
538 Search Order:
539  Home Directory:
540  - ZF_HOME, then HOME (*nix), then HOMEPATH (windows)
541  Storage Directory:
542  - ZF_STORAGE_DIR, then {home}/.zf/
543  Config File:
544  - ZF_CONFIG_FILE, then {home}/.zf.ini, then {home}/zf.ini,
545  then {storage}/zf.ini
546 
547 Commands:
548  zf --setup storage-directory
549  - setup the storage directory, directory will be created
550  zf --setup config-file
551  - create the config file with some default values
552 
553 
554 EOS;
555  }
556 
562  protected function _runTool()
563  {
564 
565  $configOptions = array();
566  if (isset($this->_configFile) && $this->_configFile) {
567  $configOptions['configOptions']['configFilepath'] = $this->_configFile;
568  }
569  if (isset($this->_storageDirectory) && $this->_storageDirectory) {
570  $configOptions['storageOptions']['directory'] = $this->_storageDirectory;
571  }
572 
573  // ensure that zf.php loads the Zend_Tool_Project features
574  $configOptions['classesToLoad'] = 'Zend_Tool_Project_Provider_Manifest';
575 
576  $console = new Zend_Tool_Framework_Client_Console($configOptions);
577  $console->dispatch();
578  }
579 
587  protected function _logMessage($message, $storeMessage = true)
588  {
589  if (!$storeMessage) {
590  return;
591  }
592 
593  $this->_messages[] = $message;
594  }
595 
596 
597 }
598 
599 if (!getenv('ZF_NO_MAIN')) {
600  ZF::main();
601 }
bootstrap()
bootstrap()
Definition: zf.php:59
$_homeDirectory
Definition: zf.php:41
Error condition
Definition: PollEvent.h:48
_detectConfigFile($mustExist=true, $returnMessages=true)
_detectConfigFile() - Detect config file location from a variety of possibilities ...
Definition: zf.php:235
$_messages
Definition: zf.php:39
_setupToolRuntime()
_setupToolRuntime() - setup the tools include_path and load the proper framwork parts that enable Zen...
Definition: zf.php:312
_runSetupMoreInfo()
_runSetupMoreInfo() - return more information about what can be setup, and what is setup ...
Definition: zf.php:508
_detectStorageDirectory($mustExist=true, $returnMessages=true)
_detectStorageDirectory() - Detect where the storage directory is from a variaty of possiblities ...
Definition: zf.php:199
_runTool()
_runTool() - This is where the magic happens, dispatch Zend_Tool
Definition: zf.php:562
_detectMode()
_detectMode()
Definition: zf.php:106
_runInfo()
_runInfo() - this command will produce information about the setup of this script and Zend_Tool ...
Definition: zf.php:414
$_storageDirectory
Definition: zf.php:43
$_mode
Definition: zf.php:37
$_clientLoaded
Definition: zf.php:35
run()
run()
Definition: zf.php:77
_detectHomeDirectory($mustExist=true, $returnMessages=true)
_detectHomeDirectory() - detect the home directory in a variety of different places ...
Definition: zf.php:141
_logMessage($message, $storeMessage=true)
_logMessage() - Internal method used to log setup and information messages.
Definition: zf.php:587
_runSetupStorageDirectory()
_runSetupStorageDirectory() - if the storage directory does not exist, create it
Definition: zf.php:457
_runSetupConfigFile()
_runSetupConfigFile()
Definition: zf.php:477
_tryClientLoad()
_tryClientLoad() - Attempt to load the Zend_Tool_Framework_Client_Console to enable the tool to run...
Definition: zf.php:357
static main()
Definition: zf.php:47
_runSetup()
_runSetup() - parse the request to see which setup command to run
Definition: zf.php:435
$_configFile
Definition: zf.php:45
_setupPHPRuntime()
_setupPHPRuntime() - parse the config file if it exists for php ini values to set ...
Definition: zf.php:280
Definition: zf.php:32
_runError()
_runError() - Output the error screen that tells the user that the tool was not setup in a sane way ...
Definition: zf.php:379