composer require alexmasterov/psysh-bundle
config/bundles.php:
AlexMasterov\PsyshBundle\PsyshBundle::class => ['dev' => true],
php bin/console psysh:shell
This launches an interactive REPL with Symfony’s container pre-loaded.php bin/console psysh:shell --var=@some_controller
Or configure via config/packages/psysh.yaml:
psysh:
variables:
- @some_controller
Pre-Loading Variables:
services.yaml for auto-injection:
services:
App\Service\MailService:
tags: ['psysh.variable']
config/packages/psysh.yaml:
psysh:
variables:
- { user: App\Entity\User::find(1) }
- { db: '@doctrine.dbal.connection' }
Integration with Artisan Commands: Use PsySH to debug commands interactively:
php bin/console psysh:shell --var=@app.command.mailer
Then inspect $mailer->getOptions() or modify state dynamically.
Event Listeners/Debugging:
Attach to kernel events (e.g., kernel.request) and inspect request/response:
// In REPL:
>>> $event = new GetResponseForControllerResultEvent($controller, $request, $response);
>>> $listener->onKernelRequest($event, $request->get('some_key'));
Testing Edge Cases: Simulate user input or exceptions in REPL:
>>> $user = new User(); $user->setRole('admin');
>>> $controller->handle($request)->getStatusCode(); // Test logic
Custom PsySH Configuration:
Extend the bundle’s PsyshServiceProvider to add:
ls → dump()).// src/Psysh/PsyshServiceProvider.php
public function register()
{
$this->app['psysh']->addConfig([
'variables' => ['@router' => $this->app->get('router')],
'aliases' => ['routes' => 'dump($this->app->get(\'router\')->getRouteCollection())'],
]);
}
Environment-Specific Variables:
Use %kernel.environment% in psysh.yaml:
psysh:
variables:
- { debug: '%kernel.debug%' }
- { env: '%env(APP_ENV)%' }
Container Not Available in All Contexts:
psysh:shell outside bin/console, the container may not be initialized.php bin/console psysh:shell.Variable Naming Conflicts:
ls, exit).user_1 instead of user).Performance in Large Apps:
psysh:
variables:
- @service.a # Only load specific services
Debugging in CI/CD:
prod by default (dev: true in bundles.php).--env=dev to force-enable:
php bin/console --env=dev psysh:shell
Inspect Container Services: List all available services in REPL:
>>> $container->getServiceIds();
>>> $container->has('service.name') ? 'Exists' : 'Missing';
Clear PsySH Cache: If variables aren’t loading, clear the cache:
php bin/console cache:clear
Enable Verbose Output: Debug PsySH initialization:
php bin/console debug:config psysh
Custom Commands: Create a command to pre-load PsySH with specific variables:
// src/Command/DebugUserCommand.php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class DebugUserCommand extends Command
{
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->getApplication()->find('psysh:shell')->run([
'--var' => ['@user_repository', '@mailer'],
]);
}
}
Override PsySH Configuration: Use a compiler pass to modify PsySH’s config dynamically:
// src/DependencyInjection/Compiler/PsyshPass.php
public function process(ContainerBuilder $container)
{
$definition = $container->findDefinition('psysh');
$definition->addMethodCall('addConfig', [
['variables' => ['@custom_service']],
]);
}
Integrate with Xdebug: Use PsySH to inspect Xdebug sessions:
>>> xdebug_break(); // Trigger Xdebug
>>> $this->app->get('debug.stopwatch')->get('event_name')->getDuration();
How can I help you explore Laravel packages today?