zf.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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-2009 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. zf_main();
  23. /**
  24. * zf_main() - The main() function to run
  25. */
  26. function zf_main() {
  27. global $_zf;
  28. $_zf = array();
  29. zf_setup_home_directory();
  30. zf_setup_storage_directory();
  31. zf_setup_config_file();
  32. zf_setup_php_runtime();
  33. zf_setup_tool_runtime();
  34. zf_run($_zf);
  35. }
  36. function zf_setup_home_directory() {
  37. global $_zf;
  38. // check for explicity set ENV var ZF_HOME
  39. if (($zfHome = getenv('ZF_HOME')) && file_exists($zfHome)) {
  40. $_zf['HOME'] = $_ENV['ZF_HOME'];
  41. } elseif (($home = getenv('HOME'))) {
  42. $_zf['HOME'] = $home;
  43. } elseif (($home = getenv('HOMEPATH'))) {
  44. $_zf['HOME'] = $home;
  45. }
  46. $homeRealpath = realpath($_zf['HOME']);
  47. if ($homeRealpath) {
  48. $_zf['HOME'] = $homeRealpath;
  49. } else {
  50. unset($_zf['HOME']);
  51. }
  52. }
  53. function zf_setup_storage_directory() {
  54. global $_zf;
  55. if (($zfStorage = getenv('ZF_STORAGE_DIR')) && file_exists($zfStorage)) {
  56. $_zf['STORAGE_DIR'] = $zfStorage;
  57. } elseif (isset($_zf['HOME']) && file_exists($_zf['HOME'] . '/.zf/')) {
  58. $_zf['STORAGE_DIR'] = $_ENV['HOME'] . '/.zf/';
  59. }
  60. $storageRealpath = realpath($_zf['STORAGE_DIR']);
  61. if ($storageRealpath) {
  62. $_zf['STORAGE_DIR'] = $storageRealpath;
  63. } else {
  64. unset($_zf['STORAGE_DIR']);
  65. }
  66. }
  67. function zf_setup_config_file() {
  68. global $_zf;
  69. if (isset($_ENV['ZF_CONFIG_FILE']) && file_exists($_ENV['ZF_CONFIG_FILE'])) {
  70. $_zf['CONFIG_FILE'] = $_ENV['ZF_CONFIG_FILE'];
  71. } elseif (isset($_zf['HOME'])) {
  72. if (file_exists($_zf['HOME'] . '/.zf.ini')) {
  73. $_zf['CONFIG_FILE'] = $_zf['HOME'] . '/.zf.ini';
  74. } elseif (file_exists($_zf['HOME'] . '/zf.ini')) {
  75. $_zf['CONFIG_FILE'] = $_zf['HOME'] . '/zf.ini';
  76. }
  77. }
  78. if (isset($_zf['CONFIG_FILE']) && ($zrealpath = realpath($_zf['CONFIG_FILE']))) {
  79. $_zf['CONFIG_FILE'] = $zrealpath;
  80. $zsuffix = substr($_zf['CONFIG_FILE'], -4);
  81. if ($zsuffix === '.ini') {
  82. $_zf['CONFIG_TYPE'] = 'ini';
  83. } else {
  84. unset($_zf['CONFIG_FILE']);
  85. }
  86. }
  87. }
  88. function zf_setup_php_runtime() {
  89. global $_zf;
  90. if (!isset($_zf['CONFIG_TYPE']) || $_zf['CONFIG_TYPE'] !== 'ini') {
  91. return;
  92. }
  93. $zfini_settings = parse_ini_file($_zf['CONFIG_FILE']);
  94. $phpini_settings = ini_get_all();
  95. foreach ($zfini_settings as $zfini_key => $zfini_value) {
  96. if (substr($zfini_key, 0, 4) === 'php.') {
  97. $phpini_key = substr($zfini_key, 4);
  98. if (array_key_exists($phpini_key, $phpini_settings)) {
  99. ini_set($phpini_key, $zfini_value);
  100. }
  101. }
  102. }
  103. }
  104. function zf_setup_tool_runtime() {
  105. //global $_zf;
  106. // last ditch efforts
  107. if (zf_try_client_load()) {
  108. return;
  109. }
  110. $zfIncludePath['original'] = get_include_path();
  111. // if ZF is not in the include_path, but relative to this file, put it in the include_path
  112. if (($zfIncludePath['prepend'] = getenv('ZEND_TOOL_INCLUDE_PATH_PREPEND')) || ($zfIncludePath['whole'] = getenv('ZEND_TOOL_INCLUDE_PATH'))) {
  113. if (isset($zfIncludePath['prepend']) && ($zfIncludePath['prepend'] !== false) && (($zfIncludePath['prependRealpath'] = realpath($$zfIncludePath['prepend'])) !== false)) {
  114. set_include_path($zfIncludePath['prependRealpath'] . PATH_SEPARATOR . $zfIncludePath['original']);
  115. } elseif (isset($zfIncludePath['whole']) && ($zfIncludePath['whole'] !== false) && (($zfIncludePath['wholeRealpath'] = realpath($zfIncludePath['whole'])) !== false)) {
  116. set_include_path($zfIncludePath['wholeRealpath']);
  117. }
  118. }
  119. if (zf_try_client_load()) {
  120. return;
  121. }
  122. $zfIncludePath['relativePath'] = dirname(__FILE__) . '/../library/';
  123. if (file_exists($zfIncludePath['relativePath'] . 'Zend/Tool/Framework/Client/Console.php')) {
  124. set_include_path(realpath($zfIncludePath['relativePath']) . PATH_SEPARATOR . get_include_path());
  125. }
  126. if (!zf_try_client_load()) {
  127. zf_display_error();
  128. exit(1);
  129. }
  130. }
  131. function zf_try_client_load() {
  132. $loaded = @include_once 'Zend/Tool/Framework/Client/Console.php';
  133. return $loaded;
  134. }
  135. /**
  136. * zf_display_error()
  137. */
  138. function zf_display_error() {
  139. echo <<<EOS
  140. ***************************** ZF ERROR ********************************
  141. In order to run the zf command, you need to ensure that Zend Framework
  142. is inside your include_path. If you are running this tool without
  143. ZendFramework in your include_path, you can alternatively set one of
  144. two environment variables to for this tool to work:
  145. a) ZEND_TOOL_INCLUDE_PATH_PREPEND="/path/to/ZendFramework/library"
  146. OR alternatively
  147. b) ZEND_TOOL_INCLUDE_PATH="/path/to/ZendFramework/library"
  148. The former (a) will make the specified Zend Framework first in the
  149. include_path whereas the latter (b) will replace the include_path
  150. with the specified path.
  151. Information:
  152. EOS;
  153. echo ' attempted include_path: ' . get_include_path() . PHP_EOL;
  154. echo ' script location: ' . $_SERVER['SCRIPT_NAME'] . PHP_EOL;
  155. }
  156. function zf_run($zfConfig) {
  157. global $_zf;
  158. unset($_zf);
  159. $configOptions = array();
  160. if (isset($zfConfig['CONFIG_FILE'])) {
  161. $configOptions['configFilepath'] = $zfConfig['CONFIG_FILE'];
  162. }
  163. if (isset($zfConfig['CONFIG_FILE'])) {
  164. $configOptions['storageDirectory'] = $zfConfig['STORAGE_DIR'];
  165. }
  166. Zend_Tool_Framework_Client_Console::main($configOptions);
  167. }