承接 gonzalo123/injector 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

gonzalo123/injector

最新稳定版本:1.0

Composer 安装命令:

composer require gonzalo123/injector

包简介

InjectorServiceProvider

README 文档

README

Build Status

Alternative way to define service providers in Silex

Let's say we've got this Math class

namespace Foo

class Math
{
    public function sum($i, $j)
    {
        return $i+$j;
    }
}

And we want to use it within a Silex application

include __DIR__ . "/../vendor/autoload.php";

use Silex\Application;
use Foo\Math;

$app            = new Application(['debug' => true]);

$app['math'] = function () {
    return new Math();
};

$app->get("/", function () use ($app) {
    return $app['math']->sum(1, 2);
});

$app->run();

We have one Service available in $app['math'], but, what's the type of the class? We need to inspect Math class to figure out what public functions are available. Sometimes I'm a bit lazy to do that, and because of that I've developed this small service provider to allow us to use a different approach to define our service providers.

include __DIR__ . "/../vendor/autoload.php";

use Silex\Application;
use Injector\InjectorServiceProvider;
use Foo\Math;

$app            = new Application(['debug' => true]);

$app->register(new InjectorServiceProvider([
    'Foo\Math' => 'math',
]));

$app['math'] = function () {
    return new Math();
};

$app->get("/", function (Math $math) {
    return $math->sum(1, 2);
});

$app->run();

And that's all. Our InjectorServiceProvider allows us to define the class provided by the service provider, and its Silex/Pimple key name in the Dependency Injection Container

统计信息

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

GitHub 信息

  • Stars: 8
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-09-20