Custom Tinkerwell Integration

Using Tinkerwell with a Non-standard Laravel App

Image By Clark Young on Unsplash

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.

Shut up and take my money!

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:

  1. Tinkerwell is looking for the composer autload file in the assumed location of vendor/autoload.php.
  2. Tinkerwell is looking for the Laravel bootstrap file in the assumed location of bootstrap/app.php
  3. Tinkerwell is importing the Laravel console kernel and running your code against that.

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:

  1. A file located at /vendor/autoload.php
  2. A file located at /bootstrap/app.php that returns the console kernel
  3. Access to the vendor and global logic.

Since 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:

  1. Wordpress
  2. Autoload
  3. Entry file

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:

  • Created a file at ./vendor/autoload.php that was completely empty (Remember that Tinker only requires 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.).
  • Created a file at ./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/.

Newsletter


Get notified when I post something new.

Latest Quick Tips


Bash Alias for Creating a Laravel Project
  • A Quick Tip posted on 2018-07-31
Carbon Helper
  • A Quick Tip posted on 2017-10-05

Latest Blog Posts


Custom Tinkerwell Integration
  • A Blog Post posted on 2019-11-05
How to Persist Databases Across Homestead Instances
  • A Blog Post posted on 2018-02-16
How to Set Up Xdebug on Laravel Homestead and VSCode
  • A Blog Post posted on 2017-10-23
Compiling LESS Using Blade Templates
  • A Blog Post posted on 2016-07-21