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

Documentation generated on Thu, 22 Apr 2004 03:23:46 +0100 by phpDocumentor 1.3.0RC3