Posted on
Tinkerwell is an app that allows you to run arbitrary code against your Laravel app from within a code editor (which supports multiple cursors, by the way). It will display your results immediately, meaning you no longer have to clumsily type your code into the command line with Artisan Tinker. The use cases for this include quickly testing or prototyping ideas, debugging your app, or running quick Eloquent reports.
There are additional features as well, including running code against remote Laravel apps via SSH, playing around with views and controllers, and running code against a vanilla Laravel app without have to create a new installation. Needless to say, I love the app.
Realizing the benefits and how much time and frustration it would save me, I immediatly purchased the app, downloaded it, and opened it up. To get acquainted with it, I played around with some random collections at first. It worked very well. In an effort to actually use it though, I quickly pointed it to my local installation of our company app and received an error:
Warning: require(/Users/patrick/Sites/xxx/xxx/vendor/autoload.php): failed to open stream: No such file or directory in /Users/patrick/Library/Application Support/Tinkerwell/Laravel/6.0/public/tinker.php on line 6
Call Stack:
0.0013 425976 1. {main}() /Users/patrick/Library/Application Support/Tinkerwell/Laravel/6.0/public/tinker.php:0
Fatal error: require(): Failed opening required '/Users/patrick/Sites/xxx/xxx/vendor/autoload.php' (include_path='.:/usr/local/php5/lib/php') in /Users/patrick/Library/Application Support/Tinkerwell/Laravel/6.0/public/tinker.php on line 6
Call Stack:
0.0013 425976 1. {main}() /Users/patrick/Library/Application Support/Tinkerwell/Laravel/6.0/public/tinker.php:0
That's interesting. It looks like Tinkerwell is looking for the composer autoload script. But what is this tinker.php file? I open it up to find this at the top of the file:
<?php
$tinkerData = $argv[1];
$projectPath = $argv[2] ?? __DIR__.'/../';
require $projectPath.'/vendor/autoload.php';
$app = require_once $projectPath.'/bootstrap/app.php';
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
$kernel->bootstrap();
So there are a couple things to note here:
vendor/autoload.php
.bootstrap/app.php
Well, our app is not a standard Laravel app. It started as a Wordpress site and it is being gradually migrated over to Laravel. Needless to say, the vendor and bootstrap directories are in completely different locations.
So there are a few things that Tinkerwell needs for my app to work with it:
/vendor/autoload.php
/bootstrap/app.php
that returns the console kernelSince our app utilizes Wordpress logic along with logic from the vendor directory, I have to include the autoload script, wordpress, and our entry file in a specific order:
Our entry file routes requests to Wordpress or Laravel and exposes the Laravel kernel as a $laravel_app
global variable. To make Tinkerwell work with my app I:
./vendor/autoload.php
that was completely empty (Remember that Tinker only require
s this file to include the vendor logic. Tinkerwell doesn't actually care where the classes are coming from, it just requires that a file exists at that location.)../bootstrap/app.php
with this content:<?php
// Wordpress
require_once('../../../app/wp-load.php');
// Vendor files
require_once('./dependencies/composer/autoload.php');
// Entry file that exposes the kernel in $laravel_app
require_once('./includes/laravel/init.php');
// Return the kernel
return $laravel_app;
I am importing Wordpress, importing the vendor classes, including our entry file and router, and returning the kernel. Now Tinkerwell works! I can query my models, utilize the Wordpress methods, and anything else I need to.
This app saves me a lot of time and frustration from having to run complicated queries in my database GUI or running code in Artisan Tinker. I find it well worth the $15 price tag and encourage you to check it out and help support another artisan: https://tinkerwell.app/.