PHP Classes
elePHPant
Icontem

Simple PHP Dependency Injection Container Class: Container to register and call service providers

Recommend this page to a friend!
  Info   View files Documentation   View files View files (9)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2018-09-02 (7 months ago) RSS 2.0 feedNot yet rated by the usersTotal: 66 All time: 9,387 This week: 383Up
Version License PHP version Categories
simple-ph-dic 1.0.2GNU General Publi...7.1Language, Design Patterns, PHP 7, PSR
Description Author

This package is a container to register and call service providers.

It can register a service provider taking as parameter the name of the service and a class that implements the service that is being provided.

The container that also return the object of a class that was registered as service provider, so applications can call its functions that implement the services that the class provides.

Name: Pierre-Henry Soria <contact>
Classes: 14 packages by
Country: United Kingdom United Kingdom
Age: 28
All time rank: 59531 in United Kingdom United Kingdom
Week rank: 61 Up3 in United Kingdom United Kingdom Up

Details

P.H.'s Dependency Injection Container Library

The "P.H.'s DIC" is a simple and lightweight PHP 7.1+ Dependency Injection Container's library which lets you manage your dependencies easily for your next great project ?

Usage

Register your new DIC as below (FYI, for this example I use the Symfony's HttpFoundation Request).

For the first example, let's create your provider class with an anonymous class that implements the \PierreHenry\Container\Providable interface.

use PierreHenry\Container\Container;
use PierreHenry\Container\Providable;
use Symfony\Component\HttpFoundation\Request;

$container = new Container();

// Register your container
$container->register(
    'example.symfony.httprequest',
    new class implements Providable
    {
        public function getService(): Request
        {
            return Request::createFromGlobals();
        }
    }
);

// Retrieve the container
$httpRequest = $container->get('example.symfony.httprequest');

// Use it
$request = $httpRequest->request; // $_POST body params
if ($request->get('get_var')) {
    echo '$_POST["get var"] exists';
} else {
    echo '"get_var" has not been requested';
}

Another Example...

use DateTime;
use DateTimeZone;
use PierreHenry\Container\Container;
use PierreHenry\Container\Providable;

$container = new Container();
$container->register(
    'stubs.date.datetime',
    new class implements Providable
    {
        public function getService(): DateTime
        {
            return new DateTime('now', new DateTimeZone('America/Chicago'));
        }
    }
);

// Retrieve the container
$date = $container->get('stubs.date.datetime');

// Use it
echo $date->format('m-d-Y H:i:s');

The Requirements

Inspired By...

This project is highly inspired by my DIC I built for another side-project.

...Who Am I...? ?

Hi ? I am Pierre-Henry Soria, "PierreHenry.be": a cool passionate Belgian software engineer :belgium: :smiley:

You can keep in touch at hi {{AT}} ph7 [[D0T]] me!

License

Under GNU GPL v3 or later.

  Files folder image Files  
File Role Description
Files folder imagesrc (2 files, 1 directory)
Files folder imagetests (1 file)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file phpunit.xml.dist Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  src  
File Role Description
Files folder imageException (3 files)
  Plain text file Container.php Class Class source
  Plain text file Providable.php Class Class source

  Files folder image Files  /  src  /  Exception  
File Role Description
  Plain text file Container.php Class Class source
  Plain text file ContainerNotFound.php Class Class source
  Plain text file Provider.php Class Class source

  Files folder image Files  /  tests  
File Role Description
  Plain text file ContainerTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:66
This week:0
All time:9,387
This week:383Up