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

[WIP] ServiceManager v2 + v3 compatibility #64

Closed
Closed
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
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
},
"require": {
"php": ">=5.5",
"zendframework/zend-stdlib": "~2.5",
"zendframework/zend-servicemanager": "dev-develop as 2.7.0",
"zendframework/zend-eventmanager": "dev-develop as 2.7.0"
"zendframework/zend-stdlib": "~2.7",
"zendframework/zend-servicemanager": "^2.7.3 || ^3.0",
"zendframework/zend-eventmanager": "^2.6.2 || ^3.0"
Copy link
Member

Choose a reason for hiding this comment

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

w00t! we can use tagged versions!

},
"require-dev": {
"zendframework/zend-serializer": "dev-develop as 2.6.0",
"zendframework/zend-session": "~2.5",
"zendframework/zend-session": "dev-develop as 2.6.0",
Copy link
Member

Choose a reason for hiding this comment

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

Note to future selves: we'll want to update this to a tagged version once we've tagged a version containing the SM/EM updates.

"fabpot/php-cs-fixer": "1.7.*",
"phpunit/PHPUnit": "~4.0"
},
Expand Down
43 changes: 42 additions & 1 deletion src/PatternPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

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

/**
* Plugin manager implementation for cache pattern adapters
Expand Down Expand Up @@ -42,12 +43,52 @@ class PatternPluginManager extends AbstractPluginManager
/**
* Don't share by default
*
* @var array
* @var boolean
*/
protected $shareByDefault = false;

/**
* Don't share by default
*
* @var boolean
Copy link
Member

Choose a reason for hiding this comment

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

@ezimuel can you please write in the doc-block which property belongs to which service manager version as for me this looks like a mistake.

*/
protected $sharedByDefault = false;

/**
* @var string
*/
protected $instanceOf = Pattern\PatternInterface::class;

/**
* Validate the plugin is of the expected type (v3).
*
* Validates against `$instanceOf`.
*
* @param mixed $instance
* @throws InvalidServiceException
*/
public function validate($instance)
{
if (! $instance instanceof $this->instanceOf) {
throw new InvalidServiceException(sprintf(
'%s can only create instances of %s; %s is invalid',
get_class($this),
$this->instanceOf,
(is_object($instance) ? get_class($instance) : gettype($instance))
));
}
}

/**
* Validate the plugin is of the expected type (v2).
*
* Proxies to `validate()`.
*
* @param mixed $instance
* @throws InvalidServiceException
*/
public function validatePlugin($instance)
{
$this->validate($instance);
}
}
17 changes: 16 additions & 1 deletion src/Service/StorageCacheAbstractServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class StorageCacheAbstractServiceFactory implements AbstractFactoryInterface
* @param string $requestedName
* @return boolean
*/
public function canCreateServiceWithName(ContainerInterface $container, $requestedName)
public function canCreate(ContainerInterface $container, $requestedName)
{
$config = $this->getConfig($container);
if (empty($config)) {
Expand All @@ -44,6 +44,16 @@ public function canCreateServiceWithName(ContainerInterface $container, $request
return (isset($config[$requestedName]) && is_array($config[$requestedName]));
}

/**
* @param ServiceLocatorInterface $serviceLocator
* @param string $name
* @param string $requestedName
* @return boolean
*/
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
return $this->canCreate($serviceLocator, $requestedName);
}
Copy link
Member

Choose a reason for hiding this comment

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

Add an empty line following this new method. (CS)

/**
* Create an object
*
Expand All @@ -58,6 +68,11 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o
return StorageFactory::factory($config[$requestedName]);
}

public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
return $this($serviceLocator, $requestedName);
}

/**
* Retrieve cache configuration, if any
*
Expand Down