gonzalo123 / yaml2pimple
从配置文件构建Pimple/Container
dev-master / 1.0.x-dev
2017-04-23 13:41 UTC
Requires
- pimple/pimple: ^3.0
- symfony/config: ^3.1
- symfony/yaml: ^3.1
This package is not auto-updated.
Last update: 2024-09-14 15:06:03 UTC
README
简单工具,从配置文件构建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 G\Yaml2Pimple\ContainerBuilder; use G\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();