PHP_CompatInfo
[ class tree: PHP_CompatInfo ] [ index: PHP_CompatInfo ] [ all elements ]

Source for file Cli.php

Documentation is available at Cli.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 3.0 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available through the world-wide-web at the following url: |
  11. // | http://www.php.net/license/3_0.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | license@php.net so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Davey Shafik <davey@php.net> |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: fsource_PHP_CompatInfo__CompatInfoCli.php.html,v 1.9 2005/03/06 01:03:35 davey Exp $
  20.  
  21.  
  22.  
  23. /**
  24. * CLI Script to Check Compatibility of chunk of PHP code
  25. * @package PHP_CompatInfo
  26. * @category PHP
  27. */
  28.  
  29. require_once 'PHP/CompatInfo.php';
  30.  
  31. /**
  32. * CLI Script to Check Compatibility of chunk of PHP code
  33. *
  34. * <code>
  35. * <?php
  36. * require_once 'PHP/CompatInfo/Cli.php';
  37. * $cli = new PHP_CompatInfo_Cli;
  38. * $cli->run();
  39. * ?>
  40. * </code>
  41. *
  42. * @package PHP_CompatInfo
  43. * @author Davey Shafik <davey@php.net>
  44. * @copyright Copyright 2003 Davey Shafik and Synaptic Media. All Rights Reserved.
  45. * @example docs/examples/Cli.php Example of using PHP_CompatInfo_Cli
  46. */
  47.  
  48. class PHP_CompatInfo_Cli extends PHP_CompatInfo {
  49.  
  50. /**
  51. * @var array Current CLI Flags
  52. */
  53.  
  54. var $opts = array();
  55.  
  56. /**
  57. * @var boolean Whether an error has occured
  58. */
  59.  
  60. var $error = false;
  61.  
  62. /**
  63. * @var string File to be Processed
  64. */
  65.  
  66. var $file;
  67.  
  68. /**
  69. * @var string Directory to be Processed
  70. */
  71.  
  72. var $dir;
  73.  
  74. /**
  75. * @var boolean Whether to show debug output
  76. */
  77.  
  78. var $debug;
  79.  
  80. /**
  81. * @var boolean Whether to recurse directories when using --dir or -d
  82. */
  83.  
  84. var $recurse = true;
  85.  
  86. /**
  87. * Constructor
  88. */
  89.  
  90. function __construct() {
  91. require_once 'Console/Getopt.php';
  92. $opts = Console_Getopt::readPHPArgv();
  93. $short_opts = 'd:f:hn';
  94. $long_opts = array('dir=','file=','help','debug','no-recurse');
  95. $this->opts = Console_Getopt::getopt($opts,$short_opts,$long_opts);
  96. require_once 'PEAR.php';
  97. if (PEAR::isError($this->opts)) {
  98. $this->error = true;
  99. return;
  100. }
  101. foreach ($this->opts[0] as $option) {
  102. switch ($option[0]) {
  103. case '--no-recurse':
  104. case 'n':
  105. $this->recurse = false;
  106. break;
  107. case '--debug':
  108. $this->debug = true;
  109. break;
  110. case '--dir':
  111. $this->dir = $option[1];
  112. if($this->dir{strlen($this->dir)-1} == '/' || $this->dir{strlen($this->dir)-1} == '\\') {
  113. $this->dir = substr($this->dir,0,-1);
  114. }
  115. $this->dir = str_replace('\\','/',realpath($this->dir));
  116. break;
  117. case 'd':
  118. if ($option[1]{0} == '=') {
  119. $this->dir = substr($option[1],1);
  120. } else {
  121. $this->dir = $option[1];
  122. }
  123. if ($this->dir{strlen($this->dir)-1} == '/' || $this->dir{strlen($this->dir)-1} == '\\') {
  124. $this->dir = substr($this->dir,0,-1);
  125. }
  126. $this->dir = str_replace('\\','/',realpath($this->dir));
  127. break;
  128. case '--file':
  129. $this->file = $option[1];
  130. break;
  131. case 'f':
  132. if ($option[1]{0} == '=') {
  133. $this->file = substr($option[1],1);
  134. } else {
  135. $this->file = $option[1];
  136. }
  137. break;
  138. case 'h':
  139. case '--help':
  140. $this->_printHelp();
  141. break;
  142. }
  143. }
  144. }
  145.  
  146. /**
  147. * PHP4 Compatible Constructor
  148. */
  149.  
  150. function PHP_CompatInfo_Cli() {
  151. $this->__construct();
  152. }
  153.  
  154. /**
  155. * Run the CLI Script
  156. *
  157. * @access public
  158. * @return void
  159. */
  160.  
  161. function run() {
  162. if ($this->error == true) {
  163. echo $this->opts->message;
  164. $this->_printUsage();
  165. } else {
  166. if (isset($this->dir)) {
  167. $output = $this->_parseDir();
  168. echo $output;
  169. } elseif (isset($this->file)) {
  170. $output = $this->_parseFile();
  171. echo $output;
  172. } else {
  173. $this->_printHelp();
  174. }
  175. }
  176. }
  177.  
  178. /**
  179. * Parse Directory Input
  180. *
  181. * @access private
  182. * @return boolean|stringReturns Boolean False on fail
  183. */
  184.  
  185. function _parseDir() {
  186. require_once 'Console/Table.php';
  187. $info = $this->parseDir($this->dir,array('debug' => $this->debug,'recurse_dir' => $this->recurse));
  188. if ($info == false) {
  189. echo 'Failed opening directory ("' .$this->dir. '"). Please check your spelling and try again.';
  190. $this->_printUsage();
  191. return;
  192. }
  193. $table = new Console_Table();
  194. $table->setHeaders(array('File','Version','Extensions','Constants/Tokens'));
  195. if (!isset($info['extensions'][0])) {
  196. $ext = '';
  197. } else {
  198. $ext = array_shift($info['extensions']);
  199. }
  200.  
  201. if (!isset($info['constants'][0])) {
  202. $const = '';
  203. } else {
  204. $const = array_shift($info['constants']);
  205. }
  206. $dir = str_replace(array('\\','/'),DIRECTORY_SEPARATOR,$this->dir);
  207. $table->addRow(array($dir.DIRECTORY_SEPARATOR. '*',$info['version'],$ext,$const));
  208. if (sizeof($info['extensions']) >= sizeof($info['constants'])) {
  209. foreach ($info['extensions'] as $i => $ext) {
  210. if (isset($info['constants'][$i])) {
  211. $const = $info['constants'][$i];
  212. } else {
  213. $const = '';
  214. }
  215. $table->addRow(array('','',$ext,$const));
  216. }
  217. } else {
  218. foreach ($info['constants'] as $i => $const) {
  219. if (isset($info['extensions'][$i])) {
  220. $ext = $info['extensions'][$i];
  221. } else {
  222. $ext = '';
  223. }
  224. $table->addRow(array('','',$ext,$const));
  225. }
  226. }
  227. unset($info['version']);
  228. unset($info['extensions']);
  229. unset($info['constants']);
  230.  
  231. $ignored = $info['ignored_files'];
  232.  
  233. unset($info['ignored_files']);
  234.  
  235. foreach ($info as $file => $info) {
  236. if (!isset($info['extensions'][0])) {
  237. $ext = '';
  238. } else {
  239. $ext = array_shift($info['extensions']);
  240. }
  241.  
  242. if (!isset($info['constants'][0])) {
  243. $const = '';
  244. } else {
  245. $const = array_shift($info['constants']);
  246. }
  247. $key = str_replace(array('\\','/'),DIRECTORY_SEPARATOR,$file);
  248. $table->addRow(array($file,$info['version'],$ext,$const));
  249. if (sizeof($info['extensions']) >= sizeof($info['constants'])) {
  250. foreach ($info['extensions'] as $i => $ext) {
  251. if (isset($info['constants'][$i])) {
  252. $const = $info['constants'][$i];
  253. } else {
  254. $const = '';
  255. }
  256. $table->addRow(array('','',$ext,$const));
  257. }
  258. } else {
  259. foreach ($info['constants'] as $i => $const) {
  260. if (isset($info['extensions'][$i])) {
  261. $ext = $info['extensions'][$i];
  262. } else {
  263. $ext = '';
  264. }
  265. $table->addRow(array('','',$ext,$const));
  266. }
  267. }
  268. }
  269.  
  270. return $table->getTable();
  271. }
  272.  
  273. /**
  274. * Parse File Input
  275. *
  276. * @access private
  277. * @return boolean|stringReturns Boolean False on fail
  278. */
  279.  
  280. function _parseFile() {
  281. require_once 'Console/Table.php';
  282. $info = $this->parseFile($this->file,array('debug' => $this->debug));
  283. if ($info == false) {
  284. echo 'Failed opening file. Please check your spelling and try again.';
  285. $this->_printUsage();
  286. return false;
  287. }
  288. $table = new Console_Table();
  289. if ($this->debug == false) {
  290. $table->setHeaders(array('File','Version','Extensions','Constants/Tokens'));
  291. if (!isset($info['extensions'][0])) {
  292. $ext = '';
  293. } else {
  294. $ext = array_shift($info['extensions']);
  295. }
  296.  
  297. if (!isset($info['constants'][0])) {
  298. $const = '';
  299. } else {
  300. $const = array_shift($info['constants']);
  301. }
  302.  
  303. $table->addRow(array($this->file,$info['version'],$ext,$const));
  304. if (sizeof($info['extensions']) >= sizeof($info['constants'])) {
  305. foreach ($info['extensions'] as $i => $ext) {
  306. if (isset($info['constants'][$i])) {
  307. $const = $info['constants'][$i];
  308. } else {
  309. $const = '';
  310. }
  311. $table->addRow(array('','',$ext,$const));
  312. }
  313. } else {
  314. foreach ($info['constants'] as $i => $const) {
  315. if (isset($info['extensions'][$i])) {
  316. $ext = $info['extensions'][$i];
  317. } else {
  318. $ext = '';
  319. }
  320. $table->addRow(array('','',$ext,$const));
  321. }
  322. }
  323. } else {
  324. $table->setHeaders(array('File','Version','Extensions','Constants/Tokens'));
  325.  
  326. if (!isset($info['extensions'][0])) {
  327. $ext = '';
  328. } else {
  329. $ext = array_shift($info['extensions']);
  330. }
  331.  
  332. if (!isset($info['constants'][0])) {
  333. $const = '';
  334. } else {
  335. $const = array_shift($info['constants']);
  336. }
  337.  
  338. $table->addRow(array($this->file,$info['version'],$ext,$const));
  339.  
  340. if (sizeof($info['extensions']) >= sizeof($info['constants'])) {
  341. foreach ($info['extensions'] as $i => $ext) {
  342. if (isset($info['constants'][$i])) {
  343. $const = $info['constants'][$i];
  344. } else {
  345. $const = '';
  346. }
  347. $table->addRow(array('','',$ext,$const));
  348. }
  349. } else {
  350. foreach ($info['constants'] as $i => $const) {
  351. if (isset($info['extensions'][$i])) {
  352. $ext = $info['extensions'][$i];
  353. } else {
  354. $ext = '';
  355. }
  356. $table->addRow(array('','',$ext,$const));
  357. }
  358. }
  359. }
  360.  
  361. $output = $table->getTable();
  362.  
  363. if ($this->debug == true) {
  364. $output .= "\nDebug:\n\n";
  365.  
  366. $table = new Console_Table();
  367.  
  368. $table->setHeaders(array('Version','Function','Extension'));
  369.  
  370. unset($info['version']);
  371. unset($info['constants']);
  372. unset($info['extensions']);
  373.  
  374. foreach ($info as $version => $functions) {
  375. foreach ($functions as $func) {
  376. $table->addRow(array($version,$func['function'],$func['extension']));
  377. }
  378. }
  379.  
  380. $output .= $table->getTable();
  381. }
  382.  
  383. return $output;
  384. }
  385.  
  386. /**
  387. * Show basic Usage
  388. *
  389. * @access private
  390. * @return void
  391. */
  392. function _printUsage() {
  393. echo "\n";
  394. echo 'Usage:' . "\n";
  395. echo " " .basename(__FILE__). ' --dir=DIR [--no-recurse] | --file=FILE [--debug] | [--help]';
  396. echo "\n";
  397. }
  398. /**
  399. * Show full help information
  400. *
  401. * @access private
  402. * @return void
  403. */
  404.  
  405. function _printHelp() {
  406. $this->_printUsage();
  407. echo "Commands:\n";
  408. echo " --file=FILE (-f) \tParse FILE to get its Compatibility Info";
  409. echo "\n";
  410. echo " --dir=DIR (-d) \tParse DIR to get its Compatibility Info";
  411. echo "\n";
  412. echo " --no-recurse (-n) \tDo not Recursively parse files when using --dir";
  413. echo "\n";
  414. echo " --debug\t\tDisplay Extra (debug) Information when using --file";
  415. echo "\n";
  416. echo " --help (-h) \t\tShow this help";
  417. echo "\n";
  418. }
  419. }
  420.  
  421. ?>

Documentation generated on Sat, 05 Mar 2005 20:00:43 -0500 by phpDocumentor 1.3.0RC3