-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
174ea3f
commit a65912c
Showing
9 changed files
with
192 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php declare(strict_types=1); | ||
/* | ||
* This file is part of the phpcheck package. | ||
* | ||
* ©Marlin Forbes <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
namespace Datashaman\PHPCheck\Subscribers; | ||
|
||
use Datashaman\PHPCheck\Runner; | ||
use ReflectionMethod; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
abstract class Subscriber implements EventSubscriberInterface | ||
{ | ||
protected $input; | ||
|
||
protected $output; | ||
|
||
protected $runner; | ||
|
||
protected $state; | ||
|
||
public function __construct(Runner $runner) | ||
{ | ||
$this->input = $runner->getInput(); | ||
$this->output = $runner->getOutput(); | ||
$this->runner = $runner; | ||
$this->state = $runner->getState(); | ||
} | ||
|
||
protected function getMethodSignature(ReflectionMethod $method): string | ||
{ | ||
return $method->getDeclaringClass()->getName() . '::' . $method->getName(); | ||
} | ||
|
||
protected function convertBytes(int $bytes): string | ||
{ | ||
if ($bytes == 0) { | ||
return '0.00 B'; | ||
} | ||
|
||
$suffixes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']; | ||
$exponent = (int) \floor(\log($bytes, 1024)); | ||
|
||
return \sprintf('%.2f %s', \round($bytes / 1024 ** $exponent, 2), $suffixes[$exponent]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php declare(strict_types=1); | ||
/* | ||
* This file is part of the phpcheck package. | ||
* | ||
* ©Marlin Forbes <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
namespace Datashaman\PHPCheck\Subscribers; | ||
|
||
use Datashaman\PHPCheck\CheckEvents; | ||
use Datashaman\PHPCheck\Events; | ||
use ReflectionClass; | ||
|
||
class TextReporter extends Reporter | ||
{ | ||
protected $file; | ||
|
||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
CheckEvents::START_ALL => 'onStartAll', | ||
CheckEvents::START => 'onStart', | ||
CheckEvents::SUCCESS => 'onSuccess', | ||
CheckEvents::ERROR => 'onError', | ||
CheckEvents::FAILURE => 'onFailure', | ||
CheckEvents::END_ALL => 'onEndAll', | ||
]; | ||
} | ||
|
||
public function onStartAll(Events\StartAllEvent $event): void | ||
{ | ||
$this->file = fopen($this->input->getOption('log-text'), 'a'); | ||
$this->report('onStartAll', $event); | ||
} | ||
|
||
public function onEndAll(Events\EndAllEvent $event): void | ||
{ | ||
$this->report('onEndAll', $event); | ||
|
||
if (is_resource($this->file)) { | ||
fclose($this->file); | ||
} | ||
} | ||
|
||
public function __call(string $name, array $args) | ||
{ | ||
$this->report($name, ...$args); | ||
} | ||
|
||
protected function report(string $name, Events\Event $event) | ||
{ | ||
$shortName = preg_replace( | ||
'/Event$/', | ||
'', | ||
(new ReflectionClass($event))->getShortName() | ||
); | ||
$message = sprintf( | ||
'%s [%-10s]', | ||
strftime('%F %T', (int) $event->time), | ||
strtoupper($shortName) | ||
); | ||
|
||
if ( | ||
in_array( | ||
$name, | ||
[ | ||
'onError', | ||
'onFailure', | ||
'onStart', | ||
'onSuccess', | ||
] | ||
) | ||
) { | ||
$message .= ' ' . $this->getMethodSignature($event->method); | ||
} | ||
|
||
if ( | ||
$event instanceof Events\ResultEvent | ||
) { | ||
if (!is_null($event->args)) { | ||
$args = preg_replace( | ||
[ | ||
'/^\[/', | ||
'/\]$/', | ||
], | ||
'', | ||
json_encode($event->args) | ||
); | ||
|
||
$message .= '(' . $args . ')'; | ||
} | ||
|
||
if ($event->cause) { | ||
$message .= ' caused ' . get_class($event->cause) . '("' . $event->cause->getMessage() . '")'; | ||
} | ||
} | ||
|
||
$message .= "\n"; | ||
|
||
fwrite($this->file, $message); | ||
} | ||
} |