Skip to content
Joe Huss edited this page Nov 10, 2022 · 1 revision

Basic plugin generation and publishing process

Principle

1、Taking the cross-domain plugin as an example, the plugin is divided into three parts, one is the cross-domain middleware program file, one is the middleware configuration file middleware.php, and one is automatically generated by commandInstall.php。 2、We use the command to package and publish the three files to composer。 3、When the user installs the cross-domain plugin using composer, Install.php in the plugin will copy the cross-domain middleware program files as well as the configuration files to {main project}/config/plugin for webman to load. Implemented cross-domain middleware files are automatically configured to take effect。 4、When the user removes the plugin using composer, Install.php removes the corresponding cross-domain middleware program files and configuration files, enabling automatic plugin uninstallation。

specification

1、The plugin name consists of two parts, vendor and plugin name, e.g. webman/push, which corresponds to the composer package name。 2、Plugin configuration files are placed uniformly under config/plugin/vendor/plugin-name/ (the console command automatically creates the configuration directory). If the plugin does not need to be configured, you need to delete the automatically created configuration directory。 3、The plugin configuration directory only supports app.php plugin main configuration, bootstrap.php process startup configuration, route.php routing configuration, middleware.php middleware configuration, process.php custom process configuration, database.php database configuration, redis.php redis configuration and thinkorm.php thinkorm configuration. These configurations will be automatically recognized by webman。 4、The plugin uses the following method to get the configuration config('plugin.vendor. Plugin name. Configuration file. Specific configuration items');, for exampleconfig('plugin.webman.push.app.app_key') 5、Plugins that have their own database configuration are accessed via. illuminate/database as Db::connection('plugin.vendor. PluginName. Specific connection') and thinkrom as Db::connct('plugin.vendor. plug-in-name. Specific connection'') 6、If the plugin needs to place business files in the app/ directory, you need to make sure it does not conflict with the user project and other plugins。 7、Plugins should try to avoid copying files or directories to the main project, e.g. cross-domain plugins need to be copied to the main project except for configuration files, middleware files should be placed under vendor/webman/cros/src and do not need to be copied to the main project。 8、The plugin namespace is recommended to use uppercase, e.g. Webman/Console。

Examples

Install the webman/console command line

composer require webman/console

Create Plugin

Suppose the name of the plugin created is foo/admin (the name is also the name of the project that composer will publish later, the name needs to be lowercase) Run command php webman plugin:create --name=foo/admin

Create Pluginquery builder's vendor/foo/admin for storing plugin-related files and config/plugin/foo/admin Used to store plugin-related configuration。

Note config/plugin/foo/admin The following configurations are supported, app.php plugin main configuration, bootstrap.php process startup configuration, route.php routing configuration, middleware.php middleware configuration, process.php custom process configuration, database.php database configuration, redis.php redis configuration thinkorm.php thinkorm configuration. The configuration format is the same as webman, and these configurations are automatically recognized by webman and merged into the configuration 。 Access with plugin prefix when using, e.g. config('plugin.foo.admin.app');

Export Plugin

When we are done developing the plugin, execute the following command to export the plugin php webman plugin:export --name=foo/admin Export

Description ExportAfter will beconfig/plugin/foo/adminYour usernamevendor/foo/admin/src下,while automatically generating aInstall.php,Install.phpis used to perform some actions during automatic installation and automatic uninstallation。 The default installation action is to copy the configuration under vendor/foo/admin/src to the current project under config/plugin The default action when removing is to delete the configuration file under the current project config/plugin You can modify Install.php to do some customization when installing and uninstalling plugins。

Submit Plugin

  • after generating the directory githubpackagist Account
  • githubwith developing oneadminProject and upload the code,The solution is to use https://github.com/Click in the navigation/admin
  • Go to address https://github.com/Click in the navigation/admin/releases/new to release a release as v1.0.0
  • Accesspackagistis satisfied with bothSubmit,will be yourgithubRelated connectionshttps://github.com/Click in the navigation/adminSubmit it so that the release of a plugin is completed

hint If submitting a plugin in packagist shows word conflicts, you can rename it to a vendor, for example foo/admin to myfoo/admin

Subsequently when your plugin project code has updates, you need to sync the code to github and go to the address https://github.com/Click in the navigation/admin/releases/new again to re-release a release, then go to https://packagist.org/ packages/foo/admin page and click the Update button to update the version

Adding commands to plugins

Sometimes our plugins need some custom commands to provide some auxiliary functions, for example, after installing the webman/redis-queue plugin, the project will automatically add a redis-queue:consumer command, the user can just run php webman redis-queue:consumer send-mail and a SendMail.php consumer class will be generated in the project, which helps to quickly develop 。

Assuming that the foo/admin plugin needs to add the foo-admin:add command, refer to the following steps。

New command

Create a new command file vendor/foo/admin/src/FooAdminAddCommand.php

<?php

namespace Foo\Admin;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;

class FooAdminAddCommand extends Command
{
    protected static $defaultName = 'foo-admin:add';
    protected static $defaultDescription = 'Here is the command line description';

    /**
     * @return void
     */
    protected function configure()
    {
        $this->addArgument('name', InputArgument::REQUIRED, 'Add name');
    }

    /**
     * @param InputInterface $input
     * @param OutputInterface $output
     * @return int
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $name = $input->getArgument('name');
        $output->writeln("Admin add $name");
        return self::SUCCESS;
    }

}

Note To avoid command conflicts between plugins, the command line format is recommended to be vendor-plugin-name:specific-command, for example foo/admin All commands of a plugin should be prefixed with foo-admin:, for example foo-admin:add

Add Configuration

New Configuration config/plugin/foo/admin/command.php

<?php

use Foo\Admin\FooAdminAddCommand;

return [
    FooAdminAddCommand::class,
    // ....Multiple configurations can be added...
];

hint command.php for configuring custom commands to the plugin,Each element of the array corresponds to a command line class file,Each class file corresponds to one command。When userRun commandline whenwebman/consoleWill automatically load each plugincommand.phpCustom commands set in。 For more command line related please refer tocommand-line

Perform Export

Execute the command php webman plugin:export --name=foo/admin to export the plugin and submit it to packagist. This will add a foo-admin:add command after the user installs the foo/admin plugin. Executing php webman foo-admin:add jerry will print the Admin add jerry

Clone this wiki locally