Installation:
composer require --dev ameenross/psysh-bundle
Enable the Bundle:
Add to app/AppKernel.php (Symfony 2.x) or config/bundles.php (Symfony 3.x+):
// Symfony 2.x
if (in_array($this->getEnvironment(), ['dev', 'test'])) {
$bundles[] = new Fidry\PsyshBundle\PsyshBundle();
}
// Symfony 3.x+
return [
// ...
Fidry\PsyshBundle\PsyshBundle::class => ['dev' => true, 'test' => true],
];
First Use Case: Run the PsySH REPL directly:
app/console psysh
$app['kernel'] or $kernel = $app['kernel'].$app['service_id'].Debugging Controllers/Commands:
Psy\Shell::start().$kernel = $app['kernel'];
$kernel->boot();
$request = $app['request_stack']->getCurrentRequest();
// Inspect/modify $request or other objects interactively.
Service Inspection:
$container = $app;
$container->getServiceIds();
dump($container->getDefinition('service_id')->getArguments());
Database Queries:
EntityManager directly:
$em = $app['doctrine.orm.entity_manager'];
$em->createQuery('SELECT u FROM AppBundle:User u')->getResult();
Event Listeners/Subscribers:
$dispatcher = $app['event_dispatcher'];
$dispatcher->getListeners('kernel.request');
Custom Commands:
Psy\Shell for project-specific REPL commands:
class CustomPsyShell extends Psy\Shell {
public function __construct($app) {
parent::__construct();
$this->bind('app', $app);
$this->bind('kernel', $app['kernel']);
}
}
PsyshBundle config (if supported) or override the command.Environment-Specific Config:
config/packages/dev/psysh.yaml (Symfony 3.4+) or app/config/config_dev.yml (Symfony 2.x) to customize:
fidry_psysh:
enabled: true
commands: ['app/console psysh:custom']
Autoloading Helpers:
~/.psyshrc.php:
Psy\Configuration::getInstance()->setFileFinder(new Psy\FileFinder([
__DIR__.'/../src',
__DIR__.'/../vendor',
]));
Symfony Debug Toolbar Integration:
Psy\Shell to inspect the _profiler or _token_storage:
$profiler = $app['profiler'];
$profiler->collect();
Testing:
Psy\Shell::start() in setUp():
public function setUp(): void {
if (getenv('PSYSH')) {
Psy\Shell::start();
}
}
PSYSH=1 phpunit
Symfony 3.x+ Compatibility:
Psy\Psysh v0.3+ is compatible with your PHP version (e.g., PHP 7.1+ may need newer PsySH).Container Access:
$app variable in PsySH is the service container, not the Kernel. Access services via $app['service_id'], not $app->getService().Namespace Conflicts:
PsyshBundle or PsyShell to prevent collisions with the bundle’s classes.Performance in Production:
/psysh).Doctrine Proxy Issues:
config/packages/dev/doctrine.yaml:
orm:
proxy_dir: '%kernel.cache_dir%/doctrine/orm/Proxies'
proxy_namespace: 'AppBundle\Proxy'
PsySH Crashes:
exit to quit gracefully.rm -rf ~/.cache/psysh/
Service Not Found:
$app->getServiceIds(); // List all services
Doctrine Lazy Loading:
$entity = $em->getRepository('AppBundle:User')->find(1);
$em->getUnitOfWork()->getEntityManager()->refresh($entity);
Custom Autoloading:
vendor/autoload.php is loaded in PsySH:
require __DIR__.'/../../../vendor/autoload.php';
Custom PsySH Commands:
Psy\Command\Command and register it in the bundle’s config:
fidry_psysh:
commands:
- AppBundle\Command\CustomPsyCommand
Override the Shell:
Fidry\PsyshBundle\Command\PsyshCommand to modify behavior:
class CustomPsyshCommand extends PsyshCommand {
protected function getShellClass() {
return CustomPsyShell::class;
}
}
Add Custom Bindings:
Psy\Shell:
$shell->bind('my_service', $app['my_service']);
$shell->bind('logger', $app['logger']);
Integrate with Symfony Debug:
Psy\Shell to dump Symfony’s debug data:
dump($app['debug.parameter_bag']->all());
How can I help you explore Laravel packages today?