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

[v3] configure() now resolves aliases defined as properties #92

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/ServiceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,9 @@ public function configure(array $config)

if (isset($config['aliases'])) {
$this->aliases = $config['aliases'] + $this->aliases;
}

if (! empty($this->aliases)) {
$this->resolveAliases($this->aliases);
}

Expand Down
7 changes: 7 additions & 0 deletions test/AbstractPluginManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Zend\ServiceManager\ServiceManager;
use ZendTest\ServiceManager\TestAsset\InvokableObject;
use ZendTest\ServiceManager\TestAsset\SimplePluginManager;
use ZendTest\ServiceManager\TestAsset\V2v3PluginManager;

/**
* @covers \Zend\ServiceManager\AbstractPluginManager
Expand Down Expand Up @@ -367,4 +368,10 @@ public function testAbstractFactoryGetsCreationContext()
$pluginManager->addAbstractFactory($abstractFactory->reveal());
$this->assertInstanceOf(InvokableObject::class, $pluginManager->get('foo'));
}

public function testAliasPropertyResolves()
{
$pluginManager = new V2v3PluginManager(new ServiceManager());
$this->assertInstanceOf(InvokableObject::class, $pluginManager->get('foo'));
}
}
59 changes: 59 additions & 0 deletions test/TestAsset/V2v3PluginManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\ServiceManager\TestAsset;

use Zend\ServiceManager\AbstractPluginManager;
use Zend\ServiceManager\Exception\InvalidServiceException;
use Zend\ServiceManager\Factory\InvokableFactory;

class V2v3PluginManager extends AbstractPluginManager
{
protected $aliases = [
'foo' => InvokableObject::class,
];

protected $factories = [
InvokableObject::class => InvokableFactory::class,
// Legacy (v2) due to alias resolution
'zendtestservicemanagertestassetinvokableobject' => InvokableFactory::class,
];

protected $instanceOf = InvokableObject::class;

protected $shareByDefault = false;

protected $sharedByDefault = false;


public function validate($plugin)
{
if ($plugin instanceof $this->instanceOf) {
return;
}

throw new InvalidServiceException(sprintf(
"'%s' is not an instance of '%s'",
get_class($plugin),
$this->instanceOf
));
}

/**
* {@inheritDoc}
*/
public function validatePlugin($plugin)
{
try {
$this->validate($plugin);
} catch (InvalidServiceException $e) {
throw new \RuntimeException($e->getMessage(), $e->getCode(), $e);
}
}
}