Skip to content

Commit a08618d

Browse files
committed
Add handy 'create' static methods for detectors and launcher classes.
1 parent fced30a commit a08618d

38 files changed

Lines changed: 347 additions & 250 deletions

docs/todo.md

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,6 @@ The list of plans and ideas for future development.
8787
<details>
8888
<summary>Points to consider</summary>
8989

90-
1. - [ ] [features-manual.md](features-manual.md):
91-
1. - [x] A comparison between "plain scripts" and "classes".
92-
1. - [x] Start with a "summary" paragraph.
93-
1. - [x] Launcher performance. Describe possible approaches (including the built-in caching mechanism).
94-
1. - [x] Make a link to this from the comparison.
95-
1. - [x] [execute-class.php](../tools/cli-toolkit/execute-class.php)
96-
1. - [x] Built-in subcommands.
97-
1. - [x] `list` as a default value.
98-
No other parameters are processed correctly unless `list` is specified explicitly.
99-
1. - [x] `ConfigBuilder::shortDescription()`
100-
1. - [x] [Question.php](../src/Question/Question.php)
101-
1. - [ ] (if relevant) Update comments generated in
102-
[LauncherSkeleton.php](../tools/cli-toolkit/ScriptClasses/Generate/LauncherSkeleton.php)
103-
with links to the manual.
10490
1. - [ ] FINISHING MOVES:
10591
1. - [x] Renaming, moving and other trivial refactoring:
10692
1. - [x] `../src/Parametizer/Script` -> `.../ScriptClass`
@@ -124,7 +110,8 @@ The list of plans and ideas for future development.
124110

