Code Monkey home page Code Monkey logo

infuse's Introduction

Infuse

CI Packagist PHP License

Infuse 🍃

A minimal PSR-11 implementation.

Features

  • ✔️ Autowiring (powered by Reflection)
  • ✔️ Simple API (only 3 methods)
  • ✔️ Container can be built from definitions
  • ✔️ Singleton pattern
  • ✔️ Detects circular dependencies
  • ⏳ Compilable for production
  • ⏳ PHPStan generics support for container bindings

Documentation

Infuse has a very minimal API with only has three methods:

/**
 * Finds an entry of the container by its identifier and returns it.
 *
 * @param string $id identifier of the entry to look for
 *
 * @return mixed entry
 *
 * @throws NotFoundExceptionInterface  no entry was found for the provided identifier
 * @throws ContainerExceptionInterface error while retrieving the entry
 */
public function get(string $id): mixed;

/**
 * Returns true if the container can return an entry for the given identifier.
 * Returns false otherwise.
 *
 * `has($id)` returning true does not mean that `get($id)` will not throw an exception.
 * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`.
 *
 * @param string $id identifier of the entry to look for
 */
public function has(string $id): bool;

/**
 * @param \Closure(Container): mixed $definition
 *
 * @throws ContainerExceptionInterface if provided id is not unique
 */
public function bind(string $id, \Closure $definition): void;

Example:

use Infuse\Container;

$container = new Container();

// Container::bind tells the Container how to build a Bar object
$container->bind(Bar::class, function () {
    return new Bar();
});

// Container::has checks if there's a binding with the provided id
$container->has(Bar::class); // true

// the callable parameter receives the Container itself as argument
$container->bind(Foo::class, function (Container $c) {
    $bar = $c->get(Bar::class);
    return new Foo($bar):
});

// Container::get retrieves an instance from the Container, the bound callable will be called at this moment
$foo = $container->get(Foo::class);
$isFoo = $foo instanceof Foo; // true

// Every instance is a singleton, you'll always receive the same reference
$sameFoo = $container->get(Foo::class);
$sameInstance = $foo === $sameFoo; // true

// This will throw a ContainerException, ids must be unique
$container->bind(Bar::class, function (Container $c) {
    return new Bar();
});

// This will throw a NotFoundException, this id has not been bound
$container->get(Zee::class);

// You can bind basically anything
$container->bind('some_array', fn () => ['hello' => 'world']);
$container->bind('some_scalar', fn () => 42);

You can also create a ready to use Container from a definitions array:

// definitions.php
<?php

use Infuse\Container;

// should be shaped as array<string, callable>

return [
    GeoLocationService::class => function (Container $c) {
        $config = $c->get('config');
        return new GeoLocationService($config['GEOLOCATION_API_KEY']);
    },
    'config' => function () {
        return [
            'GEOLOCATION_API_KEY' => $_ENV['GEOLOCATION_API_KEY'],
        ];
    },
];
// something.php
use Infuse\ContainerFactory;

$definitions = require __DIR__ . '/definitions.php';
$container = ContainerFactory::FromDefinitions($definitions);
$container->has('config'); // true
$container->has(GeoLocationService::class); // true

Installing

The recommended way to install Infuse is through Composer.

composer require infuse-di/infuse

Tests

To execute the test suite, you'll need to install all development dependencies.

git clone https://github.com/davisenra/infuse
composer install
composer test

License

This project is licensed under the MIT license. See License File for more information.

infuse's People

Contributors

davisenra avatar

Stargazers

Eyüp Fidan avatar

Watchers

 avatar

infuse's Issues

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.