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 b6f46e6
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 40 deletions.
6 changes: 3 additions & 3 deletions src/Concerns/ConcreteMagic.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ trait ConcreteMagic
{
public function __sleep()
{
return ['soarPath', 'options'];
return ['options', 'soarPath'];
}

public function __wakeup(): void
Expand All @@ -35,8 +35,8 @@ public function __wakeup(): void
// public function __serialize(): array
// {
// return [
// 'soarPath' => $this->soarPath,
// 'options' => $this->options,
// 'soarPath' => $this->soarPath,
// ];
// }
//
Expand All @@ -45,8 +45,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 Down
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
1 change: 1 addition & 0 deletions src/Concerns/WithDumpable.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function dd(...$args): void
public function dump(...$args): self
{
$args[] = $this;
$args[] = (string) $this;
$args[] = $this->version();
$args[] = $this->help();

Expand Down
59 changes: 38 additions & 21 deletions src/Concerns/WithRunable.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,30 @@ trait WithRunable
/**
* @param array|string $withOptions
*/
public function run($withOptions = []): string
public function run($withOptions = [], ?callable $processTapper = null, ?callable $output = null): string
{
return $this->exec($withOptions);
if (! \is_string($withOptions) && ! \is_array($withOptions)) {
throw new InvalidArgumentException(sprintf('Invalid argument type(%s).', \gettype($withOptions)));
}

$process = \is_string($withOptions)
? Process::fromShellCommandline($this->getSoarCommand($withOptions))
: new Process($this->getSoarCommand($withOptions));

$process = (function (Process $process) use ($processTapper) {
if (\is_callable($processTapper)) {
$processTapper($process);
}

return $process;
})($process);

$process->run($output);
if (! $process->isSuccessful()) {
throw new ProcessFailedException($process);
}

return $process->getOutput();
}

/**
Expand All @@ -42,25 +63,9 @@ protected function exec($withOptions = [], ?string $cwd = null, ?array $env = nu
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 = \is_string($withOptions)
? Process::fromShellCommandline($this->getSoarCommand($withOptions), $cwd, $env, $input, $timeout)
: new Process($this->getSoarCommand($withOptions), $cwd, $env, $input, $timeout);

$process->run($output);
if (! $process->isSuccessful()) {
Expand All @@ -69,4 +74,16 @@ protected function exec($withOptions = [], ?string $cwd = null, ?array $env = nu

return $process->getOutput();
}

/**
* @param array|string $withOptions
*
* @return array|string
*/
private function getSoarCommand($withOptions)
{
return \is_string($withOptions)
? "$this->soarPath {$this->getSerializedNormalizedOptions()} $withOptions"
: array_merge([$this->soarPath], $this->clone()->mergeOptions($withOptions)->getNormalizedOptions());
}
}
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 $output = null): string;

/**
* @param array<string>|string $sqls
Expand Down

0 comments on commit b6f46e6

Please sign in to comment.