Download the PHP package neunerlei/container-delegate without Composer

On this page you can find all versions of the php package neunerlei/container-delegate. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package container-delegate

Container Delegate

This package contains a PSR-11 compatible container implementation but with a twist. This container is designed as a "delegate" container, meaning you should use it as a fallback for your services you are not configuring manually. You can learn more about container delegation here.

The container has zero configuration options and relies on interface based auto-wiring conventions. If your service needs parameters or hard wired instances use a compound container, this is not what you want from this implementation.

In addition to the "zero-configuration" approach the implementation features compiled factories, auto-wiring (duh) and lazy service injection using lazy proxies.

For my projects I normally use the league container which has a really nice configuration interface and a ton of options to work with. Of course, it supports delegate containers.

Installation

Install this package using composer:

Creating the container

Simply create a new container instance and you are good to go:

Getting an instance

The container uses PHPs reflection capabilities to automatically wire dependencies for the arguments. In this example class B requires class A, which will be automatically injected when the instance of B is created.

Getting an instance by interface

A lot of container implementations rely on the developer to map an interface with an implementation. This container implementation will take an educated guess on what class it should look up. If your interface name ends with "Interface", the container will automatically try to instantiate a class with the same name but without the "Interface" part.

Handling default parameters

If your implementation allows for additional parameters that are not directly injectable, the container will use the default value if it is present.

Always return the same instance (Singleton)

Some services should only be instantiated once but distributed for many services (e.g. a database connection). While the league container supports this feature out of the box, other implementations do not. For that reason the container implements the feature itself, but tries to use the compound container if it provides the singleton feature.

To define a singleton service instance in your application you can add the SingletonInterface to it. After that the service will only be instanced once.

Using injection methods

If you want to inject additional services after the __construct() method was executed you would normally have to wire the "set" methods using your container configuration. By using the InjectableInterface the container will automatically scan all public, non static methods that begin with "inject" and provide the arguments for them. You can inject multiple instances with a single inject method.

Lazy loading

Sometimes you want services in your objects that might be required only in certain circumstances. If you have a tiny service that does not cause overhead that is fine in general. If you on the other hand have a bulky service that might even take some time setting up (a db connection for example), you should start thinking on a lazy loading proxy.

This feature injects a tiny wrapper instead of the real implementation into your requesting instance. Only if one of the proxy features is requested, the real instance will be created, meaning you can save a lot of processing time.

The container implementation has a build in support for lazy parameter injection. Every property that has an interface as type and a name that starts with "lazy" will be injected as lazy loading object.

Compiling the factories

To avoid the overhead of reflecting every object, every time it is required the implementation has the option to compile the factories into php code.

The compilation is done on the fly and only for objects that were required at least once, meaning your container does not know every class or interface it should be able to instantiate in the future. Meaning: Good-Bye "ServiceNotFoundException" every time you forgot to add a Service class to your configuration.yml.

To provide this feature only the factories are compiled as php code and stored in a file that will be expanded every time the container "learns" a new class it has to instantiate. Giving you both speed and dynamic lookups without the configuration hassle.

If you want to use the compiling feature (it's disabled by default) you have to provide an absolute path to a writable directory on your file system, after that the container will handle the rest. To reset the cached/compiled factories just remove the files in the storage directory.

Usage as delegate (league container)

All the examples above use the implementation stand alone, but that is, as stated above NOT THE INTENDED usecase. It is designed to work as a delegate with another container, that supports manual interface/class mappings, parameters or manual factories.

To use the container I added a simple wrapper that delegates the recursive object resolution and singleton object lookup to the league container.

Postcardware

You're free to use this package, but if it makes it to your production environment I highly appreciate you sending me a postcard from your hometown, mentioning which of our package(s) you are using.

You can find my address here.

Thank you :D


All versions of container-delegate with dependencies

PHP Build Version
Package Version
Requires psr/container Version ^1.0
php Version ^7.3
neunerlei/container-autowiring-declaration Version ^1.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package neunerlei/container-delegate contains the following files

Loading the files please wait ....