Skip to content

Commit a8c4b68

Browse files
committed
Tweaks
1 parent a107f0f commit a8c4b68

File tree

3 files changed

+41
-77
lines changed

3 files changed

+41
-77
lines changed

src/Commands/Tinker.php

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
namespace Bref\Cli\Commands;
44

5+
use Bref\Cli\BrefCloudClient;
56
use Bref\Cli\Cli\IO;
67
use Bref\Cli\Cli\Styles;
78
use Bref\Cli\Tinker\BrefTinkerShell;
89
use Psy\Configuration;
910
use Symfony\Component\Console\Input\InputInterface;
1011
use Symfony\Component\Console\Output\OutputInterface;
12+
use Throwable;
1113

1214
class Tinker extends ApplicationCommand
1315
{
@@ -30,25 +32,30 @@ protected function execute(InputInterface $input, OutputInterface $output): int
3032

3133
IO::writeln([Styles::brefHeader(), '']);
3234

33-
$brefCloudConfig = $this->parseStandardOptions($input);
35+
[
36+
'appName' => $appName,
37+
'environmentName' => $environmentName,
38+
'team' => $team,
39+
] = $this->parseStandardOptions($input);
3440

3541
// Auto enable verbose to avoid verbose async listener in VerboseModeEnabler which will cause issue when executing multiple commands
3642
IO::enableVerbose();
3743
IO::writeln(sprintf(
38-
"Starting Interactive Shell Session for [%s] in the [%s] environment",
39-
Styles::green($brefCloudConfig['appName']),
40-
Styles::red($brefCloudConfig['environmentName']),
44+
"Starting interactive shell for [%s] in the [%s] environment",
45+
Styles::bold($appName),
46+
Styles::bold($environmentName),
4147
));
48+
49+
$environment = (new BrefCloudClient)->findEnvironment($team, $appName, $environmentName);
50+
$environmentId = $environment['id'];
4251

4352
$shellConfig = Configuration::fromInput($input);
44-
$shellOutput = $shellConfig->getOutput();
45-
46-
$shell = new BrefTinkerShell($shellConfig, $brefCloudConfig);
47-
$shell->setRawOutput($shellOutput);
53+
54+
$shell = new BrefTinkerShell($shellConfig, $environmentId, $shellConfig->getOutput());
4855

4956
try {
5057
return $shell->run();
51-
} catch (\Throwable $e) {
58+
} catch (Throwable $e) {
5259
IO::writeln(Styles::red($e->getMessage()));
5360
return 1;
5461
}
@@ -61,9 +68,10 @@ protected function isLaravelApplication(): bool
6168
return false;
6269
}
6370

64-
$composerJson = json_decode($composerContent, true);
65-
$requires = $composerJson['require'] ?? [];
66-
$requiresDev = $composerJson['require-dev'] ?? [];
71+
/** @var array<string, mixed> $composerJson */
72+
$composerJson = json_decode($composerContent, true, 512, JSON_THROW_ON_ERROR);
73+
$requires = (array) ($composerJson['require'] ?? []);
74+
$requiresDev = (array) ($composerJson['require-dev'] ?? []);
6775
return isset($requires['laravel/framework']) || isset($requiresDev['laravel/framework']);
6876
}
6977
}

src/Tinker/BrefTinkerLoopListener.php

Lines changed: 13 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
namespace Bref\Cli\Tinker;
44

@@ -12,44 +12,16 @@
1212
use Psy\Shell;
1313
use Symfony\Contracts\HttpClient\Exception\ExceptionInterface;
1414
use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface;
15+
use Throwable;
1516
use function Amp\delay;
1617

