td7650/yml2pimple
最新稳定版本:v1.1-beta
Composer 安装命令:
composer require td7650/yml2pimple
包简介
Build a Pimple/Container from a config file
README 文档
README
2015-05-29 forked from gonzalo123/yml2pimple
Pimple/Container builder
With this library we can create a pimple /silex container from this yaml file (mostly similar syntax than Symfony's Dependency Injection Container)
# test import
imports:
- { resource: test2.yml}
parameters:
app_class: App
name: Gonzalo
deep:
# parameters can contain other parameters
first: 'From the deep (%app_class%)'
second: [1,2,3]
third: [a,b,c]
desc1: |
<br><strong>this is a example for a lazy constructed parameter combined from
fragments defined later, its dynamic and is evaluated every time
its accessed</strong>
combined: '<p>Lazy Parameter example: %fragment1% %fragment2%</p>'
desc2: |
<br><strong>this is a example for a lazy constructed parameter combined from
fragments defined later, its like a singleton (the paramater name starts with an $)
and is frozen after its first accessed</strong>
$combined2: '<p>Lazy Parameter example2: %fragment1% %fragment2%</p>'
# used by ExpressionNormalizer to evaluate expression vars
$_normalize:
test: @service_container
services:
App:
# class names can reference parameters
class: %app_class%
# prototype returns a new instance each time
scope: prototype
# the instance is constructed lazy with a proxy factory
lazy: true
arguments: [@Proxy, %name%]
calls:
- [setName, ['Test']]
# this is a optional parameter
- [setDummy, ['@?Dummy']]
# a configurator can modify the instance
configurator: ['@Configurator', configure]
# add a aspect to method hello()
aspects:
- {pointcut: 'hello', advice:'Configurator:beforeHello'}
Proxy:
class: Proxy
lazy: true
# the instance is created by the factory class
factory: ['Factory', 'create']
arguments: [@service_container]
#arguments: [@Curl]
Curl:
class: Curl
lazy: true
Configurator:
class: Test
# we can access elements of arrays with the symfony property access style (via normalizer)
arguments: ['%deep..first%', '?"hallo"~" Welt "~test["name"]']
Factory:
class: Factory
use G\Yaml2Pimple\ContainerBuilder; use G\Yaml2Pimple\Loader\YamlFileLoader; use G\Yaml2Pimple\Loader\CacheLoader; use G\Yaml2Pimple\Normalizer\ChainNormalizer; use G\Yaml2Pimple\Normalizer\PimpleNormalizer; use G\Yaml2Pimple\Normalizer\ExpressionNormalizer; use Symfony\Component\Config\FileLocator; use G\Yaml2Pimple\Proxy\ServiceProxyAdapter; use G\Yaml2Pimple\Proxy\AspectProxyAdapter; use G\Yaml2Pimple\Factory\ServiceFactory; use G\Yaml2Pimple\Factory\ParameterFactory; use G\Yaml2Pimple\Factory\ProxyParameterFactory; $container = new \Pimple(); $builder = new ContainerBuilder($container); $ymlLoader = new YamlFileLoader( new FileLocator(__DIR__) ); $cacheLoader = new CacheLoader($ymlLoader, __DIR__ . '/cache/'); // set the normalizers $builder->setNormalizer( new ChainNormalizer( array( new PimpleNormalizer(), new ExpressionNormalizer() ) ) ); $parameterFactory = new ProxyParameterFactory(); $serviceFactory = new ServiceFactory( new ServiceProxyAdapter(__DIR__ . '/cache/') ); $serviceFactory->setAspectFactory( new AspectProxyAdapter( __DIR__ . '/cache/') ); // set our loader helper $builder->setLoader($cacheLoader); // lazy service proxy factory $builder->setServiceFactory($serviceFactory); // lazy parameter proxy factory $builder->setParameterFactory($parameterFactory); $builder->load('test.yml'); $app = $container['App']; echo $app->hello(); echo $container['desc1']; echo $container['combined']; $container['fragment2'] = 'Test'; echo $container['combined']; echo $container['desc2']; $container['fragment2'] = 'world'; echo $container['combined2']; $container['fragment2'] = 'Test'; echo $container['combined2'];
统计信息
- 总下载量: 1.32k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-09-14