PageRenderTime 27ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/bin/zf.php

https://bitbucket.org/ksekar/campus
PHP | 624 lines | 367 code | 99 blank | 158 comment | 89 complexity | ce6ff0963ac77005f8e93c31971ab63a MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, MIT
  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-2012 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-2012 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 bool $mustExist Should the returned value already exist in the file system
  140. * @param bool $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. $homeDirectory = getenv('USERPROFILE');
  174. if ($homeDirectory) {
  175. $this->_logMessage('Home directory found in environment variable USERPROFILE with value ' . $homeDirectory, $returnMessages);
  176. if (!$mustExist || ($mustExist && file_exists($homeDirectory))) {
  177. return $homeDirectory;
  178. } else {
  179. $this->_logMessage('Home directory does not exist at ' . $homeDirectory, $returnMessages);
  180. }
  181. }
  182. return false;
  183. }
  184. /**
  185. * _detectStorageDirectory() - Detect where the storage directory is from a variaty of possiblities
  186. *
  187. * @param bool $mustExist Should the returned value already exist in the file system
  188. * @param bool $returnMessages Should it log messages for output later
  189. * @return string
  190. */
  191. protected function _detectStorageDirectory($mustExist = true, $returnMessages = true)
  192. {
  193. $storageDirectory = false;
  194. $storageDirectory = getenv('ZF_STORAGE_DIR');
  195. if ($storageDirectory) {
  196. $this->_logMessage('Storage directory path found in environment variable ZF_STORAGE_DIR with value ' . $storageDirectory, $returnMessages);
  197. if (!$mustExist || ($mustExist && file_exists($storageDirectory))) {
  198. return $storageDirectory;
  199. } else {
  200. $this->_logMessage('Storage directory does not exist at ' . $storageDirectory, $returnMessages);
  201. }
  202. }
  203. $homeDirectory = ($this->_homeDirectory) ? $this->_homeDirectory : $this->_detectHomeDirectory(true, false);
  204. if ($homeDirectory) {
  205. $storageDirectory = $homeDirectory . '/.zf/';
  206. $this->_logMessage('Storage directory assumed in home directory at location ' . $storageDirectory, $returnMessages);
  207. if (!$mustExist || ($mustExist && file_exists($storageDirectory))) {
  208. return $storageDirectory;
  209. } else {
  210. $this->_logMessage('Storage directory does not exist at ' . $storageDirectory, $returnMessages);
  211. }
  212. }
  213. return false;
  214. }
  215. /**
  216. * _detectConfigFile() - Detect config file location from a variety of possibilities
  217. *
  218. * @param bool $mustExist Should the returned value already exist in the file system
  219. * @param bool $returnMessages Should it log messages for output later
  220. * @return string
  221. */
  222. protected function _detectConfigFile($mustExist = true, $returnMessages = true)
  223. {
  224. $configFile = null;
  225. $configFile = getenv('ZF_CONFIG_FILE');
  226. if ($configFile) {
  227. $this->_logMessage('Config file found environment variable ZF_CONFIG_FILE at ' . $configFile, $returnMessages);
  228. if (!$mustExist || ($mustExist && file_exists($configFile))) {
  229. return $configFile;
  230. } else {
  231. $this->_logMessage('Config file does not exist at ' . $configFile, $returnMessages);
  232. }
  233. }
  234. $homeDirectory = ($this->_homeDirectory) ? $this->_homeDirectory : $this->_detectHomeDirectory(true, false);
  235. if ($homeDirectory) {
  236. $configFile = $homeDirectory . '/.zf.ini';
  237. $this->_logMessage('Config file assumed in home directory at location ' . $configFile, $returnMessages);
  238. if (!$mustExist || ($mustExist && file_exists($configFile))) {
  239. return $configFile;
  240. } else {
  241. $this->_logMessage('Config file does not exist at ' . $configFile, $returnMessages);
  242. }
  243. }
  244. $storageDirectory = ($this->_storageDirectory) ? $this->_storageDirectory : $this->_detectStorageDirectory(true, false);
  245. if ($storageDirectory) {
  246. $configFile = $storageDirectory . '/zf.ini';
  247. $this->_logMessage('Config file assumed in storage directory at location ' . $configFile, $returnMessages);
  248. if (!$mustExist || ($mustExist && file_exists($configFile))) {
  249. return $configFile;
  250. } else {
  251. $this->_logMessage('Config file does not exist at ' . $configFile, $returnMessages);
  252. }
  253. }
  254. return false;
  255. }
  256. /**
  257. * _setupPHPRuntime() - parse the config file if it exists for php ini values to set
  258. *
  259. * @return void
  260. */
  261. protected function _setupPHPRuntime()
  262. {
  263. // set php runtime settings
  264. ini_set('display_errors', true);
  265. // support the changing of the current working directory, necessary for some providers
  266. $cwd = getenv('ZEND_TOOL_CURRENT_WORKING_DIRECTORY');
  267. if ($cwd != '' && realpath($cwd)) {
  268. chdir($cwd);
  269. }
  270. if (!$this->_configFile) {
  271. return;
  272. }
  273. $zfINISettings = parse_ini_file($this->_configFile);
  274. $phpINISettings = ini_get_all();
  275. foreach ($zfINISettings as $zfINIKey => $zfINIValue) {
  276. if (substr($zfINIKey, 0, 4) === 'php.') {
  277. $phpINIKey = substr($zfINIKey, 4);
  278. if (array_key_exists($phpINIKey, $phpINISettings)) {
  279. ini_set($phpINIKey, $zfINIValue);
  280. }
  281. }
  282. }
  283. }
  284. /**
  285. * _setupToolRuntime() - setup the tools include_path and load the proper framwork parts that
  286. * enable Zend_Tool to work.
  287. *
  288. * @return void
  289. */
  290. protected function _setupToolRuntime()
  291. {
  292. $includePathPrepend = getenv('ZEND_TOOL_INCLUDE_PATH_PREPEND');
  293. $includePathFull = getenv('ZEND_TOOL_INCLUDE_PATH');
  294. // check if the user has not provided anything
  295. if (!($includePathPrepend || $includePathFull)) {
  296. if ($this->_tryClientLoad()) {
  297. return;
  298. }
  299. }
  300. // if ZF is not in the include_path, but relative to this file, put it in the include_path
  301. if ($includePathPrepend || $includePathFull) {
  302. if (isset($includePathPrepend) && ($includePathPrepend !== false)) {
  303. set_include_path($includePathPrepend . PATH_SEPARATOR . get_include_path());
  304. } elseif (isset($includePathFull) && ($includePathFull !== false)) {
  305. set_include_path($includePathFull);
  306. }
  307. }
  308. if ($this->_tryClientLoad()) {
  309. return;
  310. }
  311. $zfIncludePath['relativePath'] = dirname(__FILE__) . '/../library/';
  312. if (file_exists($zfIncludePath['relativePath'] . 'Zend/Tool/Framework/Client/Console.php')) {
  313. set_include_path(realpath($zfIncludePath['relativePath']) . PATH_SEPARATOR . get_include_path());
  314. }
  315. if (!$this->_tryClientLoad()) {
  316. $this->_mode = 'runError';
  317. return;
  318. }
  319. }
  320. /**
  321. * _tryClientLoad() - Attempt to load the Zend_Tool_Framework_Client_Console to enable the tool to run.
  322. *
  323. * This method will return false if its not loaded to allow the consumer to alter the environment in such
  324. * a way that it can be called again to try loading the proper file/class.
  325. *
  326. * @return bool if the client is actuall loaded or not
  327. */
  328. protected function _tryClientLoad()
  329. {
  330. $this->_clientLoaded = false;
  331. $fh = @fopen('Zend/Tool/Framework/Client/Console.php', 'r', true);
  332. if (!$fh) {
  333. return $this->_clientLoaded; // false
  334. } else {
  335. fclose($fh);
  336. unset($fh);
  337. include 'Zend/Tool/Framework/Client/Console.php';
  338. $this->_clientLoaded = class_exists('Zend_Tool_Framework_Client_Console');
  339. }
  340. return $this->_clientLoaded;
  341. }
  342. /**
  343. * _runError() - Output the error screen that tells the user that the tool was not setup
  344. * in a sane way
  345. *
  346. * @return void
  347. */
  348. protected function _runError()
  349. {
  350. echo <<<EOS
  351. ***************************** ZF ERROR ********************************
  352. In order to run the zf command, you need to ensure that Zend Framework
  353. is inside your include_path. There are a variety of ways that you can
  354. ensure that this zf command line tool knows where the Zend Framework
  355. library is on your system, but not all of them can be described here.
  356. The easiest way to get the zf command running is to give it the include
  357. path via an environment variable ZEND_TOOL_INCLUDE_PATH or
  358. ZEND_TOOL_INCLUDE_PATH_PREPEND with the proper include path to use,
  359. then run the command "zf --setup". This command is designed to create
  360. a storage location for your user, as well as create the zf.ini file
  361. that the zf command will consult in order to run properly on your
  362. system.
  363. Example you would run:
  364. $ ZEND_TOOL_INCLUDE_PATH=/path/to/library zf --setup
  365. Your are encourged to read more in the link that follows.
  366. EOS;
  367. }
  368. /**
  369. * _runInfo() - this command will produce information about the setup of this script and
  370. * Zend_Tool
  371. *
  372. * @return void
  373. */
  374. protected function _runInfo()
  375. {
  376. echo 'Zend_Tool & CLI Setup Information' . PHP_EOL
  377. . '(available via the command line "zf --info")'
  378. . PHP_EOL;
  379. echo ' * ' . implode(PHP_EOL . ' * ', $this->_messages) . PHP_EOL;
  380. echo PHP_EOL;
  381. echo 'To change the setup of this tool, run: "zf --setup"';
  382. echo PHP_EOL;
  383. }
  384. /**
  385. * _runSetup() - parse the request to see which setup command to run
  386. *
  387. * @return void
  388. */
  389. protected function _runSetup()
  390. {
  391. $setupCommand = (isset($_SERVER['argv'][2])) ? $_SERVER['argv'][2] : null;
  392. switch ($setupCommand) {
  393. case 'storage-directory':
  394. $this->_runSetupStorageDirectory();
  395. break;
  396. case 'config-file':
  397. $this->_runSetupConfigFile();
  398. break;
  399. default:
  400. $this->_runSetupMoreInfo();
  401. break;
  402. }
  403. }
  404. /**
  405. * _runSetupStorageDirectory() - if the storage directory does not exist, create it
  406. *
  407. * @return void
  408. */
  409. protected function _runSetupStorageDirectory()
  410. {
  411. $storageDirectory = $this->_detectStorageDirectory(false, false);
  412. if (file_exists($storageDirectory)) {
  413. echo 'Directory already exists at ' . $storageDirectory . PHP_EOL
  414. . 'Cannot create storage directory.';
  415. return;
  416. }
  417. mkdir($storageDirectory);
  418. echo 'Storage directory created at ' . $storageDirectory . PHP_EOL;
  419. }
  420. /**
  421. * _runSetupConfigFile()
  422. *
  423. * @return void
  424. */
  425. protected function _runSetupConfigFile()
  426. {
  427. $configFile = $this->_detectConfigFile(false, false);
  428. if (file_exists($configFile)) {
  429. echo 'File already exists at ' . $configFile . PHP_EOL
  430. . 'Cannot write new config file.';
  431. return;
  432. }
  433. $includePath = get_include_path();
  434. $contents = 'php.include_path = "' . $includePath . '"';
  435. file_put_contents($configFile, $contents);
  436. $iniValues = ini_get_all();
  437. if ($iniValues['include_path']['global_value'] != $iniValues['include_path']['local_value']) {
  438. echo 'NOTE: the php include_path to be used with the tool has been written' . PHP_EOL
  439. . 'to the config file, using ZEND_TOOL_INCLUDE_PATH (or other include_path setters)' . PHP_EOL
  440. . 'is no longer necessary.' . PHP_EOL . PHP_EOL;
  441. }
  442. echo 'Config file written to ' . $configFile . PHP_EOL;
  443. }
  444. /**
  445. * _runSetupMoreInfo() - return more information about what can be setup, and what is setup
  446. *
  447. * @return void
  448. */
  449. protected function _runSetupMoreInfo()
  450. {
  451. $homeDirectory = $this->_detectHomeDirectory(false, false);
  452. $storageDirectory = $this->_detectStorageDirectory(false, false);
  453. $configFile = $this->_detectConfigFile(false, false);
  454. echo <<<EOS
  455. ZF Command Line Tool - Setup
  456. ----------------------------
  457. Current Paths (Existing or not):
  458. Home Directory: {$homeDirectory}
  459. Storage Directory: {$storageDirectory}
  460. Config File: {$configFile}
  461. Important Environment Variables:
  462. ZF_HOME
  463. - the directory this tool will look for a home directory
  464. - directory must exist
  465. ZF_STORAGE_DIR
  466. - where this tool will look for a storage directory
  467. - directory must exist
  468. ZF_CONFIG_FILE
  469. - where this tool will look for a configuration file
  470. ZF_TOOL_INCLUDE_PATH
  471. - set the include_path for this tool to use this value
  472. ZF_TOOL_INCLUDE_PATH_PREPEND
  473. - prepend the current php.ini include_path with this value
  474. Search Order:
  475. Home Directory:
  476. - ZF_HOME, then HOME (*nix), then HOMEPATH (windows)
  477. Storage Directory:
  478. - ZF_STORAGE_DIR, then {home}/.zf/
  479. Config File:
  480. - ZF_CONFIG_FILE, then {home}/.zf.ini, then {home}/zf.ini,
  481. then {storage}/zf.ini
  482. Commands:
  483. zf --setup storage-directory
  484. - setup the storage directory, directory will be created
  485. zf --setup config-file
  486. - create the config file with some default values
  487. EOS;
  488. }
  489. /**
  490. * _runTool() - This is where the magic happens, dispatch Zend_Tool
  491. *
  492. * @return void
  493. */
  494. protected function _runTool()
  495. {
  496. $configOptions = array();
  497. if (isset($this->_configFile) && $this->_configFile) {
  498. $configOptions['configOptions']['configFilepath'] = $this->_configFile;
  499. }
  500. if (isset($this->_storageDirectory) && $this->_storageDirectory) {
  501. $configOptions['storageOptions']['directory'] = $this->_storageDirectory;
  502. }
  503. // ensure that zf.php loads the Zend_Tool_Project features
  504. $configOptions['classesToLoad'] = 'Zend_Tool_Project_Provider_Manifest';
  505. $console = new Zend_Tool_Framework_Client_Console($configOptions);
  506. $console->dispatch();
  507. }
  508. /**
  509. * _logMessage() - Internal method used to log setup and information messages.
  510. *
  511. * @param string $message
  512. * @param bool $storeMessage
  513. * @return void
  514. */
  515. protected function _logMessage($message, $storeMessage = true)
  516. {
  517. if (!$storeMessage) {
  518. return;
  519. }
  520. $this->_messages[] = $message;
  521. }
  522. }
  523. if (!getenv('ZF_NO_MAIN')) {
  524. ZF::main();
  525. }