PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/xampp/php/PEAR/PHP/CompatInfo/Renderer.php

https://github.com/edmondscommerce/XAMPP-Magento-Demo-Site
PHP | 424 lines | 174 code | 29 blank | 221 comment | 42 complexity | 7d193705d63984f7d190cf5d10ea1587 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright (c) 2008, Laurent Laville <pear@laurent-laville.org>
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * * Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * * Neither the name of the authors nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
  24. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. * PHP versions 4 and 5
  33. *
  34. * @category PHP
  35. * @package PHP_CompatInfo
  36. * @author Laurent Laville <pear@laurent-laville.org>
  37. * @license http://www.opensource.org/licenses/bsd-license.php BSD
  38. * @version CVS: $Id: Renderer.php,v 1.8 2008/07/22 20:26:19 farell Exp $
  39. * @link http://pear.php.net/package/PHP_CompatInfo
  40. * @since File available since Release 1.8.0b2
  41. */
  42. /**
  43. * Base class used by all renderers
  44. *
  45. * @category PHP
  46. * @package PHP_CompatInfo
  47. * @author Laurent Laville <pear@laurent-laville.org>
  48. * @license http://www.opensource.org/licenses/bsd-license.php BSD
  49. * @version Release: 1.8.1
  50. * @link http://pear.php.net/package/PHP_CompatInfo
  51. * @since Class available since Release 1.8.0b2
  52. * @abstract
  53. */
  54. class PHP_CompatInfo_Renderer
  55. {
  56. /**
  57. * PHP_CompatInfo_Parser instance
  58. *
  59. * @var object
  60. * @access private
  61. */
  62. var $_parser;
  63. /**
  64. * @var mixed Progress bar render options (available only on CLI sapi)
  65. * @since 1.8.0b1
  66. * @access private
  67. */
  68. var $_pbar;
  69. /**
  70. * @var string End of line string (depending of server API)
  71. * @access public
  72. */
  73. var $eol;
  74. /**
  75. * Silent mode. Display or not extra info messages.
  76. *
  77. * @var boolean
  78. * @access public
  79. */
  80. var $silent;
  81. /**
  82. * Data source parsed final results
  83. *
  84. * @var array
  85. * @access public
  86. */
  87. var $parseData;
  88. /**
  89. * All console arguments that have been parsed and recognized
  90. *
  91. * @var array
  92. * @since 1.8.0RC1
  93. * @access public
  94. */
  95. var $args;
  96. /**
  97. * A hash containing any additional configuration of specific driver
  98. *
  99. * @var array
  100. * @since 1.8.0RC1
  101. * @access public
  102. */
  103. var $conf;
  104. /**
  105. * Base Renderer Class constructor
  106. *
  107. * Base Renderer Class constructor (ZE1) for PHP4
  108. *
  109. * @param object &$parser Instance of the parser (model of MVC pattern)
  110. * @param array $conf A hash containing any additional configuration
  111. *
  112. * @access public
  113. * @since version 1.8.0b2 (2008-06-03)
  114. */
  115. function PHP_CompatInfo_Renderer(&$parser, $conf)
  116. {
  117. PHP_CompatInfo_Renderer::__construct($parser, $conf);
  118. }
  119. /**
  120. * Base Renderer Class constructor
  121. *
  122. * Base Renderer Class constructor (ZE2) for PHP5+
  123. *
  124. * @param object &$parser Instance of the parser (model of MVC pattern)
  125. * @param array $conf A hash containing any additional configuration
  126. *
  127. * @access public
  128. * @since version 1.8.0b2 (2008-06-03)
  129. */
  130. function __construct(&$parser, $conf)
  131. {
  132. $this->_parser = $parser;
  133. $args = array(
  134. 'summarize' => false,
  135. 'output-level' => 31,
  136. 'verbose' => 0
  137. );
  138. if (isset($conf['args']) && is_array($conf['args'])) {
  139. $this->args = array_merge($args, $conf['args']);
  140. unset($conf['args']);
  141. } else {
  142. $this->args = $args;
  143. }
  144. $this->conf = $conf;
  145. if (php_sapi_name() == 'cli') {
  146. // when running the CLI version, take arguments from console
  147. if (isset($this->args['progress'])) {
  148. $conf['progress'] = $this->args['progress'];
  149. $conf['silent'] = false;
  150. }
  151. $this->eol = PHP_EOL;
  152. } else {
  153. $this->eol = '<br/>'. PHP_EOL;
  154. }
  155. // activate (or not) the silent mode
  156. if (!isset($conf['silent'])) {
  157. $this->silent = true; // default behavior
  158. } else {
  159. $this->silent = (bool) $conf['silent'];
  160. }
  161. if (isset($conf['progress']) && $conf['progress'] == 'bar') {
  162. // wait style = progress bar prefered (if available)
  163. $progressBar = 'Console/ProgressBar.php';
  164. if (php_sapi_name() == 'cli'
  165. && PHP_CompatInfo_Renderer::isIncludable($progressBar)) {
  166. include_once $progressBar;
  167. // default progress bar render options
  168. $default = array('formatString' => '- %fraction% files' .
  169. ' [%bar%] %percent%' .
  170. ' Elapsed Time: %elapsed%',
  171. 'barfill' => '=>',
  172. 'prefill' => '-',
  173. 'options' => array());
  174. // apply custom render options if given
  175. if (isset($conf['progressbar'])) {
  176. $pbar = $conf['progressbar'];
  177. } else {
  178. $pbar = array();
  179. }
  180. $this->_pbar = array_merge($default, $pbar);
  181. } else {
  182. // no progress bar available
  183. $this->_pbar = false;
  184. }
  185. } else {
  186. // wait style = text prefered
  187. $this->_pbar = false;
  188. }
  189. // register the compatInfo view as observer
  190. $parser->addListener(array(&$this, 'update'));
  191. }
  192. /**
  193. * Create required instance of the Output 'driver'.
  194. *
  195. * Creates a concrete instance of the renderer depending of $type
  196. *
  197. * @param object &$parser A concrete instance of the parser
  198. * @param string $type (optional) Type of instance required, case insensitive
  199. * @param array $conf (optional) A hash containing any additional
  200. * configuration information that a subclass might need.
  201. *
  202. * @return object PHP_CompatInfo_Renderer A concrete PHP_CompatInfo_Renderer
  203. * instance, or null on error.
  204. * @access public
  205. * @since version 1.8.0b2 (2008-06-03)
  206. */
  207. function &factory(&$parser, $type = 'array', $conf = array())
  208. {
  209. $class = 'PHP_CompatInfo_Renderer_' . ucfirst(strtolower($type));
  210. $file = str_replace('_', '/', $class) . '.php';
  211. /**
  212. * Attempt to include our version of the named class, but don't treat
  213. * a failure as fatal. The caller may have already included their own
  214. * version of the named class.
  215. */
  216. if (!PHP_CompatInfo_Renderer::_classExists($class)) {
  217. include_once $file;
  218. }
  219. // If the class exists, return a new instance of it.
  220. if (PHP_CompatInfo_Renderer::_classExists($class)) {
  221. $instance =& new $class($parser, $conf);
  222. } else {
  223. $instance = null;
  224. }
  225. return $instance;
  226. }
  227. /**
  228. * Update the current view
  229. *
  230. * Interface to update the view with current information.
  231. * Listen events produced by Event_Dispatcher and the PHP_CompatInfo_Parser
  232. *
  233. * @param object &$auditEvent Instance of Event_Dispatcher
  234. *
  235. * @return void
  236. * @access public
  237. * @since version 1.8.0b2 (2008-06-03)
  238. */
  239. function update(&$auditEvent)
  240. {
  241. $notifyName = $auditEvent->getNotificationName();
  242. $notifyInfo = $auditEvent->getNotificationInfo();
  243. switch ($notifyName) {
  244. case PHP_COMPATINFO_EVENT_AUDITSTARTED :
  245. $this->startWaitProgress($notifyInfo['dataCount']);
  246. break;
  247. case PHP_COMPATINFO_EVENT_AUDITFINISHED :
  248. if (!isset($this->parseData)) {
  249. // invalid data source
  250. $this->parseData = false;
  251. }
  252. $this->endWaitProgress();
  253. $this->display();
  254. break;
  255. case PHP_COMPATINFO_EVENT_FILESTARTED :
  256. $this->stillWaitProgress($notifyInfo['filename'],
  257. $notifyInfo['fileindex']);
  258. break;
  259. case PHP_COMPATINFO_EVENT_CODESTARTED :
  260. $this->stillWaitProgress($notifyInfo['stringdata'],
  261. $notifyInfo['stringindex']);
  262. break;
  263. case PHP_COMPATINFO_EVENT_FILEFINISHED :
  264. case PHP_COMPATINFO_EVENT_CODEFINISHED :
  265. $this->parseData = $notifyInfo;
  266. break;
  267. }
  268. }
  269. /**
  270. * Initialize the wait process
  271. *
  272. * Initialize the wait process, with a simple message or a progress bar.
  273. *
  274. * @param integer $maxEntries Number of source to parse
  275. *
  276. * @return void
  277. * @access public
  278. * @since version 1.8.0b2 (2008-06-03)
  279. */
  280. function startWaitProgress($maxEntries)
  281. {
  282. if ($this->silent == false) {
  283. // obey at silent mode protocol
  284. if ($maxEntries == 0) {
  285. // protect against invalid data source
  286. $this->_pbar = false;
  287. }
  288. if ($this->_pbar) {
  289. $this->_pbar = new Console_ProgressBar($this->_pbar['formatString'],
  290. $this->_pbar['barfill'],
  291. $this->_pbar['prefill'],
  292. 78,
  293. $maxEntries,
  294. $this->_pbar['options']);
  295. } else {
  296. echo 'Wait while parsing data source ...'
  297. . $this->eol;
  298. }
  299. }
  300. }
  301. /**
  302. * Update the wait message
  303. *
  304. * Update the wait message, or status of the progress bar
  305. *
  306. * @param string $source Source (file, string) currently parsing
  307. * @param string $index Position of the $source in the data source list
  308. * to parse
  309. *
  310. * @return void
  311. * @access public
  312. * @since version 1.8.0b2 (2008-06-03)
  313. */
  314. function stillWaitProgress($source, $index)
  315. {
  316. if ($this->silent == false) {
  317. // obey at silent mode protocol
  318. if ($this->_pbar) {
  319. // update the progress bar
  320. $this->_pbar->update($index);
  321. } else {
  322. if (is_file($source)) {
  323. echo 'Wait while parsing file "' . $source . '"'
  324. . $this->eol;
  325. } else {
  326. echo 'Wait while parsing string "' . $index . '"'
  327. . $this->eol;
  328. }
  329. }
  330. }
  331. }
  332. /**
  333. * Finish the wait process
  334. *
  335. * Finish the wait process, by erasing the progress bar
  336. *
  337. * @return void
  338. * @access public
  339. * @since version 1.8.0b2 (2008-06-03)
  340. */
  341. function endWaitProgress()
  342. {
  343. if ($this->silent == false) {
  344. // obey at silent mode protocol
  345. if ($this->_pbar) {
  346. // remove the progress bar
  347. $this->_pbar->erase(true);
  348. }
  349. }
  350. }
  351. /**
  352. * Checks if in the include path
  353. *
  354. * Returns whether or not a file is in the include path
  355. *
  356. * @param string $file Path to filename to check if includable
  357. *
  358. * @static
  359. * @access public
  360. * @return boolean True if the file is in the include path, false otherwise
  361. * @since version 1.7.0b4 (2008-04-03)
  362. */
  363. function isIncludable($file)
  364. {
  365. foreach (explode(PATH_SEPARATOR, get_include_path()) as $ip) {
  366. if (file_exists($ip . DIRECTORY_SEPARATOR . $file)
  367. && is_readable($ip . DIRECTORY_SEPARATOR . $file)
  368. ) {
  369. return true;
  370. }
  371. }
  372. return false;
  373. }
  374. /**
  375. * Utility function which wraps PHP's class_exists() function to ensure
  376. * consistent behavior between PHP versions 4 and 5. Autoloading behavior
  377. * is always disabled.
  378. *
  379. * @param string $class The name of the class whose existence should be tested.
  380. *
  381. * @return bool True if the class exists, false otherwiser.
  382. *
  383. * @static
  384. * @access private
  385. * @since version 1.8.0b2 (2008-06-03)
  386. */
  387. function _classExists($class)
  388. {
  389. if (version_compare(PHP_VERSION, '5.0.0', 'ge')) {
  390. return class_exists($class, false);
  391. }
  392. return class_exists($class);
  393. }
  394. }
  395. ?>