Tinkerwell 4 is out now! Get the most popular PHP Scratchpad application. Learn more

Go back to Blog

How to write a custom Tinkerwell driver

Sebastian Schlein

Tinkerwell bootstraps your application with something that we call driver and adds common use cases to a right-click context menu item. At the time of this writing, Tinkerwell supports Laravel, October CMS and Wordpress out of the box. If none of the drivers match your application, the fallback kicks in, and we try to load the autoload file if it is present.

Writing a Tinkerwell driver is not that hard, and the easiest way to get started is by creating a project-specific driver in your application. To do this, you have to create a .tinkerwell directory in the root directory of your application and create a new driver in there. Tinkerwell drivers always end with the name TinkerwellDriver, so your custom driver could be named CustomTinkerwellDriver.php.

A custom driver that extends the Laravel driver but adds project-specific context menu items can be created with this simple snippet:

use Tinkerwell\ContextMenu\SetCode;

class CustomTinkerwellDriver extends LaravelTinkerwellDriver {

	public function contextMenu(): array
	{
		return array_merge(parent::contextMenu(), [
			SetCode::create('Find latest users', <<<EOT
User::latest()->first()
EOT),
		]);
	}

}

Defining more other items like labels, submenus, or open links in the browser work in a similar fashion:

Label::create('Detected Laravel v' . app()->version()),
        
Submenu::create('Snippets', [
  SetCode::create('Perform Query', '\DB::table("example")->get();'),
]);

OpenURL::create('Documentation', 'https://tinkerwell.app'),

Providing variables

When a Tinkerwell driver gets loaded, you can tell Tinkerwell to automatically set variables with specific content, so that these variables are immediately available within the Tinkerwell application.

To define these variables, add a getAvailableVariables to your driver. This method should return an array of all variables and their values:

public function getAvailableVariables()
{
    return [
        '__blog' => get_bloginfo()
    ];
}

This method gets executed after the Tinkerwell drivers bootstrap method was called, so that you have access to any classes that got bootstrapped along with the driver.

Adding support for other frameworks

Sometimes you work on projects that use different frameworks or are extensions to products like Magento. In this case, it makes sense to write a custom driver for these frameworks that bootstrap the application for you and provide everything you need to tinker with your code.

The easiest way to get started is by having a look at our documentation and the code of existing drivers. You can do this here.

Tinkerwell: The PHP Scratchpad

Write and debug your applications in an editor designed for fast iterations.

Learn more