定制 estasi/plugin-manager 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

estasi/plugin-manager

Composer 安装命令:

composer require estasi/plugin-manager

包简介

Plugin Manager for dynamically connecting classes

README 文档

README

PluginManager is a specialized service Manager used for creating uniform objects of a certain type, such as Estasi\Validator and Estasi\Filter use specialized corresponding PluginManager.

Installation

To install with a composer:

composer require estasi/plugin-manager

Requirements

  • PHP 7.4 or newer
  • Data Structures: composer require php-ds/php-ds
    Polyfill is installed with the estasi/plugin-manager package.
  • Container interface composer require psr/container
    Is installed with the estasi/plugin-manager package.

Usage

Creating a plugin manager

To create a plugin manager, you first need to create a new class that extends Estasi\PluginManager\Abstracts\PluginManager:

<?php

declare(strict_types=1);

use Estasi\PluginManager\{Abstracts,Interfaces,Plugin,PluginsList};

class MyPluginManager extends Abstracts\PluginManager {

    public function getInstanceOf(): ?string
    {
        return 'MyBasicClass';
    }

    public function getPlugins(): Interfaces\PluginsList
    {
        return new PluginsList(
            new Plugin(MyFirstClass::class, ['first', 'First', /*...  aliases of class*/]),
            new Plugin('MySecondClass', ['second', 'Second', /*... aliases of class */], new MyFactoryForSecondClass()),
        );
    }
}

Using PluginManager

Then you can create a class from PluginManager

<?php

declare(strict_types=1);

$myPluginManager = new MyPluginManager();

// Creating a object without constructor parameters
$myFirstClass = $myPluginManager->get(MyFirstClass::class);
// or
$myFirstClass = $myPluginManager->build('first');

// With the parameters necessary (or optional) to create the object
$myFirstClass = $myPluginManager->build('First', ['foo' => 'bar', 'bar' => 'foo']);

Note

To create objects, the factory used by default uses the modified \ReflectionClass class. Its special feature is that all constructor parameters must be in CamelCase notation.

If the constructor has the options parameter, all parameters passed to create the class object, but not found in the constructor, fall into this parameter. If there are no options in the parameters passed to create the class, they are ignored.

Default parameter values are substituted if they are not found in the passed values.

<?php

declare(strict_types=1);

class MyFirstClass {

    public function __construct($foo, $bar = 'foo', iterable $options = null)
    {
        // your code
    }
}
<?php

declare(strict_types=1);

$myPluginManager = new MyPluginManager();

// "foo" required parameter. if it is not present, the \OutOfBoundsException exception will be created
// the optional "bar" parameter will get the default value "foo"
// the undeclared "baz" parameter will be included in the "options" parameter: ["baz" = "baz"]
$myFirstClass = $myPluginManager->build(MyFirstClass::class, ['foo' => 'bar', 'baz' => 'baz']);

// the same thing
// if you use this method all undeclared parameters will not be used when creating the object 
// and will not be included in "options"
$myFirstClass = $myPluginManager->build(MyFirstClass::class, ['foo' => 'bar', 'options' => ['baz' => 'baz'], 'param' => 'value']);

License

All contents of this package are licensed under the BSD-3-Clause license.

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2020-05-20

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固