Skip to content

Dependency Injection

Metapress uses a simple dependency injection container to manage class dependencies and resolve them when needed. This allows you to define your dependencies in a single place and then use them throughout your plugin.

Internally, Metapress uses the PHP-DI library to manage dependencies. It is highly recommended that you read the PHP-DI documentation to understand how the container works. This documentation will focus on how to use the container within Metapress.

Registering services

To register a service with the container, you need to define it in the services key of the plugin's configuration file. The key is an array of services that will be registered with the container.

php
return [
    'services' => [
        \Sematico\Metapress\Services\Options::class => autowire(),
    ],
];

In the example above, we are registering the Options class with the container. The autowire() function is a helper function provided by PHP-DI that will automatically resolve the class dependencies.

Using services

To use a service in your plugin, you can inject it into your classes using constructor injection. The container will automatically resolve the dependencies for you.

php
class MyService
{
    private $options;

    public function __construct(\Sematico\Metapress\Services\Options $options)
    {
        $this->options = $options;
    }
}

In the example above, we are injecting the Options service into the MyService class. The container will automatically resolve the Options service and pass it to the constructor.

Creating your own services

You can create your own services by defining them in the services key of the plugin's configuration file. You can use the autowire() function to automatically resolve the dependencies.

php
return [
    'services' => [
        MyService::class => autowire(),
    ],
];

Default services

Metapress comes with a series of pre-built services that are automatically registered with the container. These services provide common functionality that you can use in your plugin. Some of the default services include:

  • Activation: Handles the activation of your plugin.
  • AdminNotices: A service to display admin notices in the WordPress admin area.
  • FooterReview: A service to display a message to ask to review the plugin in the WordPress admin area footer.
  • I18n: Handles internationalization in your plugin.
  • Options: Stores and retrieves options for your plugin.
  • PluginLinks: Adds links to the plugin's row on the plugins page in the WordPress admin area.
  • Requirements: Checks if the plugin requirements are met.
  • RestApi: Registers REST API routes for your plugin.
  • PluginData: Retrieves data about the plugin.

TIP

Check the documentation for each service to learn more about how to use them in your plugin.

Accessing services

You can access services from the container using the get() method. This method will return an instance of the service that you can use in your plugin.

php
$options = $this->container->get(\Sematico\Metapress\Services\Options::class);

In the example above, we are retrieving the Options service from the container and storing it in a variable. You can then use the $options variable to interact with the service.

NOTE

The example above assumes that you have access to the plugin's container. Or that you are operating within a Plugin class.