Download the PHP package timostamm/injector without Composer

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

PHP Dependency Injector

Build Status

A basic autowiring / autoloading injector with good support for argument overriding.

The injector supports constructor injection and function argument injection:

// create an instance
$injector->instantiate(MyClass::class, [
    '$timeout' => 1000
]);

// call a function
$injector->invoke([$object, 'method']);

If an argument is type-hinted as a class, the injector automatically creates an instance of this class.

To control how and which values will be injected, you can create aliases, default parameters, singletons and decorators.

Alias
// configure injector to provide an instance of MyImplementation 
// for every MyInterface 
$injector->alias(MyInterface::class, MyImplementation::class); 

class Test {
    function __construct(MyInterface $my) {
        print "Test constructed with " . get_class($my);
    }
}
Singleton
// configure injector to create only one instance
$injector->singleton(MyImplementation::class)

// prints "bool(true)" 
$b = $injector->instantiate(Test::class);
$a = $injector->instantiate(Test::class);
var_dump($a === $b); 
Factories
// configure injector to call the factory function 
// to create the object
$injector->factory(Service::class, function(Database $db){
    $total = $db->countEntries();
    return new MyService( $total );
});

$service = $injector->instantiate(Service::class);
Decorators
// configure injector to call the decorator function 
// after the object is created
$injector->decorate(Service::class, function(Service $service, Logger $logger){
    $service->logger = $logger;
});

$service = $injector->instantiate(Service::class);
$service->logger; // is set
Parameters
// set a parameter value by name
$injector->defaults(Service::class, [
    '$timeout' => 1000
]);

// set a value for a type-hinted argument
$injector->defaults(Test::class, [
    MyInterface::class, new MyService()
]);

// set an alias for a type-hinted argument
$injector->defaults(Test::class, [
    MyInterface::class, MyService::class
]);

// set an alias for a type-hinted argument
$injector->defaults(Test::class, [
    MyInterface::class, MyService::class
]);

// provide a type hint for an untyped argument
$injector->defaults(Test::class, [
    'hint $mixed', MyService::class
]);

// set a rest-parameter
$injector->defaults(Service::class, [
    '...$restParam' => [1,2,3]
]):

// set a parameters array
$injector->defaults(Service::class, [1000, "hello"]):

// set a parameter value by index
$injector->defaults(Service::class, [
    '#0' => 1000
]);

Parameter configuration do not have to be complete. It is possible to provide or override arguments later:

$injector->instantiate(Service::class, [
    '$timeout' => 1000
]);

$injector->invoke([$myService, 'method'], [
    '$foo' => 'bar', 
    'hint $other' => Service::class
]);
Error handling

Parameter configurations, aliases and decorators are strictly validated.

Argument inspection

The InspectableInjectorInterface provides methods to check for missing arguments before invocation:

function inspectInvocation(callable $callable): ArgumentInspectionInterface;
function inspectInstantiation(string $className): ArgumentInspectionInterface;

This can be used to provide route parameters to a controller, for example.


All versions of injector with dependencies

PHP Build Version
Package Version
Requires php Version ^7.1.3
psr/container 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 timostamm/injector contains the following files

Loading the files please wait ...