Skip to content

Commit

Permalink
Optimize ...
Browse files Browse the repository at this point in the history
  • Loading branch information
guanguans committed May 21, 2023
1 parent 3ac24d4 commit 8615b81
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 64 deletions.
17 changes: 8 additions & 9 deletions src/Concerns/ConcreteMagic.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,19 @@

namespace Guanguans\SoarPHP\Concerns;

use Guanguans\SoarPHP\Support\EscapeArg;

/**
* @mixin \Guanguans\SoarPHP\Soar
*/
trait ConcreteMagic
{
public function __sleep()
{
return ['soarPath', 'options'];
return ['options', 'soarPath'];
}

public function __wakeup(): void
{
$this->setOptions($this->options);
// $this->setOptions($this->options);
}

// /**
Expand All @@ -35,8 +33,8 @@ public function __wakeup(): void
// public function __serialize(): array
// {
// return [
// 'soarPath' => $this->soarPath,
// 'options' => $this->options,
// 'soarPath' => $this->soarPath,
// ];
// }
//
Expand All @@ -45,8 +43,8 @@ public function __wakeup(): void
// */
// public function __unserialize(array $data): void
// {
// $this->setSoarPath($data['soarPath']);
// $this->setOptions($data['options']);
// $this->setSoarPath($data['soarPath']);
// }

public function __debugInfo()
Expand All @@ -59,10 +57,11 @@ public static function __set_state(array $properties)
return new static($properties['options'], $properties['soarPath']);
}

/**
* @noinspection MagicMethodsValidityInspection
*/
public function __toString()
{
$escapeOptions = EscapeArg::escapeCommand($this->getNormalizedOptions());

return "$this->soarPath $escapeOptions";
return $this->createProcess()->getCommandLine();
}
}
30 changes: 15 additions & 15 deletions src/Concerns/HasOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1445,13 +1445,13 @@ public function removeOption(string $key): self

public function onlyOptions(array $keys = ['-test-dsn', '-online-dsn']): self
{
$this->options = array_reduce($keys, function (array $options, $key): array {
if (isset($this->options[$key])) {
$options[$key] = $this->options[$key];
}

return $options;
}, []);
$this->options = array_filter(
$this->options,
static function ($key) use ($keys): bool {
return \in_array($key, $keys, true);
},
ARRAY_FILTER_USE_KEY
);

return $this;
}
Expand Down Expand Up @@ -1491,24 +1491,24 @@ public function mergeOption(string $key, $value): self
return $this;
}

public function getSerializedNormalizedOptions(): string
public function getOptions(): array
{
return implode(' ', $this->getNormalizedOptions());
return $this->options;
}

public function getNormalizedOptions(): array
public function getOption(string $key, $default = null)
{
return $this->normalizeOptions($this->options);
return $this->options[$key] ?? $default;
}

public function getOptions(): array
public function getSerializedNormalizedOptions(): string
{
return $this->options;
return implode(' ', $this->getNormalizedOptions());
}

public function getOption(string $key, $default = null)
public function getNormalizedOptions(): array
{
return $this->options[$key] ?? $default;
return $this->normalizeOptions($this->options);
}

/**
Expand Down
6 changes: 2 additions & 4 deletions src/Concerns/WithDumpable.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,9 @@ public function dd(...$args): void
*/
public function dump(...$args): self
{
$args[] = $this;
$args[] = $this->version();
$args[] = $this->help();

$args = array_merge($args, [$this, (string) $this, $this->version(), $this->help()]);
$varDumperExists = class_exists(VarDumper::class);

foreach ($args as $arg) {
$varDumperExists ? VarDumper::dump($arg) : var_dump($arg);
}
Expand Down
63 changes: 29 additions & 34 deletions src/Concerns/WithRunable.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,49 +24,44 @@ trait WithRunable
/**
* @param array|string $withOptions
*/
public function run($withOptions = []): string
{
return $this->exec($withOptions);
}

/**
* @param array|string $withOptions
* @param mixed $input The input as stream resource, scalar or \Traversable, or null for no input
*
* @throws \Guanguans\SoarPHP\Exceptions\InvalidArgumentException
* @throws \Guanguans\SoarPHP\Exceptions\ProcessFailedException
*/
protected function exec($withOptions = [], ?string $cwd = null, ?array $env = null, $input = null, ?float $timeout = 60, ?callable $output = null): string
public function run($withOptions = [], ?callable $processTapper = null, ?callable $callback = null): string
{
if (! \is_string($withOptions) && ! \is_array($withOptions)) {
throw new InvalidArgumentException(sprintf('Invalid argument type(%s).', \gettype($withOptions)));
}

if (\is_string($withOptions)) {
$process = Process::fromShellCommandline(
"$this->soarPath {$this->getSerializedNormalizedOptions()} $withOptions",
$cwd,
$env,
$input,
$timeout
);
}

if (\is_array($withOptions)) {
$process = new Process(
array_merge([$this->soarPath], $this->clone()->mergeOptions($withOptions)->getNormalizedOptions()),
$cwd,
$env,
$input,
$timeout
);
}

$process->run($output);
$process = $this->createProcess($withOptions, $processTapper);
$process->run($callback);
if (! $process->isSuccessful()) {
throw new ProcessFailedException($process);
}

return $process->getOutput();
}

/**
* @param array|string $withOptions
*/
protected function exec($withOptions = [], ?callable $processTapper = null, ?callable $callback = null): string
{
return $this->run($withOptions, $processTapper, $callback);
}

/**
* @param array|string $withOptions
*/
private function createProcess($withOptions = [], ?callable $processTapper = null): Process
{
$process = \is_string($withOptions)
? Process::fromShellCommandline("$this->soarPath {$this->getSerializedNormalizedOptions()} $withOptions")
: new Process(array_merge([$this->soarPath], $this->clone()->mergeOptions($withOptions)->getNormalizedOptions()));

return (static function (Process $process) use ($processTapper): Process {
if (\is_callable($processTapper)) {
$processTapper($process);
}

return $process;
})($process);
}
}
2 changes: 1 addition & 1 deletion src/Contracts/Soar.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ interface Soar
/**
* @param array|string $withOptions
*/
public function run($withOptions = []): string;
public function run($withOptions = [], ?callable $processTapper = null, ?callable $callback = null): string;

/**
* @param array<string>|string $sqls
Expand Down
5 changes: 4 additions & 1 deletion src/Soar.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ class Soar implements Contracts\Soar
use WithDumpable;
use WithRunable;

/**
* @noinspection MissingParentCallInspection
*/
public function __construct(array $options = [], ?string $soarPath = null)
{
$this->setSoarPath($soarPath ?? $this->getDefaultSoarPath());
$this->setOptions($options);
$this->setSoarPath($soarPath ?? $this->getDefaultSoarPath());
}

public static function create(array $options = [], ?string $soarPath = null): self
Expand Down

0 comments on commit 8615b81

Please sign in to comment.