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

Commit 9d439ee

Browse files
committed
Merge branch 'kynx-refactor-service-event-manager' into develop
2 parents fa3cd9f + 8732361 commit 9d439ee

12 files changed

+208
-107
lines changed

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,16 @@
1414
},
1515
"require": {
1616
"php": ">=5.5",
17-
"zendframework/zend-eventmanager": "~2.5",
17+
"zendframework/zend-eventmanager": "dev-develop as 2.7",
1818
"zendframework/zend-stdlib": "~2.5"
1919
},
2020
"require-dev": {
2121
"zendframework/zend-cache": "~2.5",
2222
"zendframework/zend-db": "~2.5",
2323
"zendframework/zend-http": "~2.5",
24-
"zendframework/zend-servicemanager": "~2.5",
24+
"zendframework/zend-servicemanager": "dev-develop as 2.5",
2525
"zendframework/zend-validator": "~2.5",
26+
"container-interop/container-interop": "^1.1",
2627
"fabpot/php-cs-fixer": "1.7.*",
2728
"phpunit/PHPUnit": "~4.0"
2829
},
@@ -47,4 +48,4 @@
4748
"ZendTest\\Session\\": "test/"
4849
}
4950
}
50-
}
51+
}

src/Service/ContainerAbstractServiceFactory.php

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
namespace Zend\Session\Service;
1111

12-
use Zend\ServiceManager\AbstractFactoryInterface;
13-
use Zend\ServiceManager\ServiceLocatorInterface;
12+
use Interop\Container\ContainerInterface;
13+
use Zend\ServiceManager\Factory\AbstractFactoryInterface;
1414
use Zend\Session\Container;
1515

