Skip to content

Commit cfc54ca

Browse files
author
ityaozm@gmail.com
committed
feat(app): Add xdebug and configuration options to Artisan commands
- Introduce '--xdebug' option to display xdebug output during command execution. - Enhance configuration handling with a '--configuration' option for dynamic key-value pairs. - Improve command starting event to manage xdebug behavior based on options. - Integrate new environment variables for PHPUnit configuration in the testing context. These changes enhance the flexibility of Artisan commands, allowing users to control debugging output and pass configurations dynamically. The updated PHPUnit environment settings ensure a consistent testing environment.
1 parent dc7b19a commit cfc54ca

3 files changed

Lines changed: 64 additions & 0 deletions

File tree

bootstrap/app.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,18 @@
2020
use App\ConfigManager;
2121
use App\GeneratorManager;
2222
use App\Listeners\DefineTraceIdListener;
23+
use Composer\XdebugHandler\XdebugHandler;
24+
use Illuminate\Console\Events\CommandStarting;
2325
use Illuminate\Foundation\Configuration\Exceptions;
2426
use Illuminate\Log\LogManager;
27+
use Illuminate\Support\Collection;
28+
use Illuminate\Support\Facades\Artisan;
29+
use Illuminate\Support\Facades\Event;
2530
use Illuminate\Validation\ValidationException;
2631
use Intonate\TinkerZero\TinkerZeroServiceProvider;
2732
use Psr\Log\LoggerInterface;
33+
use Symfony\Component\Console\Command\Command as SymfonyCommand;
34+
use Symfony\Component\Console\Input\InputOption;
2835

2936
return Application::configure(basePath: \dirname(__DIR__))
3037
->withSingletons([
@@ -47,6 +54,49 @@
4754
): LogManager => $logger instanceof LogManager ? $logger : new LogManager($app)
4855
);
4956
})
57+
->booted(static function (): void {
58+
collect(Artisan::all())
59+
->each(
60+
static fn (SymfonyCommand $command): SymfonyCommand => $command
61+
->addOption('xdebug', null, InputOption::VALUE_NONE, 'Display xdebug output')
62+
->addOption(
63+
'configuration',
64+
null,
65+
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
66+
'Used to dynamically pass one or more configuration key-value pairs(e.g. `--configuration=app.name=guanguans` or `--configuration app.name=guanguans`).',
67+
)
68+
)
69+
->tap(static function (): void {
70+
/**
71+
* @see \Illuminate\Foundation\Console\Kernel::rerouteSymfonyCommandEvents()
72+
* @see \Rector\Console\ConsoleApplication::doRun()
73+
*/
74+
Event::listen(CommandStarting::class, static function (CommandStarting $commandStarting): void {
75+
$isXdebugAllowed = $commandStarting->input->hasParameterOption('--xdebug') || app()->runningUnitTests();
76+
77+
if (!$isXdebugAllowed) {
78+
$xdebugHandler = new XdebugHandler(config('app.name'));
79+
$xdebugHandler->setPersistent();
80+
$xdebugHandler->check();
81+
unset($xdebugHandler);
82+
}
83+
84+
collect($commandStarting->input->getOption('configuration'))
85+
// ->dump()
86+
->mapWithKeys(static function (string $configuration): array {
87+
\assert(
88+
str_contains($configuration, '='),
89+
"The configureable option [$configuration] must be formatted as key=value."
90+
);
91+
92+
[$key, $value] = str($configuration)->explode('=', 2)->all();
93+
94+
return [$key => $value];
95+
})
96+
->tap(static fn (Collection $configuration): mixed => config($configuration->all()));
97+
});
98+
});
99+
})
50100
->withExceptions(static function (Exceptions $exceptions): void {
51101
$exceptions->map(
52102
ValidationException::class,

composer-dependency-analyser.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
)
4949
->ignoreErrorsOnPackages(
5050
[
51+
'composer/xdebug-handler',
5152
'guzzlehttp/psr7',
5253
'laravel-lang/config',
5354
'laravel-lang/locale-list',

phpunit.xml.dist

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,17 @@
2626
<file>app/Support/helpers.php</file>
2727
</exclude>
2828
</source>
29+
<php>
30+
<env name="APP_ENV" value="testing"/>
31+
<env name="APP_MAINTENANCE_DRIVER" value="file"/>
32+
<env name="BCRYPT_ROUNDS" value="4"/>
33+
<env name="CACHE_STORE" value="array"/>
34+
<env name="DB_CONNECTION" value="sqlite"/>
35+
<env name="DB_DATABASE" value=":memory:"/>
36+
<env name="MAIL_MAILER" value="array"/>
37+
<env name="PULSE_ENABLED" value="false"/>
38+
<env name="QUEUE_CONNECTION" value="sync"/>
39+
<env name="SESSION_DRIVER" value="array"/>
40+
<env name="TELESCOPE_ENABLED" value="false"/>
41+
</php>
2942
</phpunit>

0 commit comments

Comments
 (0)