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

Source for file Renderer.php

Documentation is available at Renderer.php

  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. /**
  44.  * Base class used by all renderers
  45.  *
  46.  * @category PHP
  47.  * @package  PHP_CompatInfo
  48.  * @author   Laurent Laville <pear@laurent-laville.org>
  49.  * @license  http://www.opensource.org/licenses/bsd-license.php  BSD
  50.  * @version  Release: 1.9.0b1
  51.  * @link     http://pear.php.net/package/PHP_CompatInfo
  52.  * @since    Class available since Release 1.8.0b2
  53.  * @abstract
  54.  */
  55. {
  56.     /**
  57.      * PHP_CompatInfo_Parser instance
  58.      *
  59.      * @var    object 
  60.      * @access private
  61.      */
  62.     var $_parser;
  63.  
  64.     /**
  65.      * @var    mixed    Progress bar render options (available only on CLI sapi)
  66.      * @since  1.8.0b1
  67.      * @access private
  68.      */
  69.     var $_pbar;
  70.  
  71.     /**
  72.      * @var    string   End of line string (depending of server API)
  73.      * @access public
  74.      */
  75.     var $eol;
  76.  
  77.     /**
  78.      * Silent mode. Display or not extra info messages.
  79.      *
  80.      * @var    boolean 
  81.      * @access public
  82.      */
  83.     var $silent;
  84.  
  85.     /**
  86.      * Data source parsed final results
  87.      *
  88.      * @var    array 
  89.      * @access public
  90.      */
  91.     var $parseData;
  92.  
  93.     /**
  94.      * All console arguments that have been parsed and recognized
  95.      *
  96.      * @var   array 
  97.      * @since 1.8.0RC1
  98.      * @access public
  99.      */
  100.     var $args;
  101.  
  102.     /**
  103.      * A hash containing any additional configuration of specific driver
  104.      *
  105.      * @var    array 
  106.      * @since  1.8.0RC1
  107.      * @access public
  108.      */
  109.     var $conf;
  110.  
  111.     /**
  112.      * Base Renderer Class constructor
  113.      *
  114.      * Base Renderer Class constructor (ZE1) for PHP4
  115.      *
  116.      * @param object &$parser Instance of the parser (model of MVC pattern)
  117.      * @param array  $conf    A hash containing any additional configuration
  118.      *
  119.      * @access public
  120.      * @since  version 1.8.0b2 (2008-06-03)
  121.      */
  122.     function PHP_CompatInfo_Renderer(&$parser$conf)
  123.     {
  124.         PHP_CompatInfo_Renderer::__construct($parser$conf);
  125.     }
  126.  
  127.     /**
  128.      * Base Renderer Class constructor
  129.      *
  130.      * Base Renderer Class constructor (ZE2) for PHP5+
  131.      *
  132.      * @param object &$parser Instance of the parser (model of MVC pattern)
  133.      * @param array  $conf    A hash containing any additional configuration
  134.      *
  135.      * @access public
  136.      * @since  version 1.8.0b2 (2008-06-03)
  137.      */
  138.     function __construct(&$parser$conf)
  139.     {
  140.         $this->_parser $parser;
  141.  
  142.         $args = array(
  143.             'summarize' => false,
  144.             'output-level' => 31,
  145.             'verbose' => 0
  146.             );
  147.         if (isset($conf['args']&& is_array($conf['args'])) {
  148.             $this->args = array_merge($args$conf['args']);
  149.             unset($conf['args']);
  150.         else {
  151.             $this->args = $args;
  152.         }
  153.         $this->conf = $conf;
  154.  
  155.         if (php_sapi_name(== 'cli'{
  156.             // when running the CLI version, take arguments from console
  157.             if (isset($this->args['progress'])) {
  158.                 $conf['progress'$this->args['progress'];
  159.                 $conf['silent']   = false;
  160.             }
  161.             $this->eol = PHP_EOL;
  162.         else {
  163.             $this->eol = '<br/>'. PHP_EOL;
  164.         }
  165.  
  166.         // activate (or not) the silent mode
  167.         if (!isset($conf['silent'])) {
  168.             $this->silent = true;  // default behavior
  169.         else {
  170.             $this->silent = (bool) $conf['silent'];
  171.         }
  172.  
  173.         if (isset($conf['progress']&& $conf['progress'== 'bar'{
  174.             // wait style = progress bar prefered (if available)
  175.             $progressBar 'Console/ProgressBar.php';
  176.             if (php_sapi_name(== 'cli'
  177.                 && PHP_CompatInfo_Renderer::isIncludable($progressBar)) {
  178.  
  179.                 include_once $progressBar;
  180.  
  181.                 // default progress bar render options
  182.                 $default = array('formatString' => '- %fraction% files' .
  183.                                                    ' [%bar%] %percent%' .
  184.                                                    ' Elapsed Time: %elapsed%',
  185.                                  'barfill' => '=>',
  186.                                  'prefill' => '-',
  187.                                  'options' => array());
  188.  
  189.                 // apply custom render options if given
  190.                 if (isset($conf['progressbar'])) {
  191.                     $pbar $conf['progressbar'];
  192.                 else {
  193.                     $pbar = array();
  194.                 }
  195.                 $this->_pbar array_merge($default$pbar);
  196.             else {
  197.                 // no progress bar available
  198.                 $this->_pbar = false;
  199.             }
  200.         else {
  201.             // wait style = text prefered
  202.             $this->_pbar = false;
  203.         }
  204.  
  205.         // register the compatInfo view as observer
  206.         $parser->addListener(array(&$this'update'));
  207.     }
  208.  
  209.     /**
  210.      * Create required instance of the Output 'driver'.
  211.      *
  212.      * Creates a concrete instance of the renderer depending of $type
  213.      *
  214.      * @param object &$parser A concrete instance of the parser
  215.      * @param string $type    (optional) Type of instance required, case insensitive
  216.      * @param array  $conf    (optional) A hash containing any additional
  217.      *                         configuration information that a subclass might need.
  218.      *
  219.      * @return object PHP_CompatInfo_Renderer A concrete PHP_CompatInfo_Renderer
  220.      *                                         instance, or null on error.
  221.      * @access public
  222.      * @since  version 1.8.0b2 (2008-06-03)
  223.      */
  224.     function &factory(&$parser$type 'array'$conf = array())
  225.     {
  226.         $class 'PHP_CompatInfo_Renderer_' ucfirst(strtolower($type));
  227.         $file  str_replace('_''/'$class'.php';
  228.  
  229.         /**
  230.          * Attempt to include our version of the named class, but don't treat
  231.          * a failure as fatal.  The caller may have already included their own
  232.          * version of the named class.
  233.          */
  234.         if (!PHP_CompatInfo_Renderer::_classExists($class)) {
  235.             include_once $file;
  236.         }
  237.  
  238.         // If the class exists, return a new instance of it.
  239.         if (PHP_CompatInfo_Renderer::_classExists($class)) {
  240.             $instance =new $class($parser$conf);
  241.         else {
  242.             $instance = null;
  243.         }
  244.  
  245.         return $instance;
  246.     }
  247.  
  248.     /**
  249.      * Update the current view
  250.      *
  251.      * Interface to update the view with current information.
  252.      * Listen events produced by Event_Dispatcher and the PHP_CompatInfo_Parser
  253.      *
  254.      * @param object &$auditEvent Instance of Event_Dispatcher
  255.      *
  256.      * @return void 
  257.      * @access public
  258.      * @since  version 1.8.0b2 (2008-06-03)
  259.      */
  260.     function update(&$auditEvent)
  261.     {
  262.         $notifyName $auditEvent->getNotificationName();
  263.         $notifyInfo $auditEvent->getNotificationInfo();
  264.  
  265.         switch ($notifyName{
  266.         case PHP_COMPATINFO_EVENT_AUDITSTARTED :
  267.             $this->startWaitProgress($notifyInfo['dataCount']);
  268.             break;
  269.         case PHP_COMPATINFO_EVENT_AUDITFINISHED :
  270.             if (!isset($this->parseData)) {
  271.                 // invalid data source
  272.                 $this->parseData = false;
  273.             }
  274.             $this->endWaitProgress();
  275.             $this->display();
  276.             break;
  277.         case PHP_COMPATINFO_EVENT_FILESTARTED :
  278.             $this->stillWaitProgress($notifyInfo['filename'],
  279.                                      $notifyInfo['fileindex']);
  280.             break;
  281.         case PHP_COMPATINFO_EVENT_CODESTARTED :
  282.             $this->stillWaitProgress($notifyInfo['stringdata'],
  283.                                      $notifyInfo['stringindex']);
  284.             break;
  285.         case PHP_COMPATINFO_EVENT_FILEFINISHED :
  286.         case PHP_COMPATINFO_EVENT_CODEFINISHED :
  287.             $this->parseData = $notifyInfo;
  288.             break;
  289.         }
  290.     }
  291.  
  292.     /**
  293.      * Initialize the wait process
  294.      *
  295.      * Initialize the wait process, with a simple message or a progress bar.
  296.      *
  297.      * @param integer $maxEntries Number of source to parse
  298.      *
  299.      * @return void 
  300.      * @access public
  301.      * @since  version 1.8.0b2 (2008-06-03)
  302.      */
  303.     function startWaitProgress($maxEntries)
  304.     {
  305.         if ($this->silent == false{
  306.             // obey at silent mode protocol
  307.             if ($maxEntries == 0{
  308.                 // protect against invalid data source
  309.                 $this->_pbar = false;
  310.             }
  311.             if ($this->_pbar{
  312.                 $this->_pbar = new Console_ProgressBar($this->_pbar['formatString'],
  313.                                                $this->_pbar['barfill'],
  314.                                                $this->_pbar['prefill'],
  315.                                                78,
  316.                                                $maxEntries,
  317.                                                $this->_pbar['options']);
  318.             else {
  319.                 echo 'Wait while parsing data source ...'
  320.                    . $this->eol;
  321.             }
  322.         }
  323.     }
  324.  
  325.     /**
  326.      * Update the wait message
  327.      *
  328.      * Update the wait message, or status of the progress bar
  329.      *
  330.      * @param string $source Source (file, string) currently parsing
  331.      * @param string $index  Position of the $source in the data source list
  332.      *                        to parse
  333.      *
  334.      * @return void 
  335.      * @access public
  336.      * @since  version 1.8.0b2 (2008-06-03)
  337.      */
  338.     function stillWaitProgress($source$index)
  339.     {
  340.         if ($this->silent == false{
  341.             // obey at silent mode protocol
  342.             if ($this->_pbar{
  343.                 // update the progress bar
  344.                 $this->_pbar->update($index);
  345.             else {
  346.                 if (is_file($source)) {
  347.                     echo 'Wait while parsing file "' $source '"'
  348.                        . $this->eol;
  349.                 else {
  350.                     echo 'Wait while parsing string "' $index '"'
  351.                        . $this->eol;
  352.                 }
  353.             }
  354.         }
  355.     }
  356.  
  357.     /**
  358.      * Finish the wait process
  359.      *
  360.      * Finish the wait process, by erasing the progress bar
  361.      *
  362.      * @return void 
  363.      * @access public
  364.      * @since  version 1.8.0b2 (2008-06-03)
  365.      */
  366.     function endWaitProgress()
  367.     {
  368.         if ($this->silent == false{
  369.             // obey at silent mode protocol
  370.             if ($this->_pbar{
  371.                 // remove the progress bar
  372.                 $this->_pbar->erase(true);
  373.             }
  374.         }
  375.     }
  376.  
  377.     /**
  378.      * Checks if in the include path
  379.      *
  380.      * Returns whether or not a file is in the include path
  381.      *
  382.      * @param string $file Path to filename to check if includable
  383.      *
  384.      * @static
  385.      * @access public
  386.      * @return boolean True if the file is in the include path, false otherwise
  387.      * @since  version 1.7.0b4 (2008-04-03)
  388.      */
  389.     function isIncludable($file)
  390.     {
  391.         foreach (explode(PATH_SEPARATORget_include_path()) as $ip{
  392.             if (file_exists($ip . DIRECTORY_SEPARATOR . $file)
  393.                 && is_readable($ip . DIRECTORY_SEPARATOR . $file)
  394.                 {
  395.                 return true;
  396.             }
  397.         }
  398.         return false;
  399.     }
  400.  
  401.     /**
  402.      * Utility function which wraps PHP's class_exists() function to ensure
  403.      * consistent behavior between PHP versions 4 and 5. Autoloading behavior
  404.      * is always disabled.
  405.      *
  406.      * @param string $class The name of the class whose existence should be tested.
  407.      *
  408.      * @return bool         True if the class exists, false otherwiser.
  409.      *
  410.      * @static
  411.      * @access private
  412.      * @since  version 1.8.0b2 (2008-06-03)
  413.      */
  414.     function _classExists($class)
  415.     {
  416.         if (version_compare(PHP_VERSION'5.0.0''ge')) {
  417.             return class_exists($classfalse);
  418.         }
  419.  
  420.         return class_exists($class);
  421.     }
  422. }
  423. ?>

Documentation generated on Sun, 30 Nov 2008 16:30:34 -0500 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.