Writing Plugins


Writing Plugins

Plugins are loaded on a per Project bases through the Project Kernel. Ideally each plugin will have a Service Provider that hooks into the IoC and loads the plugin into the work space. When initiated by the Project Kernel boot method a plugin will have full access to the entire core of Tapestry and be able to extend or replace any component.

Plugin folder structure

To keep things consistent each plugin should follow the below folder structure:

webroot/
├── src/
|   ├── ServiceProvider.php
|   └── ...
├── README.md
└── composer.json

Service Providers

The most consistent method of loading your project into Tapestry is via a Service Provider. The IoC in Tapestry is provided by the PHP League container library and so each Service Provider class must extend League\Container\ServiceProvider\AbstractServiceProvider.

Because Tapestry plugins require booting upon registration the plugin service provider also must implement the League\Container\ServiceProvider\BootableServiceProviderInterface.

An example of this can be seen in the Skeleton Plugin example here.

Installing your plugin

Plugins should be registered with packagist.org enabling them to be imported into users project via use of composer.

Once imported into a Tapestry project your plugin can be installed by registering its Service Provider within the boot method of the project Kernel, for example:

<?php

namespace MyProject;

use Tapestry\Modules\Kernel\KernelInterface;

class Kernel implements KernelInterface
{
    /**
     * @var Tapestry
     */
    private $tapestry;

    public function __construct(Tapestry $tapestry)
    {
        $this->tapestry = $tapestry;
    }

    /**
     * This method is executed by Tapestry when the Kernel is registered.
     *
     * @return void
     */
    public function register()
    {
        // Use project autoloader
        require_once(__DIR__ . '/vendor/autoload.php');
    }

    /**
     * This method of executed by Tapestry as part of the build process.
     *
     * @return void
     */
    public function boot()
    {
        $this->tapestry->register(\PluginAuther\PluginName\ServiceProvider::class);
    }
}