## 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. */
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. class ezcConsoleStatusbar
59. {
60. /**
61. * Container to hold the properties
62. *
63. * @var array(string=>mixed)
64. */
65. protected $properties ;
67. /**
68. * The ezcConsoleOutput object to use.
69. *
70. * @var ezcConsoleOutput
71. */
72. protected $outputHandler ;
74. /**
75. * Counter for success and failure outputs.
76. *
77. * @var array(bool=>int)
78. */
79. protected $counter = array(
80. true => 0,
81. false => 0,
82. );
84. /**
85. * Creates a new status bar.
86. *
87. * @param ezcConsoleOutput $outHandler Handler to utilize for output
88. * @param array(string=>string) $options Options
89. *
90. * @see ezcConsoleStatusbar::$options
91. */
92. public function __construct ( ezcConsoleOutput $outHandler, array $options = array() )
93. {
94. $this-> outputHandler = $outHandler;
95. $this-> properties ['options'] = new ezcConsoleStatusbarOptions ( $options );
96. }
98. /**
99. * Property read access.
100. *
101. * @param string $key Name of the property.
102. * @return mixed Value of the property or null.
103. *
104. * @throws ezcBasePropertyNotFoundException
105. * If the the desired property is not found.
106. * @ignore
107. */
108. public function __get( $key )
109. {
110. switch ( $key )
111. {
112. case 'options':
113. return $this-> properties ['options'];
114. break;
115. }
116. if ( isset( $this-> properties ['options']->$key ) )
117. {
118. return $this-> properties ['options']->$key;
119. }
120. throw new ezcBasePropertyNotFoundException ( $key );
121. }
123. /**
124. * Set new options.
125. * This method allows you to change the options of a statusbar.
126. *
127. * @param array(string=>string)|ezcConsoleOutputOptions$options The options to set.
128. *
129. * @throws ezcBaseSettingNotFoundException
130. * If you tried to set a non-existent option value.
131. * @throws ezcBaseSettingValueException
132. * If the value is not valid for the desired option.
133. * @throws ezcBaseValueException
134. * If you submit neither an array nor an instance of
135. * ezcConsoleOutputOptions.
136. */
137. public function setOptions ( $options )
138. {
139. if ( is_array ( $options ) )
140. {
141. $this-> properties ['options']->merge( $options );
142. }
143. else if ( $options instanceof ezcConsoleStatusbarOptions )
144. {
145. $this-> properties ['options'] = $options;
146. }
147. else
148. {
149. throw new ezcBaseValueException ( "options", $options, "instance of ezcConsoleStatusbarOptions" );
150. }
151. }
153. /**
154. * Property write access.
155. *
156. * @param string $key Name of the property.
157. * @param mixed $val The value for the property.
158. *
159. * @throws ezcBasePropertyNotFoundException
160. * If a desired property could not be found.
161. * @throws ezcBaseValueException
162. * If a desired property value is out of range.
163. * @ignore
164. */
165. public function __set( $key, $val )
166. {
167. switch ( $key )
168. {
169. // Those two are here for BC reasons only, it is proper to
170. // use $statusbar->options->successChar instead of just
171. // $statusbar->successChar.
172. case 'successChar':
173. case 'failureChar':
174. // No checks necessary here, already performed in
175. // ezcConsoleStatusbarOptions
176. break;
177. case "options":
178. if ( ( $val instanceof ezcConsoleStatusbarOptions ) === false )
179. {
180. throw new ezcBaseValueException ( $key, $val, 'ezcConsoleStatusbarOptions' );
181. }
182. $this-> properties [$key] = $val;
183. return;
184. default:
185. throw new ezcBasePropertyNotFoundException ( $key );
186. }
187. $this-> properties ['options'][$key] = $val;
188. }
190. /**
191. * Property isset access.
192. *
193. * @param string $key Name of the property.
194. * @return bool True is the property is set, otherwise false.
195. * @ignore
196. */
197. public function __isset( $key )
198. {
199. return isset( $this-> properties ['options'][$key] ) || isset( $this-> properties [$key] );
200. }
202. /**
203. * Returns the current options.
204. * Returns the options currently set for this progressbar.
205. *
206. * @return ezcConsoleStatusbarOptions The current options.
207. */
208. public function getOptions ()
209. {
210. return $this-> properties ['options'];
211. }
213. /**
214. * Add a status to the status bar.
215. * Adds a new status to the bar which is printed immediately. If the
216. * cursor is currently not at the beginning of a line, it will move to
217. * the next line.
218. *
219. * @param bool $status Print successChar on true, failureChar on false.
220. * @return void
221. */
222. public function add ( $status )
223. {
224. if ( is_bool ( $status ) === false )
225. {
226. trigger_error ( 'Unknown status '.var_export ( $status, true ).'.', E_USER_WARNING );
227. $status = (bool) $status;
228. }
229. switch ( $status )
230. {
231. case true:
232. $this-> outputHandler -> outputText ( $this-> properties ['options']['successChar'], 'success' );
233. break;
235. case false:
236. $this-> outputHandler -> outputText ( $this-> properties ['options']['failureChar'], 'failure' );
237. break;
238. }
239. $this-> counter [$status]++;
240. }
242. /**
243. * Reset the state of the status-bar object to its initial one.
244. *
245. * @return void
246. */
247. public function reset ()
248. {
249. foreach ( $this-> counter as $status => $count )
250. {
251. $this-> counter [$status] = 0;
252. }
253. }
255. /**
256. * Returns number of successes during the run.
257. * Returns the number of success characters printed from this status bar.
258. *
259. * @return int Number of successes.
260. */
261. public function getSuccessCount ()
262. {
263. return $this-> counter [true];
264. }
266. /**
267. * Returns number of failures during the run.
268. * Returns the number of failure characters printed from this status bar.
269. *
270. * @return int Number of failures.
271. */
272. public function getFailureCount ()
273. {
274. return $this-> counter [false];
275. }
276. }
277. ?>

Documentation generated by phpDocumentor 1.4.3