开源精炼器/yaml2pimple

从配置文件构建Pimple/Container

2.0.1 2019-09-11 20:06 UTC

This package is auto-updated.

Last update: 2024-08-29 04:37:05 UTC


README

Build Status

一个简单的工具,可以从配置文件构建Pimple容器

想象这个简单的应用程序

use Pimple\Container;

$container         = new Container();
$container['name'] = 'Gonzalo';

$container['Curl']  = function () {
    return new Curl();
};
$container['Proxy'] = function ($c) {
    return new Proxy($c['Curl']);
};

$container['App'] = function ($c) {
    return new App($c['Proxy'], $c['name']);
};

$app = $container['App'];
echo $app->hello();

我们用代码定义依赖项。但我们想用yml文件定义依赖项,例如

parameters:
  name: Gonzalo

services:
  App:
    class:     App
    arguments: [@Proxy, %name%]
  Proxy:
    class:     Proxy
    arguments: [@Curl]
  Curl:
    class:     Curl

使用这个库,我们可以从这个yml文件创建一个Pimple容器(类似于Symfony的依赖注入容器语法)

use Pimple\Container;
use OpenSourceRefinery\Yaml2Pimple\ContainerBuilder;
use OpenSourceRefinery\Yaml2Pimple\YamlFileLoader;
use Symfony\Component\Config\FileLocator;

$container = new Container();

$builder = new ContainerBuilder($container);
$locator = new FileLocator(__DIR__);
$loader = new YamlFileLoader($builder, $locator);
$loader->load('services.yml');

$app = $container['App'];
echo $app->hello();

许可证

Yaml2Pimple采用MIT许可证。

变更日志

1.1.0

1.0.0

  • 仅因为需要

0.1.1 从 opensourcerefinery/yml2pimple 分支

将pimple容器作为最后一个参数添加

0.1.0 从 opensourcerefinery/yml2pimple 分支

创建一个发布版本以获得稳定版本。