composer require alexmasterov/psysh-bundle
config/bundles.php:
return [
// ...
AlexMasterov\PsyshBundle\PsyshBundle::class => ['dev' => true],
];
php bin/console psysh:shell
# config/services.yaml
App\Controller\HomeController:
tags: ['psysh.variable']
Then access $homeController in PsySH.Preloading Variables:
psysh.variable tag to auto-load services into the REPL:
# config/services.yaml
services:
App\Service\Mailer:
tags: ['psysh.variable']
config/packages/psysh.yaml:
psysh:
variables:
- { db: '@doctrine.dbal.connection' }
- { user: App\Entity\User::class }
Customizing PsySH Behavior:
config/packages/psysh.yaml:
psysh:
color_mode: forced
use_tab_completion: true
startup_message: 'Welcome to your Symfony REPL!'
Integrating with Tests:
phpunit.xml.dist for debugging:
<env name="PSYSH" value="1"/>
Dynamic Variable Injection:
// src/DependencyInjection/Compiler/PsyshCompilerPass.php
public function process(ContainerBuilder $container) {
$variables = $container->getParameter('psysh.variables');
foreach ($variables as $name => $service) {
$container->setParameter("psysh.variable.$name", $service);
}
}
console.terminate) for post-request debugging.%kernel.environment% in psysh.yaml to toggle features (e.g., disable PsySH in production).PsyshCommand to add project-specific helpers:
// src/Command/CustomPsyshCommand.php
class CustomPsyshCommand extends PsyshCommand {
protected function configure() {
$this->setName('app:psysh');
$this->addOption('model', null, InputOption::VALUE_REQUIRED, 'Load a model class');
}
protected function execute(InputInterface $input, OutputInterface $output) {
$variables = [];
if ($modelClass = $input->getOption('model')) {
$variables['model'] = $modelClass;
}
$this->psysh->setVariables($variables);
parent::execute($input, $output);
}
}
Service Container Scope:
psysh.variable are singletons by default. Use autowire: true and public: false in services.yaml to avoid scope issues:
services:
App\Service\TransientService:
public: false
PsySH Version Mismatch:
^0.9. Ensure compatibility:
composer require psy/psysh:^0.9
0.8), which may break tab completion or syntax highlighting.Configuration Overrides:
config/packages/psysh.yaml override defaults. Use !import to merge configs:
imports:
- { resource: '@PsyshBundle/Resources/config/config.yaml' }
psysh:
<<: *default_config # Merge defaults
color_mode: forced
Debugging in CI/CD:
# config/packages/dev/psysh.yaml
psysh:
enabled: '%env(bool:PSYSH_ENABLED)%'
Then set PSYSH_ENABLED=false in CI.Memory Leaks:
--no-history to avoid persisting state:
php bin/console psysh:shell --no-history
Inspect Loaded Variables:
>>> get_defined_vars()
Lists all available variables in the REPL.
Clear PsySH Cache: If configs aren’t applying, clear the cache:
php bin/console cache:clear
Enable Verbose Mode:
Run PsySH with -v to debug initialization:
php bin/console psysh:shell -v
Custom Variable Providers:
PsyshVariableProviderInterface to dynamically load variables:
class UserVariableProvider implements PsyshVariableProviderInterface {
public function getVariables() {
return ['user' => User::find(1)];
}
}
services.yaml:
services:
App\Service\UserVariableProvider:
tags: ['psysh.variable_provider']
Hook into PsySH Initialization:
PsyshCommand class to modify the REPL environment:
// src/Command/CustomPsyshCommand.php
class CustomPsyshCommand extends PsyshCommand {
protected function getPsyshConfig() {
$config = parent::getPsyshConfig();
$config['startup_message'] = 'Custom message!';
return $config;
}
}
Add Custom Helpers:
config/packages/psysh.yaml:
psysh:
default_includes:
- '%kernel.project_dir%/config/psysh_helpers.php'
psysh_helpers.php:
function dumpEntity($entity) {
return (new \Symfony\Component\VarDumper\Cloner\VarCloner())->cloneVar($entity);
}
How can I help you explore Laravel packages today?