I made a series about psysh and Laravel’s tinker – powerful REPL where you can play with your whole application.

ICYMI check it out here: tinker like a boss and here’s example of what we can achieve:

There is, however, missing piece of the puzzle in the series. It describes how you can custimze presenters in REPL (called casters in PsySH), but I describe generic, global casters that reside in PsySH config. This is very handy, but you cannot distribute them among your team members (or anyone else for that matter) in the repo.

Today we are going to add that piece to the puzzle and see how to customize and override psysh casters in Laravel app.

We need just a few simple steps:

  1. Override tinker command
  2. Override service provider
  3. Create our casters and define them in the application config

 

Override original ServiceProvider and Command

First let’s create our customized Command, which will allows us to provide array with customized casters:

 

Next step is to use this command instead of the original – we can achieve that by replacing the service provider:

Create and register casters

Finally, let’s create example caster and set it up in the config/tinker.php configuration file:

 

And we are up & running!

 

Let’s see that in action before we dive into the code:

 

 

PS. Soon you will be able to customize casters without hassle, as I’ve already pushed a PR, which provides this functionality out-of-the-box:

https://github.com/laravel/tinker/pull/39

It's only fair to share...Tweet about this on TwitterShare on RedditPin on PinterestShare on FacebookShare on Google+

Related Post

20 Eloquent tricks – more Recently I stumbled upon interesting article by Povilas Korop on laravel-news 20 eloquent tips and tricks. First of all thanks for sharing your knowle...
A story about Laravel Resources Leo is a brilliant developer. He turns every idea into code in no time. He's prepared for any task thrown at him and dives into coding straight away, ...
Too much M4g1c will kill you (or at least bite you... So, as you know, I'm a big fan of Laravel, in particular Eloquent is my favourite. The beauty and value of both is a lot of conventions and magic...
Tinker like a boss – WTF?! This is part of the series tinker like a boss (in psysh) tinker... what? includes casters (presenters) aliases http (and other) helpers ...