Skip to content

Commit dd6cd69

Browse files
author
ityaozm@gmail.com
committed
refactor(BitoCliGenerator): improve code structure and readability
- Introduce ProcessHelper to simplify process handling - Enhance verbosity control by setting debug level for output style - Refactor process execution to use mustRun method with ProcessHelper - Clean up unused variables and improve code organization
1 parent 961e632 commit dd6cd69

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

app/Generators/BitoCliGenerator.php

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use App\ConfigManager;
1616
use App\Contracts\GeneratorContract;
1717
use Illuminate\Console\OutputStyle;
18+
use Illuminate\Support\Facades\Artisan;
19+
use Symfony\Component\Console\Output\OutputInterface;
1820
use Symfony\Component\Process\Process;
1921

2022
final class BitoCliGenerator implements GeneratorContract
@@ -29,10 +31,22 @@ final class BitoCliGenerator implements GeneratorContract
2931
*/
3032
private $outputStyle;
3133

34+
/**
35+
* @var \Symfony\Component\Console\Helper\ProcessHelper
36+
*/
37+
private $processHelper;
38+
39+
/**
40+
* @psalm-suppress UndefinedMethod
41+
*/
3242
public function __construct(array $config)
3343
{
3444
$this->config = $config;
35-
$this->outputStyle = resolve(OutputStyle::class);
45+
$this->outputStyle = tap(clone resolve(OutputStyle::class))->setVerbosity(OutputInterface::VERBOSITY_DEBUG);
46+
$this->processHelper = (function () {
47+
/** @noinspection PhpUndefinedMethodInspection */
48+
return $this->getArtisan()->getHelperSet()->get('process');
49+
})->call(Artisan::getFacadeRoot());
3650
}
3751

3852
/**
@@ -41,12 +55,23 @@ public function __construct(array $config)
4155
public function generate(string $prompt): string
4256
{
4357
// file_put_contents($promptFile = ConfigManager::globalPath($this->config['prompt_filename']), $prompt);
58+
//
59+
// return resolve(
60+
// Process::class,
61+
// ['command' => [$this->config['path'] ?: 'bito', '-p', $promptFile]] + $this->config['parameters']
62+
// )->mustRun(function (string $type, string $data): void {
63+
// Process::OUT === $type ? $this->outputStyle->write($data) : $this->outputStyle->write("<fg=red>$data</>");
64+
// })->getOutput();
4465

45-
return resolve(Process::class, ['command' => [$this->config['path']]] + $this->config['parameters'])
46-
->setInput($prompt)
47-
->mustRun(function (string $type, string $data): void {
48-
Process::OUT === $type ? $this->outputStyle->write($data) : $this->outputStyle->write("<fg=red>$data</>");
49-
})
66+
return $this
67+
->processHelper
68+
->mustRun(
69+
$this->outputStyle,
70+
resolve(
71+
Process::class,
72+
['command' => [$this->config['path']]] + $this->config['parameters']
73+
)->setInput($prompt)
74+
)
5075
->getOutput();
5176
}
5277
}

0 commit comments

Comments
 (0)