zf.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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-2010 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. * ZF
  24. *
  25. * @category Zend
  26. * @package Zend_Tool
  27. * @subpackage Framework
  28. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class ZF
  32. {
  33. /**
  34. * @var bool
  35. */
  36. protected $_clientLoaded = false;
  37. /**
  38. * @var string
  39. */
  40. protected $_mode = 'runTool';
  41. /**
  42. * @var array of messages
  43. */
  44. protected $_messages = array();
  45. /**
  46. * @var string
  47. */
  48. protected $_homeDirectory = null;
  49. /**
  50. * @var string
  51. */
  52. protected $_storageDirectory = null;
  53. /**
  54. * @var string
  55. */
  56. protected $_configFile = null;
  57. /**
  58. * main()
  59. *
  60. * @return void
  61. */
  62. public static function main()
  63. {
  64. $zf = new self();
  65. $zf->bootstrap();
  66. $zf->run();
  67. }
  68. /**
  69. * bootstrap()
  70. *
  71. * @return ZF
  72. */
  73. public function bootstrap()
  74. {
  75. // detect settings
  76. $this->_mode = $this->_detectMode();
  77. $this->_homeDirectory = $this->_detectHomeDirectory();
  78. $this->_storageDirectory = $this->_detectStorageDirectory();
  79. $this->_configFile = $this->_detectConfigFile();
  80. // setup
  81. $this->_setupPHPRuntime();
  82. $this->_setupToolRuntime();
  83. }
  84. /**
  85. * run()
  86. *
  87. * @return ZF
  88. */
  89. public function run()
  90. {
  91. switch ($this->_mode) {
  92. case 'runError':
  93. $this->_runError();
  94. $this->_runInfo();
  95. break;
  96. case 'runSetup':
  97. if ($this->_runSetup() === false) {
  98. $this->_runInfo();
  99. }
  100. break;
  101. case 'runInfo':
  102. $this->_runInfo();
  103. break;
  104. case 'runTool':
  105. default:
  106. $this->_runTool();
  107. break;
  108. }
  109. return $this;
  110. }
  111. /**
  112. * _detectMode()
  113. *
  114. * @return ZF
  115. */
  116. protected function _detectMode()
  117. {
  118. $arguments = $_SERVER['argv'];
  119. $mode = 'runTool';
  120. if (!isset($arguments[0])) {
  121. return $mode;
  122. }
  123. if ($arguments[0] == $_SERVER['PHP_SELF']) {
  124. $this->_executable = array_shift($arguments);
  125. }
  126. if (!isset($arguments[0])) {
  127. return $mode;
  128. }
  129. if ($arguments[0] == '--setup') {
  130. $mode = 'runSetup';
  131. } elseif ($arguments[0] == '--info') {
  132. $mode = 'runInfo';
  133. }
  134. return $mode;
  135. }
  136. /**
  137. * _detectHomeDirectory() - detect the home directory in a variety of different places
  138. *
  139. * @param $mustExist Should the returned value already exist in the file system
  140. * @param $returnMessages Should it log messages for output later
  141. * @return string
  142. */
  143. protected function _detectHomeDirectory($mustExist = true, $returnMessages = true)
  144. {
  145. $homeDirectory = null;
  146. $homeDirectory = getenv('ZF_HOME'); // check env var ZF_HOME
  147. if ($homeDirectory) {
  148. $this->_logMessage('Home directory found in environment variable ZF_HOME with value ' . $homeDirectory, $returnMessages);
  149. if (!$mustExist || ($mustExist && file_exists($homeDirectory))) {
  150. return $homeDirectory;
  151. } else {
  152. $this->_logMessage('Home directory does not exist at ' . $homeDirectory, $returnMessages);
  153. }
  154. }
  155. $homeDirectory = getenv('HOME'); // HOME environment variable
  156. if ($homeDirectory) {
  157. $this->_logMessage('Home directory found in environment variable HOME with value ' . $homeDirectory, $returnMessages);
  158. if (!$mustExist || ($mustExist && file_exists($homeDirectory))) {
  159. return $homeDirectory;
  160. } else {
  161. $this->_logMessage('Home directory does not exist at ' . $homeDirectory, $returnMessages);
  162. }
  163. }
  164. $homeDirectory = getenv('HOMEPATH');
  165. if ($homeDirectory) {
  166. $this->_logMessage('Home directory found in environment variable HOMEPATH with value ' . $homeDirectory, $returnMessages);
  167. if (!$mustExist || ($mustExist && file_exists($homeDirectory))) {
  168. return $homeDirectory;
  169. } else {
  170. $this->_logMessage('Home directory does not exist at ' . $homeDirectory, $returnMessages);
  171. }
  172. }
  173. return false;
  174. }
  175. /**
  176. * _detectStorageDirectory() - Detect where the storage directory is from a variaty of possiblities
  177. *
  178. * @param $mustExist Should the returned value already exist in the file system
  179. * @param $returnMessages Should it log messages for output later
  180. * @return string
  181. */
  182. protected function _detectStorageDirectory($mustExist = true, $returnMessages = true)
  183. {
  184. $storageDirectory = false;
  185. $storageDirectory = getenv('ZF_STORAGE_DIR');
  186. if ($storageDirectory) {
  187. $this->_logMessage('Storage directory path found in environment variable ZF_STORAGE_DIR with value ' . $storageDirectory, $returnMessages);
  188. if (!$mustExist || ($mustExist && file_exists($storageDirectory))) {
  189. return $storageDirectory;
  190. } else {
  191. $this->_logMessage('Storage directory does not exist at ' . $storageDirectory, $returnMessages);
  192. }
  193. }
  194. $homeDirectory = ($this->_homeDirectory) ? $this->_homeDirectory : $this->_detectHomeDirectory(true, false);
  195. if ($homeDirectory) {
  196. $storageDirectory = $homeDirectory . '/.zf/';
  197. $this->_logMessage('Storage directory assumed in home directory at location ' . $storageDirectory, $returnMessages);
  198. if (!$mustExist || ($mustExist && file_exists($storageDirectory))) {
  199. return $storageDirectory;
  200. } else {
  201. $this->_logMessage('Storage directory does not exist at ' . $storageDirectory, $returnMessages);
  202. }
  203. }
  204. return false;
  205. }
  206. /**
  207. * _detectConfigFile() - Detect config file location from a variety of possibilities
  208. *
  209. * @param $mustExist Should the returned value already exist in the file system
  210. * @param $returnMessages Should it log messages for output later
  211. * @return string
  212. */
  213. protected function _detectConfigFile($mustExist = true, $returnMessages = true)
  214. {
  215. $configFile = null;
  216. $configFile = getenv('ZF_CONFIG_FILE');
  217. if ($configFile) {
  218. $this->_logMessage('Config file found environment variable ZF_CONFIG_FILE at ' . $configFile, $returnMessages);
  219. if (!$mustExist || ($mustExist && file_exists($configFile))) {
  220. return $configFile;
  221. } else {
  222. $this->_logMessage('Config file does not exist at ' . $configFile, $returnMessages);
  223. }
  224. }
  225. $homeDirectory = ($this->_homeDirectory) ? $this->_homeDirectory : $this->_detectHomeDirectory(true, false);
  226. if ($homeDirectory) {
  227. $configFile = $homeDirectory . '/.zf.ini';
  228. $this->_logMessage('Config file assumed in home directory at location ' . $configFile, $returnMessages);
  229. if (!$mustExist || ($mustExist && file_exists($configFile))) {
  230. return $configFile;
  231. } else {
  232. $this->_logMessage('Config file does not exist at ' . $configFile, $returnMessages);
  233. }
  234. }
  235. $storageDirectory = ($this->_storageDirectory) ? $this->_storageDirectory : $this->_detectStorageDirectory(true, false);
  236. if ($storageDirectory) {
  237. $configFile = $storageDirectory . '/zf.ini';
  238. $this->_logMessage('Config file assumed in storage directory at location ' . $configFile, $returnMessages);
  239. if (!$mustExist || ($mustExist && file_exists($configFile))) {
  240. return $configFile;
  241. } else {
  242. $this->_logMessage('Config file does not exist at ' . $configFile, $returnMessages);
  243. }
  244. }
  245. return false;
  246. }
  247. /**
  248. * _setupPHPRuntime() - parse the config file if it exists for php ini values to set
  249. *
  250. * @return void
  251. */
  252. protected function _setupPHPRuntime()
  253. {
  254. // set php runtime settings
  255. ini_set('display_errors', true);
  256. // support the changing of the current working directory, necessary for some providers
  257. if (isset($_ENV['ZEND_TOOL_CURRENT_WORKING_DIRECTORY'])) {
  258. chdir($_ENV['ZEND_TOOL_CURRENT_WORKING_DIRECTORY']);
  259. }
  260. if (!$this->_configFile) {
  261. return;
  262. }
  263. $zfINISettings = parse_ini_file($this->_configFile);
  264. $phpINISettings = ini_get_all();
  265. foreach ($zfINISettings as $zfINIKey => $zfINIValue) {
  266. if (substr($zfINIKey, 0, 4) === 'php.') {
  267. $phpINIKey = substr($zfINIKey, 4);
  268. if (array_key_exists($phpINIKey, $phpINISettings)) {
  269. ini_set($phpINIKey, $zfINIValue);
  270. }
  271. }
  272. }
  273. return null;
  274. }
  275. /**
  276. * _setupToolRuntime() - setup the tools include_path and load the proper framwork parts that
  277. * enable Zend_Tool to work.
  278. *
  279. * @return void
  280. */
  281. protected function _setupToolRuntime()
  282. {
  283. // last ditch efforts
  284. if ($this->_tryClientLoad()) {
  285. return;
  286. }
  287. // if ZF is not in the include_path, but relative to this file, put it in the include_path
  288. if (($includePathPrepend = getenv('ZEND_TOOL_INCLUDE_PATH_PREPEND')) || ($includePathFull = getenv('ZEND_TOOL_INCLUDE_PATH'))) {
  289. if (isset($includePathPrepend) && ($includePathPrepend !== false)) {
  290. set_include_path($includePathPrepend . PATH_SEPARATOR . get_include_path());
  291. } elseif (isset($includePathFull) && ($includePathFull !== false)) {
  292. set_include_path($includePathFull);
  293. }
  294. }
  295. if ($this->_tryClientLoad()) {
  296. return;
  297. }
  298. $zfIncludePath['relativePath'] = dirname(__FILE__) . '/../library/';
  299. if (file_exists($zfIncludePath['relativePath'] . 'Zend/Tool/Framework/Client/Console.php')) {
  300. set_include_path(realpath($zfIncludePath['relativePath']) . PATH_SEPARATOR . get_include_path());
  301. }
  302. if (!$this->_tryClientLoad()) {
  303. $this->_mode = 'runError';
  304. return;
  305. }
  306. return null;
  307. }
  308. /**
  309. * _tryClientLoad() - Attempt to load the Zend_Tool_Framework_Client_Console to enable the tool to run.
  310. *
  311. * This method will return false if its not loaded to allow the consumer to alter the environment in such
  312. * a way that it can be called again to try loading the proper file/class.
  313. *
  314. * @return bool if the client is actuall loaded or not
  315. */
  316. protected function _tryClientLoad()
  317. {
  318. $this->_clientLoaded = false;
  319. $fh = @fopen('Zend/Tool/Framework/Client/Console.php', 'r', true);
  320. if (!$fh) {
  321. return $this->_clientLoaded; // false
  322. } else {
  323. fclose($fh);
  324. unset($fh);
  325. include 'Zend/Tool/Framework/Client/Console.php';
  326. $this->_clientLoaded = class_exists('Zend_Tool_Framework_Client_Console');
  327. }
  328. return $this->_clientLoaded;
  329. }
  330. /**
  331. * _runError() - Output the error screen that tells the user that the tool was not setup
  332. * in a sane way
  333. *
  334. * @return void
  335. */
  336. protected function _runError()
  337. {
  338. echo <<<EOS
  339. ***************************** ZF ERROR ********************************
  340. In order to run the zf command, you need to ensure that Zend Framework
  341. is inside your include_path. There are a variety of ways that you can
  342. ensure that this zf command line tool knows where the Zend Framework
  343. library is on your system, but not all of them can be described here.
  344. The easiest way to get the zf command running is to allow is to give it
  345. the include path via an environment variable ZEND_TOOL_INCLUDE_PATH or
  346. ZEND_TOOL_INCLUDE_PATH_PREPEND with the proper include path to use,
  347. then run the command "zf --setup". This command is designed to create
  348. a storage location for your user, as well as create the zf.ini file
  349. that the zf command will consult in order to run properly on your
  350. system.
  351. Example you would run:
  352. $ ZEND_TOOL_INCLUDE_PATH=/path/to/library zf --setup
  353. Your are encourged to read more in the link that follows.
  354. EOS;
  355. return null;
  356. }
  357. /**
  358. * _runInfo() - this command will produce information about the setup of this script and
  359. * Zend_Tool
  360. *
  361. * @return void
  362. */
  363. protected function _runInfo()
  364. {
  365. echo 'Zend_Tool & CLI Setup Information' . PHP_EOL
  366. . '(available via the command line "zf --info")'
  367. . PHP_EOL;
  368. echo ' * ' . implode(PHP_EOL . ' * ', $this->_messages) . PHP_EOL;
  369. echo PHP_EOL;
  370. echo 'To change the setup of this tool, run: "zf --setup"';
  371. echo PHP_EOL;
  372. }
  373. /**
  374. * _runSetup() - parse the request to see which setup command to run
  375. *
  376. * @return void
  377. */
  378. protected function _runSetup()
  379. {
  380. $setupCommand = (isset($_SERVER['argv'][2])) ? $_SERVER['argv'][2] : null;
  381. switch ($setupCommand) {
  382. case 'storage-directory':
  383. $this->_runSetupStorageDirectory();
  384. break;
  385. case 'config-file':
  386. $this->_runSetupConfigFile();
  387. break;
  388. default:
  389. $this->_runSetupMoreInfo();
  390. break;
  391. }
  392. return null;
  393. }
  394. /**
  395. * _runSetupStorageDirectory() - if the storage directory does not exist, create it
  396. *
  397. * @return void
  398. */
  399. protected function _runSetupStorageDirectory()
  400. {
  401. $storageDirectory = $this->_detectStorageDirectory(false, false);
  402. if (file_exists($storageDirectory)) {
  403. echo 'Directory already exists at ' . $storageDirectory . PHP_EOL
  404. . 'Cannot create storage directory.';
  405. return;
  406. }
  407. mkdir($storageDirectory);
  408. echo 'Storage directory created at ' . $storageDirectory . PHP_EOL;
  409. }
  410. /**
  411. * _runSetupConfigFile()
  412. *
  413. * @return void
  414. */
  415. protected function _runSetupConfigFile()
  416. {
  417. $configFile = $this->_detectConfigFile(false, false);
  418. if (file_exists($configFile)) {
  419. echo 'File already exists at ' . $configFile . PHP_EOL
  420. . 'Cannot write new config file.';
  421. return;
  422. }
  423. $includePath = get_include_path();
  424. $contents = 'php.include_path = "' . $includePath . '"';
  425. file_put_contents($configFile, $contents);
  426. $iniValues = ini_get_all();
  427. if ($iniValues['include_path']['global_value'] != $iniValues['include_path']['local_value']) {
  428. echo 'NOTE: the php include_path to be used with the tool has been written' . PHP_EOL
  429. . 'to the config file, using ZF_INCLUDE_PATH (or other include_path setters)' . PHP_EOL
  430. . 'is no longer necessary.' . PHP_EOL . PHP_EOL;
  431. }
  432. echo 'Config file written to ' . $configFile . PHP_EOL;
  433. return null;
  434. }
  435. /**
  436. * _runSetupMoreInfo() - return more information about what can be setup, and what is setup
  437. *
  438. * @return void
  439. */
  440. protected function _runSetupMoreInfo()
  441. {
  442. $homeDirectory = $this->_detectHomeDirectory(false, false);
  443. $storageDirectory = $this->_detectStorageDirectory(false, false);
  444. $configFile = $this->_detectConfigFile(false, false);
  445. echo <<<EOS
  446. ZF Command Line Tool - Setup
  447. ----------------------------
  448. Current Paths (Existing or not):
  449. Home Directory: {$homeDirectory}
  450. Storage Directory: {$storageDirectory}
  451. Config File: {$configFile}
  452. Important Environment Variables:
  453. ZF_HOME
  454. - the directory this tool will look for a home directory
  455. - directory must exist
  456. ZF_STORAGE_DIRECTORY
  457. - where this tool will look for a storage directory
  458. - directory must exist
  459. ZF_CONFIG_FILE
  460. - where this tool will look for a configuration file
  461. ZF_INCLUDE_PATH
  462. - set the include_path for this tool to use this value
  463. ZF_INCLUDE_PATH_PREPEND
  464. - prepend the current php.ini include_path with this value
  465. Search Order:
  466. Home Directory:
  467. - ZF_HOME, then HOME (*nix), then HOMEPATH (windows)
  468. Storage Directory:
  469. - ZF_STORAGE_DIR, then {home}/.zf/
  470. Config File:
  471. - ZF_CONFIG_FILE, then {home}/.zf.ini, then {home}/zf.ini,
  472. then {storage}/zf.ini
  473. Commands:
  474. zf --setup storage-directory
  475. - setup the storage directory, directory will be created
  476. zf --setup config-file
  477. - create the config file with some default values
  478. EOS;
  479. }
  480. /**
  481. * _runTool() - This is where the magic happens, dispatch Zend_Tool
  482. *
  483. * @return void
  484. */
  485. protected function _runTool()
  486. {
  487. $configOptions = array();
  488. if (isset($this->_configFile) && $this->_configFile) {
  489. $configOptions['configOptions']['configFilepath'] = $this->_configFile;
  490. }
  491. if (isset($this->_storageDirectory) && $this->_storageDirectory) {
  492. $configOptions['storageOptions']['directory'] = $this->_storageDirectory;
  493. }
  494. // ensure that zf.php loads the Zend_Tool_Project features
  495. $configOptions['classesToLoad'] = 'Zend_Tool_Project_Provider_Manifest';
  496. $console = new Zend_Tool_Framework_Client_Console($configOptions);
  497. $console->dispatch();
  498. return null;
  499. }
  500. /**
  501. * _logMessage() - Internal method used to log setup and information messages.
  502. *
  503. * @param $message
  504. * @param $storeMessage
  505. * @return void
  506. */
  507. protected function _logMessage($message, $storeMessage = true)
  508. {
  509. if (!$storeMessage) {
  510. return;
  511. }
  512. $this->_messages[] = $message;
  513. }
  514. }
  515. if (!getenv('ZF_NO_MAIN')) {
  516. ZF::main();
  517. }