125111
- generating an empty `ConfigBuilder` instance "automatically" (mainly for temp scripts);
126112
- ~~making `getConfigBuilder()` non-static, creating `ConfigBuilder` instance inside `__construct()`.~~
127-
1. - [ ] Make creating buildable instances (like detectors) with `create` static methods.
113+
1. - [x] Make creating buildable instances that are expected to be called by users (like detectors)
114+
with `create` static methods.
128115
1. - [ ] Consider adding even more [backward incompatibilities](todo.md#next-major-release) ~~or delaying
129116
the next major release, see [already implemented backward incompatibilities](changelog.md#v300)~~.
130117

@@ -311,6 +298,20 @@ The list of plans and ideas for future development.
311298
1. - [ ] ~~Composer post install message with the generator launch command.~~
312299

313300
~~See https://getcomposer.org/doc/articles/scripts.md~~
301+
1. - [x] [features-manual.md](features-manual.md):
302+
1. - [x] A comparison between "plain scripts" and "classes".
303+
1. - [x] Start with a "summary" paragraph.
304+
1. - [x] Launcher performance. Describe possible approaches (including the built-in caching mechanism).
305+
1. - [x] Make a link to this from the comparison.
306+
1. - [x] [execute-class.php](../tools/cli-toolkit/execute-class.php)
307+
1. - [x] Built-in subcommands.
308+
1. - [x] `list` as a default value.
309+
No other parameters are processed correctly unless `list` is specified explicitly.
310+
1. - [x] `ConfigBuilder::shortDescription()`
311+
1. - [x] [Question.php](../src/Question/Question.php)
312+
1. - [ ] ~~(if relevant) Update comments generated in
313+
[LauncherSkeleton.php](../tools/cli-toolkit/ScriptClasses/Generate/LauncherSkeleton.php)
314+
with links to the manual.~~
314315
</details>
315316

316317
## Next major release

src/Parametizer/ScriptClass/ScriptClassLauncher/ScriptClassLauncher.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,19 @@ class ScriptClassLauncher {
1717
protected bool $throwOnException = false;
1818

1919

20-
public function __construct(
21-
protected readonly ScriptClassDetector $scriptClassDetector,
20+
protected function __construct(protected readonly ScriptClassDetector $scriptClassDetector) { }
21+
22+
public static function create(
23+
ScriptClassDetector $scriptClassDetector,
2224
?ConfigBuilder $configBuilder = null,
23-
) {
25+
): static {
26+
$launcher = new static($scriptClassDetector);
27+
2428
if (null !== $configBuilder) {
25-
$this->configBuilder = $configBuilder;
29+
$launcher->configBuilder = $configBuilder;
2630
}
31+
32+
return $launcher;
2733
}
2834

2935
/**
@@ -37,7 +43,7 @@ public function useParentEnvConfigForSubcommands(bool $isEnabled = true): static
3743

3844
/**
3945
* Always affects subcommands. Also, affects main {@see ConfigBuilder} instance only if the latter is created
40-
* automatically - if `null` (default value) is passed to {@see static::__construct()} as the relevant parameter.
46+
* automatically - if `null` (default value) is passed to {@see static::__construct()}'s connected parameter.
4147
*
4248
* By default (without this method call), the corresponding internal property value is `false`.
4349
*

src/Parametizer/ScriptDetector/ScriptDetectorAbstract.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,14 @@ abstract class ScriptDetectorAbstract {
2424
protected array $excludedDirectoryPaths = [];
2525

2626

27+
protected function __construct(protected readonly bool $throwOnException) { }
28+
2729
/**
2830
* @param bool $throwOnException Useful to debug issues with paths (read / write).
2931
*/
30-
public function __construct(protected readonly bool $throwOnException = true) { }
32+
public static function create(bool $throwOnException = true): static {
33+
return new static($throwOnException);
34+
}
3135

3236
public function cacheFilePath(?string $cacheFilePath): static {
3337
$cacheFilePathReal = null !== $cacheFilePath ? realpath($cacheFilePath) : false;

src/Parametizer/ScriptDetector/ScriptFileDetector.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ class ScriptFileDetector extends ScriptDetectorAbstract {
1515
protected const string SUBSTR_PARAMETIZER_CONSTRUCT = 'Parametizer::newConfig(';
1616
/** @see Parametizer::run() */
1717
protected const string SUBSTR_PARAMETIZER_EXEC = '->run()';
18-
/** @see ScriptClassLauncher::__construct() */
19-
protected const string SUBSTR_LAUNCHER_CONSTRUCT = 'ScriptClassLauncher(';
18+
/** @see ScriptClassLauncher::create() */
19+
protected const string SUBSTR_LAUNCHER_CONSTRUCT = 'ScriptClassLauncher::create(';
2020
/** @see ScriptClassLauncher::execute() */
2121
protected const string SUBSTR_LAUNCHER_EXEC = '->execute()';
2222

tests/Tests/Parametizer/Completion/CompletionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ public static function provideSubcommandSwitch(): array {
391391
/**
392392
* Tests completion affected by default subcommand switch value deeply in configs tree.
393393
*
394-
* For now the only possible default subcommand is {@see ListSubcommands::getScriptName()}, it is hardcoded.
394+
* For now, the only possible default subcommand is {@see ListSubcommands::getScriptName()}, it is hardcoded.
395395
* However if different default values are possible, we may test here any default value.
396396
*
397397
* @param string[] $expectedOutputLines

tests/Tests/Parametizer/ScriptClass/scripts/by-single-name.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
$scriptClassName = $_SERVER['argv'][1];
1111
unset($_SERVER['argv'][1]);
1212

13-
$scriptClassDetector = (new ScriptClassDetector(throwOnException: true))
13+
$scriptClassDetector = ScriptClassDetector::create(throwOnException: true)
1414
->scriptClassName($scriptClassName);
15-
(new ScriptClassLauncher($scriptClassDetector))
15+
ScriptClassLauncher::create($scriptClassDetector)
1616
->throwOnException()
1717
->execute();

tests/Tests/Parametizer/ScriptClass/scripts/local-names.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use MagicPush\CliToolkit\Parametizer\ScriptDetector\ScriptClassDetector;
99

1010
/** @noinspection PhpFullyQualifiedNameUsageInspection */
11-
$scriptClassDetector = (new ScriptClassDetector(throwOnException: true))
11+
$scriptClassDetector = ScriptClassDetector::create(throwOnException: true)
1212
->scriptClassNames([
1313
\MagicPush\CliToolkit\Tests\Tests\Parametizer\ScriptClass\ScriptClasses\LocalNames\lowercasename::class,
1414
\MagicPush\CliToolkit\Tests\Tests\Parametizer\ScriptClass\ScriptClasses\LocalNames\Uppercasename::class,
@@ -19,6 +19,6 @@
1919
\MagicPush\CliToolkit\Tests\Tests\Parametizer\ScriptClass\ScriptClasses\LocalNames\SomeABBRWord::class,
2020
]);
2121

22-
(new ScriptClassLauncher($scriptClassDetector))
22+
ScriptClassLauncher::create($scriptClassDetector)
2323
->throwOnException()
2424
->execute();

tests/Tests/Parametizer/ScriptClass/scripts/sections.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
use MagicPush\CliToolkit\Parametizer\ScriptDetector\ScriptClassDetector;
99

1010
/** @noinspection PhpFullyQualifiedNameUsageInspection */
11-
$scriptClassDetector = (new ScriptClassDetector(throwOnException: true))
11+
$scriptClassDetector = ScriptClassDetector::create(throwOnException: true)
1212
->scriptClassNames([
1313
\MagicPush\CliToolkit\Tests\Tests\Parametizer\ScriptClass\ScriptClasses\Sections\Single::class,
1414
\MagicPush\CliToolkit\Tests\Tests\Parametizer\ScriptClass\ScriptClasses\Sections\Double::class,
1515
\MagicPush\CliToolkit\Tests\Tests\Parametizer\ScriptClass\ScriptClasses\Sections\Triple::class,
1616
\MagicPush\CliToolkit\Tests\Tests\Parametizer\ScriptClass\ScriptClasses\Sections\Spaced::class,
1717
]);
1818

19-
(new ScriptClassLauncher($scriptClassDetector))
19+
ScriptClassLauncher::create($scriptClassDetector)
2020
->throwOnException()
2121
->execute();

tests/Tests/Parametizer/ScriptClassLauncher/SameEnvConfig/scripts/setting-parent-config-for-subcommands.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
$cacheFilePath = $_SERVER['argv'][3];
1515
unset($_SERVER['argv'][1], $_SERVER['argv'][2], $_SERVER['argv'][3]);
1616

17-
$scriptClassDetector = (new ScriptClassDetector(true))
17+
$scriptClassDetector = ScriptClassDetector::create(true)
1818
->cacheFilePath($cacheFilePath)
1919
->searchDirectory(__DIR__ . '/../ScriptClasses');
2020

@@ -28,6 +28,6 @@
2828
$configBuilder = null;
2929
}
3030

31-
(new ScriptClassLauncher($scriptClassDetector, $configBuilder))
31+
ScriptClassLauncher::create($scriptClassDetector, $configBuilder)
3232
->useParentEnvConfigForSubcommands($isSameEnvConfigForSubcommands)
3333
->execute();

tests/Tests/Parametizer/ScriptClassLauncher/ThrowOnException/setting-throw-on-exception.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212

1313
// No ConfigBuilder instance must be specified to ensure `throwOnException` flag
1414
// is passed to the automatically created instance.
15-
(new ScriptClassLauncher((new ScriptClassDetector())->searchDirectory(__DIR__), configBuilder: null))
15+
ScriptClassLauncher::create(ScriptClassDetector::create()->searchDirectory(__DIR__), configBuilder: null)
1616
->throwOnException($launcherThrowOnException)
1717
->execute();

0 commit comments

Comments
 (0)