Download the PHP package alex-patterson-webdev/container without Composer

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

Build Status codecov Scrutinizer Code Quality

Arp\Container

About

A simple PSR-11 compatible Dependency Injection Container

Installation

Installation via Composer.

require alex-patterson-webdev/container ^0.1

Usage

To begin using the container, we simply need to create an instance of it

use Arp\Container\Container;

$container = new Container();

The Arp\Container\Container implements the Psr\ContainerInterface and therefore can be used to check and fetch services by name.

if (true === $container->has('ServiceName')) {
    $service = $container->get('ServiceName');
}

Registering services with the container

There are a number of different ways we can register a 'service' with the container, the method you choose will depend on how you wish the service to be created.

Objects and Values

The simplest use case is when you need to set() an object or value on the container. These values do not require instantiation, the container will simply store and return this value unmodified when requested via get().

$container = new Container();
$container->set('TodaysDate', new \DateTime('today'));
$todaysDate = $container->get('TodaysDate');

Factories

Factories provide us with a location to construct and resolve dependencies using the container. The factory can be any php callable and can be set by calling $container->setFactory().

$container = new Container();
$container->setFactory('TodaysDate', static function() {
    return new \DateTime('today');
});

When invoked the factory class will also have the container injected into it as the first argument. We can use the container to resolve other dependencies.

$container->setFactory('TodaysDateService', static function(ContainerInterface $container) {
    return new TodayDateService($container->get('TodaysDate');
});

We also have access to the requested service name as the second argument, $name. By being aware of the name of the service which is being created it allows the creation of reusable factories.

$factory = static function(ContainerInterface $container, string $name) {
   $todaysDate = $container->get('TodaysDate');
   if ('EnglishDateService' === $name) {
        return new EnglishDateService($todaysDate);
   }
   return new FrenchDateService($todaysDate);
};

We can then assign the same factory with different service names.

$container->setFactory('EnglishDateService', $factory);
$container->setFactory('FrenchDateService', $factory);

Object Factory

In cases where you need have a service without dependencies we can use the Arp\Container\Factory\ObjectFactory and the container will create the class for us based on the service $name. If the service $name is not a valid class name an exception is thrown.

use Arp\Container\Factory\ObjectFactory;
$container = new Container();
$container->setFactory(\stdClass(), ObjectFactory::class);

// @var \stdClass $object
$object = $container->get(\stcClass());

The above configuration isn't explicitly required as any service $name using a FQCN not registered with the container with be automatically registered to use ObjectFactory. We recommended that you explicitly define the service for clarity.

Unit Tests

The project unit tests can be executed using PHPUnit

php vendor/bin/phpunit

All versions of container with dependencies

PHP Build Version
Package Version
Requires php Version >=7.4 || >=8.0
psr/container Version ^1
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 alex-patterson-webdev/container contains the following files

Loading the files please wait ....