2017 © Pedro Peláez
 

library php-servaxle

Servaxle is a fast Dependency Injection Container for PHP

image

lingualeo/php-servaxle

Servaxle is a fast Dependency Injection Container for PHP

  • Monday, February 22, 2016
  • by LinguaLeo
  • Repository
  • 19 Watchers
  • 1 Stars
  • 10,366 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 13 Versions
  • 2 % Grown

The README.md

Servaxle

Servaxle is a fast Dependency Injection Container for PHP., (*1)

How it use?

Dependency Injection Container is presented as a scope. The scope discovers dependencies by reflection. Classes in namespaces will be resolved only., (*2)

namespace Tutorial;

class Foo
{
}

class Bar
{
    public function __construct(Foo $foo)
    {
    }
}

$scope = new \LinguaLeo\DI\Scope([
    'bar' => Bar::class
]);

$scope->bar; // instance of Tutorial\Bar class

You can define common interface implementation., (*3)

namespace Tutorial;

interface AnimalInterface {}

class Dog implements AnimalInterface {}

class Cat implements AnimalInterface {}

class Home
{
    public function __construct(AnimalInterface $animal)
    {
    }
}

$scope = new \LinguaLeo\DI\Scope([
    'home' => Home::class,
    AnimalInterface::class => Dog::class
]);

$scope->home; // instanse of Tutorial\Home class with Tutorial\Dog object injection

But you can put simple data types into the scope too., (*4)

$scope = new LinguaLeo\DI\Scope([
    'user' => 'root'
]);

$scope->getValue('user'); // "root"

You can define a constant also. Constants in a class namespace will be processed too., (*5)

$scope = new LinguaLeo\DI\Scope([
    'max' => 'PHP_INT_MAX'
]);

$scope->max; // 9223372036854775807 (in 64 bit OS)

You can define a symlink to another variable., (*6)

$scope = new LinguaLeo\DI\Scope([
    'super' => 'admin',
    '@user' => 'super',
    'member' => '@user'
]);

$scope->getValue('member'); // "admin" as @user -> super -> admin

Variables

You can define a target variable without symlinks lookup., (*7)

$scope = new LinguaLeo\DI\Scope([
    'super' => 'admin',
    'member' => '$super'
]);

$scope->getValue('member'); // "admin"

Cache

The scope parses dependencies every time when you call getValue method. But if you access to a value as a property the scope will cache it., (*8)

$scope = new LinguaLeo\DI\Scope([
    'now' => function () {
        return uniqid();
    }
]);

$scope->getValue('now') === $scope->getValue('now'); // false
$scope->now === $scope->now; // true

Factory Design Pattern

You can create own factories for custom init. No special interfaces or classes are required. Just use a magic!, (*9)

namespace Tutorial;

class RedisFactory
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function __invoke()
    {
        $redis = new \Redis();
        $redis->connect($this->host);
        return $redis;
    }
}

$scope = new \LinguaLeo\DI\Scope([
    'redis' => RedisFactory::class,
    'redis.host' => '192.168.1.1'
]);

$scope->redis; // instance of \Redis class

Compilation

For complex tree of dependencies we created the compilation into PHP script., (*10)

namespace Tutorial;

// define classes
class Foo {}

class Bar
{
    public function __construct(Foo $foo) {}
}

class Baz
{
    public function __construct(Bar $bar) {}
}

// compiles a tree
$script = \LinguaLeo\DI\Scope::compile([
    'baz' => Baz::class
]);

// put into PHP file
file_put_contents('cache.php', '<?php return '.$script.';');

// init a scope from file
$scope = new \LinguaLeo\DI\Scope(include 'cache.php');

$scope->baz; // instance of Tutorial\Baz class

Immutable scope

You should instantiate ImmutableScope class if you don't require auto reflection. It's useful for early compiled dependencies., (*11)

$scope = new LinguaLeo\DI\ImmutableScope(include 'cache.php');

Source: https://github.com/LinguaLeo/php-servaxle, (*12)

The Versions

22/02 2016

dev-master

9999999-dev

Servaxle is a fast Dependency Injection Container for PHP

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

by Avatar LinguaLeo

dependency injection container

22/02 2016

2.2.0

2.2.0.0

Servaxle is a fast Dependency Injection Container for PHP

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

by Avatar LinguaLeo

dependency injection container

15/12 2015

dev-php7-support

dev-php7-support

Servaxle is a fast Dependency Injection Container for PHP

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

by Avatar LinguaLeo

dependency injection container

08/05 2015

2.1.5

2.1.5.0

Servaxle is a fast Dependency Injection Container for PHP

  Sources   Download

MIT

The Requires

  • php ~5.5

 

The Development Requires

by Avatar LinguaLeo

dependency injection container

22/08 2014

2.1.4

2.1.4.0

Servaxle is a fast Dependency Injection Container for PHP

  Sources   Download

MIT

The Requires

  • php 5.5.*

 

The Development Requires

by Avatar LinguaLeo

dependency injection container

22/08 2014

dev-empty-string-token

dev-empty-string-token

Servaxle is a fast Dependency Injection Container for PHP

  Sources   Download

MIT

The Requires

  • php 5.5.*

 

The Development Requires

by Avatar LinguaLeo

dependency injection container

22/08 2014

2.1.3

2.1.3.0

Servaxle is a fast Dependency Injection Container for PHP

  Sources   Download

MIT

The Requires

  • php 5.5.*

 

The Development Requires

by Avatar LinguaLeo

dependency injection container

08/05 2014

2.1.2

2.1.2.0

Servaxle is a fast Dependency Injection Container for PHP

  Sources   Download

MIT

The Requires

  • php 5.5.*

 

by Avatar LinguaLeo

dependency injection container

29/04 2014

2.1.1

2.1.1.0

Servaxle is a fast Dependency Injection Container for PHP

  Sources   Download

MIT

The Requires

  • php 5.5.*

 

by Avatar LinguaLeo

dependency injection container

17/04 2014

2.1.0

2.1.0.0

Servaxle is a fast Dependency Injection Container for PHP

  Sources   Download

MIT

The Requires

  • php 5.5.*

 

by Avatar LinguaLeo

dependency injection container

10/04 2014

2.0.1

2.0.1.0

  Sources   Download

MIT

The Requires

  • php 5.5.*

 

by Avatar LinguaLeo

04/04 2014

2.0.0

2.0.0.0

  Sources   Download

MIT

The Requires

  • php 5.5.*

 

by Avatar LinguaLeo

31/03 2014

1.0.0

1.0.0.0

  Sources   Download

MIT

The Requires

  • php 5.5.*

 

by Avatar LinguaLeo