Download the PHP package sterzik/di without Composer

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

sterzik/di

This is a simple dependency-injection library. The goals of this library are:

Installation

Basic usage

In this simplest scenario (without any external configuration) this holds:

custom DI container

Using configuration in a DI container

In general there are two options how to configure a DI container. You may either pass an configuration array of service definitions or you may pass a php configuration file which returns the service definitions in the same format. The configuration is therefore either an array (direct passing of the service definitions) or a php filename (indirect passing of the service definitions) which return value will be the same service definitions. The filename may be either specified as an absolute path or a relative path, which will be resolved relatively to the project root (the directory where composer.json is present).

Configuring the global DI container

Without any setting, the global DI container (instantiated as DI::instance()) uses the config file config/services.php if present, or no configuration if the file is missing. There are 4 options, how to pass some another custom configuration:

  1. define the constant DI_SERVICE_DEFINITIONS which must be defined before the first call of DI::instance().
  2. Call DI::setServiceDefinitions($filename) with either relative or absolute file path before the first call of DI::instance() (calling DI::setServiceDefinitions() will override the DI_SERVICE_DEFINITIONS if both is present)
  3. Call DI::setServiceDefinitions($configurationArray) with an explicit configuration array before the first call of DI::instance().
  4. Use your own custom container storage based on a custom container.

Examples:

Configure custom DI container

To configure a custom DI container, just pass the configuration (or config file name) to the constructor of the DI object:

Service names

A service name may be any string not starting or ending with a backslash (\). If the service name starts with a backslash, it will be stripped automatically. The service container is designed in a way to support best cases when a service name corresponds to the class name, but this rule is not mandatory. The only mandatory rules for service names are:

Loading service definitions

Multiple service definitions play a role when instantiating a service. Let say, we want to instantiate a service Some\Cool\Service. In this case, these service definitions will be loaded (in the given order):

All these service definitions are cascaded and the next service definition overrides the previous one.

Service definitions

These data types are valid service definitions:

A service may be any php value. It does need to be necessarily an object, but having services as objects is the most common practice.

When the service definition is a callable, this holds for the return value:

Examples:

Container parameters

DI container may be parametrized by a bundle of parameters. Each parameter has a string name and may have an arbitrary value.

There are two ways how container parameters may be passed to the DI container:

  1. Pass the parameters as an array of named parameters as the second argument of the constructor (not available for the global DI instance)
  2. By calling the methods $di->setParameter() or $di->setParameters(). These methods must be called before the first service is obtained from the container.

Examples:

There is also a method $di->freezeParameters() which freezes the parameters and makes all parameters immutable, like $di->get() or $id->has() do.

Accessing any parameter may be done by the method $di->parameter($parameterName).

The builder

The builder passed to the callable service definition contains many methods which may control the build process of the service. All setters return the builder itself and therefore setters may be chained.

$builder->setClass($class)

Sets the class of the built object (constructed by a regular constructor).

Example services.php:

$builder->setArgument($argument, $value)

Sets one constructor argument. Argument may be specified either as an integer (position index) or a string (argument name).

Example services.php:

$builder->setArguments(...$arguments)

Set multiple constructor arguments given in the variadic argument $arguments.

Example services.php:

$builder->putArguments($arguments, $resetArguments = false)

Set multiple constructor arguments given in the array $arguments. If $resetArguments is true, then all previously set arguments will be reset.

Example services.php:

$builder->resetArguments()

Reset all previously set arguments. Equivalent to call $builder->putArguments([], true).

Example services.php:

$builder->setFactory($factory)

Use the callable $factory instead of calling the constructor. Arguments set by setArgument(), setArguments() or putArguments() are passed to the factory callable if set.

Example services.php:

$builder->setAutowire($autowire = true)

Enable or disable the autowiring functionality. If autowire is enabled (default state) arguments of the constructor or the factory not explicitely defined will be autowired to services using the defined type of the argument.

Example services.php:

$builder->setRequireExplicitClass($requireExplicitClass = true)

Enable or disable the automatic class resolving. By default, if the class is not specified, the service name is used as the class. If this function is enabled, classes must be explicitely specified for each service.

Example services.php:

$builder->setPublic($public = true)

Set the service as public (default) or private ($public = false). If the service is set to be private, then it cannot be instantiated outside of the DI container. It may be instantiated only as a dependency of other public classes.

Example services.php:

$builder->call($method, ...$arguments)

Call a method $method of the service after creation. The service must be an object if you want to use this feature.

Example services.php:

$builder->callArguments($method, $arguments, $autowire = null)

Same as call() but arguments are passed as a single argument instead of using a variadic argument. The $autowire argument specifies, if autowiring may be used for resolving method arguments. Possible values:

Example services.php:

$builder->setService($service)

Explicitely set the service. It has the same effect as returning the service in the service definition callback.

Example services.php:

$builder->get($serviceName)

get the service of the given service name from the DI container.

Example services.php:

$builder->has($serviceName)

Test if the service of the given service name does exist in the DI container.

$builder->parameter($parameter)

Return the container parameter named $parameter.

Example services.php:

$builder->getAppRoot()

Return the application root directory (directory with composer.json).

Example services.php:

$builder->resolvePath($path)

Return the path $path resolved relatively to the application root. I.e. if $path is an absolute path, keep it as it is, if it is an relative path, it will be converted to an absolute path where application root is the base directory to calculate relative path to.

Example services.php:


All versions of di with dependencies

PHP Build Version
Package Version
Requires php Version >=8.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 sterzik/di contains the following files

Loading the files please wait ...