定制 mattferris/provider 二次开发

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

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

mattferris/provider

最新稳定版本:0.3

Composer 安装命令:

composer require mattferris/provider

包简介

Implementation standard for consumer/provider interfaces

README 文档

README

Common interfaces ProviderInterface and ConsumerInterface for inverting control of configuration. This allows a consumer to pass control to a provider so that the provider can configure the consumer.

The consumer must implement a register() method and the provider must implement a provides() method. An instance of provider is passed to register(), and register() in turn calls provides(), passing an instance of itself. This then allows the provider to call methods on the consumer and configure as needed.

In practice, this might look like:

use MattFerris\Provider\ConsumerInterface;
use MattFerris\Provider\ProviderInterface;

// service container which contains service definitions
class ServiceContainer implements ConsumerInterface
{
    public function register(ProviderInterface $provider)
    {
        $provider->provides($this);
        return $this;
    }

    ...
}

class DatabaseProvider implements ProviderInterface
{
    public function provides(ConsumerInterface $consumer)
    {
        $consumer->set('DatabaseService', $databaseServiceDefinition);
    }
}

$container = new ServiceContainer();
$container->register(new DatabaseProvider());
$db = $container->get('DatabaseService');
$db->query(...);

Note: ProviderInterface doesn't type-hint what kind of consumer it expects. This is on purpose, as providers implementing the interface can then validate the consumer based on what type they want. If a consumer isn't of they correct type, providers can throw an InvalidConsumerException.

Taking this example further, if an application contains multiple packages that each provide services, packages can configure their services automatically by implementing their own providers.

$container = new ServiceContainer();
$container
    ->register(new DatabasePackage\ServiceProvider())
    ->register(new Authentication\ServiceProvider())
    ->register(new EventDispatcher\ServiceProvider());

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Unknown
  • 更新时间: 2015-10-22