Skip to content

Commit 59bddca

Browse files
committed
Rearrange Sli classes, review AbstractCommand
1 parent b6280e1 commit 59bddca

14 files changed

+109
-117
lines changed

bin/sli

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ $loader = require $_composer_autoload_path
1919
$loader->addPsr4('Salient\\Sli\\', dirname(__DIR__) . '/src/Sli/');
2020

2121
(new CliApplication())
22-
->resumeCache()
23-
->logOutput()
2422
->command(['generate', 'builder'], GenerateBuilder::class)
2523
->command(['generate', 'facade'], GenerateFacade::class)
2624
->command(['generate', 'tests'], GenerateTests::class)
@@ -34,4 +32,6 @@ $loader->addPsr4('Salient\\Sli\\', dirname(__DIR__) . '/src/Sli/');
3432
->command(['http', 'put'], SendHttpRequest::class)
3533
->command(['http', 'delete'], SendHttpRequest::class)
3634
->command(['http', 'patch'], SendHttpRequest::class)
35+
->resumeCache()
36+
->logOutput()
3737
->runAndExit();

phpstan-baseline-7.4.neon

Lines changed: 0 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpstan-baseline-8.3.neon

Lines changed: 0 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/generate.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@
3030
use Salient\Core\MetricCollector;
3131
use Salient\Curler\Curler;
3232
use Salient\Curler\CurlerBuilder;
33-
use Salient\Sli\Catalog\EnvVar;
34-
use Salient\Sli\Command\Generate\Concept\GenerateCommand;
33+
use Salient\Sli\Command\Generate\AbstractGenerateCommand;
3534
use Salient\Sli\Command\Generate\GenerateBuilder;
3635
use Salient\Sli\Command\Generate\GenerateFacade;
3736
use Salient\Sli\Command\Generate\GenerateSyncEntity;
3837
use Salient\Sli\Command\Generate\GenerateSyncProvider;
38+
use Salient\Sli\EnvVar;
3939
use Salient\Sync\DbSyncDefinition;
4040
use Salient\Sync\DbSyncDefinitionBuilder;
4141
use Salient\Sync\HttpSyncDefinition;
@@ -131,13 +131,13 @@
131131
];
132132

