A Tinkerwell driver is a simple class that will be called when your project gets opened in the Tinkerwell application.
Here is how a basic driver for Wordpress looks like:
class MyCustomTinkerwellDriver extends TinkerwellDriver
{
/**
* Determine if the driver can be used with the selected project path.
* You most likely want to check the existence of project / framework specific files.
*
* @param string $projectPath
* @return bool
*/
public function canBootstrap($projectPath)
{
return file_exists($projectPath . '/wp-load.php');
}
/**
* Bootstrap the application so that any executed can access the application in your desired state.
*
* @param string $projectPath
*/
public function bootstrap($projectPath)
{
require $projectPath . '/wp-load.php';
}
}
The canBootstrap
and bootstrap
methods are the important pieces of every driver.
They determine if the driver can be used for the given project path and how the application should get bootstrapped and prepared,
so that the application is in the correct state when any code gets executed.
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.
You can create dynamic snippets that Tinkerwell loads on project connection. It is useful if you don't want to create new methods within the project because these snippets are only run by developers via Tinkerwell.
class CustomTinkerwellDriver extends LaravelTinkerwellDriver
{
public function snippets()
{
return [
Snippet::create('Generate Coupon Codes', function () {
$paddle = app(\App\Services\Paddle\Paddle::class);
$paddle->generateCouponCode($numCouponCodes = 1, $prefix = '');
})
];
}
}
If you want to use your custom Tinkerwell driver in an existing project, create a folder called .tinkerwell
in the application directory where you want to customize the context menu.
Then you can create a new file called CustomTinkerwellDriver.php
. This file should now contain the custom driver that you want to use.
Custom drivers can either be local within a project or used globally for all your projects. This makes it easier if you run multiple projects that have a tech stack that is not covered by the included driver setup. You cann add global drivers to your global Tinkerwell configuration path:
# Mac/Linux
~/.config/tinkerwell/CustomDriver.php
Please be aware that global drivers override local drivers. So if you have a local driver for a project and a global driver for the same framework, the global driver will be used.
If you feel like your custom driver might be useful for others, for example if you add support for a commonly used framework or PHP application, please consider sending the custom driver as a pull request to the Tinkerwell Drivers repository so that everyone can benefit from the driver in the future.