Download the PHP package jasny/container without Composer

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

Jasny Container

Build Status Code Coverage Scrutinizer Code Quality Packagist Stable Version Packagist License

This package contains a simple dependency injection container compatible with PSR-11.

The container supports (explicit) subcontainers. Container objects are immutable.

Containers are used to help with dependency injection, creating loosly coupled applications. DI helps in making making your application better testable and maintainable.

The following type of entries are typically added to the container;

Pro tip: It's sometimes harder to get a dependency to a (deeply nested) object. In this case you might be tempted to resort to using the global scope via a Service Locator, Facade or Singleton. Do not do this! It will make your code much harder to test, maintain and reuse. Instead resolve the nesting by creating an abstract factory for the nested object and inject the service into the factory.

This library is based on Picontainer.

Installation

The Jasny Container package is available on packagist. Install it using composer:

composer require jasny/container

The packages adheres to the SemVer specification, and there will be full backward compatibility between minor versions.

Declaring entries in the container

Creating a container is a matter of creating a Container instance passing the list of entries, as an array of anonymous functions.

The list of entries is an associative array. The order of entries doesn't matter.

The entry can be anything (an object, a scalar value, a resource, etc...) The anonymous function must accept one parameter: the container on which dependencies will be fetched.

Any iterable may be passed to the container, not just plain arrays. Once the container has been created it's immutable entries can't be added, removed or replaced.

Delegated lookup

If a delegate-lookup container was passed as the second argument of the constructor, it will be passed to the anonymous function instead.

Entry loader

The EntryLoader can be used to load entries from PHP files in a directory. This is useful for larger applications to organize service declarations.

The EntryLoader takes an Iterator. This can be an simple ArrayIterator, but more typically a GlobIterator or a RecursiveDirectoryIterator. See SPL Iterators.

Class loader

The ClassLoader is an alternative to the entry loader, to create entries based on a list of classes. The loader takes an Iterator with fully qualified classnames (FQCNs).

By default the entry key is the class name and autowiring is used to instantiate the service.

Custom instantiation

The second (optional) argument is a callback that is applied to each class to create the container entries. This function must return an array of Closures.

Load all files in a folder

Instead of just supplying a list of classes, you might want to scan a folder and add all the classes from that folder. This can be done with FQCNIterator from jasny/fqcn-reader.

This can also be combined with a callback.

Modified copy

The container is immutable. It isn't possible to add or change entries after it has been created. However it is possible to get a copy of the container with modified entries using the with() method.

The with() methods accepts an iterable with callbacks, similar to the container constructor. This means that loaders may be used to load replacement entries.

Fetching entries from the container

Fetching entries from the container is done using the get() method:

Calls to the get method should only return an entry if the entry is part of the container. If the entry is not part of the container, a Jasny\Container\NotFoundException is thrown.

Type checking

If the entry identifier is an interface or class name, a TypeError is thrown if the entry doesn't implement the interface or extend the class.

Any identifier that starts with a capital and doesn't contain a . is seen a potential interface or class name, this is checked with class_exists.

It's recommended to keep non-class/interface identifiers lowercase.

Subcontainers

Entries of the container may also be a container themselves. In this case, you can use the entry.subentry to get an entry from the subcontainer. The subcontainer needs to implement Psr\Container\ContainerInterface, it doesn't need to be a Jasny\Container object.

If the container contains a config.secret entry, the config container is not consulted. A multiple levels are used like config.db.settings.host, the container tries finding the an entry in the following order; config.db.settings.host, config.db.settings, config.db, config.

Checking entries

To check if the container has an entry you can use the has method. It return true if the entry is part of the container and false otherwise.

Null object

Pro tip: Rather than using the has method, create a Null object. A null object correctly implements an interface, but does nothing. For example a NoCache object that doesn't actually cache values. Removing the if statements reduces complexity. The function calls are typically not more expensive than the if statement, so it doesn't hurt performance.

Autowiring

The container can be used to instantiate an object (instead of using new), automatically determining the dependencies. This can be handy when you find yourself constantly modifying specific entries.

To use autowiring, add a Jasny\Autowire\AutowireInterface entry to the container.

The Container class implements AutowireContainerInterface which defines an autowire method. The first argument is the class name.

Additional arguments may be provided, which are passed directly to the constructor. No autowiring is applied to these parameters.

For more information see jasny/autowire.

Pro tip: Autowiring increases coupling, so use it sparsely.

Notes for the reader

If you're using the PHPStorm IDE, install the dynamic return type plugin to get the correct type hint when doing $container->get(SomeInterface::class).


All versions of container with dependencies

PHP Build Version
Package Version
Requires php Version >=7.2.0
ext-ctype Version *
improved/type Version ^0.1.1
jasny/autowire Version ^1.2
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 jasny/container contains the following files

Loading the files please wait ....