Skip to content

Commit

Permalink
Quiet mode. (#146)
Browse files Browse the repository at this point in the history
- quiet mode = not verbose
- phpoole/library 2.2.2
- debug mode
- dedicated error messages
  • Loading branch information
ArnaudLigny authored Aug 12, 2018
1 parent 4c44530 commit 547acc4
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 24 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
"require": {
"php": "^7.1",
"phpoole/library": "2.2.2",
"phpoole/library": "2.3.0",
"padraic/humbug_get_contents": "1.1.2",
"padraic/phar-updater": "1.0.6",
"symfony/filesystem": "4.1.2",
Expand Down
18 changes: 9 additions & 9 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,18 @@
],
[
'name' => 'build',
'route' => '[<path>] [--drafts|-d] [--baseurl=]',
'route' => '[<path>] [--drafts|-d] [--baseurl=] [--quiet|-q]',
'aliases' => [
'd' => 'drafts',
'q' => 'quiet',
],
'short_description' => 'Build the website',
'description' => 'Build the website.',
'options_descriptions' => [
'<path>' => 'Website path',
'--drafts|-d' => 'Include drafts',
'--baseurl' => 'Base URL',
'--quiet|-q' => 'Not verbose messages',
],
'defaults' => [
'path' => getcwd(),
Expand Down
63 changes: 50 additions & 13 deletions src/Command/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ abstract class AbstractCommand
* @var int
*/
protected $pbMax = 0;
/**
* @var bool
*/
protected $debug = false;
/**
* @var bool
*/
protected $quiet = false;

/**
* Start command processing.
Expand Down Expand Up @@ -113,14 +121,16 @@ public function getPath()
*/
protected function newPB($start, $max)
{
if ($this->progressBar == null || $max != $this->pbMax) {
if ($this->progressBar === null || $max != $this->pbMax) {
$this->pbMax = $max;
$adapter = new \Zend\ProgressBar\Adapter\Console([
'elements' => [
\Zend\ProgressBar\Adapter\Console::ELEMENT_PERCENT,
\Zend\ProgressBar\Adapter\Console::ELEMENT_BAR,
\Zend\ProgressBar\Adapter\Console::ELEMENT_TEXT,
], ]);
],
'textWidth' => 30,
]);
$this->progressBar = new ProgressBar($adapter, $start, $max);
}

Expand All @@ -142,11 +152,22 @@ protected function getPB()
*/
public function getPHPoole(array $options = [])
{
$messageCallback = function ($code, $message = '', $itemsCount = 0, $itemsMax = 0, $verbose = false) {
// debug mode?
if (array_key_exists('debug', $options) && $options['debug']) {
$this->debug = true;
}
// quiet mode?
if (array_key_exists('quiet', $options) && $options['quiet']) {
$this->quiet = true;
}

// CLI custom message callback function
$messageCallback = function ($code, $message = '', $itemsCount = 0, $itemsMax = 0) {
switch ($code) {
case 'CREATE':
case 'CONVERT':
case 'GENERATE':
case 'MENU':
case 'COPY':
case 'RENDER':
case 'TIME':
Expand All @@ -155,26 +176,42 @@ public function getPHPoole(array $options = [])
case 'CREATE_PROGRESS':
case 'CONVERT_PROGRESS':
case 'GENERATE_PROGRESS':
case 'MENU_PROGRESS':
case 'COPY_PROGRESS':
case 'RENDER_PROGRESS':
if ($verbose) {
if ($itemsCount > 0 && $verbose !== false) {
$this->wlDone(sprintf("\r (%u/%u) %s", $itemsCount, $itemsMax, $message));
if ($this->debug) {
if ($itemsCount > 0) {
$this->wlDone(sprintf('(%u/%u) %s', $itemsCount, $itemsMax, $message));
break;
}
$this->wlDone(" $message");
$this->wlDone("$message");
} else {
if ($itemsMax && $itemsCount) {
$this->newPB(1, $itemsMax);
$this->getPB()->update($itemsCount, "$message");
} else {
$this->wl($message);
if (!$this->quiet) {
if ($itemsMax && $itemsCount) {
$this->newPB(1, $itemsMax);
$this->getPB()->update($itemsCount, "$message");
if ($itemsCount == $itemsMax) {
$this->getPB()->update($itemsCount, "[$itemsCount/$itemsMax]");
$this->getPB()->finish();
}
} else {
$this->wl($message);
}
}
}
break;
case 'CREATE_ERROR':
case 'CONVERT_ERROR':
case 'GENERATE_ERROR':
case 'MENU_ERROR':
case 'COPY_ERROR':
case 'RENDER_ERROR':
$this->wlError($message);
break;
}
};

// instanciate PHPoole?
if (!$this->phpoole instanceof PHPoole) {
if (!file_exists($this->getPath().'/'.self::CONFIG_FILE)) {
throw new \Exception(sprintf('Config file not found in "%s"!', $this->getPath()));
Expand Down Expand Up @@ -231,7 +268,7 @@ public function wlAlert($text)
}

/**
* @param $text
* @param string $text
*/
public function wlError($text)
{
Expand Down
8 changes: 8 additions & 0 deletions src/Command/Build.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@ class Build extends AbstractCommand
* @var string
*/
protected $baseurl;
/**
* @var bool
*/
protected $quiet;

public function processCommand()
{
$this->drafts = $this->route->getMatchedParam('drafts', false);
$this->baseurl = $this->route->getMatchedParam('baseurl');
$this->quiet = $this->route->getMatchedParam('quiet', false);

$message = 'Building website%s...';

Expand All @@ -36,6 +41,9 @@ public function processCommand()
if ($this->baseurl) {
$options['site']['baseurl'] = $this->baseurl;
}
if ($this->quiet) {
$options['quiet'] = true;
}

$this->wl(sprintf($message, $messageOpt));

Expand Down

0 comments on commit 547acc4

Please sign in to comment.