Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow configuration of AdapterPluginManager via config file #266

Merged
merged 9 commits into from
Jul 31, 2023
28 changes: 28 additions & 0 deletions docs/book/v3/storage/adapter-plugin-manager.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Adapter Plugin Manager

The `AdapterPluginManager` extends the laminas-servicemanager `AbstractPluginManager`, and has the following behaviors:

- It will only return `Laminas\Cache\Storage\StorageInterface` instances.
- All services are not shared by default; a new instance will be created each time you call `get()`.
boesing marked this conversation as resolved.
Show resolved Hide resolved

## Factory

`Laminas\Cache\Storage\AdapterPluginManager` is mapped to the factory `Laminas\Cache\Service\StorageAdapterPluginManagerFactory` when wired to the dependency injection container.

The factory will be automatically registered when loading/installing the `Laminas\Cache` module in `laminas-mvc` and/or loading/installing the `ConfigProvider` into a Mezzio application.

Since version 3.11.0, the factory will look for the `config` service, and use the `storage_adapters` configuration key to seed it with additional services.
boesing marked this conversation as resolved.
Show resolved Hide resolved
This configuration key should map to an array that follows [standard laminas-servicemanager configuration](https://docs.laminas.dev/laminas-servicemanager/configuring-the-service-manager/).

To add your own storage adapter you can add the following configuration:

```php
// config/autoload/storage_adapters.global.php
return [
'storage_adapters' => [
'factories' => [
\App\MyCustomStorageAdapter::class => \App\Container\MyCustomStorageAdapterFactory::class,
],
],
];
```
boesing marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions docs/bookdown.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"target": "html/",
"content": [
"book/laminas.cache.storage.adapter.md",
"book/laminas.cache.storage.adapter-plugin-manager.md",
rohm1 marked this conversation as resolved.
Show resolved Hide resolved
"book/laminas.cache.storage.capabilities.md",
"book/laminas.cache.storage.plugin.md",
"book/laminas.cache.pattern.md",
Expand Down
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ nav:
- Installation: v3/installation.md
- Storage:
- Adapters: v3/storage/adapter.md
- "Adapter Plugin Manager": v3/storage/adapter-plugin-manager.md
- Capabilities: v3/storage/capabilities.md
- Plugins: v3/storage/plugin.md
- "Cache Patterns":
Expand Down
2 changes: 2 additions & 0 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

class ConfigProvider
{
public const ADAPTER_PLUGIN_MANAGER_CONFIGURATION_KEY = 'storage_adapters';

/**
* Return default configuration for laminas-cache.
*
Expand Down
33 changes: 32 additions & 1 deletion src/Service/StorageAdapterPluginManagerFactory.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,45 @@
<?php

declare(strict_types=1);

namespace Laminas\Cache\Service;

use Laminas\Cache\ConfigProvider;
use Laminas\Cache\Storage\AdapterPluginManager;
use Laminas\ServiceManager\ServiceManager;
use Psr\Container\ContainerInterface;

use function is_array;

/**
* @psalm-import-type ServiceManagerConfiguration from ServiceManager
*/
final class StorageAdapterPluginManagerFactory
{
public function __invoke(ContainerInterface $container): AdapterPluginManager
{
return new AdapterPluginManager($container);
$pluginManager = new AdapterPluginManager($container);

// If we do not have a config service, nothing more to do
if (! $container->has('config')) {
return $pluginManager;
}

$config = $container->get('config');

// If we do not have a configuration, nothing more to do
if (
! isset($config[ConfigProvider::ADAPTER_PLUGIN_MANAGER_CONFIGURATION_KEY])
|| ! is_array($config[ConfigProvider::ADAPTER_PLUGIN_MANAGER_CONFIGURATION_KEY])
) {
return $pluginManager;
}

// Wire service configuration
/** @var ServiceManagerConfiguration $config */
$config = $config[ConfigProvider::ADAPTER_PLUGIN_MANAGER_CONFIGURATION_KEY];
$pluginManager->configure($config);

return $pluginManager;
}
}