plugin-master/container

The PluginMaster DI Container.

1.0.0 2023-06-19 19:48 UTC

This package is auto-updated.

Last update: 2024-05-21 00:01:31 UTC


README

Simplified dependency injection container for PHP project. We create this package for PluginMaster(WordPress Plugin Development Framework)

Dependency Injection Containers are mainly used for managing the dependencies required by classes and their methods. When we need to create an instance of a class or call a method of a class, a container helps us deal with these dependencies smoothly.

I've developed this new PHP container with the goal of streamlining and simplifying dependency management. The functions it provides:

  1. get: This function allows you to retrieve an instance of a class or an alias that's already been defined.

  2. make: for creating a new instance of a class, using any necessary parameter also it will replace resolved objects that created with provided class name.

  3. call: To invoke a method of a class. It can take callable arguments in various formats like

    a. [class::class, 'method']
    b. [new class(), 'method']
    c. 'class@method'
    d. class::class ( will fire _invoke method).

  4. set: Assigns an alias to a class or a callback function. has: Checks if the container has resolved a certain class or alias.

In short, I developed this new PHP container to simplify dependency management, making it more accessible and straightforward, especially for projects that don't require more advanced features.

Install Package:

composer require plugin-master/container

Example:


$container = new \PluginMaster\Container\Container();

1. Set Alias


$container->set('User', function () {
    return new User();
});

2. Get Alias

$user = $container->get('User') ;

3. Call A method of class or callback function

$container->call('Project@user', ['name' => 'PHP IS POWER' ]);
 $container->call('Project#user', ['name' => 'AL EMRAN' ], ['methodSeparator'=>'#']);
$container->call([Project::class, 'user'], ['name' => 'AL EMRAN' ]);

4. Make A Object from Class

$project = $container->make(Project::class, ['name' => 'Make User' ]);
$user = $container->make(User::class);

5. get Object from provided class

$user = $container->get(User::class);

6. Check Resolved Object or alias exist

$user = $container->has(User::class);

Test

1. Clone Repo

git clone https://github.com/WP-PluginMaster/container.git

2. Install Dependency

composer install

3. Run Index.php file

php Index.php