133133
/**
134-
* @param GenerateCommand|string $commandOrFile
134+
* @param AbstractGenerateCommand|string $commandOrFile
135135
*/
136136
function generated($commandOrFile): void
137137
{
138138
global $generated;
139139

140-
$file = $commandOrFile instanceof GenerateCommand
140+
$file = $commandOrFile instanceof AbstractGenerateCommand
141141
? $commandOrFile->OutputFile
142142
: $commandOrFile;
143143

src/Sli/Command/AbstractCommand.php

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

3-
namespace Salient\Sli\Command\Concept;
3+
namespace Salient\Sli\Command;
44

55
use Salient\Cli\Exception\CliInvalidArgumentsException;
66
use Salient\Cli\CliCommand;
7-
use Salient\Contract\Core\ProviderInterface;
87
use Salient\Core\Exception\FilesystemErrorException;
98
use Salient\Core\Utility\Env;
109
use Salient\Core\Utility\File;
1110
use Salient\Core\Utility\Get;
1211
use Salient\Core\Utility\Json;
13-
use Salient\Sli\Catalog\EnvVar;
12+
use Salient\Core\Utility\Test;
13+
use Salient\Sli\EnvVar;
1414
use JsonException;
1515

1616
/**
1717
* Base class for sli commands
1818
*/
19-
abstract class Command extends CliCommand
19+
abstract class AbstractCommand extends CliCommand
2020
{
2121
/**
2222
* Normalise a user-supplied class name, optionally assigning its base name
@@ -25,23 +25,36 @@ abstract class Command extends CliCommand
2525
* @return class-string|''
2626
*/
2727
protected function getFqcnOptionValue(
28+
string $valueName,
2829
string $value,
2930
?string $namespaceEnvVar = null,
3031
?string &$class = null,
3132
?string &$namespace = null
3233
): string {
34+
if ($value === '') {
35+
return '';
36+
}
37+
3338
$namespace = null;
3439
if ($namespaceEnvVar !== null) {
3540
$namespace = Env::get($namespaceEnvVar, null);
3641
}
37-
if ($namespace === null) {
38-
$namespace = Env::get(EnvVar::NS_DEFAULT, null);
39-
}
40-
if ($namespace !== null && trim($value) && strpos($value, '\\') === false) {
42+
$namespace ??= Env::get(EnvVar::NS_DEFAULT, null);
43+
44+
if ($namespace !== null && strpos($value, '\\') === false) {
4145
$fqcn = trim($namespace, '\\') . "\\$value";
4246
} else {
4347
$fqcn = ltrim($value, '\\');
4448
}
49+
50+
if (!Test::isFqcn($fqcn)) {
51+
throw new CliInvalidArgumentsException(sprintf(
52+
'invalid %s: %s',
53+
$valueName,
54+
$value,
55+
));
56+
}
57+
4558
$class = Get::basename($fqcn);
4659
$namespace = Get::namespace($fqcn);
4760

@@ -54,85 +67,88 @@ protected function getFqcnOptionValue(
5467
*
5568
* @return class-string
5669
*/
57-
protected function getRequiredFqcnOptionValue(
70+
protected function requireFqcnOptionValue(
5871
string $valueName,
5972
string $value,
6073
?string $namespaceEnvVar = null,
6174
?string &$class = null,
6275
?string &$namespace = null
6376
): string {
64-
$fqcn = $this->getFqcnOptionValue($value, $namespaceEnvVar, $class, $namespace);
65-
77+
$fqcn = $this->getFqcnOptionValue($valueName, $value, $namespaceEnvVar, $class, $namespace);
6678
if ($fqcn === '') {
6779
throw new CliInvalidArgumentsException(sprintf('invalid %s: %s', $valueName, $value));
6880
}
69-
7081
return $fqcn;
7182
}
7283

7384
/**
74-
* Normalise mandatory user-supplied class names
85+
* Normalise an array of user-supplied class names
7586
*
7687
* @param string[] $values
7788
* @return array<class-string>
7889
*/
79-
protected function requireMultipleFqcnValues(
90+
protected function requireFqcnOptionValues(
8091
string $valueName,
8192
array $values,
8293
?string $namespaceEnvVar = null
8394
): array {
8495
$fqcn = [];
8596
foreach ($values as $value) {
86-
$fqcn[] = $this->getRequiredFqcnOptionValue($valueName, $value, $namespaceEnvVar);
97+
$fqcn[] = $this->requireFqcnOptionValue($valueName, $value, $namespaceEnvVar);
8798
}
8899
return $fqcn;
89100
}
90101

91102
/**
92-
* Resolve a user-supplied provider name to a concrete instance
103+
* Normalise a user-supplied class name and resolve it to a concrete
104+
* instance of a given class
93105
*
94-
* @template TBaseProvider of ProviderInterface
95-
* @template TProvider of TBaseProvider
106+
* @template TClass of object
96107
*
97-
* @param class-string<TProvider> $provider
98-
* @param class-string<TBaseProvider> $class
99-
* @return TProvider
108+
* @param class-string<TClass> $class
109+
* @return TClass
100110
*/
101-
protected function getProvider(string $provider, string $class = ProviderInterface::class): ProviderInterface
102-
{
103-
$provider = $this->getFqcnOptionValue($provider, EnvVar::NS_PROVIDER);
104-
if (is_a($provider, $class, true)) {
105-
return $this->App->get($provider);
111+
protected function getFqcnOptionInstance(
112+
string $valueName,
113+
string $value,
114+
string $class,
115+
?string $namespaceEnvVar = null
116+
) {
117+
$value = $this->getFqcnOptionValue($valueName, $value, $namespaceEnvVar);
118+
if (is_a($value, $class, true)) {
119+
return $this->App->get($value);
106120
}
107-
108-
throw class_exists($provider)
109-
? new CliInvalidArgumentsException("not a subclass of $class: $provider")
110-
: new CliInvalidArgumentsException("class does not exist: $provider");
121+
throw new CliInvalidArgumentsException(
122+
class_exists($value) ? sprintf(
123+
'class does not inherit %s: %s',
124+
$class,
125+
$value,
126+
) : sprintf(
127+
'class does not exist: %s',
128+
$value,
129+
)
130+
);
111131
}
112132

113133
/**
114134
* Get data from a user-supplied JSON file, optionally assigning the file's
115-
* "friendly pathname" to a variable before returning
135+
* "friendly pathname" to a variable passed by reference
116136
*
117137
* @return mixed
118138
*/
119139
protected function getJson(string $file, ?string &$path = null, bool $associative = true)
120140
{
121-
$_file = $file;
122141
if ($file === '-') {
123142
$file = 'php://stdin';
124143
} else {
125144
try {
126-
$file = File::realpath($file);
145+
$path = File::relativeToParent($file, $this->App->getBasePath(), $file);
127146
} catch (FilesystemErrorException $ex) {
128147
throw new CliInvalidArgumentsException(sprintf(
129148
'file not found: %s',
130-
$_file,
149+
$file,
131150
));
132151
}
133-
134-
$relative = File::relativeToParent($file, $this->App->getBasePath());
135-
$path = $relative === null ? $file : "./{$relative}";
136152
}
137153

138154
$json = File::getContents($file);
@@ -144,7 +160,7 @@ protected function getJson(string $file, ?string &$path = null, bool $associativ
144160
} catch (JsonException $ex) {
145161
throw new CliInvalidArgumentsException(sprintf(
146162
"invalid JSON in '%s': %s",
147-
$_file,
163+
$file,
148164
$ex->getMessage(),
149165
));
150166
}

0 commit comments

Comments
 (0)