DEV Community

Cover image for New Powerful "Dependency Injection Container" and "Service Provider" package in php
Hesam Mousavi
Hesam Mousavi

Posted on • Edited on

New Powerful "Dependency Injection Container" and "Service Provider" package in php

Hello friends

For one of my projects, which was a WordPress plugin (I'll write about it later so that you can get to know it, by the way, it's already available on the github page), I needed a container service and a provider service so that I could hijack my services like Laravel and use it wherever I needed.

Well, I came to the existing packages, but each of them had a flaw that discouraged me from installing them.
For example, one of them did not have a provider service. One of them had a provider but it only worked in its own framework, one of them said either use autowire or bind and it was not possible to use it at the same time, and some of them were extremely complicated.

So I decided to write my own container and put it in the git, and of course, I prepared a composer version of it.

I will leave the source of the project so that you can also use it.
If you have any suggestions, write to me under this post.

github: https://github.com/hesam-mousavi/falcon-container
Composer: https://packagist.org/packages/hesam-mousavi/falcon-container

Getting Started

  • add this package to your project:
composer require hesam-mousavi/falcon-container
Enter fullscreen mode Exit fullscreen mode
  • add composer autoload:
<?php
require_once __DIR__.'/vendor/autoload.php';
Enter fullscreen mode Exit fullscreen mode
  • then create the container:
$container = \HesamMousavi\FalconContainer\FalconContainer::getInstance();
Enter fullscreen mode Exit fullscreen mode

Singleton Service

If you want to use a service as a singleton:

$container->singleton('test', Test::class);
// Or
$container->singleton(Test::class);
Enter fullscreen mode Exit fullscreen mode

To execute the service in the first case:

$container->get('test');
Enter fullscreen mode Exit fullscreen mode

In the second case:

$container->get(Test::class);
Enter fullscreen mode Exit fullscreen mode

Non-Singleton Service

If you don't want to use the service as a singleton, you need to bind it:

$container->bind('test', Test::class);
// Or
$container->bind(Test::class);
Enter fullscreen mode Exit fullscreen mode

Calling them is the same as with the singleton and both are accessible with get method:

$container->get('test');
$container->get(Test::class);
Enter fullscreen mode Exit fullscreen mode

Using Closures

You can also use closures:

$container->singleton('test', function ($container) {
    return $container->get(Test::class);
});
Enter fullscreen mode Exit fullscreen mode

Example

Suppose we have a class titled Test:

namespace HesamMousavi\FalconContainer;

class Test {}
Enter fullscreen mode Exit fullscreen mode

Result of Calling This Class:

$container = \HesamMousavi\FalconContainer\FalconContainer::getInstance();

$container->singleton('test', \HesamMousavi\FalconContainer\Test::class);
//or
//$container->bind('test', \HesamMousavi\FalconContainer\Test::class);

dump($container->get('test'));
dump($container->get('test'));
Enter fullscreen mode Exit fullscreen mode

Output in Bind Mode:

HesamMousavi\FalconContainer\Test {#6}
HesamMousavi\FalconContainer\Test {#13}
Enter fullscreen mode Exit fullscreen mode

Output in Singleton Mode:

HesamMousavi\FalconContainer\Test {#6}
HesamMousavi\FalconContainer\Test {#6}
Enter fullscreen mode Exit fullscreen mode

Autowire Mode

If the class is not bound and called directly via the get method, it will be automatically resolved using reflection. This means that the autowire mode is always active, and if a bind(or singleton) is defined for a service, it uses it; otherwise, it resolves it automatically.


Using Service Provider in Falcon Container

You can also use service providers. To do this, you need to create a class that extends ServiceProvider. You can have a directory called providers and place your providers there.

Creating a ServiceProvider

namespace HesamMousavi\FalconContainer\Providers;

use HesamMousavi\FalconContainer\FalconServiceProvider;
use HesamMousavi\FalconContainer\Test;

class TestServiceProvider extends FalconServiceProvider {
    public function register() {
        $this->container->singleton('test', Test::class);
    }

    public function boot() {}
}
Enter fullscreen mode Exit fullscreen mode

Registering Providers

Each provider consists of two methods: register and boot. As you know, first the register method of all providers is executed, then the boot method is called.

providers.php File

Then in a file, for example named providers.php, you should include the path to the created provider.

<?php
return [
    \HesamMousavi\FalconContainer\Providers\TestServiceProvider::class,
];
Enter fullscreen mode Exit fullscreen mode

Providers

After creating an instance of the container, you can run all providers by calling the runProviders method and passing the path to the providers.php file.

$container = \HesamMousavi\FalconContainer\FalconContainer::getInstance();
$container->runProviders(__DIR__.'/bootstrap/providers.php');
Enter fullscreen mode Exit fullscreen mode

These steps help you use service providers in your Falcon Container plugin, adding more capabilities to your project. 🚀

Top comments (0)