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

ServiceManager returns that service does not exist with has() but can create with get() #50

Closed
@svycka

Description

@svycka

I created service manager like this:

use Zend\ServiceManager\AbstractPluginManager;
use Zend\ServiceManager\Exception\RuntimeException;

class TypesManager extends AbstractPluginManager
{
    protected $invokableClasses = [
        InArrayType::class => InArrayType::class,
        RegexType::class   => RegexType::class,
    ];
    protected $aliases = [
        'in_array' => InArrayType::class,
        'regex'    => RegexType::class,
    ];

    public function validatePlugin($type)
    {
        if ($type instanceof SettingTypeInterface) {
            return; // we're okay
        }
        throw new RuntimeException(sprintf(
            'SettingType of type %s is invalid; must implement %s',
            (is_object($type) ? get_class($type) : gettype($type)),
            SettingTypeInterface::class
        ));
    }
}

I am using like this:

$manager = new TypesManager();
$manager->has(InArrayType::class); // returns false
$manager->has('in_array'); // returns false(canonicalization problem because of `_` ?)
$manager->has(RegexType::class); // returns false
$manager->has('regex'); // returns true
$manager->get(InArrayType::class); // returns instance off InArrayType::class
$manager->get('in_array'); // // returns instance off InArrayType::class
$manager->get(RegexType::class); // returns instance off RegexType::class
$manager->get('regex'); // // returns instance off RegexType::class

when I sow this I thought maybe I doing it wrong so checked other zend modules like zend-filter and wrote some tests:

    public function testFirstGet()
    {
        $manager = new FilterPluginManager();
        $this->assertInstanceOf('Zend\Filter\Boolean', $manager->get('Zend\Filter\Boolean'));
        $this->assertTrue($manager->has('Zend\Filter\Boolean'));
    }
    public function testFirstHas()
    {
        $manager = new FilterPluginManager();
        $this->assertTrue($manager->has('Zend\Filter\Boolean')); // fails here
        $this->assertInstanceOf('Zend\Filter\Boolean', $manager->get('Zend\Filter\Boolean'));
    }

First pass second fails.
order of has() and get() is important?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions