Download the PHP package david2m/syringe without Composer

On this page you can find all versions of the php package david2m/syringe. 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 syringe

PHP Dependency Injector

A dependency injection container capable of:

  1. Auto-wiring.
  2. Delegating object creation to factories.
  3. Dealing with multiple instances of the same class.
  4. Calling methods after instantiation.
  5. Mapping interfaces and abstract classes to concrete implementations.
  6. Circular dependency detection.
  7. And more.

Table of Contents

- Requirements
- Installation
- Instantiating an Object
- Unresolvable Arguments
- Setting Arguments
- Mapping to Concrete Implementations
- Calling a Method After Instantiation
- Using Factories (Delegating Instantiation)
- Multiple Instances of the Same Class
- Sharing Instances
- Invoking a Method or Function

Requirements

Syringe requires PHP 5.4 or higher.

Installation

Composer

`

Instantiating an Object

For the sake of simplicity (this being the first example and all), say we want to instantiate an object which has no dependencies:

Recursively Resolving Parameters

The UserMapper now depends on a PdoAdapter

When making a UserMapper the injector discovers that it depends on a PdoAdapter so it creates a PdoAdapter object first and injects it into the UserMapper.

Unresolvable Arguments

Two scenarios exist where it is impossible to automatically resolve an argument:

  1. No type-hint exists - In this situation you must explicitly tell the injector what the argument is. See setting arguments.
  2. The type-hint is an interface or abstract class - See mapping to concrete implementations.

Setting Arguments

You can set the arguments of any method, not just the constructor.

You can also set arguments on the fly:

Arguments set on the fly always take precedence over arguments set prior to calling make().

This is useful when invoke() method.

Callable Arguments

Callable arguments get invoked and their return value gets passed into the method.

If the type-hint of the parameter is callable and the set argument is callable then the argument will NOT be invoked before passing it into the method.

String Argument Resolved to an Object

You can set a class name argument for a parameter which has a class or interface type-hint.

When the injector is resolving the argument it will notice it is a string but the type-hint is a class. It will then resolve the string argument (class name) into an object.

This technique is especially powerful when you need to use multiple instances of the same class. Say you have two databases, a local and a remote one and both obviously have different connection details. The UserMapper connects to the local database and the Logger connects to a remote database.

Both objects need a different instance of a PdoAdapter, one which can connect to the local database and another which can connect to the remote database.

The UserMapper will get the #default instance of the PdoAdapter and the Logger will get the #remote instance of the PdoAdapter. For more information about dealing with multiple instances click here.

Mapping to Concrete Implementations

When trying to make a UserMapper the injector will throw an InjectorException because it cannot resolve DatabaseAdapterInterface to a concrete implementation. To prevent this you simply map the interface to a concrete implementation i.e. a class which implements the interface.

You can also map abstract classes to concrete implementations.

Calling a Method After Instantiation

Once the PdoAdapter has been instantiated the connect() method will be called before the object is returned. If the method being called has any parameters they will be resolved and passed into the method. If the method has any set them before you make the object or else pass them to the addCall() method.

Using Factories (Delegating Instantiation)

A factory consists of a regular expression and a callable. The regular expression is matched against the name of the class being made, if there is a match then the instantiation of the object is delegated to the factory.

If the class name you're trying to match contains backslashes you do not need to escape them, this is done automatically by the injector.

Advanced Factory Usage

Let's say you have some services in your application which reside in the Service namespace. For some reason you don't want the injector to instantiate any of the services and want to delegate that job to your own ServiceFactory.

The instantiation of any class name that begins with Service\ is delegated to the callable factory. If the callable factory has a parameter named $className then the injector will pass the full name of the class which matched the regular expression of the factory.

Multiple Instances of the Same Class

So far we have only dealt with a single instance of the same class. Sometimes you may want multiple instances of the same class. Such a use case would be dealing with multiple databases. Your application may need to connect to a local database and a remote database.

When making an object, setting its parameters or adding calls to its methods you can specify which instance of the object you are referring to by putting a #instance-name after the class name. If you do not supply an instance name then you are dealing with the #default instance of the class.

Two different instances of the same class:

Sharing Instances

By default, if an object is created by the injector it is stored and used every time an instance of that object is needed.

You can tell the injector to always create a new instance of an object:

Sharing an object:

Sharing an object and specifying the instance name:

Invoking a Method or Function

The invoke() method accepts any valid PHP callable and an optional second parameter which contains arguments you want to pass into the method/function when it is invoked.


All versions of syringe with dependencies

PHP Build Version
Package Version
Requires php Version >=5.4.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 david2m/syringe contains the following files

Loading the files please wait ....