Download the PHP package mnapoli/simplex without Composer
On this page you can find all versions of the php package mnapoli/simplex. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mnapoli/simplex
More information about mnapoli/simplex
Files in mnapoli/simplex
Package simplex
Short Description Pimple fork with full container-interop support
License MIT
Homepage https://github.com/mnapoli/simplex
Informations about the package simplex
Simplex
Simplex is a Pimple 3 fork with full PSR-11 compliance and cross-framework service-provider support.
Simplex is a small dependency injection container for PHP.
Differences with Pimple
Simplex is a fork of Pimple's code. The only differences are the following:
Simplex\Container
implementsContainerInterface
, which means the following methods exist:$container->get($id)
which is an alias to$container[$id]
$container->has($id)
which is an alias toisset($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.
Installation
Usage
Creating a container is a matter of creating a Container
instance:
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.
Services are defined by anonymous functions that return an instance of an object:
Notice that the anonymous function has access to the current container instance, allowing references to other services or parameters.
As objects are only created when you get them, the order of the definitions does not matter.
Using the defined services is also very easy:
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
Now, each call to $container['session']
returns a new instance of the
session.
Defining Parameters
Defining a parameter allows to ease the configuration of your container from the outside and to store global values:
If you change the session_storage
service definition like below:
You can now easily change the cookie name by overriding the
session_storage_class
parameter instead of redefining the service
definition.
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:
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:
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.
Service providers
Simplex supports registering cross-framework service providers.
To register service providers, pass an array of service providers as first constructor argument.
All versions of simplex with dependencies
psr/container Version ^1.0
container-interop/service-provider Version ~0.4.0