Skip to content

Latest commit

 

History

History
129 lines (95 loc) · 3.7 KB

File metadata and controls

129 lines (95 loc) · 3.7 KB

Container Documentation

Register Service Provider

  1. Step 1: Create a service provider inside includes/DependencyManagement/Providers that extends WeDevs\Dokan\DependencyManagement\BaseServiceProvider.
  2. Step 2: Register the service provider in the boot method of includes/DependencyManagement/Providers/ServiceProvider.php.

You can see the already registered service providers inside the boot method of the ServiceProvider class.

Register Services in the Service Provider

  1. Step 1: Register the services inside the register method of your service provider.
  2. Step 2: Implement a provides method that returns true or false when the container invokes it with a service name.
namespace WeDevs\Dokan\DependencyManagement\Providers;

use WeDevs\Dokan\DependencyManagement\BaseServiceProvider;

class SomeServiceProvider extends BaseServiceProvider
{
    /**
     * The provides method lets the container know
     * which services are provided by this provider.
     * The alias must be added to this array or it will
     * be ignored.
     */
    public function provides(string $id): bool
    {
        $services = [
            'key',
            Some\Controller::class,
            Some\Model::class,
            Some\Request::class,
        ];

        return in_array($id, $services);
    }

    /**
     * The register method defines services in the container.
     * Services must have an alias in the `provides` method
     * or they will be ignored.
     */
    public function register(): void
    {
        $this->getContainer()->add('key', 'value');

        $this->getContainer()
            ->add(Some\Controller::class)
            ->addArgument(Some\Request::class)
            ->addArgument(Some\Model::class);

        $this->getContainer()->add(Some\Request::class);
        $this->getContainer()->add(Some\Model::class);
    }
}

Add Services to the Container

  • Add a service:
$this->getContainer()->add(ServiceClass::class);
  • Add a shared service (one instance per request lifecycle):
$this->getContainer()->addShared(ServiceClass::class);
  • Add a service with constructor parameters:
$this->getContainer()->addShared(ServiceClass::class, function () { 
    return new ServiceClass($params); 
});
  • Add a shared service with constructor parameters and tag it:
$this->getContainer()->addShared(ServiceClass::class, function () { 
    return new ServiceClass($params); 
})->addTag('tag_name');
  • Add a service and tag all implemented interfaces:
$this->getContainer()->share_with_implements_tags(ServiceClass::class);

Get Service from the Container

  • Get a single instance:
$service = dokan()->get_container()->get(ServiceClass::class);
  • Get an array of instances by tag:
$service_list = dokan()->get_container()->get('tag-name');

Override the Existing Service

dokan()->get_container()->extend(ServiceClass::class)->setConcrete(new OtherServiceClass());

Check if a Service is Registered

$is_registered = dokan()->get_container()->has(ServiceClass::class);

For more details, visit the League Container documentation.