PageRenderTime 136ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/ConsoleTools/src/statusbar.php

https://github.com/Yannix/zetacomponents
PHP | 277 lines | 105 code | 14 blank | 158 comment | 10 complexity | d42d9c0ca646b1d1acf98826cf970825 MD5 | raw file
  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. * Creating and maintaining status-bars to be printed to the console.
  29. *
  30. * <code>
  31. * // Construction
  32. * $status = new ezcConsoleStatusbar( new ezcConsoleOutput() );
  33. *
  34. * // Set option
  35. * $status->options['successChar'] = '*';
  36. *
  37. * // Run statusbar
  38. * foreach ( $files as $file )
  39. * {
  40. * $res = $file->upload();
  41. * // Add status if form of bool true/false to statusbar.
  42. * $status->add( $res ); // $res is true or false
  43. * }
  44. *
  45. * // Retreive and display final statusbar results
  46. * $msg = $status->getSuccess() . ' succeeded, ' . $status->getFailure() . ' failed.';
  47. * $out->outputText( "Finished uploading files: $msg\n" );
  48. * </code>
  49. *
  50. * @property ezcConsoleStatusbarOptions $options
  51. * Contains the options for this class.
  52. *
  53. * @package ConsoleTools
  54. * @version //autogen//
  55. * @mainclass
  56. */
  57. class ezcConsoleStatusbar
  58. {
  59. /**
  60. * Container to hold the properties
  61. *
  62. * @var array(string=>mixed)
  63. */
  64. protected $properties;
  65. /**
  66. * The ezcConsoleOutput object to use.
  67. *
  68. * @var ezcConsoleOutput
  69. */
  70. protected $outputHandler;
  71. /**
  72. * Counter for success and failure outputs.
  73. *
  74. * @var array(bool=>int)
  75. */
  76. protected $counter = array(
  77. true => 0,
  78. false => 0,
  79. );
  80. /**
  81. * Creates a new status bar.
  82. *
  83. * @param ezcConsoleOutput $outHandler Handler to utilize for output
  84. * @param array(string=>string) $options Options
  85. *
  86. * @see ezcConsoleStatusbar::$options
  87. */
  88. public function __construct( ezcConsoleOutput $outHandler, array $options = array() )
  89. {
  90. $this->outputHandler = $outHandler;
  91. $this->properties['options'] = new ezcConsoleStatusbarOptions( $options );
  92. }
  93. /**
  94. * Property read access.
  95. *
  96. * @param string $key Name of the property.
  97. * @return mixed Value of the property or null.
  98. *
  99. * @throws ezcBasePropertyNotFoundException
  100. * If the the desired property is not found.
  101. * @ignore
  102. */
  103. public function __get( $key )
  104. {
  105. switch ( $key )
  106. {
  107. case 'options':
  108. return $this->properties['options'];
  109. break;
  110. }
  111. if ( isset( $this->properties['options']->$key ) )
  112. {
  113. return $this->properties['options']->$key;
  114. }
  115. throw new ezcBasePropertyNotFoundException( $key );
  116. }
  117. /**
  118. * Set new options.
  119. * This method allows you to change the options of a statusbar.
  120. *
  121. * @param array(string=>string)|ezcConsoleOutputOptions $options The options to set.
  122. *
  123. * @throws ezcBaseSettingNotFoundException
  124. * If you tried to set a non-existent option value.
  125. * @throws ezcBaseSettingValueException
  126. * If the value is not valid for the desired option.
  127. * @throws ezcBaseValueException
  128. * If you submit neither an array nor an instance of
  129. * ezcConsoleOutputOptions.
  130. */
  131. public function setOptions( $options )
  132. {
  133. if ( is_array( $options ) )
  134. {
  135. $this->properties['options']->merge( $options );
  136. }
  137. else if ( $options instanceof ezcConsoleStatusbarOptions )
  138. {
  139. $this->properties['options'] = $options;
  140. }
  141. else
  142. {
  143. throw new ezcBaseValueException( "options", $options, "instance of ezcConsoleStatusbarOptions" );
  144. }
  145. }
  146. /**
  147. * Property write access.
  148. *
  149. * @param string $key Name of the property.
  150. * @param mixed $val The value for the property.
  151. *
  152. * @throws ezcBasePropertyNotFoundException
  153. * If a desired property could not be found.
  154. * @throws ezcBaseValueException
  155. * If a desired property value is out of range.
  156. * @ignore
  157. */
  158. public function __set( $key, $val )
  159. {
  160. switch ( $key )
  161. {
  162. // Those two are here for BC reasons only, it is proper to
  163. // use $statusbar->options->successChar instead of just
  164. // $statusbar->successChar.
  165. case 'successChar':
  166. case 'failureChar':
  167. // No checks necessary here, already performed in
  168. // ezcConsoleStatusbarOptions
  169. break;
  170. case "options":
  171. if ( ( $val instanceof ezcConsoleStatusbarOptions ) === false )
  172. {
  173. throw new ezcBaseValueException( $key, $val, 'ezcConsoleStatusbarOptions' );
  174. }
  175. $this->properties[$key] = $val;
  176. return;
  177. default:
  178. throw new ezcBasePropertyNotFoundException( $key );
  179. }
  180. $this->properties['options'][$key] = $val;
  181. }
  182. /**
  183. * Property isset access.
  184. *
  185. * @param string $key Name of the property.
  186. * @return bool True is the property is set, otherwise false.
  187. * @ignore
  188. */
  189. public function __isset( $key )
  190. {
  191. return isset( $this->properties['options'][$key] ) || isset( $this->properties[$key] );
  192. }
  193. /**
  194. * Returns the current options.
  195. * Returns the options currently set for this progressbar.
  196. *
  197. * @return ezcConsoleStatusbarOptions The current options.
  198. */
  199. public function getOptions()
  200. {
  201. return $this->properties['options'];
  202. }
  203. /**
  204. * Add a status to the status bar.
  205. * Adds a new status to the bar which is printed immediately. If the
  206. * cursor is currently not at the beginning of a line, it will move to
  207. * the next line.
  208. *
  209. * @param bool $status Print successChar on true, failureChar on false.
  210. * @return void
  211. */
  212. public function add( $status )
  213. {
  214. if ( is_bool( $status ) === false )
  215. {
  216. trigger_error( 'Unknown status '.var_export( $status, true ).'.', E_USER_WARNING );
  217. $status = (bool) $status;
  218. }
  219. switch ( $status )
  220. {
  221. case true:
  222. $this->outputHandler->outputText( $this->properties['options']['successChar'], 'success' );
  223. break;
  224. case false:
  225. $this->outputHandler->outputText( $this->properties['options']['failureChar'], 'failure' );
  226. break;
  227. }
  228. $this->counter[$status]++;
  229. }
  230. /**
  231. * Reset the state of the status-bar object to its initial one.
  232. *
  233. * @return void
  234. */
  235. public function reset()
  236. {
  237. foreach ( $this->counter as $status => $count )
  238. {
  239. $this->counter[$status] = 0;
  240. }
  241. }
  242. /**
  243. * Returns number of successes during the run.
  244. * Returns the number of success characters printed from this status bar.
  245. *
  246. * @return int Number of successes.
  247. */
  248. public function getSuccessCount()
  249. {
  250. return $this->counter[true];
  251. }
  252. /**
  253. * Returns number of failures during the run.
  254. * Returns the number of failure characters printed from this status bar.
  255. *
  256. * @return int Number of failures.
  257. */
  258. public function getFailureCount()
  259. {
  260. return $this->counter[false];
  261. }
  262. }
  263. ?>