8
0
daux.io/libs/Console/RunAction.php

44 Zeilen
1.3 KiB
PHP

2015-07-23 17:44:24 +02:00
<?php namespace Todaymade\Daux\Console;
2015-07-14 22:06:01 +02:00
use Symfony\Component\Console\Output\OutputInterface;
2018-06-05 20:31:51 +02:00
use Todaymade\Daux\Daux;
2015-07-14 22:06:01 +02:00
trait RunAction
{
2020-04-22 21:55:53 +02:00
protected function getLength($content)
{
return function_exists('mb_strlen') ? mb_strlen($content) : strlen($content);
}
2018-06-05 20:31:51 +02:00
protected function runAction($title, $width, \Closure $closure)
2015-07-14 22:06:01 +02:00
{
2018-06-05 20:31:51 +02:00
$verbose = Daux::getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE;
Daux::write($title, $verbose);
2015-07-23 17:44:24 +02:00
2015-07-14 22:06:01 +02:00
// 8 is the length of the label + 2 let it breathe
$padding = $width - $this->getLength($title) - 10;
2018-06-05 20:31:51 +02:00
2015-07-14 22:06:01 +02:00
try {
2020-04-22 21:55:53 +02:00
$response = $closure(function ($content) use (&$padding, $verbose) {
$padding -= $this->getLength($content);
2018-06-05 20:31:51 +02:00
Daux::write($content, $verbose);
});
2015-07-14 22:06:01 +02:00
} catch (\Exception $e) {
2018-06-05 20:31:51 +02:00
$this->status($padding, '[ <fg=red>FAIL</fg=red> ]');
2020-04-22 22:24:52 +02:00
2015-07-14 22:06:01 +02:00
throw $e;
}
2018-06-05 20:31:51 +02:00
$this->status($padding, '[ <fg=green>OK</fg=green> ]');
2015-07-14 22:06:01 +02:00
return $response;
}
2018-06-05 20:31:51 +02:00
protected function status($padding, $content)
{
$verbose = Daux::getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE;
$padding = $verbose ? '' : str_pad(' ', $padding);
Daux::writeln($padding . $content);
}
2015-07-14 22:06:01 +02:00
}