Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Zend\Db\Adapter\AdapterServiceFactory always invoke Zend\Db\Adapter\Adapter #353

@raivirtual

Description

@raivirtual

I created my own Db\Adapter\AdapterServiceFactory so I could be able to call my own Db\Adapter.

namespace RVMvp\Db\Adapter;

use Interop\Container\ContainerInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Db\Adapter\AdapterServiceFactory as ZendAdapterServiceFactory;

class AdapterServiceFactory extends ZendAdapterServiceFactory
{
    public function createService(ServiceLocatorInterface $container)
    {
        return $this($container, Adapter::class);
    }
}

It should create a service of RVMvp\Db\Adapter\Adapter but keep creating Zend\Db\Adapter\Adapter.

This is happening because the invokable method is calling directly the Zend\Db\Adapter\Adapter instead of the requested name provided on createService method:

namespace Zend\Db\Adapter;

use Interop\Container\ContainerInterface;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class AdapterServiceFactory implements FactoryInterface
{
    /**
     * Create db adapter service
     *
     * @param ContainerInterface $container
     * @param string $requestedName
     * @param array $options
     * @return Adapter
     */
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
    {
        $config = $container->get('config');
        return new Adapter($config['db']); // HERE IT'S CALLING DIRECTLY
    }

    /**
     * Create db adapter service (v2)
     *
     * @param ServiceLocatorInterface $container
     * @return Adapter
     */
    public function createService(ServiceLocatorInterface $container)
    {
        return $this($container, Adapter::class);
    }
}

I did my workaround adding the invokable method to my own AdapterServiceFactory:

public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
 {
        $config = $container->get('config');
        return new $requestedName($config['db']); // CHANGED TO REQUESTED NAME
 }

I'm suggesting a bugfix so we don't need to override the invokable method.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions