osflab/container

This package is not installable via Composer 1.x, please make sure you upgrade to Composer 2+. Read more about our Composer 1.x deprecation policy.

OSF dynamic object container mechanism

3.0.0 2018-02-26 14:34 UTC

This package is not auto-updated.

Last update: 2021-10-16 13:27:32 UTC


README

OSF Container manages instances of your classes in order to optimize performance and accessibility. Define the instantiation policy of each class and let OSF Container create and manage your objects.

Features

  • Lazy classes instanciations
  • Optimized management of the instantiation policy
  • Dependency management
  • Mock objects generation & advanced mocking policy
  • Extremely simplified access to objects

Installation

You need at least php 7.1 and composer:

sudo apt install composer

In your application via composer

This is the recommended way to use this feature in a non-osf project.

Just add osflab/container in your composer.json file.

From github

To test the component or participate in its development.

git clone https://github.com/osflab/container.git
cd container && composer update

Unit tests launch:

vendor/bin/runtests

Usage

For example, to use Osf\Cache component (anyware in your code):

$cache = \Osf\Container\OsfContainer::getCache();

To create your own container:

use Osf\Container\AbstractContainer;

class MyContainer extends AbstractContainer
{
    /**
     * Build and get on-demand \My\Class instance(s)
     * @return \My\Class
     */
    public static function getMyClass(): \My\Class
    {
        return self::buildObject('\My\Class');
    }
}

The AbstractContainer::buildObject() method takes 3 args:

  • The class name
  • An array of class constructor values
  • A namespace string

See OsfContainer, ZendContainer and VendorContainer for more examples.