2017 © Pedro Peláez
 

library yaco

YACO (Yet Another COmpiler) is a PHP tool that generates a PHP container based on entry definitions.

image

thecodingmachine/yaco

YACO (Yet Another COmpiler) is a PHP tool that generates a PHP container based on entry definitions.

  • Wednesday, September 20, 2017
  • by thecodingmachine
  • Repository
  • 5 Watchers
  • 11 Stars
  • 133 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 11 Versions
  • 3 % Grown

The README.md

Scrutinizer Code Quality Build Status Coverage Status, (*1)

YACO - Yet another compiler

YACO (Yet Another COmpiler) is a PHP tool that generates a PHP container based on entry definitions. It is fully compatible with entry definitions from definition-interop., (*2)

Installation

You can install this package through Composer:, (*3)

{
    "require": {
        "thecodingmachine/yaco": "^1.2"
    }
}

The packages adheres to the SemVer specification, and there will be full backward compatibility between minor versions., (*4)

Usage

This package contains a Compiler class. The goal of this class is to take a number of "entry definitions" (as defined in definition-interop) and to transform those into a PHP class that implements the ContainerInterface, (*5)

use TheCodingMachine\Yaco\Compiler;

$compiler = new Compiler();

// ...

foreach ($definitions as $identifier => $definition) {
    /* @var $definition Interop\Container\Definition\DefinitionInterface */
    $compiler->addDefinition($identifier, $definition);
}

// Let's dump the code of the My\Container class.
file_put_contents("Container.php", $compiler->compile("My\\Container"));

Now, you can instantiate your container using this code:, (*6)

$container = new My\Container();

$service = $container->get('a_service');

Note: Yaco is consuming container definitions (implementing Interop\Container\Definition\DefinitionInterface). Out of the box, Yaco does not provide any classes implementing this interface. However, you can find such classes in a package like mnapoli/assembly., (*7)

Here is a complete sample using Yaco and Assembly:, (*8)

Compile phase:, (*9)

use TheCodingMachine\Yaco\Compiler;
use function \Assembly\object;
use function \Assembly\alias;

$compiler = new Compiler();

$loggerDefinition = object('MyLogger')
    ->setConstructorArguments('warning')
    ->addMethodCall('setDebug', true);

$compiler->addDefinition('logger', $loggerDefinition);

// Let's dump the code of the My\Container class.
file_put_contents("Container.php", $compiler->compile("My\\Container"));

Usage:, (*10)

require_once('Container.php');

$container = new My\Container();
$logger = $container->get('logger');

Note: the My\Container class implements the Interop\Container\ContainerInterface. Therefore, it can be used with any framework compatible with container-interop., (*11)

Service provider support

This package supports container agnostic service providers as defined in container-interop/service-provider., (*12)

To register service providers, you must pass to the compiler a TheCodingMachine\ServiceProvider\Registry object., (*13)

Here is a sample:, (*14)

use TheCodingMachine\Yaco\Compiler;
use TheCodingMachine\ServiceProvider\Registry;

$registry = new Registry([
    MyServiceProvider::class
]);

// The registry is passed as first argument to the compiler. 
$compiler = new Compiler($registry);

// Let's dump the code of the My\Container class.
file_put_contents("MyContainer.php", $compiler->compile("MyContainer"));

Important! When you want to use the compiled container, you have to pass a registry containing the same service providers, in the same order., (*15)

Below is an instantiation of the container generated by the code above:, (*16)

use TheCodingMachine\ServiceProvider\Registry;

$registry = new Registry([
    MyServiceProvider::class
]);

// The registry is passed as first argument to the compiler. 
$container = new MyContainer($registry);

$service = $container->get('a_service');

Miscellaneous

Delegate lookup support

Containers generated by Yaco support the "delegate lookup" feature of container-interop., (*17)

If you pass a container as the second argument to the generated container, all dependency lookups will be done on the passed container rather than on the generated container., (*18)

Here is a sample:, (*19)

// $rootContainer is a composite container from the acclimate library
$rootContainer = new Acclimate\CompositeContainer();

$myContainer = new MyContainer(null, $rootContainer);

$rootContainer->addContainer($myContainer);

Definition providers

You can directly register a definition provider using the register method:, (*20)

use TheCodingMachine\Yaco\Compiler;

$compiler = new Compiler();

// ...

$compiler->register($definitionProvider);

// Let's dump the code of the My\Container class.
file_put_contents("Container.php", $compiler->compile("My\\Container"));

Definition providers are classes implementing the Interop\Container\Definition\DefinitionProviderInterface. They provide a list of container definitions to be compiled by the compiler., (*21)

The Versions

01/03 2016

1.0.x-dev

1.0.9999999.9999999-dev

YACO (Yet Another COmpiler) is a PHP tool that generates a PHP container based on entry definitions.

  Sources   Download

MIT

The Requires

 

The Development Requires

01/03 2016

v0.4.1

0.4.1.0

YACO (Yet Another COmpiler) is a PHP tool that generates a PHP container based on entry definitions.

  Sources   Download

MIT

The Requires

 

The Development Requires

01/03 2016

v1.0.0

1.0.0.0

YACO (Yet Another COmpiler) is a PHP tool that generates a PHP container based on entry definitions.

  Sources   Download

MIT

The Requires

 

The Development Requires

29/02 2016

v0.4.0

0.4.0.0

YACO (Yet Another COmpiler) is a PHP tool that generates a PHP container based on entry definitions.

  Sources   Download

MIT

The Requires

 

The Development Requires

30/11 2015

0.3.x-dev

0.3.9999999.9999999-dev

YACO (Yet Another COmpiler) is a PHP tool that generates a PHP container based on entry definitions.

  Sources   Download

MIT

The Requires

 

The Development Requires

30/11 2015

v0.3.0

0.3.0.0

YACO (Yet Another COmpiler) is a PHP tool that generates a PHP container based on entry definitions.

  Sources   Download

MIT

The Requires

 

The Development Requires

04/09 2015

v0.2.0

0.2.0.0

YACO (Yet Another COmpiler) is a PHP tool that generates a PHP container based on entry definitions.

  Sources   Download

MIT

The Requires

 

The Development Requires

04/09 2015

0.2.x-dev

0.2.9999999.9999999-dev

YACO (Yet Another COmpiler) is a PHP tool that generates a PHP container based on entry definitions.

  Sources   Download

MIT

The Requires

 

The Development Requires

17/08 2015

v0.1.0

0.1.0.0

YACO (Yet Another COmpiler) is a PHP tool that generates a PHP container based on entry definitions.

  Sources   Download

MIT

The Requires

 

The Development Requires