Giter Club home page Giter Club logo

yml2pimple's Introduction

2015-05-29 forked from gonzalo123/yml2pimple

Pimple/Container builder

SensioLabsInsight

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'];

yml2pimple's People

Contributors

td7650 avatar gonzalo123 avatar cordoval avatar

Stargazers

Jesus Franco avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.