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

Documentation generated by phpDocumentor 1.4.3