Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Psysh Laravel Package

psy/psysh

PsySH is an interactive PHP REPL and runtime developer console for debugging and exploring code. Inspect variables, run snippets, and get contextual help in a powerful shell, with configuration, themes, and integrations available.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation:

    composer require psy/psysh
    

    For Laravel projects, use laravel/tinker (built on PsySH) or install PsySH directly.

  2. First Use:

    vendor/bin/psysh
    

    Or in Laravel:

    php artisan tinker
    
  3. First Command:

    $user = App\Models\User::first();
    $user->name; // Inspect property
    

Where to Look First

  • Usage Wiki – Covers magic variables ($_, $_E, $_S), history, and shell integration.
  • Magic Variables – Essential for debugging (e.g., $_ for last result, $_E for last exception).
  • Commands – List of built-in commands like ls, doc, edit, and copy.

First Use Case: Debugging a Laravel Model

// Load a model
$user = App\Models\User::find(1);

// Inspect properties
$user->name;

// Call a method interactively
$user->posts()->count();

// Use magic variables
$_ // Last result (count)
$_E // Last exception (if any)

Implementation Patterns

Daily Workflows

  1. Interactive Debugging:

    • Use ls to list methods/properties of an object:
      $user = App\Models\User::first();
      ls($user); // Lists all methods/properties
      
    • Inspect with show:
      show($user->posts); // Detailed output
      
  2. Code Exploration:

    • Use doc to view PHPDoc for a class/method:
      doc(App\Models\User); // Shows class docblock
      
    • Autocomplete with Tab (e.g., $user-> → press Tab to list methods).
  3. Quick Tests:

    • Test snippets without writing a full script:
      $valid = Validator::make(['email' => 'test@example.com'], ['email' => 'required|email']);
      $valid->fails(); // Check validation
      $_ // Last result (bool)
      
  4. Shell Integration:

    • Use !! to execute shell commands:
      !!ls -la // Lists files in the current directory
      
    • Pipe output to PsySH:
      echo "<?php return 1 + 1;" | vendor/bin/psysh
      
  5. Editing Code:

    • Use edit to open a file in $EDITOR:
      edit(app_path('Http/Controllers/UserController.php'));
      
    • Hot Reloading (with uopz extension): Modify a file and switch back to PsySH—changes apply instantly.
  6. Configuration:

    • Runtime config changes (e.g., disable syntax highlighting):
      config('useSyntaxHighlighting', false);
      
    • Persistent config in .psysh.php (project root):
      return [
          'useExperimentalReadline' => true,
          'theme' => 'monokai',
      ];
      

Integration Tips

  • Laravel: Use php artisan tinker (built on PsySH) for seamless integration with Laravel’s service container.
  • Custom Commands: Extend PsySH with custom commands by implementing Psy\Command\Command.
  • Autoloading: Ensure your project’s composer.json autoloads are visible in PsySH (check autoload_psr-4 in .psysh.php if needed).
  • Environment-Specific Config: Use PSYSH_CONFIG env var to point to a custom config file:
    PSYSH_CONFIG=config/psysh-local.php vendor/bin/psysh
    

Gotchas and Tips

Pitfalls

  1. Trust Project Warnings:

    • PsySH v0.12.19+ requires explicit trust for local config (.psysh.php) or Composer autoloads in untrusted directories.
    • Fix: Run vendor/bin/psysh --trust-project or set 'trustProject' => 'always' in config.
    • Non-interactive contexts (e.g., CI/CD) may fail silently—use --no-trust-project to skip checks.
  2. Hot Reloading Limitations:

    • Does not work with:
      • New class methods/properties.
      • Changes to class inheritance or interfaces.
      • Static variables or conditional definitions (use yolo to bypass safety checks).
    • Requires: uopz extension (pecl install uopz).
  3. Experimental Readline:

    • Enable with --experimental-readline or 'useExperimentalReadline' => true.
    • Pros: Syntax-aware completions, multi-line editing, live syntax highlighting.
    • Cons: May feel sluggish on older terminals; disable with config('useExperimentalReadline', false).
  4. Magic Variables Overwritten:

    • $_, $_E, $_S are overwritten on each command. Use show($_) to inspect the last result persistently.
  5. Pager Issues:

    • Output may hang if the pager (e.g., less) is closed early. Use --no-pager to disable:
      vendor/bin/psysh --no-pager
      
  6. Windows Line Endings:

    • Use dos2unix or configure PsySH to handle CRLF line endings in .psysh.php:
      'lineEnding' => "\n", // Force LF
      

Debugging Tips

  1. Inspect PsySH State:

    • Use psy\info() to check config, readline state, and environment:
      psy\info();
      
    • Run with --info flag for CLI output:
      vendor/bin/psysh --info
      
  2. Enable Verbose Mode:

    • Debug issues with:
      vendor/bin/psysh -vvv
      
    • Or in-shell:
      config('verbosity', 3);
      
  3. Clipboard Issues:

    • If copy command fails, configure clipboardCommand in .psysh.php:
      'clipboardCommand' => 'pbcopy', // macOS
      // 'clipboardCommand' => 'xclip -selection clipboard', // Linux
      // 'clipboardCommand' => 'clip', // Windows
      
    • For SSH/remote terminals, use OSC 52:
      'useOsc52Clipboard' => true,
      
  4. Command Ambiguity:

    • Prefix with ; to force PHP execution (e.g., ;config('key', 'value') instead of config('key', 'value')).
  5. Performance:

    • Disable syntax highlighting for large outputs:
      config('useSyntaxHighlighting', false);
      
    • Use compact output for deep objects:
      config('compactOutput', true);
      

Extension Points

  1. Custom Commands:

    • Create a class extending Psy\Command\Command:
      namespace App\PsySH;
      use Psy\Command\Command;
      
      class MyCommand extends Command {
          public function __invoke() {
              $this->output->writeln('Hello from custom command!');
          }
      }
      
    • Register in .psysh.php:
      return [
          'commands' => [
              'mycmd' => App\PsySH\MyCommand::class,
          ],
      ];
      
  2. Themes:

    • Create a custom theme by extending Psy\Theme\Theme and add to .psysh.php:
      'theme' => App\PsySH\CustomTheme::class,
      
  3. Exception Details:

    • Add context to exceptions via exceptionDetails callback:
      'exceptionDetails' => function (\Throwable $e) {
          return "Validation errors: " . json_encode($e->validator->errors());
      },
      
  4. Pre-Initialization:

    • Run code before PsySH starts by defining PSYSH_PRE_INIT in .psysh.php:
      'preInit' => [
          'App\PsySH\Bootstrap::init',
      ],
      

Pro Tips

  1. Aliases:
    • Define shortcuts in .psysh.php:
      'aliases' => [
          'u' => 'App\Models\User',
          'p' => 'App\Models\Post',
      ];
      
    • Now u::first() works instead of `App\Models
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport