Skip to content

Commit d7f8e59

Browse files
committed
Start working with progress bar
1 parent 23a96b2 commit d7f8e59

File tree

3 files changed

+72
-6
lines changed

3 files changed

+72
-6
lines changed

lib/Psf/Helpers/ProgressBar.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
}

lib/Psf/Output/Writer.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ class Writer
1515
const VERBOSITY_QUIET = 0;
1616
const VERBOSITY_NORMAL = 1;
1717
const VERBOSITY_VERBOSE = 2;
18+
const LF = PHP_EOL;
19+
const CR = "\r";
1820

1921
private $_streamHandle;
2022
private $_formatters = array();
@@ -31,12 +33,12 @@ public function setFormatter($xmlTag, StyleFormatter $formatter)
3133
$this->_formatters[$xmlTag] = $formatter;
3234
}
3335

34-
public function writeMessage($message, $numberOfNewLines = 1)
36+
public function writeMessage($message, $numberOfNewLines = 1, $eol = self::LF)
3537
{
3638
if ($this->_checkVerbosityCanNotWrite()) {
3739
return false;
3840
}
39-
$messageWithNewLines = $message . str_repeat(PHP_EOL, $numberOfNewLines);
41+
$messageWithNewLines = $message . str_repeat($eol, $numberOfNewLines);
4042
$parsedMessage = $this->_parseMessage($messageWithNewLines);
4143
fwrite($this->_streamHandle, $parsedMessage);
4244
}

psf.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
#!/usr/bin/env php
22
<?php
3+
error_reporting(E_ALL);
4+
$isComposer = true;
5+
36
use Psf\Dispatcher;
47
use Psf\Loader;
58

6-
error_reporting(E_ALL);
79
define('ROOT_PATH', realpath(dirname(__FILE__)) . DIRECTORY_SEPARATOR);
8-
define('APPLICATION_PATH', __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR);
910

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';
1223

1324
$loader = new Loader();
1425
$loader

0 commit comments

Comments
 (0)