daux.io/libs/Console/RunAction.php

31 lines
979 B
PHP
Raw Normal View History

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;
trait RunAction
{
protected function getLength($content) {
return function_exists('mb_strlen') ? mb_strlen($content) : strlen($content);
}
2015-07-14 22:06:01 +02:00
protected function runAction($title, OutputInterface $output, $width, \Closure $closure)
{
$output->write($title);
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;
2015-07-14 22:06:01 +02:00
try {
$response = $closure(function ($content) use ($output, &$padding) {
$padding -= $this->getLength($content);
$output->write($content);
});
2015-07-14 22:06:01 +02:00
} catch (\Exception $e) {
2016-07-27 21:32:51 +02:00
$output->writeln(str_pad(' ', $padding) . '[ <fg=red>FAIL</fg=red> ]');
2015-07-14 22:06:01 +02:00
throw $e;
}
2016-07-27 21:32:51 +02:00
$output->writeln(str_pad(' ', $padding) . '[ <fg=green>OK</fg=green> ]');
2015-07-14 22:06:01 +02:00
return $response;
}
}