2017 © Pedro Peláez
 

library simplex

Pimple fork with full container-interop support

image

mnapoli/simplex

Pimple fork with full container-interop support

  • Sunday, March 4, 2018
  • by mnapoli
  • Repository
  • 3 Watchers
  • 12 Stars
  • 23,699 Installations
  • PHP
  • 14 Dependents
  • 0 Suggesters
  • 6 Forks
  • 0 Open issues
  • 8 Versions
  • 33 % Grown

The README.md

Build Status, (*1)

Simplex

Simplex is a Pimple 3 fork with full PSR-11 compliance and cross-framework service-provider support., (*2)

Simplex is a small dependency injection container for PHP., (*3)

Differences with Pimple

Simplex is a fork of Pimple's code. The only differences are the following:, (*4)

  • Simplex\Container implements ContainerInterface, which means the following methods exist:
    • $container->get($id) which is an alias to $container[$id]
    • $container->has($id) which is an alias to isset($container[$id])
  • for symmetry reasons, Simplex\Container also provides an additional method:
    • $container->set($id, $value) which is an alias to $container[$id] = ...
  • the constructor takes an optional ContainerInterface $rootContainer = null argument to support the delegate lookup feature: if provided, this container will be injected in factories instead
  • service providers have been completely replaced by container-interop's service providers: that allows to load cross-framework modules in this container
  • it is possible to extend a scalar value with $container->extend() (for compatibility reasons with cross-framework service providers)

Below is the documentation of Pimple/Simplex., (*5)

Installation

composer require mnapoli/simplex

Usage

Creating a container is a matter of creating a Container instance:, (*6)

$container = new \Simplex\Container();

Defining Services

A service is an object that does something as part of a larger system. Examples of services: a database connection, a templating engine, or a mailer. Almost any global object can be a service., (*7)

Services are defined by anonymous functions that return an instance of an object:, (*8)

// define some services
$container['session_storage'] = function ($c) {
    return new SessionStorage('SESSION_ID');
};

$container['session'] = function ($c) {
    return new Session($c['session_storage']);
};

Notice that the anonymous function has access to the current container instance, allowing references to other services or parameters., (*9)

As objects are only created when you get them, the order of the definitions does not matter., (*10)

Using the defined services is also very easy:, (*11)

// get the session object
$session = $container['session'];

// the above call is roughly equivalent to the following code:
// $storage = new SessionStorage('SESSION_ID');
// $session = new Session($storage);

Defining Factory Services

By default, each time you get a service, Pimple returns the same instance of it. If you want a different instance to be returned for all calls, wrap your anonymous function with the factory() method, (*12)

$container['session'] = $container->factory(function ($c) {
    return new Session($c['session_storage']);
});

Now, each call to $container['session'] returns a new instance of the session., (*13)

Defining Parameters

Defining a parameter allows to ease the configuration of your container from the outside and to store global values:, (*14)

// define some parameters
$container['cookie_name'] = 'SESSION_ID';
$container['session_storage_class'] = 'SessionStorage';

If you change the session_storage service definition like below:, (*15)

$container['session_storage'] = function ($c) {
    return new $c['session_storage_class']($c['cookie_name']);
};

You can now easily change the cookie name by overriding the session_storage_class parameter instead of redefining the service definition., (*16)

Protecting Parameters

Because Pimple sees anonymous functions as service definitions, you need to wrap anonymous functions with the protect() method to store them as parameters:, (*17)

$container['random_func'] = $container->protect(function () {
    return rand();
});

Modifying Services after Definition

In some cases you may want to modify a service definition after it has been defined. You can use the extend() method to define additional code to be run on your service just after it is created:, (*18)

$container['session_storage'] = function ($c) {
    return new $c['session_storage_class']($c['cookie_name']);
};

$container->extend('session_storage', function ($storage, $c) {
    $storage->...();

    return $storage;
});

The first argument is the name of the service to extend, the second a function that gets access to the object instance and the container., (*19)

Service providers

Simplex supports registering cross-framework service providers., (*20)

To register service providers, pass an array of service providers as first constructor argument., (*21)

$container = new \Simplex\Container([new MyServiceProvider()]);

The Versions

04/03 2018

dev-master

9999999-dev https://github.com/mnapoli/simplex

Pimple fork with full container-interop support

  Sources   Download

MIT

The Requires

 

The Development Requires

by Matthieu Napoli

dependency injection container

13/02 2018

0.5.0

0.5.0.0 https://github.com/mnapoli/simplex

Pimple fork with full container-interop support

  Sources   Download

MIT

The Requires

 

The Development Requires

by Matthieu Napoli

dependency injection container

28/11 2017

0.4.1

0.4.1.0 https://github.com/mnapoli/simplex

Pimple fork with full container-interop support

  Sources   Download

MIT

The Requires

 

The Development Requires

by Matthieu Napoli

dependency injection container

14/11 2017

0.4.0

0.4.0.0 https://github.com/mnapoli/simplex

Pimple fork with full container-interop support

  Sources   Download

MIT

The Requires

 

The Development Requires

by Matthieu Napoli

dependency injection container

21/09 2017

0.3.0

0.3.0.0 https://github.com/mnapoli/simplex

Pimple fork with full container-interop support

  Sources   Download

MIT

The Requires

 

The Development Requires

by Matthieu Napoli

dependency injection container

01/06 2016

0.2.1

0.2.1.0 https://github.com/mnapoli/simplex

Pimple fork with full container-interop support

  Sources   Download

MIT

The Requires

 

The Development Requires

by Matthieu Napoli

dependency injection container

16/05 2016

0.2.0

0.2.0.0 https://github.com/mnapoli/simplex

Pimple fork with full container-interop support

  Sources   Download

MIT

The Requires

 

The Development Requires

by Matthieu Napoli

dependency injection container

25/02 2016

0.1.0

0.1.0.0 https://github.com/mnapoli/simplex

Pimple fork with full container-interop support

  Sources   Download

MIT

The Requires

 

The Development Requires

by Matthieu Napoli

dependency injection container