Download the PHP package bylexus/php-injector without Composer

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

Tests

php-injector

A Function / Method parameter injection and Class autowire helper. Makes your Dependency Injection work.

Features

Installation

via Composer:

composer require bylexus/php-injector

Then just use the composer's autoload facility:

Summary - Method / Function invocation

The PHP Injector helps you calling functions / methods with parameter injection. There are two use cases for this application:

Scenario: Dependency Injection

In Dependency Injection scenarios, a developing user want to request a service from a dependency injection container. The easiest way is to allow the developer to just state the service type as function argument:

The Request object in this case gets magically injected by the surrounding framework. This is where PHPInjector comes into play: It allows the framework builder to inject the requested services.

Scenario: Web Request parameter injection

Under some circumstances it is needed or wanted to call a function not directly but to "collect" the parameters somehow dynamically and then inject them to a certain function.

Why?

Think for example you want to pass Web request parameters as function parameters of a controller, like this:

Now THAT is certainly a bad idea! Don't use request parameters as function parameters directly! All kind of bad things can happen here (injections, remote code invocation ...).

This is where the Injector comes into play: It allows you to:

  1. define which parameters you want to accept for a function
  2. call the function with an array of parameters, indepentent of their order (so you don't have to implement a complex variable ordering algorithm)
  3. define a TYPE converstion, e.g. "I accept an $age variable, but it is casted to an int."
  4. even allows you to define conditions (e.g. 'age must be > 20')

Using the Injector, this looks as follows:

The injector makes sure that your request parameters are passed to the function in the correct order AND cast the types, so in the example, $age is a properly casted Integer.

This also works in object methods:

Function / Method definition

No type casting / conditions

If you just want to call your functions / methods without do any type casting / conditions, you just define your functions as usual:

You can then use the Injector to invoke the functions (note that the argument array parameters don't have to be in the function args order):

In this example, the teaser function / methods will be called with the $text, the $maxlen = 10 and the $tail = '...' arguments.

Injecting by Class types (Object injection)

If you want to match parameters by Class type (not by parameter name), you just type-hint the class:

This method can be invoked as follows:

Note that you have to provide the full namespaced class name for a match in the parameter array.

Force type casting / parameter conditions

The big advantage of the PhpInjector is its ability to cast input parameters to a certain (base) type, and to check the values if they match certain conditions. This is especially useful for frontend input parameter validation.

Because PHP's lack of proper type hinting (base types cannot be used as hints in function signature), we decided to use the standard DocBlock Comments for describing parameters. This has the advantage that we also can define special types and conditions in a standard and readable syntax, without interfering with the default PHP function definition.

DocBlock Comment syntax

The syntax is similar to the PhpDocumentor's code>@param definition:

@param [Type] [name] [description]

An example:

This is perfectly compatible with the standard DocBlock comment for e.g. PhpDocumentor, but at the same time gives the Injector the needed information to cast the input values.

Supported types

Standard PHP types:

  • bool, boolean
  • int, integer
  • float, double (results in float)
  • string
  • array
  • object
  • mixed (no casting is done)

Special types:

  • timestamp: converts the parameter to a Unix timestamp, trying to guess the input via strtotime (e.g.: Input: '22.01.2015 22:00', parameter value: 1421964000)
  • json: Converts the input via json_decode

Using parameter conditions

(not yet implemented)

Especially for frontend input validation it is useful to check the input value if they match certain conditions, e.g. if a number is in a given range, if a string fits into a maxlength, if a date is in a certain range etc. PhpInjector comes with a set of condition definitions. We also use the DocBlock comment as shown above, and extend the Type field as shown in the following example:

This defines some conditions for the input parameters. If they do not match, an exception is thrown when invoking the metod via Injector.

Available conditions

  • <, <=, >, >=, <> [nr|timestamp]
    • numbers: Input must match the comparison (e.g. <=100: input must be less or equal 100)
    • strings: input length must match the comparison
    • timestamps: input date/time must match the comparison date/time
  • [lower]..[upper]: Input must be within the border
    • For numbers: e.g. int[1..100]: Input value must be between 1 to 100 (including 1 and 100)
    • For strings: e.g. string[5..20]: Input must be at least 5 characters, but max. 20 characters long
    • For timestamps: e.g. timestamp[1.1.2000..31.12.2010]: Input date must be within the given date range
  • word1[[|word2]...]: Input string must contain one of the words. Example: @param string[word1|word2|word3] $str

Summary - AutoWiring of Classes

AutoWiring is useful if you have service classes that depend on other services. For example:

In this scenario, ServiceA can only be instantiated by retrieving ServiceB as its constructor parameter. This is a typical use case in Dependency Injection scenarios.

The AutoWire class helps you resolve the dependencies and instantiate a dependant class:

If you're using a Dependency Injection container, you can use auto-wiring while setting up service instances:

Using manual parameters for class construction

Sometimes a class needs more than just other services in the constructor. Consider the following class:

Here, $name and $isActive cannot be determined as a type or service to be injected. In this cases, you can use additional parameters when auto-wiring a class:

The additional parameters are passed to the constructor of the class, if the name matches.

Examples - Function / Method injection

Simple function parameter injection

Parameter injection with type casting

Parameter injection with type casting and conditions

using native PHP functions

You can even use native PHP functions:

Inject class objects

You can also inject class object by type name, instead of variable name. This is exceptionnaly useful if you want to implement a Dependency Injection framework within your calls:

To call this function, you can use the Invoker as follows:

Inject services from a PSR-11 Service Container

You can provide a PSR-11 Service Container if the method's type signature requests a Service class from such a service container. This is especially useful in Dependency Injection scenarios:

To call this function, you can use the Invoker as follows:

Examples - Auto-Wiring classes

Simple Auto-Wiring

In its simplest form, you don't need a DI container, it's enough if the dependant classes exists. In this case, AutoWire just instantiates those classes:

Auto-Wiring with manual parameters

If you don't have a service container, or want to deliver additional constructor arguments manually, you can just provide a parameter array:

Here, $name is found by name in the parameter array, while $a is found by its Type declaration: the parameter array is searched for a matching type, and assigned.

Auto-Wiring with a PSR-11 Service Container

If you're using a PSR-11 compatible dependency injection container, you can use it for AutoWire to lookup its dependencies:

Developing

setup dev env

run unit tests

or manually, using PHPUnit:

Compatibility

Changelog

V3.0.0

License

Licensed under the MIT license, copyright 2015-2021 Alexander Schenkel


All versions of php-injector with dependencies

PHP Build Version
Package Version
Requires php Version >= 8.0
ext-mbstring Version *
psr/container 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 bylexus/php-injector contains the following files

Loading the files please wait ....