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

Source for file progressmonitor.php

Documentation is available at progressmonitor.php

  1. <?php
  2. /**
  3.  * File containing the ezcConsoleProgressMonitor 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.  * Printing structured status information on the console.
  30.  *
  31.  * <code>
  32.  * // Construction
  33.  * $status = new ezcConsoleProgressMonitor( new ezcConsoleOutput(), 42 );
  34.  *
  35.  * // Run statusbar
  36.  * foreach ( $files as $file )
  37.  * {
  38.  *      $res = $file->upload();
  39.  *      // Add a status-entry to be printed.
  40.  *      $status->addEntry( 'UPLOAD', $file->path );
  41.  *      // Result like "    2.5% UPLOAD /var/upload/test.png"
  42.  * }
  43.  * </code>
  44.  *  
  45.  * @property ezcConsoleProgressMonitorOptions $options 
  46.  *            Contains the options for this class.
  47.  * 
  48.  * @package ConsoleTools
  49.  * @version //autogen//
  50.  * @mainclass
  51.  */
  52. {
  53.     /**
  54.      * Options
  55.      *
  56.      * @var ezcConsoleProgressMonitorOptions 
  57.      */
  58.     protected $options;
  59.  
  60.     /**
  61.      * The ezcConsoleOutput object to use.
  62.      *
  63.      * @var ezcConsoleOutput 
  64.      */
  65.     protected $outputHandler;
  66.  
  67.     /**
  68.      * Counter for the items already printed.
  69.      * 
  70.      * @var int 
  71.      */
  72.     protected $counter = 0;
  73.  
  74.     /**
  75.      * The number of entries to expect.
  76.      * 
  77.      * @var int 
  78.      */
  79.     protected $max;
  80.  
  81.     /**
  82.      * Creates a new progress monitor.
  83.      * The $outputHandler parameter will be used to display the progress
  84.      * monitor. $max is the number of monitor items to expect. $options can be
  85.      * used to define the behaviour of the monitor
  86.      * {@link ezcConsoleProgressMonitorOptions}.
  87.      *
  88.      * @param ezcConsoleOutput $outHandler   Handler to utilize for output
  89.      * @param int $max                       Number of items to expect
  90.      * @param array(string=>string) $options Options.
  91.      *
  92.      * @see ezcConsoleProgressMonitor::$options
  93.      */
  94.     public function __constructezcConsoleOutput $outHandler$maxarray $options array() )
  95.     {
  96.         $this->outputHandler = $outHandler;
  97.         $this->max = $max;
  98.         $this->options = new ezcConsoleProgressMonitorOptions$options );
  99.     }
  100.  
  101.     /**
  102.      * Property read access.
  103.      * 
  104.      * @param string $key Name of the property.
  105.      * @return mixed Value of the property or null.
  106.      *
  107.      * @throws ezcBasePropertyNotFoundException
  108.      *          If the the desired property is not found.
  109.      */
  110.     public function __get$key )
  111.     {
  112.         switch $key )
  113.         {
  114.             case 'options':
  115.                 return $this->options;
  116.                 break;
  117.         }
  118.         throw new ezcBasePropertyNotFoundException$key );
  119.     }
  120.     
  121.     /**
  122.      * Property write access.
  123.      * 
  124.      * @param string $propertyName Name of the property.
  125.      * @param mixed $val  The value for the property.
  126.      *
  127.      * @throws ezcBaseValueException
  128.      *          If a the value for the property options is not an instance of
  129.      *          ezcConsoleProgressMonitorOptions.
  130.      * @return void 
  131.      */
  132.     public function __set$propertyName$val )
  133.     {
  134.         switch $propertyName 
  135.         {
  136.             case 'options':
  137.                 if !$val instanceof ezcConsoleProgressMonitorOptions ) )
  138.                 {
  139.                     throw new ezcBaseValueException$propertyName$val'instance of ezcConsoleProgressMonitorOptions' );
  140.                 }
  141.                 $this->options = $val;
  142.                 return;
  143.         }
  144.         throw new ezcBasePropertyNotFoundException$propertyName );
  145.     }
  146.     
  147.     
  148.     /**
  149.      * Property isset access.
  150.      * 
  151.      * @param string $propertyName Name of the property.
  152.      * @return bool True is the property is set, otherwise false.
  153.      */
  154.     public function __isset$propertyName )
  155.     {
  156.         switch $propertyName )
  157.         {
  158.             case 'options':
  159.                 return true;
  160.         }
  161.         return false;
  162.     }
  163.  
  164.     /**
  165.      * Set new options.
  166.      * This method allows you to change the options of an progress monitor.
  167.      *  
  168.      * @param ezcConsoleProgressMonitorOptions $options The options to set.
  169.      *
  170.      * @throws ezcBaseSettingNotFoundException
  171.      *          If you tried to set a non-existent option value.
  172.      * @throws ezcBaseSettingValueException
  173.      *          If the value is not valid for the desired option.
  174.      * @throws ezcBaseValueException
  175.      *          If you submit neither an array nor an instance of
  176.      *          ezcConsoleProgressMonitorOptions.
  177.      */
  178.     public function setOptions$options 
  179.     {
  180.         if is_array$options ) ) 
  181.         {
  182.             $this->options->merge$options );
  183.         
  184.         else if $options instanceof ezcConsoleProgressMonitorOptions 
  185.         {
  186.             $this->options = $options;
  187.         }
  188.         else
  189.         {
  190.             throw new ezcBaseValueException"options"$options"instance of ezcConsoleProgressMonitorOptions" );
  191.         }
  192.     }
  193.  
  194.     /**
  195.      * Returns the currently set options.
  196.      * Returns the currently set option array.
  197.      * 
  198.      * @return ezcConsoleProgressMonitorOptions The options.
  199.      */
  200.     public function getOptions()
  201.     {
  202.         return $this->options;
  203.     }
  204.  
  205.     /**
  206.      * Print a status entry.
  207.      * Prints a new status entry to the console and updates the internal counter.
  208.      *
  209.      * @param string $tag  The tag to print (second argument in the
  210.      *                      formatString).
  211.      * @param string $data The data to be printed in the status entry (third
  212.      *                      argument in the format string).
  213.      * @return void 
  214.      */
  215.     public function addEntry$tag$data )
  216.     {
  217.         $this->counter++;
  218.         $percentage $this->counter / $this->max * 100;
  219.  
  220.         $this->outputHandler->outputLine(
  221.             sprintf(
  222.                 $this->options['formatString'],
  223.                 $percentage,
  224.                 $tag,
  225.                 $data
  226.             )
  227.         );
  228.     }
  229. }
  230. ?>
Documentation generated by phpDocumentor 1.4.3