enzyme/laravel-binder

Laravel 5 的服务容器上下文绑定助手。

v1.0.0 2016-04-03 07:55 UTC

This package is not auto-updated.

Last update: 2024-09-23 06:18:06 UTC


README

Build Status Coverage Status Scrutinizer Code Quality

Laravel 5 的上下文绑定助手。

示例

从服务提供者。

use Enzyme\LaravelBinder\Binder;

// Inside the app service provider...
public function register()
{
    $binder = new Binder($this->app);

    $binder->setAlias(
        'controller.listing',
        'App\Http\Controllers\ListingController'
    );

    $binder->setAlias(
        'repos.interface',
        'Acme\Repositories\RepositoryInterface'
    );

    // Option 1 for binding, using aliases.
    $binder->setBinding(
        'repos.listing',
        'repos.interface',
        'Acme\Repositories\ListingRepository'
    );

    // Option 2 for binding, using FQNs.
    $binder->setBinding(
        'factories.listing',
        'Acme\Factories\FactoryInterface',
        'Acme\Factories\ListingFactory'
    );

    // Tell the service container that the ListingController
    // needs the ListingRepository & ListingFactory.
    $binder->setNeeds(
        'controller.listing',
        ['repos.listing', 'factories.listing']
    );

    $binder->register();
}

从控制器。

// You don't need to inject the specific concrete classes, just the
// interfaces. The Binder + Service Container has taken care of it for you.
public function __construct(RepositoryInterface $repo, FactoryInterface $factory)
{
    $this->repo = $repo;
    $this->factory = $factory;
}

有什么用途?

随着您的服务提供者(们)成为唯一有权决定哪些类获取哪些实现的权威机构,如果您需要切换这些实现,这将非常简单,只需一个位置即可!