Download the PHP package brightnucleus/injector without Composer

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

Bright Nucleus Injector Component

Config-driven Dependency Injector, based in large parts on Auryn.

Latest Stable Version Total Downloads Latest Unstable Version License

This is a config-driven dependency injector, to allow easy registration of alias mappings through the brightnucleus/config component.

It includes large parts of code from the rdlowrey/auryn package.

Notable changes compared to Auryn:

Table Of Contents

Requirements

BrightNucleus Injector requires PHP 7.0+.

Installation

The best way to use this component is through Composer:

Basic Usage

This documentation only deals with passing in mappings through a Config file. Documentation for the basic methods still needs to be synced. For now, just refer to the Auryn README for these..

The Bright Nucleus Injector expects to get an object through its constructor that implements the BrightNucleus\Config\ConfigInterface. You need to pass in the correct "sub-configuration", so that the keys that the Injector is looking for are found at the root level.

The Injector looks for three configuration keys: standardAliases, sharedAliases and configFiles.

The injector works by letting you map aliases to implementations. An alias is a specific name that you want to be able to instantiate. Aliases can be classes, abstract classes, interfaces or arbitrary strings. You can map each alias to a concrete implementation that the injector should instantiate.

This allows you to have your classes only depend on interfaces, through constructor injection, and choose the specific implementation to use through the injector config file.

As an example, imagine the following class:

If we now define an alias 'BookInterface' => 'LatestBestseller', we can have code like the following:

Standard Aliases

A standard alias is an alias that behaves like a normal class. So, for each new instantiation (using Injector::make()), you'll get a fresh new instance.

Standard aliases are defined through the Injector::STANDARD_ALIASES configuration key:

Shared Aliases

A shared alias is an alias that behaves similarly to a static variable, in that they get reused across all instantiations. So, for each new instantiation (using Injector::make()), you'll get exactly the same instance each time. The object is only truly instantiated the first time it is needed, and this instance is then shared.

Shared aliases are defined through the Injector::SHARED_ALIASES configuration key:

Argument Definitions

The argument definitions allow you to let the Injector know what to pass in to arguments when you need to inject scalar values.

By default, the values you pass in as definitions are assumed to be raw values to be used as they are. If you want to pass in an alias through the Injector::ARGUMENT_DEFINITIONS key, wrap it in a BrightNucleus\Injector\Injcetion class, like so:

Argument Providers

The argument providers allow you to let the Injector know what to pass in to arguments when you need to instantiate objects, like $config or $logger. As these are probably different for each object, we need a way to map them to specific aliases (instead of having one global value to pass in). This is done by mapping each alias to a callable that returns an object of the correct type.

The Injector will create a light-weight proxy object for each of these. These proxies are instantiated and replaced by the real objects when they are first referenced.

As an example, pretty much all of the Bright Nucleus components use Config files to do project-specific work.

If you want to map aliases to specific subtrees of Config files, you can do this by providing a callable for each alias. When the Injector tries to instantiate that specific alias, it will invoke the corresponding callable and hopefully get a matching Config back.

Delegations

The delegations allow you to let the Injector delegate the instantiation for a given alias to a provided factory. The factory can be any callable that will return an object that is of a matching type to satisfy the alias.

If you need to act on the injection chain, like finding out what the object is for which you currently need to instantiate a dependency, add a BrightNucleus\Injector\InjectionChain $injectionChain argument to your factory callable. You will then be able to query the passed-in injection chain. To query the injection chain, pass the index you want to fetch into InjectionChain::getByIndex($index). If you provide a negative index, you will get the nth element starting from the end of the queue counting backwards.

As an example, consider an ExampleClass with a constructor __construct( ExampleDependency $dependency ). The injection chain would be the following (in namespace Example\Namespace) :

So, in the example below, we use getByIndex(-2) to fetch the second-to-last element from the list of injections.

Preparations

The preparations allow you to let the Injector define additional preparation steps that need to be done after instantiation, but before the object if actually used.

The callable will receive two arguments, the object to prepare, as well as a reference to the injector.

Registering Additional Mappings

You can register additional mappings at any time by simply passing additional Configs to the Injector::registerMappings() method. It takes the exact same format as the constructor.

Note: For such a simple example, creating a configuration file is of course overkill. You can just as well use the basic Auryn alias functionality and just Injector::alias() an additional alias. Refer to the Auryn Documentation to read more about the different ways of configuring the injector manually.

Contributing

All feedback / bug reports / pull requests are welcome.

This package uses the PHP Composter PHPCS PSR-2 package to check committed files for compliance with the PSR-2 Coding Style Guide. If you have valid reasons to skip this check, add the --no-verify option to your commit command:

License

This code is released under the MIT license.

For the full copyright and license information, please view the LICENSE file distributed with this source code.

Large parts of this code were initially taken from the rdlowrey/auryn project.

Copyright for the original Auryn code is (c) 2013-2014 Daniel Lowrey, Levi Morrison, Dan Ackroyd.

Auryn contributor list


All versions of injector with dependencies

PHP Build Version
Package Version
Requires brightnucleus/exceptions Version >=0.2
brightnucleus/config Version >=0.4.7
ocramius/proxy-manager Version ~2.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 brightnucleus/injector contains the following files

Loading the files please wait ....