Skip to content

Commit 324db99

Browse files
committed
Runtime: Add --quiet switch
1 parent 1a22115 commit 324db99

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Name / Shorthand | Type | Description
5151
`--dir`, `-d` | multi | Adds a sources directory to the archive<br/>_Possible dir spec formats:<br/>- `$dir` => include all files in directory<br/>- `$dir:$extension` => filter files on a specific extension_ |n
5252
`--meta`, `-m` | multi | Adds a metadata to the archive<br/>_Metadata must be specified in the `$key:$value` format_ |n
5353
`--shebang-less` | flag | Produce a stub deprived of the shebang directive<br/>_Useful when the phar is meant to be included instead of being executed directly_ |n
54+
`--quiet`, `-q` | flag | Reduce output messages amount: set verbosity level to `INFO` instead of default `DEBUG` |n
5455

5556

5657
### Examples

src/Command/Compile.php

+41-7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Yannoff\Component\Console\Command;
1919
use Yannoff\Component\Console\Definition\Option;
2020
use Yannoff\Component\Console\Exception\RuntimeException;
21+
use Yannoff\Component\Console\IO\Output\Verbosity;
2122
use Yannoff\PhpCodeCompiler\Contents;
2223
use Yannoff\PhpCodeCompiler\Directory;
2324
use Yannoff\PhpCodeCompiler\PharBuilder;
@@ -37,13 +38,16 @@ public function configure()
3738
{
3839
$this
3940
->setHelp('PHP Code compiler - Phar executable compiling utility')
41+
// Compiling options
4042
->addOption('main', 'e', Option::VALUE, 'Set the PHAR stub\'s main entrypoint script')
4143
->addOption('dir', 'd', Option::MULTI, 'Add directory contents ("-d $dir") optionally filtered on a specific file extension ("$dir:$extension")')
4244
->addOption('file', 'f', Option::MULTI, 'Add a single file to the archive')
4345
->addOption('meta', 'm', Option::MULTI, 'Add a metadata property (eg: "-m $key:$value")')
4446
->addOption('output', 'o', Option::VALUE, 'Set the compiled archive output name')
4547
->addOption('banner', 'b', Option::VALUE, 'Load legal notice from the given banner file')
4648
->addOption('shebang-less', '', Option::FLAG, 'Produce a stub deprived of the shebang directive')
49+
// Global options
50+
->addOption('quiet', 'q', Option::FLAG, 'Set output verbosity level to INFO instead of DEBUG')
4751
;
4852
}
4953

@@ -63,6 +67,9 @@ public function execute()
6367

6468
$shebang = (!$this->getOption('shebang-less'));
6569

70+
$quiet = $this->getOption('quiet');
71+
$this->setVerbosity($quiet ? Verbosity::INFO : Verbosity::DEBUG);
72+
6673
$this
6774
->initBuilder($main)
6875
->addFiles($files)
@@ -119,7 +126,7 @@ protected function addFile(string $file)
119126
{
120127
$fullpath = $this->fullpath($file);
121128

122-
$this->info('+ ' . $file, 'grey');
129+
$this->debug('+ ' . $file, 'grey');
123130

124131
// Only minify pure PHP source files, other files such as
125132
// code templates for instance, should be left as-is
@@ -180,7 +187,7 @@ protected function addMetadata(array $definitions): self
180187
foreach ($definitions as $definition) {
181188
list($name, $value) = explode(':', $definition);
182189
$this->info("Adding <strong>$name</strong> metadata property");
183-
$this->info("-> $name: $value", 'grey');
190+
$this->debug("-> $name: $value", 'grey');
184191
$this->builder->addMetadata($name, $value);
185192
}
186193

@@ -200,7 +207,7 @@ protected function setNotice(string $banner = null): self
200207
$this->info("Loading banner contents from <strong>$banner</strong> file...");
201208
$header = $this->commentOut($banner);
202209

203-
$this->info(implode("\n", $header), 'grey');
210+
$this->debug(implode("\n", $header), 'grey');
204211
$this->builder->setBanner($header);
205212
}
206213

@@ -282,17 +289,44 @@ protected function commentOut(string $banner): array
282289
/**
283290
* Print a message to STDERR, optionally encapsulated by styling tags
284291
*
285-
* @param string $message
286-
* @param string $tag
292+
* @param string $message Console-flavour pre-formatted message
293+
* @param int $level Verbosity level (defaults to ERROR)
294+
* @param string $tag Optional encapsulating style tags name
287295
*
288296
* @return self
289297
*/
290-
protected function info(string $message, string $tag = ''): self
298+
protected function print(string $message, int $level = Verbosity::ERROR, string $tag = ''): self
291299
{
292300
$otag = $tag ? "<$tag>" : '';
293301
$ctag = $tag ? "</$tag>" : '';
294-
$this->error(sprintf('%s%s%s', $otag, $message, $ctag));
302+
$this->dmesg(sprintf('%s%s%s', $otag, $message, $ctag), $level);
295303

296304
return $this;
297305
}
306+
307+
/**
308+
* Print an INFO message to standard error
309+
*
310+
* @param string $message Pre-formatted info message
311+
* @param string $tag Optional styling tag name
312+
*
313+
* @return self
314+
*/
315+
protected function info(string $message, string $tag = ''): self
316+
{
317+
return $this->print($message, Verbosity::INFO, $tag);
318+
}
319+
320+
/**
321+
* Print a DEBUG message to standard error
322+
*
323+
* @param string $message Pre-formatted debug message
324+
* @param string $tag Optional styling tag name
325+
*
326+
* @return self
327+
*/
328+
protected function debug(string $message, string $tag = ''): self
329+
{
330+
return $this->print($message, Verbosity::DEBUG, $tag);
331+
}
298332
}

0 commit comments

Comments
 (0)