zf.php 6.2 KB

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