Skip to content

Commit

Permalink
fixing pint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
vasu authored and vasu committed Oct 26, 2022
1 parent bd2b9a4 commit 35b691e
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 83 deletions.
62 changes: 34 additions & 28 deletions src/CLI/CLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,17 @@ class CLI
/**
* CLI constructor.
*
* @param array $args
* @param array $args
*
* @throws Exception
*/
public function __construct(array $args = [])
{
if (\php_sapi_name() !== "cli") {
if (\php_sapi_name() !== 'cli') {
throw new Exception('CLI tasks can only work from the command line');
}

$this->args = $this->parse((!empty($args) || !isset($_SERVER['argv'])) ? $args : $_SERVER['argv']);
$this->args = $this->parse((! empty($args) || ! isset($_SERVER['argv'])) ? $args : $_SERVER['argv']);

$this->error = function (Exception $error): void {
Console::error($error->getMessage());
Expand All @@ -104,6 +105,7 @@ public function init(): Hook
{
$hook = new Hook();
$this->init[] = $hook;

return $hook;
}

Expand All @@ -118,6 +120,7 @@ public function shutdown(): Hook
{
$hook = new Hook();
$this->shutdown[] = $hook;

return $hook;
}

Expand All @@ -132,6 +135,7 @@ public function error(): Hook
{
$hook = new Hook();
$this->errors[] = $hook;

return $hook;
}

Expand All @@ -140,8 +144,7 @@ public function error(): Hook
*
* Add a new command task
*
* @param string $name
*
* @param string $name
* @return Task
*/
public function task(string $name): Task
Expand All @@ -156,16 +159,17 @@ public function task(string $name): Task
/**
* If a resource has been created return it, otherwise create it and then return it
*
* @param string $name
* @param bool $fresh
* @param string $name
* @param bool $fresh
* @return mixed
*
* @throws Exception
*/
public function getResource(string $name, bool $fresh = false): mixed
{
if (!\array_key_exists($name, $this->resources) || $fresh || self::$resourcesCallbacks[$name]['reset']) {
if (!\array_key_exists($name, self::$resourcesCallbacks)) {
throw new Exception('Failed to find resource: "' . $name . '"');
if (! \array_key_exists($name, $this->resources) || $fresh || self::$resourcesCallbacks[$name]['reset']) {
if (! \array_key_exists($name, self::$resourcesCallbacks)) {
throw new Exception('Failed to find resource: "'.$name.'"');
}

$this->resources[$name] = \call_user_func_array(
Expand All @@ -182,7 +186,7 @@ public function getResource(string $name, bool $fresh = false): mixed
/**
* Get Resources By List
*
* @param array $list
* @param array $list
* @return array
*/
public function getResources(array $list): array
Expand All @@ -199,13 +203,12 @@ public function getResources(array $list): array
/**
* Set a new resource callback
*
* @param string $name
* @param callable $callback
* @param array $injections
* @param string $name
* @param callable $callback
* @param array $injections
* @return void
*
* @throws Exception
*
* @return void
*/
public static function setResource(string $name, callable $callback, array $injections = []): void
{
Expand All @@ -215,9 +218,10 @@ public static function setResource(string $name, callable $callback, array $inje
/**
* task-name --foo=test
*
* @param array $args
* @throws Exception
* @param array $args
* @return array
*
* @throws Exception
*/
public function parse(array $args): array
{
Expand All @@ -244,7 +248,7 @@ public function parse(array $args): array
unset($arg);

foreach ($args as $arg) {
$pair = explode("=", $arg);
$pair = explode('=', $arg);
$key = $pair[0];
$value = $pair[1];
$output[$key][] = $value;
Expand Down Expand Up @@ -277,7 +281,7 @@ public function match(): ?Task
* Get Params
* Get runtime params for the provided Hook
*
* @param Hook $hook
* @param Hook $hook
* @return array
*/
protected function getParams(Hook $hook): array
Expand All @@ -297,6 +301,7 @@ protected function getParams(Hook $hook): array
}

ksort($params);

return $params;
}

Expand Down Expand Up @@ -359,9 +364,10 @@ public function getArgs(): array
*
* Creates an validator instance and validate given value with given rules.
*
* @param string $key
* @param array $param
* @param mixed $value
* @param string $key
* @param array $param
* @param mixed $value
*
* @throws Exception
*/
protected function validate(string $key, array $param, $value): void
Expand All @@ -375,16 +381,16 @@ protected function validate(string $key, array $param, $value): void
}

// is the validator object an instance of the Validator class
if (!$validator instanceof Validator) {
if (! $validator instanceof Validator) {
throw new Exception('Validator object is not an instance of the Validator class', 500);
}

if (!$validator->isValid($value)) {
throw new Exception('Invalid ' . $key . ': ' . $validator->getDescription(), 400);
if (! $validator->isValid($value)) {
throw new Exception('Invalid '.$key.': '.$validator->getDescription(), 400);
}
} else {
if (!$param['optional']) {
throw new Exception('Param "' . $key . '" is not optional.', 400);
if (! $param['optional']) {
throw new Exception('Param "'.$key.'" is not optional.', 400);
}
}
}
Expand Down
61 changes: 31 additions & 30 deletions src/CLI/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Console
*
* Sets the process title visible in tools such as top and ps.
*
* @param string $title
* @param string $title
* @return bool
*/
public static function title(string $title): bool
Expand All @@ -22,84 +22,84 @@ public static function title(string $title): bool
*
* Log messages to console
*
* @param string $message
* @param string $message
* @return bool|int
*/
public static function log(string $message): int|false
{
return \fwrite(STDOUT, $message . "\n");
return \fwrite(STDOUT, $message."\n");
}

/**
* Success
*
* Log success messages to console
*
* @param string $message
* @param string $message
* @return bool|int
*/
public static function success(string $message): int|false
{
return \fwrite(STDOUT, "\033[32m" . $message . "\033[0m\n");
return \fwrite(STDOUT, "\033[32m".$message."\033[0m\n");
}

/**
* Error
*
* Log error messages to console
*
* @param string $message
* @param string $message
* @return bool|int
*/
public static function error(string $message): int|false
{
return \fwrite(STDERR, "\033[31m" . $message . "\033[0m\n");
return \fwrite(STDERR, "\033[31m".$message."\033[0m\n");
}

/**
* Info
*
* Log informative messages to console
*
* @param string $message
* @param string $message
* @return bool|int
*/
public static function info(string $message): int|false
{
return \fwrite(STDOUT, "\033[34m" . $message . "\033[0m\n");
return \fwrite(STDOUT, "\033[34m".$message."\033[0m\n");
}

/**
* Warning
*
* Log warning messages to console
*
* @param string $message
* @param string $message
* @return bool|int
*/
public static function warning(string $message): int|false
{
return \fwrite(STDERR, "\033[1;33m" . $message . "\033[0m\n");
return \fwrite(STDERR, "\033[1;33m".$message."\033[0m\n");
}

/**
* Warning
*
* Log warning messages to console
*
* @param string $question
* @param string $question
* @return string
*/
public static function confirm(string $question): string
{
if (!self::isInteractive()) {
if (! self::isInteractive()) {
return '';
}

self::log($question);

$handle = \fopen('php://stdin', 'r');
$line = \trim(\fgets($handle));
$line = \trim(\fgets($handle));

\fclose($handle);

Expand All @@ -111,7 +111,7 @@ public static function confirm(string $question): string
*
* Log warning messages to console
*
* @param string $message
* @param string $message
* @return void
*/
public static function exit(int $status = 0): void
Expand All @@ -124,21 +124,21 @@ public static function exit(int $status = 0): void
*
* This function was inspired by: https://stackoverflow.com/a/13287902/2299554
*
* @param string $cmd
* @param string $stdin
* @param string $stdout
* @param string $stderr
* @param int $timeout
* @param string $cmd
* @param string $stdin
* @param string $stdout
* @param string $stderr
* @param int $timeout
* @return int
*/
public static function execute(string $cmd, string $stdin, string &$stdout, string &$stderr, int $timeout = -1): int
{
$cmd = '( ' . $cmd . ' ) 3>/dev/null ; echo $? >&3';
$cmd = '( '.$cmd.' ) 3>/dev/null ; echo $? >&3';

$pipes = [];
$process = \proc_open(
$cmd,
[['pipe','r'],['pipe','w'],['pipe','w'],['pipe', 'w']],
[['pipe', 'r'], ['pipe', 'w'], ['pipe', 'w'], ['pipe', 'w']],
$pipes
);
$start = \time();
Expand All @@ -163,15 +163,16 @@ public static function execute(string $cmd, string $stdin, string &$stdout, stri

if ($timeout > 0 && \time() - $start > $timeout) {
\proc_terminate($process, 9);

return 1;
}

if (!\proc_get_status($process)['running']) {
if (! \proc_get_status($process)['running']) {
\fclose($pipes[1]);
\fclose($pipes[2]);
\proc_close($process);

$exitCode = (int) str_replace("\n", "", $status);
$exitCode = (int) str_replace("\n", '', $status);

return $exitCode;
}
Expand All @@ -189,21 +190,21 @@ public static function execute(string $cmd, string $stdin, string &$stdout, stri
*/
public static function isInteractive(): bool
{
return ('cli' === PHP_SAPI && defined('STDOUT'));
return 'cli' === PHP_SAPI && defined('STDOUT');
}

/**
* @param callable $callback
* @param float $sleep // in seconds!
* @param callable $onError
* @param callable $callback
* @param float $sleep // in seconds!
* @param callable $onError
*/
public static function loop(callable $callback, $sleep = 1 /* seconds */, callable $onError = null): void
{
gc_enable();

$time = 0;

while (!connection_aborted() || PHP_SAPI == "cli") {
while (! connection_aborted() || PHP_SAPI == 'cli') {
try {
$callback();
} catch (\Exception $e) {
Expand All @@ -227,7 +228,7 @@ public static function loop(callable $callback, $sleep = 1 /* seconds */, callab

$time = $time + $sleep;

if (PHP_SAPI == "cli") {
if (PHP_SAPI == 'cli') {
if ($time >= 60 * 5) { // Every 5 minutes
$time = 0;
gc_collect_cycles(); //Forces collection of any existing garbage cycles
Expand Down
Loading

0 comments on commit 35b691e

Please sign in to comment.