1616
/**
@@ -56,14 +56,13 @@ class ContainerAbstractServiceFactory implements AbstractFactoryInterface
5656
protected $sessionManager;
5757

5858
/**
59-
* @param ServiceLocatorInterface $services
60-
* @param string $name
59+
* @param ContainerInterface $container
6160
* @param string $requestedName
6261
* @return bool
6362
*/
64-
public function canCreateServiceWithName(ServiceLocatorInterface $services, $name, $requestedName)
63+
public function canCreateServiceWithName(ContainerInterface $container, $requestedName)
6564
{
66-
$config = $this->getConfig($services);
65+
$config = $this->getConfig($container);
6766
if (empty($config)) {
6867
return false;
6968
}
@@ -73,35 +72,35 @@ public function canCreateServiceWithName(ServiceLocatorInterface $services, $nam
7372
}
7473

7574
/**
76-
* @param ServiceLocatorInterface $services
77-
* @param string $name
75+
* @param ContainerInterface $container
7876
* @param string $requestedName
7977
* @return Container
8078
*/
81-
public function createServiceWithName(ServiceLocatorInterface $services, $name, $requestedName)
79+
80+
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
8281
{
83-
$manager = $this->getSessionManager($services);
82+
$manager = $this->getSessionManager($container);
8483
return new Container($requestedName, $manager);
8584
}
8685

8786
/**
8887
* Retrieve config from service locator, and cache for later
8988
*
90-
* @param ServiceLocatorInterface $services
89+
* @param ContainerInterface $container
9190
* @return false|array
9291
*/
93-
protected function getConfig(ServiceLocatorInterface $services)
92+
protected function getConfig(ContainerInterface $container)
9493
{
9594
if (null !== $this->config) {
9695
return $this->config;
9796
}
9897

99-
if (!$services->has('Config')) {
98+
if (!$container->has('config')) {
10099
$this->config = [];
101100
return $this->config;
102101
}
103102

104-
$config = $services->get('Config');
103+
$config = $container->get('config');
105104
if (!isset($config[$this->configKey]) || !is_array($config[$this->configKey])) {
106105
$this->config = [];
107106
return $this->config;
@@ -118,17 +117,17 @@ protected function getConfig(ServiceLocatorInterface $services)
118117
/**
119118
* Retrieve the session manager instance, if any
120119
*
121-
* @param ServiceLocatorInterface $services
120+
* @param ContainerInterface $container
122121
* @return null|\Zend\Session\ManagerInterface
123122
*/
124-
protected function getSessionManager(ServiceLocatorInterface $services)
123+
protected function getSessionManager(ContainerInterface $container)
125124
{
126125
if ($this->sessionManager !== null) {
127126
return $this->sessionManager;
128127
}
129128

130-
if ($services->has('Zend\Session\ManagerInterface')) {
131-
$this->sessionManager = $services->get('Zend\Session\ManagerInterface');
129+
if ($container->has('Zend\Session\ManagerInterface')) {
130+
$this->sessionManager = $container->get('Zend\Session\ManagerInterface');
132131
}
133132

134133
return $this->sessionManager;

src/Service/SessionConfigFactory.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
namespace Zend\Session\Service;
1111

12+
use Interop\Container\ContainerInterface;
1213
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
13-
use Zend\ServiceManager\FactoryInterface;
14-
use Zend\ServiceManager\ServiceLocatorInterface;
14+
use Zend\ServiceManager\Factory\FactoryInterface;
1515
use Zend\Session\Config\ConfigInterface;
1616

1717
class SessionConfigFactory implements FactoryInterface
@@ -24,14 +24,14 @@ class SessionConfigFactory implements FactoryInterface
2424
* you may also specify a specific implementation variant using the
2525
* "config_class" subkey.
2626
*
27-
* @param ServiceLocatorInterface $services
27+
* @param ContainerInterface $container
2828
* @return ConfigInterface
2929
* @throws ServiceNotCreatedException if session_config is missing, or an
3030
* invalid config_class is used
3131
*/
32-
public function createService(ServiceLocatorInterface $services)
32+
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
3333
{
34-
$config = $services->get('Config');
34+
$config = $container->get('config');
3535
if (!isset($config['session_config']) || !is_array($config['session_config'])) {
3636
throw new ServiceNotCreatedException(
3737
'Configuration is missing a "session_config" key, or the value of that key is not an array'

src/Service/SessionManagerFactory.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
namespace Zend\Session\Service;
1111

12+
use Interop\Container\ContainerInterface;
1213
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
13-
use Zend\ServiceManager\FactoryInterface;
14-
use Zend\ServiceManager\ServiceLocatorInterface;
14+
use Zend\ServiceManager\Factory\FactoryInterface;
1515
use Zend\Session\Config\ConfigInterface;
1616
use Zend\Session\Container;
1717
use Zend\Session\SaveHandler\SaveHandlerInterface;
@@ -55,21 +55,21 @@ class SessionManagerFactory implements FactoryInterface
5555
* this is true; set it to false to disable.
5656
* - validators: ...
5757
*
58-
* @param ServiceLocatorInterface $services
58+
* @param ContainerInterface $container
59+
* @param string $requestedName
60+
* @param array $options
5961
* @return SessionManager
60-
* @throws ServiceNotCreatedException if any collaborators are not of the
61-
* correct type
6262
*/
63-
public function createService(ServiceLocatorInterface $services)
63+
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
6464
{
6565
$config = null;
6666
$storage = null;
6767
$saveHandler = null;
6868
$validators = [];
6969
$managerConfig = $this->defaultManagerConfig;
7070

71-
if ($services->has('Zend\Session\Config\ConfigInterface')) {
72-
$config = $services->get('Zend\Session\Config\ConfigInterface');
71+
if ($container->has('Zend\Session\Config\ConfigInterface')) {
72+
$config = $container->get('Zend\Session\Config\ConfigInterface');
7373
if (!$config instanceof ConfigInterface) {
7474
throw new ServiceNotCreatedException(sprintf(
7575
'SessionManager requires that the %s service implement %s; received "%s"',
@@ -80,8 +80,8 @@ public function createService(ServiceLocatorInterface $services)
8080
}
8181
}
8282

83-
if ($services->has('Zend\Session\Storage\StorageInterface')) {
84-
$storage = $services->get('Zend\Session\Storage\StorageInterface');
83+
if ($container->has('Zend\Session\Storage\StorageInterface')) {
84+
$storage = $container->get('Zend\Session\Storage\StorageInterface');
8585
if (!$storage instanceof StorageInterface) {
8686
throw new ServiceNotCreatedException(sprintf(
8787
'SessionManager requires that the %s service implement %s; received "%s"',
@@ -92,8 +92,8 @@ public function createService(ServiceLocatorInterface $services)
9292
}
9393
}
9494

95-
if ($services->has('Zend\Session\SaveHandler\SaveHandlerInterface')) {
96-
$saveHandler = $services->get('Zend\Session\SaveHandler\SaveHandlerInterface');
95+
if ($container->has('Zend\Session\SaveHandler\SaveHandlerInterface')) {
96+
$saveHandler = $container->get('Zend\Session\SaveHandler\SaveHandlerInterface');
9797
if (!$saveHandler instanceof SaveHandlerInterface) {
9898
throw new ServiceNotCreatedException(sprintf(
9999
'SessionManager requires that the %s service implement %s; received "%s"',
@@ -105,8 +105,8 @@ public function createService(ServiceLocatorInterface $services)
105105
}
106106

107107
// Get session manager configuration, if any, and merge with default configuration
108-
if ($services->has('Config')) {
109-
$configService = $services->get('Config');
108+
if ($container->has('config')) {
109+
$configService = $container->get('config');
110110
if (isset($configService['session_manager'])
111111
&& is_array($configService['session_manager'])
112112
) {

src/Service/StorageFactory.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
namespace Zend\Session\Service;
1111

12+
use Interop\Container\ContainerInterface;
1213
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
13-
use Zend\ServiceManager\FactoryInterface;
14-
use Zend\ServiceManager\ServiceLocatorInterface;
14+
use Zend\ServiceManager\Factory\FactoryInterface;
1515
use Zend\Session\Exception\ExceptionInterface as SessionException;
1616
use Zend\Session\Storage\Factory;
1717
use Zend\Session\Storage\StorageInterface;
@@ -26,14 +26,14 @@ class StorageFactory implements FactoryInterface
2626
* type to use, and optionally "options", containing any options to be used in
2727
* creating the StorageInterface instance.
2828
*
29-
* @param ServiceLocatorInterface $services
29+
* @param ContainerInterface $container
3030
* @return StorageInterface
3131
* @throws ServiceNotCreatedException if session_storage is missing, or the
3232
* factory cannot create the storage instance.
3333
*/
34-
public function createService(ServiceLocatorInterface $services)
34+
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
3535
{
36-
$config = $services->get('Config');
36+
$config = $container->get('config');
3737
if (!isset($config['session_storage']) || !is_array($config['session_storage'])) {
3838
throw new ServiceNotCreatedException(
3939
'Configuration is missing a "session_storage" key, or the value of that key is not an array'

src/SessionManager.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,9 +372,10 @@ public function getValidatorChain()
372372
public function isValid()
373373
{
374374
$validator = $this->getValidatorChain();
375-
$responses = $validator->trigger('session.validate', $this, [$this], function ($test) {
375+
$falseResult = function ($test) {
376376
return false === $test;
377-
});
377+
};
378+
$responses = $validator->triggerUntil($falseResult, 'session.validate', $this, [$this]);
378379
if ($responses->stopped()) {
379380
// If execution was halted, validation failed
380381
return false;

src/ValidatorChain.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
*/
99
namespace Zend\Session;
1010

11+
use Zend\EventManager\Event;
1112
use Zend\EventManager\EventManager;
13+
use Zend\EventManager\SharedEventManagerInterface;
1214
use Zend\Session\Storage\StorageInterface as Storage;
1315
use Zend\Session\Validator\ValidatorInterface as Validator;
1416

@@ -22,17 +24,25 @@ class ValidatorChain extends EventManager
2224
*/
2325
protected $storage;
2426

27+
/**
28+
* @var Event
29+
*/
30+
protected $eventPrototype;
31+
2532
/**
2633
* Construct the validation chain
2734
*
2835
* Retrieves validators from session storage and attaches them.
2936
*
3037
* @param Storage $storage
38+
* @param SharedEventManagerInterface $sharedEventManager
39+
* @param array $identifiers
3140
*/
32-
public function __construct(Storage $storage)
41+
public function __construct(Storage $storage, SharedEventManagerInterface $sharedEventManager = null, array $identifiers = [])
3342
{
34-
$this->storage = $storage;
43+
parent::__construct($sharedEventManager, $identifiers);
3544

45+
$this->storage = $storage;
3646
$validators = $storage->getMetadata('_VALID');
3747
if ($validators) {
3848
foreach ($validators as $validator => $data) {
@@ -44,12 +54,12 @@ public function __construct(Storage $storage)
4454
/**
4555
* Attach a listener to the session validator chain
4656
*
47-
* @param string $event
57+
* @param string $eventName
4858
* @param callable $callback
4959
* @param int $priority
5060
* @return \Zend\Stdlib\CallbackHandler
5161
*/
52-
public function attach($event, $callback = null, $priority = 1)
62+
public function attach($eventName, callable $callback, $priority = 1)
5363
{
5464
$context = null;
5565
if ($callback instanceof Validator) {
@@ -67,7 +77,7 @@ public function attach($event, $callback = null, $priority = 1)
6777
$this->getStorage()->setMetadata('_VALID', [$name => $data]);
6878
}
6979

70-
$listener = parent::attach($event, $callback, $priority);
80+
$listener = parent::attach($eventName, $callback, $priority);
7181
return $listener;
7282
}
7383

test/SaveHandler/CacheTest.php

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99

1010
namespace ZendTest\Session\SaveHandler;
1111

12+
use Prophecy\Argument;
1213
use Zend\Session\SaveHandler\Cache;
13-
use Zend\Cache\StorageFactory as CacheFactory;
14-
use Zend\Cache\Storage\Adapter\AdapterInterface as CacheAdapter;
1514

1615
/**
1716
* Unit testing for DbTable include all tests for
@@ -43,13 +42,19 @@ class CacheTest extends \PHPUnit_Framework_TestCase
4342

4443
public function setUp()
4544
{
46-
$this->cache = CacheFactory::adapterFactory('memory', ['memory_limit' => 0]);
4745
$this->testArray = ['foo' => 'bar', 'bar' => ['foo' => 'bar']];
4846
}
4947

5048
public function testReadWrite()
5149
{
52-
$this->usedSaveHandlers[] = $saveHandler = new Cache($this->cache);
50+
$cacheStorage = $this->prophesize('Zend\Cache\Storage\StorageInterface');
51+
$cacheStorage->setItem('242', Argument::type('string'))
52+
->will(function ($args) {
53+
$this->getItem('242')->willReturn($args[1]);
54+
return true;
55+
}
56+
);
57+
$this->usedSaveHandlers[] = $saveHandler = new Cache($cacheStorage->reveal());
5358

5459
$id = '242';
5560

@@ -61,7 +66,13 @@ public function testReadWrite()
6166

6267
public function testReadWriteComplex()
6368
{
64-
$this->usedSaveHandlers[] = $saveHandler = new Cache($this->cache);
69+
$cacheStorage = $this->prophesize('Zend\Cache\Storage\StorageInterface');
70+
$cacheStorage->setItem('242', Argument::type('string'))
71+
->will(function ($args) {
72+
$this->getItem('242')->willReturn($args[1]);
73+
return true;
74+
});
75+
$this->usedSaveHandlers[] = $saveHandler = new Cache($cacheStorage->reveal());
6576
$saveHandler->open('savepath', 'sessionname');
6677

6778
$id = '242';
@@ -73,7 +84,14 @@ public function testReadWriteComplex()
7384

7485
public function testReadWriteTwice()
7586
{
76-
$this->usedSaveHandlers[] = $saveHandler = new Cache($this->cache);
87+
$cacheStorage = $this->prophesize('Zend\Cache\Storage\StorageInterface');
88+
$cacheStorage->setItem('242', Argument::type('string'))
89+
->will(function ($args) {
90+
$this->getItem('242')->willReturn($args[1])->shouldBeCalledTimes(2);
91+
return true;
92+
})
93+
->shouldBeCalledTimes(2);
94+
$this->usedSaveHandlers[] = $saveHandler = new Cache($cacheStorage->reveal());
7795

7896
$id = '242';
7997

0 commit comments

Comments
 (0)