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.7 2004/05/06 16:10:25 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. if ($option[1]{0} == '=') {
  111. $this->dir = substr($option[1],1);
  112. } else {
  113. $this->dir = $option[1];
  114. }
  115. if ($this->dir{strlen($this->dir)-1} == '/' || $this->dir{strlen($this->dir)-1} == '\\') {
  116. $this->dir = substr($this->dir,0,-1);
  117. }
  118. $this->dir = str_replace('\\','/',realpath($this->dir));
  119. break;
  120. case '--file':
  121. $this->file = $option[1];
  122. break;
  123. case 'f':
  124. if ($option[1]{0} == '=') {
  125. $this->file = substr($option[1],1);
  126. } else {
  127. $this->file = $option[1];
  128. }
  129. break;
  130. case 'h':
  131. case '--help':
  132. $this->_printHelp();
  133. break;
  134. }
  135. }
  136. }
  137.  
  138. /**
  139. * PHP4 Compatible Constructor
  140. */
  141.  
  142. function PHP_CompatInfo_Cli() {
  143. $this->__construct();
  144. }
  145.  
  146. /**
  147. * Run the CLI Script
  148. *
  149. * @access public
  150. * @return void
  151. */
  152.  
  153. function run() {
  154. if ($this->error == true) {
  155. echo $this->opts->message;
  156. $this->_printUsage();
  157. } else {
  158. if (isset($this->dir)) {
  159. $output = $this->_parseDir();
  160. echo $output;
  161. } elseif (isset($this->file)) {
  162. $output = $this->_parseFile();
  163. echo $output;
  164. }
  165. }
  166. }
  167.  
  168. /**
  169. * Parse Directory Input
  170. *
  171. * @access private
  172. * @return boolean|stringReturns Boolean False on fail
  173. */
  174.  
  175. function _parseDir() {
  176. require_once 'Console/Table.php';
  177. $info = $this->parseDir($this->dir,array('debug' => $this->debug,'recurse_dir' => $this->recurse));
  178. if ($info == false) {
  179. echo 'Failed opening directory ("' .$this->dir. '"). Please check your spelling and try again.';
  180. $this->_printUsage();
  181. return;
  182. }
  183. $table = new Console_Table();
  184. $table->setHeaders(array('File','Version','Extensions','Constants'));
  185. if (!isset($info['extensions'][0])) {
  186. $ext = '';
  187. } else {
  188. $ext = array_shift($info['extensions']);
  189. }
  190.  
  191. if (!isset($info['constants'][0])) {
  192. $const = '';
  193. } else {
  194. $const = array_shift($info['constants']);
  195. }
  196. $dir = str_replace(array('\\','/'),DIRECTORY_SEPARATOR,$this->dir);
  197. $table->addRow(array($dir.DIRECTORY_SEPARATOR. '*',$info['version'],$ext,$const));
  198. if (sizeof($info['extensions']) >= sizeof($info['constants'])) {
  199. foreach ($info['extensions'] as $i => $ext) {
  200. if (isset($info['constants'][$i])) {
  201. $const = $info['constants'][$i];
  202. } else {
  203. $const = '';
  204. }
  205. $table->addRow(array('','',$ext,$const));
  206. }
  207. } else {
  208. foreach ($info['constants'] as $i => $const) {
  209. if (isset($info['extensions'][$i])) {
  210. $ext = $info['extensions'][$i];
  211. } else {
  212. $ext = '';
  213. }
  214. $table->addRow(array('','',$ext,$const));
  215. }
  216. }
  217. unset($info['version']);
  218. unset($info['extensions']);
  219. unset($info['constants']);
  220.  
  221. $ignored = $info['ignored_files'];
  222.  
  223. unset($info['ignored_files']);
  224.  
  225. foreach ($info as $file => $info) {
  226. if (!isset($info['extensions'][0])) {
  227. $ext = '';
  228. } else {
  229. $ext = array_shift($info['extensions']);
  230. }
  231.  
  232. if (!isset($info['constants'][0])) {
  233. $const = '';
  234. } else {
  235. $const = array_shift($info['constants']);
  236. }
  237. $key = str_replace(array('\\','/'),DIRECTORY_SEPARATOR,$file);
  238. $table->addRow(array($file,$info['version'],$ext,$const));
  239. if (sizeof($info['extensions']) >= sizeof($info['constants'])) {
  240. foreach ($info['extensions'] as $i => $ext) {
  241. if (isset($info['constants'][$i])) {
  242. $const = $info['constants'][$i];
  243. } else {
  244. $const = '';
  245. }
  246. $table->addRow(array('','',$ext,$const));
  247. }
  248. } else {
  249. foreach ($info['constants'] as $i => $const) {
  250. if (isset($info['extensions'][$i])) {
  251. $ext = $info['extensions'][$i];
  252. } else {
  253. $ext = '';
  254. }
  255. $table->addRow(array('','',$ext,$const));
  256. }
  257. }
  258. }
  259.  
  260. return $table->getTable();
  261. }
  262.  
  263. /**
  264. * Parse File Input
  265. *
  266. * @access private
  267. * @return boolean|stringReturns Boolean False on fail
  268. */
  269.  
  270. function _parseFile() {
  271. require_once 'Console/Table.php';
  272. $info = $this->parseFile($this->file,array('debug' => $this->debug));
  273. if ($info == false) {
  274. echo 'Failed opening file. Please check your spelling and try again.';
  275. $this->_printUsage();
  276. return false;
  277. }
  278. $table = new Console_Table();
  279. if ($this->debug == false) {
  280. $table->setHeaders(array('File','Version','Extensions','Constants'));
  281. if (!isset($info['extensions'][0])) {
  282. $ext = '';
  283. } else {
  284. $ext = array_shift($info['extensions']);
  285. }
  286.  
  287. if (!isset($info['constants'][0])) {
  288. $const = '';
  289. } else {
  290. $const = array_shift($info['constants']);
  291. }
  292.  
  293. $table->addRow(array($this->file,$info['version'],$ext,$const));
  294. if (sizeof($info['extensions']) >= sizeof($info['constants'])) {
  295. foreach ($info['extensions'] as $i => $ext) {
  296. if (isset($info['constants'][$i])) {
  297. $const = $info['constants'][$i];
  298. } else {
  299. $const = '';
  300. }
  301. $table->addRow(array('','',$ext,$const));
  302. }
  303. } else {
  304. foreach ($info['constants'] as $i => $const) {
  305. if (isset($info['extensions'][$i])) {
  306. $ext = $info['extensions'][$i];
  307. } else {
  308. $ext = '';
  309. }
  310. $table->addRow(array('','',$ext,$const));
  311. }
  312. }
  313. } else {
  314. $table->setHeaders(array('File','Version','Extensions','Constants'));
  315.  
  316. if (!isset($info['extensions'][0])) {
  317. $ext = '';
  318. } else {
  319. $ext = array_shift($info['extensions']);
  320. }
  321.  
  322. if (!isset($info['constants'][0])) {
  323. $const = '';
  324. } else {
  325. $const = array_shift($info['constants']);
  326. }
  327.  
  328. $table->addRow(array($this->file,$info['version'],$ext,$const));
  329.  
  330. if (sizeof($info['extensions']) >= sizeof($info['constants'])) {
  331. foreach ($info['extensions'] as $i => $ext) {
  332. if (isset($info['constants'][$i])) {
  333. $const = $info['constants'][$i];
  334. } else {
  335. $const = '';
  336. }
  337. $table->addRow(array('','',$ext,$const));
  338. }
  339. } else {
  340. foreach ($info['constants'] as $i => $const) {
  341. if (isset($info['extensions'][$i])) {
  342. $ext = $info['extensions'][$i];
  343. } else {
  344. $ext = '';
  345. }
  346. $table->addRow(array('','',$ext,$const));
  347. }
  348. }
  349. }
  350.  
  351. $output = $table->getTable();
  352.  
  353. if ($this->debug == true) {
  354. $output .= "\nDebug:\n\n";
  355.  
  356. $table = new Console_Table();
  357.  
  358. $table->setHeaders(array('Version','Function','Extension'));
  359.  
  360. unset($info['version']);
  361. unset($info['constants']);
  362. unset($info['extensions']);
  363.  
  364. foreach ($info as $version => $functions) {
  365. foreach ($functions as $func) {
  366. $table->addRow(array($version,$func['function'],$func['extension']));
  367. }
  368. }
  369.  
  370. $output .= $table->getTable();
  371. }
  372.  
  373. return $output;
  374. }
  375.  
  376. /**
  377. * Show basic Usage
  378. *
  379. * @access private
  380. * @return void
  381. */
  382. function _printUsage() {
  383. echo "\n";
  384. echo 'Usage:' . "\n";
  385. echo " " .basename(__FILE__). ' --dir=DIR [--no-recurse] | --file=FILE [--debug] | [--help]';
  386. echo "\n";
  387. }
  388. /**
  389. * Show full help information
  390. *
  391. * @access private
  392. * @return void
  393. */
  394.  
  395. function _printHelp() {
  396. $this->_printUsage();
  397. echo "Commands:\n";
  398. echo " --file=FILE (-f) \tParse FILE to get its Compatibility Info";
  399. echo "\n";
  400. echo " --dir=DIR (-d) \tParse DIR to get its Compatibility Info";
  401. echo "\n";
  402. echo " --no-recurse (-n) \tDo not Recursively parse files when using --dir";
  403. echo "\n";
  404. echo " --debug\t\tDisplay Extra (debug) Information when using --file";
  405. echo "\n";
  406. echo " --help (-h) \t\tShow this help";
  407. echo "\n";
  408. }
  409. }
  410.  
  411. ?>

Documentation generated on Thu, 6 May 2004 16:59:09 +0100 by phpDocumentor 1.3.0RC3. PEAR Logo Copyright © PHP Group 2004.