2017 © Pedro Peláez
 

library sdic

Simple dependency injection container

image

theorx/sdic

Simple dependency injection container

  • Friday, December 16, 2016
  • by theorx
  • Repository
  • 1 Watchers
  • 0 Stars
  • 9 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 7 Versions
  • 13 % Grown

The README.md

Simple dependency injection container for PHP

There are many dependency injection containers out there, this one is minimalistic and straight forward. Doesn't have any third-party dependencies., (*1)

Build Status

Author

Lauri Orgla theorx@hotmail.com, (*2)

Documentation

Dependency registration usage


$container = new \Theorx\SDIC\SDIC(); //Register single dependency $container->register(DependencyClassInterface::class, function(Theorx\SDIC\SDIC $container) { return new DependencyClass($container->get(DependencyClassDependency::class)); });

Register shared dependency ( Singleton wrapper ), (*3)


$container->register(DependencyClassInterface::class, function(Theorx\SDIC\SDIC $container) { return $container->shared(DependencyClassInterface::class, function() { return new DependencyClass(); }); });

Register array of dependencies, (*4)


$container->registerArray([ DependencyAInterface::class => function(){ return new DependencyA(); }, DependencyBInterface::class => function (){ return new DependencyB(); } ]);

You can check whether container contains dependency by name using has method, (*5)


if($container->has(DependencyAInterface::class)){ //has dependency }

Getting dependencies from the container using get method, (*6)


$instance = $container->get(DependencyBInterface::class);

Container extension

Container extensions are meant to be used for adding dependencies to the container Container extensions must implement SDICExtension interface which requires you to implement single method for defining array of dependencies, (*7)

Example extension


/** * Class ExampleExtension */ class ExampleExtension implements \Theorx\SDIC\Interfaces\SDICExtension { /** * @return array */ public function registerDependencies() : array { return [ DependencyDInterface::class => function() { return new DependencyD(); }, DependencyEInterface::class => function() { return new DependencyE(); } //... ]; } }

Loading container extension / extensions

Method loadExtension loads extension to the container and registers new dependencies, (*8)


$container->loadExtension(new ExampleExtension);

For loading multiple extensions there is method loadExtensions which accepts array of objects which implement SDICExtension interface, (*9)


SDIC api overview


/** * @author Lauri Orgla <theorx@hotmail.com> * * Register name => callback, * Callback example: * function ($container) { * return $container->shared(NAME, function($container){ * return {{shared instance}}; * }); * } * * This method is chainable, register()->register()->register()-> etc.. * * @param string $name * @param callable $callback * * @return SDIC */
  • register(string $name, callable $callback) : SDIC
    /**
     * Accepts array of dependencies. Example. [name => callback]
     *
     * @author Lauri Orgla <theorx@hotmail.com>
     *
     * @param array $dependencies
     */
  • registerArray(array $dependencies)
    /**
     * Used for creating shared instances
     *
     * @author Lauri Orgla <theorx@hotmail.com>
     *
     * @param string   $name
     * @param callable $callback
     *
     * @return mixed
     */
  • shared(string $name, callable $callback)
    /**
     * Fetches dependency by name
     *
     * @author Lauri Orgla <theorx@hotmail.com>
     *
     * @param string $name
     *
     * @return mixed
     * @throws DependencyNotFoundException
     */
  • get(string $name)
    /**
     * Checks whether the container has given dependency
     *
     * @author Lauri Orgla <theorx@hotmail.com>
     *
     * @param string $name
     *
     * @return bool
     */
  • has(string $name)
    /**
     * Gets shared instances
     *
     * @author Lauri Orgla <theorx@hotmail.com>
     * @return array
     */
  • getInstances()
    /**
     * Gets current registry
     *
     * @author Lauri Orgla <theorx@hotmail.com>
     * @return array
     */
  • getRegistry()
    /**
     * Load container extension which is instance of class that implements SDICExtension
     *
     * @author Lauri Orgla <theorx@hotmail.com>
     *
     * @param SDICExtension $extension
     */
  • loadExtension(SDICExtension $extension)
    /**
     * Loads a set of extensions
     *
     * @author Lauri Orgla <theorx@hotmail.com>
     *
     * @param array $extensions
     */
  • loadExtensions(array $extensions)
    /**
     * Gets list of loaded extensions
     *
     * @author Lauri Orgla <theorx@hotmail.com>
     * @return SDICExtension[]
     */
  • getLoadedExtensions() : array

The Versions

16/12 2016

dev-master

9999999-dev

Simple dependency injection container

  Sources   Download

GPLv3

The Requires

  • php >=7

 

The Development Requires

16/12 2016

dev-develop

dev-develop

Simple dependency injection container

  Sources   Download

GPLv3

The Requires

  • php >=7

 

The Development Requires

16/12 2016

0.4.0

0.4.0.0

Simple dependency injection container

  Sources   Download

GPLv3

The Requires

  • php >=7

 

The Development Requires

15/12 2016

0.3.0

0.3.0.0

Simple dependency injection container

  Sources   Download

GPLv3

The Requires

  • php >=7

 

The Development Requires

14/12 2016

0.2.0

0.2.0.0

Simple dependency injection container

  Sources   Download

MIT

The Requires

  • php >=7

 

The Development Requires

13/12 2016

0.1.1

0.1.1.0

Simple dependency injection container

  Sources   Download

MIT

13/12 2016

0.1.0

0.1.0.0

Simple dependency injection container

  Sources   Download

MIT