1718
class BrefTinkerLoopListener extends AbstractListener
1819
{
19-
/**
20-
* @var array{appName: string, environmentName: string, team: string}
21-
*/
22-
protected array $brefConfig;
23-
24-
/**
25-
* @var array{
26-
* id: int,
27-
* name: string,
28-
* region: string|null,
29-
* url: string|null,
30-
* outputs: array<string, string>,
31-
* app: array{id: int, name: string},
32-
* }
33-
*/
34-
protected array $environment;
35-
36-
protected BrefCloudClient $brefCloudClient;
37-
38-
/**
39-
* @param array<string, string> $brefConfig
40-
* @throws ExceptionInterface
41-
* @throws HttpExceptionInterface
42-
*/
43-
public function __construct(array $brefConfig)
20+
public function __construct(
21+
private readonly int $environmentId,
22+
private readonly BrefCloudClient $brefCloudClient,
23+
)
4424
{
45-
$this->brefConfig = $brefConfig;
46-
[
47-
'appName' => $appName,
48-
'environmentName' => $environmentName,
49-
'team' => $team,
50-
] = $brefConfig;
51-
$this->brefCloudClient = new BrefCloudClient;
52-
$this->environment = $this->brefCloudClient->findEnvironment($team, $appName, $environmentName);
5325
}
5426

5527
public static function isSupported(): bool
@@ -64,7 +36,7 @@ public static function isSupported(): bool
6436
*/
6537
public function onExecute(Shell $shell, string $code)
6638
{
67-
if ($code == '\Psy\Exception\BreakException::exitShell();') {
39+
if ($code === '\Psy\Exception\BreakException::exitShell();') {
6840
return $code;
6941
}
7042

@@ -94,16 +66,15 @@ public function onExecute(Shell $shell, string $code)
9466
if (!empty($context)) {
9567
// Extract _context into shell's scope variables for next code execution
9668
// Return NoValue as output and return value were printed out
97-
return "extract(['_context' => '{$context}']); return new \Psy\CodeCleaner\NoReturnValue();";
98-
} else {
99-
// Return NoValue as output and return value were printed out
100-
return "return new \Psy\CodeCleaner\NoReturnValue();";
69+
return "extract(['_context' => '$context']); return new \Psy\CodeCleaner\NoReturnValue();";
10170
}
71+
// Return NoValue as output and return value were printed out
72+
return "return new \Psy\CodeCleaner\NoReturnValue();";
10273
}
10374

10475
return ExecutionClosure::NOOP_INPUT;
105-
} catch (\Throwable $_e) {
106-
throw new BreakException($_e->getMessage());
76+
} catch (Throwable $e) {
77+
throw new BreakException($e->getMessage());
10778
}
10879
}
10980

@@ -119,7 +90,7 @@ protected function evaluateCode(string $code, string $context): array
11990
'--execute=\"'.base64_encode($code).'\"',
12091
'--context=\"'.$context.'\"',
12192
]);
122-
$id = $this->brefCloudClient->startCommand($this->environment['id'], $command);
93+
$id = $this->brefCloudClient->startCommand($this->environmentId, $command);
12394

12495
// Timeout after 2 minutes and 10 seconds
12596
$timeout = 130;

src/Tinker/BrefTinkerShell.php

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,34 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
namespace Bref\Cli\Tinker;
44

5+
use Bref\Cli\BrefCloudClient;
56
use Psy\Configuration;
67
use Psy\ExecutionLoop\AbstractListener;
78
use Psy\Output\ShellOutput;
89
use Psy\Shell;
9-
use Symfony\Contracts\HttpClient\Exception\ExceptionInterface;
10-
use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface;
1110

1211
class BrefTinkerShell extends Shell
1312
{
14-
public ShellOutput $rawOutput;
15-
16-
/**
17-
* @var array<string, string>
18-
*/
19-
protected array $brefCloudConfig;
20-
21-
public function __construct(?Configuration $config = null, array $brefCloudConfig = [])
13+
public function __construct(
14+
Configuration $config,
15+
private readonly int $environmentId,
16+
public readonly ShellOutput $rawOutput,
17+
)
2218
{
23-
$this->brefCloudConfig = $brefCloudConfig;
24-
2519
parent::__construct($config);
2620
}
27-
28-
public function setRawOutput($rawOutput): self
29-
{
30-
$this->rawOutput = $rawOutput;
31-
32-
return $this;
33-
}
3421

3522
/**
3623
* Gets the default command loop listeners.
3724
*
3825
* @return array<AbstractListener> An array of Execution Loop Listener instances
39-
* @throws ExceptionInterface
40-
* @throws HttpExceptionInterface
4126
*/
4227
protected function getDefaultLoopListeners(): array
4328
{
4429
$listeners = parent::getDefaultLoopListeners();
4530

46-
$listeners[] = new BrefTinkerLoopListener($this->brefCloudConfig);
31+
$listeners[] = new BrefTinkerLoopListener($this->environmentId, new BrefCloudClient);
4732

4833
return $listeners;
4934
}

0 commit comments

Comments
 (0)