fresns/cmd-word-manager

Command word manager(in laravel) helps plugins(individual functional modules) to communicate with each other easily.

v1.5.3 2024-03-25 11:37 UTC

README

PHP Laravel License

About

Command word manager(in laravel) helps plugins(individual functional modules) to communicate with each other easily.

Sponsors

Fresns is an Apache-2.0-licensed open source project with its ongoing development made possible entirely by the support of these awesome backers. If you'd like to join them, please consider sponsoring Fresns development.

Install

To install through Composer, by run the following command:

composer require fresns/cmd-word-manager

Using

Create cmd word service providers

// Generate cmd word providers: /app/Providers/CmdWordServiceProvider.php
php artisan make:cmd-word-provider
// Generate a cmd word provider for the specified name or directory
php artisan make:cmd-word-provider [Name] [--path Name]

php artisan make:cmd-word-provider FooBar --path Demo
// path directory: /demo/FooBar/Providers/CmdWordServiceProvider.php

Registered service providers

In the providers key value of the /config/app.php file, add the generated command word service provider.

  • App\Providers\CmdWordServiceProvider::class
  • or
  • Demo/FooBar/Providers/CmdWordServiceProvider::class
<?php

return [
    <...>
    'providers' => [
        <...>
        App\Providers\CmdWordServiceProvider::class,
    ],
    <...>
];

Mapping command word

In the properties of the command word provider file /app/Providers/CmdWordServiceProvider.php, in $cmdWordsMap, add the command word mapping config.

<?php

namespace App\Providers;

use Plugins\BarBaz\Models\TestModel;
use Plugins\BarBaz\Services\AWordService;
use Plugins\BarBaz\Services\BWordService;

class CmdWordServiceProvider extends ServiceProvider implements \Fresns\CmdWordManager\Contracts\CmdWordProviderContract
{
    <...>
    protected $fsKeyName = 'FooBar';

    protected $cmdWordsMap = [
        ['word' => 'test', 'provider' => [AWordService::class, 'handleTest']],
        ['word' => 'staticTest', 'provider' => [BWordService::class, 'handleStaticTest']],
        ['word' => 'modelTest', 'provider' => [TestModel::class, 'handleModelTest']],
    ];
    <...>
}

Using cmd words

Request input

// $parameter list = (parameter array);
$wordBody = [
    "email" => "Mail address",
    "title" => "Mail title",
    "content" => "Mail content"
];

// \facades::plugin('plugin name')->cmd word($parameter list): Define the contract for the return object
\FresnsCmdWord::plugin('FresnsEmail')->sendEmail($wordBody);

Another way to write

\FresnsCmdWord::plugin('FresnsEmail')->sendEmail([
    "email" => "Mail address",
    "title" => "Mail title",
    "content" => "Mail content"
]);

Result output

// Success
{
    "code": 0,
    "message": "ok",
    "data": {
        //Command word output data
    }
}

// Failure
{
    "code": 21001,
    "message": "Plugin does not exist",
    "data": {
        //Command word output data
    }
}

Result processing($fresnsResp)

If you are standardized to use command word return results, you can use Fresns Response to help you quickly handle the return of the request.

Example:

$fresnsResp = \FresnsCmdWord::plugin('FresnsEmail')->sendEmail($wordBody);

Handling abnormal situations

if ($fresnsResp->isErrorResponse()) {
    return $fresnsResp->getErrorResponse();
}

Handling normal situations

$fresnsResp->getOrigin(); // Obtaining raw data(code+message+data)

$fresnsResp->getCode(); // Get code only
$fresnsResp->getMessage(); // Get only the message
$fresnsResp->getData(); // Get only the full amount of data
$fresnsResp->getData('user.nickname'); // Get only the parameters specified in data, for example: data.user.nickname

$fresnsResp->isSuccessResponse(); // Determine if the request is true
$fresnsResp->isErrorResponse(); // Determine if the request is false

$fresnsResp->getErrorResponse(); // Internal use returns raw data, API calls return JSON.

Contributing

You can contribute in one of three ways:

  1. File bug reports using the issue tracker.
  2. Answer questions or fix bugs on the issue tracker.
  3. Contribute new features or update the wiki.

The code contribution process is not very formal. You just need to make sure that you follow the PSR-0, PSR-1, and PSR-2 coding guidelines. Any new code contributions must be accompanied by unit tests where applicable.

License

Fresns Command Word Manager is open-sourced software licensed under the Apache-2.0 license.