Giter VIP home page Giter VIP logo

embryo-container's Introduction

Embryo Container

Embryo Container is a small and PSR-11 compatible Dependency Injection Container for PHP.

Requirements

  • PHP >= 7.1

Installation

Using Composer:

$ composer require davidecesarano/embryo-container

Usage

Create Service

Services are defined by anonymous functions that return an instance of an object:

$container = new \Embryo\Container\ContainerBuilder;
$container->set('connection', function(){
    return [
        'host' => 'localhost',
        'db'   => 'database',
        'user' => 'user',
        'pass' => 'pass'
    ];
});

$container->set('pdo', function($container){
    $connection = $container->get('connection');
    $mysql = 'mysql:host='.$connection['host'].';dbname='.$connection['db'], $connection['user'], $connection['pass']);
    return new \PDO($mysql);
});

Note that the anonymous function has access to the current container instance.

Using Service

Using the defined services:

$pdo = $container->get('pdo');

Automatic Injection

Container can automatically create and inject dependencies with Autowiring. Autowiring is enabled by default. You can disable it using the container builder:

$container->useAutowiring(false);

Autowiring is the ability of the container to automatically create and inject dependencies. Embryo Container uses PHP's reflection to detect what parameters a constructor needs.

class Person
{
    public function getName()
    {
        return 'David';
    }
}

class Hello
{
    public $name;
    
    public function __construct(Person $person)
    {
        $this->name = $person->getName();
    }
    
    public function getHello()
    {
        return 'Hello '.$this->name;
    }
}

$container = new \Embryo\Container\ContainerBuilder;
$hello = $container->get('Hello');
echo $hello->getHello(); // Hello David

In this example, Embryo Container creates a Person instance (if it wasn't already created) and pass it as a constructor parameter.

Service Provider

Embryo Container can defines a service into a provider extending ServiceProvider instance:

use Embryo\Container\ContainerBuilder;
use Embryo\Container\ServiceProvider;

class TestServiceProvider extends ServiceProvider 
{
    public function register()
    {
        $this->container->set('testService', function($container){
            $other = $container->get('otherService');
            return 'This is a Test Service! '.$other;
        });
    }
}

$container = new ContainerBuilder;
$test_service_provider = new TestServiceProvider($container);
$test_service_provider->register();
echo $container['testService']; // This is a Test Service!

Example

You may quickly test this using the built-in PHP server:

$ cd example
$ php -S localhost:8000

Going to http://localhost:8000.

embryo-container's People

Stargazers

 avatar

Watchers

 avatar  avatar  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.