kokspflanze / BjyAuthorize

MIT License
19 stars 18 forks source link

Unable to resolve service "memory" to a factory #42

Open jjwdesign opened 2 years ago

jjwdesign commented 2 years ago

Hi Guru,

Let me first say a thank you for forking the original BjyAuthorize and modifying it for Laminas. I'm currently in the middle of an application migration from ZF2 to Laminas and am trying to setup BjyAuthorize. I'm getting errors with the cache service named "memory". I've tried to set it up for memcahce, but am obviously missing something. Could you please post an example of how to configure the service factory with cache (memcache preferrable)?

Also, I noticed the Module Dependencies message that Laminas\Cache module needed to be loaded before the BjyAuthorize module. That was a nice addition. I re-ordered by modules list in my application config to correct that. Thanks.

Here's what I've done so far for the cache.

FIrst, I changed the config option "name" from "memory" to something more meaningful to me.

`<?php

    // cache options have to be compatible with Laminas\Cache\StorageAdapterFactoryInterface::create
    'cache_options'         => [
        'adapter'   => [
            'name' => 'BjyauthorizeMemcache',
        ],
        'plugins'   => [
            [
                'name'=> 'serializer',
            ],
        ],
    ],

` Then I went ahead and added a service_manager factories entry.

'BjyauthorizeMemcache' => \MyApp\Factory\BjyauthorizeMemcacheFactory::class,

Then I created a Factory class file in /MyApp/src/MyApp/Factory/BjyauthorizeMemcacheFactory.php. MyApp was used/changed for this post.

`<?php namespace MyApp\Factory;

use Laminas\ServiceManager\Factory\FactoryInterface; use Laminas\Cache\Service\StorageAdapterFactoryInterface; use Laminas\Cache\Service\StoragePluginFactoryInterface; use Psr\Container\ContainerInterface;

class BjyauthorizeMemcacheFactory implements FactoryInterface { public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null): mixed {

    $storageFactory = $container->get(StorageAdapterFactoryInterface::class);
    return $storageFactory->createFromArrayConfiguration($options);
}

} `

But, then I noticed you have a BjyAuthorize\Service\CacheFactory class available. I should probably be using that Factory class to create the cache object. I see there's similar code to what I was trying to do. Any advice would be appreciated. So, I tried to use BjyAuthorize\Service\CacheFactory, but I got the same resolve service error.

Thank you, Jeff

jjwdesign commented 2 years ago

I figured it out last night. I was indeed trying to make it more complicated than necessary. I just needed to add a few modules into my composer.json. I had the base laminas/laminas-cache, but none of the adapters. Added and ran a composer update.

    "laminas/laminas-cache": "*",
    "laminas/laminas-cache-storage-adapter-memcached": "*",
    "laminas/laminas-cache-storage-adapter-session": "*",
    "laminas/laminas-cache-storage-adapter-memory": "*",
    "laminas/laminas-cache-storage-adapter-filesystem": "*",
    "laminas/laminas-cache-storage-adapter-blackhole": "*",
    "laminas/laminas-cache-storage-adapter-apcu": "*",

Then I added them to my app module list of classes.

'modules' => array(
    'LmcUser',
    'LmcUserDoctrineORM',
    'ZfcTwig',
    'Laminas\Cache',
    'Laminas\Cache\Storage\Adapter\Memory',
    'BjyAuthorize',
    'DoctrineModule',
    'DoctrineORMModule',

The order is significant, but the Doctrine and Twig modules can be ignored if you don't use them.

kokspflanze commented 2 years ago

Hello,

yes that stuff comes from the cache-v3 support, with that you need to use the storage-adapters