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

Updated to zend-servicemanager v3 #8

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"zendframework/zend-i18n": "~2.5",
"zendframework/zend-json": "~2.5",
"zendframework/zend-mvc": "~2.5",
"zendframework/zend-servicemanager": "~2.5",
"zendframework/zend-servicemanager": "dev-develop as 2.6.0",
"fabpot/php-cs-fixer": "1.7.*",
"phpunit/PHPUnit": "~4.0"
},
Expand Down
22 changes: 11 additions & 11 deletions src/AbstractConfigFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@

namespace Zend\Config;

use Interop\Container\ContainerInterface;
use Traversable;
use Zend\ServiceManager;
use Zend\ServiceManager\Factory\AbstractFactoryInterface;

/**
* Class AbstractConfigFactory
*/
class AbstractConfigFactory implements ServiceManager\AbstractFactoryInterface
class AbstractConfigFactory implements AbstractFactoryInterface
{
/**
* @var array
Expand All @@ -38,18 +39,17 @@ class AbstractConfigFactory implements ServiceManager\AbstractFactoryInterface
/**
* Determine if we can create a service with name
*
* @param ServiceManager\ServiceLocatorInterface $serviceLocator
* @param string $name
* @param ContainerInterface $container
* @param string $requestedName
* @return bool
*/
public function canCreateServiceWithName(ServiceManager\ServiceLocatorInterface $serviceLocator, $name, $requestedName)
public function canCreateServiceWithName(ContainerInterface $container, $requestedName)
{
if (isset($this->configs[$requestedName])) {
return true;
}

if (!$serviceLocator->has('Config')) {
if (! $container->has('Config')) {
return false;
}

Expand All @@ -58,19 +58,19 @@ public function canCreateServiceWithName(ServiceManager\ServiceLocatorInterface
return false;
}

$config = $serviceLocator->get('Config');
$config = $container->get('Config');
return isset($config[$key]);
}

/**
* Create service with name
*
* @param ServiceManager\ServiceLocatorInterface $serviceLocator
* @param string $name
* @param ContainerInterface $container
* @param string $requestedName
* @param array $options
* @return string|mixed|array
*/
public function createServiceWithName(ServiceManager\ServiceLocatorInterface $serviceLocator, $name, $requestedName)
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
if (isset($this->configs[$requestedName])) {
return $this->configs[$requestedName];
Expand All @@ -82,7 +82,7 @@ public function createServiceWithName(ServiceManager\ServiceLocatorInterface $se
return $this->configs[$key];
}

$config = $serviceLocator->get('Config');
$config = $container->get('Config');
$this->configs[$requestedName] = $this->configs[$key] = $config[$key];
return $config[$key];
}
Expand Down
12 changes: 6 additions & 6 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Zend\Config;

use Zend\ServiceManager\ServiceManager;
use Zend\Stdlib\ArrayUtils;

class Factory
Expand Down Expand Up @@ -114,7 +115,7 @@ public static function fromFile($filename, $returnConfigObject = false, $useIncl
static::$extensions[$extension] = $reader;
}

/** @var Reader\ReaderInterface $reader */
/* @var Reader\ReaderInterface $reader */
$config = $reader->fromFile($filepath);
} else {
throw new Exception\RuntimeException(sprintf(
Expand Down Expand Up @@ -156,9 +157,8 @@ public static function fromFiles(array $files, $returnConfigObject = false, $use
*/
public static function toFile($filename, $config)
{
if (
(is_object($config) && !($config instanceof Config)) ||
(!is_object($config) && !is_array($config))
if ((is_object($config) && !($config instanceof Config))
|| (!is_object($config) && !is_array($config))
) {
throw new Exception\InvalidArgumentException(
__METHOD__." \$config should be an array or instance of Zend\\Config\\Config"
Expand Down Expand Up @@ -220,7 +220,7 @@ public static function setReaderPluginManager(ReaderPluginManager $readers)
public static function getReaderPluginManager()
{
if (static::$readers === null) {
static::$readers = new ReaderPluginManager();
static::$readers = new ReaderPluginManager(new ServiceManager());
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is required, as abstract plugin manager implementations now require the parent service manager during construction. I did the same with the writer plugin manager as well.

}
return static::$readers;
}
Expand All @@ -244,7 +244,7 @@ public static function setWriterPluginManager(WriterPluginManager $writers)
public static function getWriterPluginManager()
{
if (static::$writers === null) {
static::$writers = new WriterPluginManager();
static::$writers = new WriterPluginManager(new ServiceManager());
}

return static::$writers;
Expand Down
25 changes: 6 additions & 19 deletions src/ReaderPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@

namespace Zend\Config;

use Interop\Container\ContainerInterface;
use Zend\ServiceManager\AbstractPluginManager;

class ReaderPluginManager extends AbstractPluginManager
{
protected $instanceOf = Reader\ReaderInterface::class;

/**
* Default set of readers
*
Expand All @@ -26,25 +29,9 @@ class ReaderPluginManager extends AbstractPluginManager
'javaproperties' => 'Zend\Config\Reader\JavaProperties',
];

/**
* Validate the plugin
* Checks that the reader loaded is an instance of Reader\ReaderInterface.
*
* @param Reader\ReaderInterface $plugin
* @return void
* @throws Exception\InvalidArgumentException if invalid
*/
public function validatePlugin($plugin)
public function __construct(ContainerInterface $container, array $config = [])
{
if ($plugin instanceof Reader\ReaderInterface) {
// we're okay
return;
}

throw new Exception\InvalidArgumentException(sprintf(
'Plugin of type %s is invalid; must implement %s\Reader\ReaderInterface',
(is_object($plugin) ? get_class($plugin) : gettype($plugin)),
__NAMESPACE__
));
$config = array_merge_recursive(['invokables' => $this->invokableClasses], $config);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach is necessary as the SM doesn't have an $invokableClasses property any longer; it instead munges invokables into factory and alias definitions. As such, the configuration needs to be passed in during instantiation.

parent::__construct($container, $config);
}
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

validatePlugin() is no longer necessary, as the AbstractPluginManager has an implementation that refers to the $instanceOf property for the most common use case of validating against a type.

16 changes: 6 additions & 10 deletions src/WriterPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@

namespace Zend\Config;

use Interop\Container\ContainerInterface;
use Zend\ServiceManager\AbstractPluginManager;

class WriterPluginManager extends AbstractPluginManager
{
protected $instanceof = Writer\AbstractWriter::class;

protected $invokableClasses = [
'ini' => 'Zend\Config\Writer\Ini',
'json' => 'Zend\Config\Writer\Json',
Expand All @@ -21,16 +24,9 @@ class WriterPluginManager extends AbstractPluginManager
'xml' => 'Zend\Config\Writer\Xml',
];

public function validatePlugin($plugin)
public function __construct(ContainerInterface $container, array $config = [])
{
if ($plugin instanceof Writer\AbstractWriter) {
return;
}

$type = is_object($plugin) ? get_class($plugin) : gettype($plugin);

throw new Exception\InvalidArgumentException(
"Plugin of type {$type} is invalid. Plugin must extend ". __NAMESPACE__ . '\Writer\AbstractWriter'
);
$config = array_merge_recursive(['invokables' => $this->invokableClasses], $config);
parent::__construct($container, $config);
}
}
22 changes: 10 additions & 12 deletions test/AbstractConfigFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace ZendTest\Config;

use Zend\Config\AbstractConfigFactory;
use Zend\Mvc\Service\ServiceManagerConfig;
use Zend\ServiceManager;

/**
Expand All @@ -31,7 +30,7 @@ class AbstractConfigFactoryTest extends \PHPUnit_Framework_TestCase
protected $application;

/**
* @var \Zend\ServiceManager\ServiceManager
* @var ServiceManager
*/
protected $serviceManager;

Expand All @@ -53,15 +52,14 @@ public function setUp()
]
];

$sm = $this->serviceManager = new ServiceManager\ServiceManager(
new ServiceManagerConfig([
$this->serviceManager = new ServiceManager\ServiceManager([
'abstract_factories' => [
'Zend\Config\AbstractConfigFactory',
]
])
);

$sm->setService('Config', $this->config);
AbstractConfigFactory::class,
],
'services' => [
'Config' => $this->config,
],
]);
}

/**
Expand Down Expand Up @@ -118,8 +116,8 @@ public function testCanCreateService()
$factory = new AbstractConfigFactory();
$serviceLocator = $this->serviceManager;

$this->assertFalse($factory->canCreateServiceWithName($serviceLocator, 'mymodulefail', 'MyModule\Fail'));
$this->assertTrue($factory->canCreateServiceWithName($serviceLocator, 'mymoduleconfig', 'MyModule\Config'));
$this->assertFalse($factory->canCreateServiceWithName($serviceLocator, 'MyModule\Fail'));
$this->assertTrue($factory->canCreateServiceWithName($serviceLocator, 'MyModule\Config'));
}

/**
Expand Down
19 changes: 13 additions & 6 deletions test/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@

namespace ZendTest\Config;

use Interop\Container\ContainerInterface;
use Zend\Config\Factory;
use Zend\Config\ReaderPluginManager;
use Zend\Config\WriterPluginManager;

/**
* @group Zend_Config
Expand Down Expand Up @@ -168,9 +171,11 @@ public function testFactoryCanRegisterCustomReaderInstance()

public function testFactoryCanRegisterCustomReaderPlugin()
{
$dummyReader = new Reader\TestAssets\DummyReader();
Factory::getReaderPluginManager()->setService('DummyReader', $dummyReader);

$services = $this->prophesize(ContainerInterface::class);
$pluginManager = new ReaderPluginManager($services->reveal(), ['services' => [
'DummyReader' => new Reader\TestAssets\DummyReader(),
]]);
Factory::setReaderPluginManager($pluginManager);
Factory::registerReader('dum', 'DummyReader');

$configObject = Factory::fromFile(__DIR__ . '/TestAssets/dummy.dum', true);
Expand Down Expand Up @@ -237,9 +242,11 @@ public function testFactoryCanRegisterCustomWriterInstance()

public function testFactoryCanRegisterCustomWriterPlugin()
{
$dummyWriter = new Writer\TestAssets\DummyWriter();
Factory::getWriterPluginManager()->setService('DummyWriter', $dummyWriter);

$services = $this->prophesize(ContainerInterface::class);
$pluginManager = new WriterPluginManager($services->reveal(), ['services' => [
'DummyWriter' => new Writer\TestAssets\DummyWriter(),
]]);
Factory::setWriterPluginManager($pluginManager);
Factory::registerWriter('dum', 'DummyWriter');

$file = $this->getTestAssetFileName('dum');
Expand Down
Loading