Apache Zeta Components Manual :: File Source for statusbar.php

Source for file statusbar.php

Documentation is available at statusbar.php

  1. <?php
  2. /**
  3.  * File containing the ezcConsoleStatusbar class.
  4.  *
  5.  * Licensed to the Apache Software Foundation (ASF) under one
  6.  * or more contributor license agreements.  See the NOTICE file
  7.  * distributed with this work for additional information
  8.  * regarding copyright ownership.  The ASF licenses this file
  9.  * to you under the Apache License, Version 2.0 (the
  10.  * "License"); you may not use this file except in compliance
  11.  * with the License.  You may obtain a copy of the License at
  12.  * 
  13.  *   http://www.apache.org/licenses/LICENSE-2.0
  14.  * 
  15.  * Unless required by applicable law or agreed to in writing,
  16.  * software distributed under the License is distributed on an
  17.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  18.  * KIND, either express or implied.  See the License for the
  19.  * specific language governing permissions and limitations
  20.  * under the License.
  21.  *
  22.  * @package ConsoleTools
  23.  * @version //autogentag//
  24.  * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  25.  * @filesource
  26.  */
  27.  
  28. /**
  29.  * Creating  and maintaining status-bars to be printed to the console.
  30.  *
  31.  * <code>
  32.  * // Construction
  33.  * $status = new ezcConsoleStatusbar( new ezcConsoleOutput() );
  34.  *
  35.  * // Set option
  36.  * $status->options['successChar'] = '*';
  37.  *
  38.  * // Run statusbar
  39.  * foreach ( $files as $file )
  40.  * {
  41.  *      $res = $file->upload();
  42.  *      // Add status if form of bool true/false to statusbar.
  43.  *      $status->add( $res ); // $res is true or false
  44.  * }
  45.  *
  46.  * // Retreive and display final statusbar results
  47.  * $msg = $status->getSuccess() . ' succeeded, ' . $status->getFailure() . ' failed.';
  48.  * $out->outputText( "Finished uploading files: $msg\n" );
  49.  * </code>
  50.  *  
  51.  * @property ezcConsoleStatusbarOptions $options 
  52.  *            Contains the options for this class.
  53.  * 
  54.  * @package ConsoleTools
  55.  * @version //autogen//
  56.  * @mainclass
  57.  */
  58. {
  59.     /**
  60.      * Container to hold the properties
  61.      *
  62.      * @var array(string=>mixed) 
  63.      */
  64.     protected $properties;
  65.  
  66.     /**
  67.      * The ezcConsoleOutput object to use.
  68.      *
  69.      * @var ezcConsoleOutput 
  70.      */
  71.     protected $outputHandler;
  72.  
  73.     /**
  74.      * Counter for success and failure outputs.
  75.      * 
  76.      * @var array(bool=>int) 
  77.      */
  78.     protected $counter = array
  79.         true  => 0,
  80.         false => 0,
  81.     );
  82.  
  83.     /**
  84.      * Creates a new status bar.
  85.      *
  86.      * @param ezcConsoleOutput $outHandler Handler to utilize for output
  87.      * @param array(string=>string) $options       Options
  88.      *
  89.      * @see ezcConsoleStatusbar::$options
  90.      */
  91.     public function __constructezcConsoleOutput $outHandlerarray $options array() )
  92.     {
  93.         $this->outputHandler = $outHandler;
  94.         $this->properties['options'new ezcConsoleStatusbarOptions$options );
  95.     }
  96.  
  97.     /**
  98.      * Property read access.
  99.      * 
  100.      * @param string $key Name of the property.
  101.      * @return mixed Value of the property or null.
  102.      *
  103.      * @throws ezcBasePropertyNotFoundException
  104.      *          If the the desired property is not found.
  105.      * @ignore
  106.      */
  107.     public function __get$key )
  108.     {
  109.         switch $key )
  110.         {
  111.             case 'options':
  112.                 return $this->properties['options'];
  113.                 break;
  114.         }
  115.         if isset$this->properties['options']->$key ) )
  116.         {
  117.             return $this->properties['options']->$key;
  118.         }
  119.         throw new ezcBasePropertyNotFoundException$key );
  120.     }
  121.  
  122.     /**
  123.      * Set new options.
  124.      * This method allows you to change the options of a statusbar.
  125.      *  
  126.      * @param array(string=>string)|ezcConsoleOutputOptions$options The options to set.
  127.      *
  128.      * @throws ezcBaseSettingNotFoundException
  129.      *          If you tried to set a non-existent option value.
  130.      * @throws ezcBaseSettingValueException
  131.      *          If the value is not valid for the desired option.
  132.      * @throws ezcBaseValueException
  133.      *          If you submit neither an array nor an instance of
  134.      *          ezcConsoleOutputOptions.
  135.      */
  136.     public function setOptions$options 
  137.     {
  138.         if is_array$options ) ) 
  139.         {
  140.             $this->properties['options']->merge$options );
  141.         
  142.         else if $options instanceof ezcConsoleStatusbarOptions 
  143.         {
  144.             $this->properties['options'$options;
  145.         }
  146.         else
  147.         {
  148.             throw new ezcBaseValueException"options"$options"instance of ezcConsoleStatusbarOptions" );
  149.         }
  150.     }
  151.  
  152.     /**
  153.      * Property write access.
  154.      * 
  155.      * @param string $key Name of the property.
  156.      * @param mixed $val  The value for the property.
  157.      *
  158.      * @throws ezcBasePropertyNotFoundException
  159.      *          If a desired property could not be found.
  160.      * @throws ezcBaseValueException
  161.      *          If a desired property value is out of range.
  162.      * @ignore
  163.      */
  164.     public function __set$key$val )
  165.     {
  166.         switch $key )
  167.         {
  168.             // Those two are here for BC reasons only, it is proper to
  169.             // use $statusbar->options->successChar instead of just
  170.             // $statusbar->successChar.
  171.             case 'successChar':
  172.             case 'failureChar':
  173.                 // No checks necessary here, already performed in
  174.                 // ezcConsoleStatusbarOptions
  175.                 break;
  176.             case "options":
  177.                 if ( ( $val instanceof ezcConsoleStatusbarOptions === false )
  178.                 {
  179.                     throw new ezcBaseValueException$key$val'ezcConsoleStatusbarOptions' );
  180.                 }
  181.                 $this->properties[$key$val;
  182.                 return;
  183.             default:
  184.                 throw new ezcBasePropertyNotFoundException$key );
  185.         }
  186.         $this->properties['options'][$key$val;
  187.     }
  188.  
  189.     /**
  190.      * Property isset access.
  191.      * 
  192.      * @param string $key Name of the property.
  193.      * @return bool True is the property is set, otherwise false.
  194.      * @ignore
  195.      */
  196.     public function __isset$key )
  197.     {
  198.         return isset$this->properties['options'][$key|| isset$this->properties[$key);
  199.     }
  200.     
  201.     /**
  202.      * Returns the current options.
  203.      * Returns the options currently set for this progressbar.
  204.      * 
  205.      * @return ezcConsoleStatusbarOptions The current options.
  206.      */
  207.     public function getOptions()
  208.     {
  209.         return $this->properties['options'];
  210.     }
  211.  
  212.     /**
  213.      * Add a status to the status bar.
  214.      * Adds a new status to the bar which is printed immediately. If the
  215.      * cursor is currently not at the beginning of a line, it will move to
  216.      * the next line.
  217.      *
  218.      * @param bool $status Print successChar on true, failureChar on false.
  219.      * @return void 
  220.      */
  221.     public function add$status )
  222.     {
  223.         if is_bool$status === false )
  224.         {
  225.             trigger_error'Unknown status '.var_export$statustrue ).'.'E_USER_WARNING );
  226.             $status = (bool) $status;
  227.         }
  228.         switch $status )
  229.         {
  230.             case true:
  231.                 $this->outputHandler->outputText$this->properties['options']['successChar']'success' );
  232.                 break;
  233.  
  234.             case false:
  235.                 $this->outputHandler->outputText$this->properties['options']['failureChar']'failure' );
  236.                 break;
  237.         }
  238.         $this->counter[$status]++;
  239.     }
  240.  
  241.     /**
  242.      * Reset the state of the status-bar object to its initial one.
  243.      * 
  244.      * @return void 
  245.      */
  246.     public function reset()
  247.     {
  248.         foreach $this->counter as $status => $count )
  249.         {
  250.             $this->counter[$status0;
  251.         }
  252.     }
  253.  
  254.     /**
  255.      * Returns number of successes during the run.
  256.      * Returns the number of success characters printed from this status bar.
  257.      * 
  258.      * @return int Number of successes.
  259.      */
  260.     public function getSuccessCount()
  261.     {
  262.         return $this->counter[true];
  263.     }
  264.  
  265.     /**
  266.      * Returns number of failures during the run.
  267.      * Returns the number of failure characters printed from this status bar.
  268.      * 
  269.      * @return int Number of failures.
  270.      */
  271.     public function getFailureCount()
  272.     {
  273.         return $this->counter[false];
  274.     }
  275. }
  276. ?>
Documentation generated by phpDocumentor 1.4.3