File tree Expand file tree Collapse file tree 3 files changed +72
-6
lines changed Expand file tree Collapse file tree 3 files changed +72
-6
lines changed Original file line number Diff line number Diff line change
1
+ <?php
2
+ /**
3
+ * ProgressBar
4
+ *
5
+ * @author Piotr Olaszewski
6
+ */
7
+ namespace Psf \Helpers ;
8
+
9
+ use Psf \Output \Writer ;
10
+
11
+ class ProgressBar
12
+ {
13
+ /**
14
+ * @var Writer
15
+ */
16
+ private $ _output ;
17
+ private $ _size = 0 ;
18
+ private $ _current = 0 ;
19
+ private $ _percent = 0 ;
20
+ private $ _counterMask = '{current}/{all} ({percent}%) ' ;
21
+
22
+ public function initialize (Writer $ output , $ size )
23
+ {
24
+ $ this ->_output = $ output ;
25
+ $ this ->_size = $ size ;
26
+ }
27
+
28
+ public function increment ()
29
+ {
30
+ $ this ->_current ++;
31
+ $ this ->_calculatePercent ();
32
+ if ($ this ->_current == $ this ->_size ) {
33
+ $ this ->_output ->writeMessage ($ this ->_fillMask ());
34
+ } else {
35
+ $ this ->_output ->writeMessage ($ this ->_fillMask (), 1 , Writer::CR );
36
+ }
37
+ }
38
+
39
+ private function _calculatePercent ()
40
+ {
41
+ $ this ->_percent = round ($ this ->_current * 100 / $ this ->_size );
42
+ }
43
+
44
+ private function _fillMask ()
45
+ {
46
+ $ matches = array (
47
+ '{current} ' => $ this ->_current ,
48
+ '{all} ' => $ this ->_size ,
49
+ '{percent} ' => $ this ->_percent
50
+ );
51
+ return str_replace (array_keys ($ matches ), array_values ($ matches ), $ this ->_counterMask );
52
+ }
53
+ }
Original file line number Diff line number Diff line change @@ -15,6 +15,8 @@ class Writer
15
15
const VERBOSITY_QUIET = 0 ;
16
16
const VERBOSITY_NORMAL = 1 ;
17
17
const VERBOSITY_VERBOSE = 2 ;
18
+ const LF = PHP_EOL ;
19
+ const CR = "\r" ;
18
20
19
21
private $ _streamHandle ;
20
22
private $ _formatters = array ();
@@ -31,12 +33,12 @@ public function setFormatter($xmlTag, StyleFormatter $formatter)
31
33
$ this ->_formatters [$ xmlTag ] = $ formatter ;
32
34
}
33
35
34
- public function writeMessage ($ message , $ numberOfNewLines = 1 )
36
+ public function writeMessage ($ message , $ numberOfNewLines = 1 , $ eol = self :: LF )
35
37
{
36
38
if ($ this ->_checkVerbosityCanNotWrite ()) {
37
39
return false ;
38
40
}
39
- $ messageWithNewLines = $ message . str_repeat (PHP_EOL , $ numberOfNewLines );
41
+ $ messageWithNewLines = $ message . str_repeat ($ eol , $ numberOfNewLines );
40
42
$ parsedMessage = $ this ->_parseMessage ($ messageWithNewLines );
41
43
fwrite ($ this ->_streamHandle , $ parsedMessage );
42
44
}
Original file line number Diff line number Diff line change 1
1
#!/usr/bin/env php
2
2
<?php
3
+ error_reporting (E_ALL );
4
+ $ isComposer = true ;
5
+
3
6
use Psf \Dispatcher ;
4
7
use Psf \Loader ;
5
8
6
- error_reporting (E_ALL );
7
9
define ('ROOT_PATH ' , realpath (dirname (__FILE__ )) . DIRECTORY_SEPARATOR );
8
- define ('APPLICATION_PATH ' , __DIR__ . DIRECTORY_SEPARATOR . '.. ' . DIRECTORY_SEPARATOR . '.. ' . DIRECTORY_SEPARATOR . '.. ' . DIRECTORY_SEPARATOR );
9
10
10
- $ psfConfig = require_once APPLICATION_PATH . 'psf.conf.php ' ;
11
- require_once ROOT_PATH . DIRECTORY_SEPARATOR . 'lib ' . DIRECTORY_SEPARATOR . 'Psf ' . DIRECTORY_SEPARATOR . 'Loader.php ' ;
11
+ if ($ isComposer ) {
12
+ define ('APPLICATION_PATH ' , __DIR__ . DIRECTORY_SEPARATOR . '.. ' . DIRECTORY_SEPARATOR . '.. ' . DIRECTORY_SEPARATOR . '.. ' . DIRECTORY_SEPARATOR );
13
+ /** @noinspection PhpIncludeInspection */
14
+ $ psfConfig = require_once APPLICATION_PATH . 'psf.conf.php ' ;
15
+ } else {
16
+ define ('APPLICATION_PATH ' , __DIR__ . DIRECTORY_SEPARATOR );
17
+ /** @noinspection PhpIncludeInspection */
18
+ $ psfConfig = require_once APPLICATION_PATH . 'config/psf.conf.php ' ;
19
+ }
20
+
21
+ /** @noinspection PhpIncludeInspection */
22
+ require_once ROOT_PATH . 'lib ' . DIRECTORY_SEPARATOR . 'Psf ' . DIRECTORY_SEPARATOR . 'Loader.php ' ;
12
23
13
24
$ loader = new Loader ();
14
25
$ loader
You can’t perform that action at this time.
0 commit comments