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
Version 3 should be useable without specific DI container #160
Comments
@SvenRtbg You can always create all the instances by yourself using constructor injection. As per design, v3 will and can never now which storage adapters are in upstream projects without populating them to the Could you please provide more informations on how you are using Happy to help here so you don't have to require plenty of dependencies you wont use otherwise. |
I'll add more details if necessary on Monday, but for the time being: I am using the This setup requires a bit of configuration, so I add the original class into the cache class to do the real work, and I have to add a storage adapter. I create that storage adapter effectively by the static call to The library factory is passing it into the caching instance together with additional config to get it into a use case specific state (that's where the real strength in Laminas Cache is: Ability to add about any custom code to influence the caching workflow if necessary - I liked it when it was called Zend Cache ;) ). So the task I am facing right now: How would I instantiate a Investigating with the 3.0.x branch: Requires the And both require the ServiceManagers And that's where I am stuck. I will be perfectly able to "just" push all these instances into each other until this particular step. And maybe I am already missing something until here, e.g. the two Anyways: Happy to have your help here to understand things better! :) |
@SvenRtbg As far as I can see,
Exactly, in the So one solution for you could be to replace the class YourOwnStorageFactory
{
/**
* @var StorageAdapterFactoryInterface|null
*/
private static $storageAdapterFactory;
public static function factory(array $config): StorageInterface
{
$storageFactory = self::storageAdapterFactorySingleton();
return $storageFactory->createFromArrayConfiguration($config);
}
/**
* Singletons are considered bad, for more about this topic read this article
* https://www.michaelsafyan.com/tech/design/patterns/singleton
* But as you don't use dependency injection, thats actually a way to restore `StorageFactory` behavior as it was done
* in laminas-cache v2.
*/
private static function storageAdapterFactorySingleton(): StorageAdapterFactoryInterface
{
if (self::$storageAdapterFactory) {
return self::$storageAdapterFactory;
}
$config = array_merge_recursive(
(new \Laminas\Cache\ConfigProvider())(),
// Starting with v3 you will have to uncomment these
// (new \Laminas\Cache\Storage\Adapter\Memory\ConfigProvider())(),
// (new \Laminas\Cache\Storage\Adapter\Filesystem\ConfigProvider())(),
);
$containerConfiguration = $config['dependencies'] ?? [];
$container = new ServiceManager($containerConfiguration);
return self::$storageAdapterFactory = $container->get(StorageAdapterFactoryInterface::class);
}
} The reason why YOU have to create this static factory is, that only YOU know what adapters your project requires and which not. Thats the main idea behind the changes regarding If you only use the So wherever you need the If you have any other questions, feel free to drop them here. Please keep in mind that if you want to use https://docs.laminas.dev/laminas-cache/storage/adapter/#quick-start |
I am closing here as I think I've provided some examples which enable projects which do use If you have any problems during the migration to |
I'm sorry for not replying earlier. Your suggestions do work nicely, but I wasn't able to look into the 3.0 release. Will come back with any issues if I encounter them. |
Just coming back reporting that 3.0 (or more precisely 3.1 now) behave exactly as stated above. Thanks again. |
Feature Request
This library should be usable standalone when attempting to create instances of caching objects.
Summary
The static factories
StorageFactory
andPatternFactory
are deprecated in the latest V2.12, however their use case does not seem to be recreated properly in alternative methods.The way I think I'm supposed to consume this library in V3 is that I have to use either Mezzio or Laminas MVC because they provide a means to consume the output of the
ConfigProvider
class - which is very Laminas-specific configuration code that will not run on its own or explain by itself what has to be done in order to create a class that is able to do caching.My use case: I have multiple applications, which use about any possible DI container, for example PHP-DI, Pimple or Laminas. I cannot rule out homebrew solutions as well, and possibly no DI in very old applications.
These applications have access to quite a number of libraries that access HTTP-based services (SOAP and REST), so caching them is one required feature. And each of these library has a static factory call that anybody can use to get a working instance of the service client. I intentionally did not want to tie these libraries into any DI container, i.e. I am unable to "just use one DI everywhere", so instead I opted for an interface that any DI container is able to access.
Going one step deeper, these client factories have to include the caching layer, and for that they are calling another static factory of mine, that is eventually calling the
StorageFactory
mentioned above. This acts mostly as a convenience layer.I would be able to add any dependency injection required for Laminas to build
StorageInterface
instances in the future, however I have no clue how to do this without being required to effectively grab quite an amount of otherwise unused dependencies unrelated to the task, build one of the mentioned DI containers by injecting the configuration, and then get a working instance. Something that used to be a simple static call with some carefully prepared configuration array.I do understand removing legacy waste is an issue, and I have no intention of reverting the deprecation, but there is some information gap right now, and maybe functionality gap as well.
Currently I cannot "inject
StorageAdapterFactoryInterface
" because the implementing classStorageAdapterFactory
requires two dependencies, both of them having non-trivial constructor parameters themselves.The
StorageAdapterFactoryFactory
also doesn't help because it requires a properly configured container.The text was updated successfully, but these errors were encountered: