Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.
Open
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
Prev Previous commit
Replaced ArrayObject with plain arrays
  • Loading branch information
marc-mabe committed Apr 19, 2016
commit f3d97933a004200e274a45fa1f29a03f4a06f1b1
382 changes: 200 additions & 182 deletions src/Storage/Adapter/AbstractAdapter.php

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions src/Storage/Adapter/AdapterOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

namespace Zend\Cache\Storage\Adapter;

use ArrayObject;
use Zend\Cache\Exception;
use Zend\Cache\Storage\Event;
use Zend\Cache\Storage\StorageInterface;
Expand Down Expand Up @@ -242,7 +241,7 @@ public function getWritable()
protected function triggerOptionEvent($optionName, $optionValue)
{
if ($this->adapter instanceof EventsCapableInterface) {
$event = new Event('option', $this->adapter, new ArrayObject([$optionName => $optionValue]));
$event = new Event('option', $this->adapter, [$optionName => $optionValue]);
$this->adapter->getEventManager()->triggerEvent($event);
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/Storage/Adapter/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
use Zend\Cache\Storage\TaggableInterface;
use Zend\Cache\Storage\TotalSpaceCapableInterface;
use Zend\Stdlib\ErrorHandler;
use ArrayObject;

class Filesystem extends AbstractAdapter implements
AvailableSpaceCapableInterface,
Expand Down Expand Up @@ -166,7 +165,7 @@ public function clearExpired()
$result = false;
return $this->triggerException(
__FUNCTION__,
new ArrayObject(),
[],
$result,
new Exception\RuntimeException('Failed to clear expired items', 0, $error)
);
Expand Down
4 changes: 1 addition & 3 deletions src/Storage/Capabilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -568,9 +568,7 @@ protected function setCapability(stdClass $marker, $property, $value)

// trigger event
if ($this->storage instanceof EventsCapableInterface) {
$this->storage->getEventManager()->trigger('capability', $this->storage, new ArrayObject([
$property => $value
]));
$this->storage->getEventManager()->trigger('capability', $this->storage, [$property => $value]);
}
}

Expand Down
38 changes: 35 additions & 3 deletions src/Storage/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

namespace Zend\Cache\Storage;

use ArrayObject;
use Zend\EventManager\Event as BaseEvent;

class Event extends BaseEvent
Expand All @@ -21,9 +20,9 @@ class Event extends BaseEvent
*
* @param string $name Event name
* @param StorageInterface $storage
* @param ArrayObject $params
* @param array $params
*/
public function __construct($name, StorageInterface $storage, ArrayObject $params)
public function __construct($name, StorageInterface $storage, array $params = null)
{
parent::__construct($name, $storage, $params);
}
Expand Down Expand Up @@ -62,4 +61,37 @@ public function getStorage()
{
return $this->getTarget();
}

/**
* {@inheritdoc}
*
* Overwritten for performance reasons as the storage adapter events will handle
* params as plain arrays only.
*
* @param string $name
* @param mixed $default
* @return mixed
*/
public function getParam($name, $default = null)
{
if (array_key_exists($name, $this->params)) {
return $this->params[$name];
}

return $default;
}

/**
* {@inheritdoc}
*
* Overwritten for performance reasons as the storage adapter events will handle
* params as plain arrays only.
*
* @param string $name
* @param mixed $value
*/
public function setParam($name, $value)
{
$this->params[$name] = $value;
}
}
9 changes: 4 additions & 5 deletions src/Storage/ExceptionEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

namespace Zend\Cache\Storage;

use ArrayObject;
use Exception;

class ExceptionEvent extends PostEvent
Expand All @@ -33,13 +32,13 @@ class ExceptionEvent extends PostEvent
*
* Accept a target and its parameters.
*
* @param string $name
* @param string $name
* @param StorageInterface $storage
* @param ArrayObject $params
* @param mixed $result
* @param array $params
* @param mixed $result
* @param Exception $exception
*/
public function __construct($name, StorageInterface $storage, ArrayObject $params, $result, Exception $exception)
public function __construct($name, StorageInterface $storage, array $params, $result, Exception $exception)
{
parent::__construct($name, $storage, $params, $result);
$this->setException($exception);
Expand Down
11 changes: 4 additions & 7 deletions src/Storage/Plugin/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ public function onReadItemsPost(PostEvent $event)
public function onWriteItemPre(Event $event)
{
$serializer = $this->getOptions()->getSerializer();
$params = $event->getParams();
$params['value'] = $serializer->serialize($params['value']);
$event->setParam('value', $serializer->serialize($event->getParam('value')));
}

/**
Expand All @@ -107,11 +106,9 @@ public function onWriteItemPre(Event $event)
*/
public function onWriteItemsPre(Event $event)
{
$serializer = $this->getOptions()->getSerializer();
$params = $event->getParams();
foreach ($params['keyValuePairs'] as &$value) {
$value = $serializer->serialize($value);
}
$serializer = $this->getOptions()->getSerializer();
$keyValuePairs = array_map([$serializer, 'serialize'], $event->getParam('keyValuePairs'));
$event->setParam('keyValuePairs', $keyValuePairs);
}

/**
Expand Down
6 changes: 2 additions & 4 deletions src/Storage/PostEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

namespace Zend\Cache\Storage;

use ArrayObject;

class PostEvent extends Event
{
/**
Expand All @@ -27,10 +25,10 @@ class PostEvent extends Event
*
* @param string $name
* @param StorageInterface $storage
* @param ArrayObject $params
* @param array $params
* @param mixed $result
*/
public function __construct($name, StorageInterface $storage, ArrayObject $params, $result)
public function __construct($name, StorageInterface $storage, array $params, $result)
{
parent::__construct($name, $storage, $params);
$this->setResult($result);
Expand Down
21 changes: 9 additions & 12 deletions test/Storage/Adapter/AbstractAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,18 @@ public function testPluginRegistry()
$this->assertEquals(0, count($this->_storage->getPluginRegistry()));
$this->assertEquals(0, count($plugin->getHandles()));
}

/*
public function testInternalTriggerPre()
{
$this->_storage = $this->getMockForAbstractAdapter();

$plugin = new \ZendTest\Cache\Storage\TestAsset\MockPlugin();
$this->_storage->addPlugin($plugin);

$params = new \ArrayObject([
$params = [
'key' => 'key1',
'value' => 'value1'
]);
];

// call protected method
$method = new \ReflectionMethod(get_class($this->_storage), 'triggerPre');
Expand All @@ -174,18 +174,18 @@ public function testInternalTriggerPre()
$this->assertSame($this->_storage, $event->getTarget());
$this->assertSame($params, $event->getParams());
}

*/
public function testInternalTriggerPost()
{
$this->_storage = $this->getMockForAbstractAdapter();

$plugin = new \ZendTest\Cache\Storage\TestAsset\MockPlugin();
$this->_storage->addPlugin($plugin);

$params = new \ArrayObject([
$params = [
'key' => 'key1',
'value' => 'value1'
]);
];
$result = true;

// call protected method
Expand Down Expand Up @@ -216,10 +216,10 @@ public function testInternalTriggerExceptionThrowRuntimeException()
$this->_storage->addPlugin($plugin);

$result = null;
$params = new \ArrayObject([
$params = [
'key' => 'key1',
'value' => 'value1'
]);
];

// call protected method
$method = new \ReflectionMethod(get_class($this->_storage), 'triggerException');
Expand Down Expand Up @@ -1053,10 +1053,7 @@ protected function checkPreEventCanChangeArguments($method, array $args, array $
// init mock
$this->_storage = $this->getMockForAbstractAdapter([$internalMethod]);
$this->_storage->getEventManager()->attach($eventName, function ($event) use ($expectedArgs) {
$params = $event->getParams();
foreach ($expectedArgs as $k => $v) {
$params[$k] = $v;
}
$event->setParams($expectedArgs);
});

// set expected arguments of internal method call
Expand Down
2 changes: 1 addition & 1 deletion test/Storage/Adapter/AdapterOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public function testTriggerOptionEvent()
// assert (hopefully) called listener and arguments
$this->assertCount(1, $calledArgs, '"option" event was not triggered or got a wrong number of arguments');
$this->assertInstanceOf(Event::class, $calledArgs[0]);
$this->assertEquals(['writable' => false], $calledArgs[0]->getParams()->getArrayCopy());
$this->assertEquals(['writable' => false], $calledArgs[0]->getParams());
}

public function testSetFromArrayWithoutPrioritizedOptions()
Expand Down
2 changes: 1 addition & 1 deletion test/Storage/Adapter/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ public function testClearExpiredExceptionTriggersEvent()
}
chmod($dirs[0], 0500); //make directory rx, unlink should fail
sleep(1); //wait for the entry to expire
$plugin = new ExceptionHandler();
$plugin = new ExceptionHandler();
$options = new PluginOptions(['throw_exceptions' => false]);
$plugin->setOptions($options);
$this->_storage->addPlugin($plugin);
Expand Down
1 change: 0 additions & 1 deletion test/Storage/CapabilitiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ public function testTriggerCapabilityEvent()
$this->assertSame($this->_adapter, $event->getTarget());

$params = $event->getParams();
$this->assertInstanceOf('ArrayObject', $params);
$this->assertTrue(isset($params ['maxTtl']));
$this->assertEquals(100, $params['maxTtl']);
}
Expand Down
5 changes: 2 additions & 3 deletions test/Storage/Plugin/ClearExpiredByFactorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

namespace ZendTest\Cache\Storage\Plugin;

use ArrayObject;
use Zend\Cache;
use Zend\Cache\Storage\PostEvent;
use Zend\EventManager\Test\EventListenerIntrospectionTrait;
Expand Down Expand Up @@ -89,9 +88,9 @@ public function testClearExpiredByFactor()

// call event callback
$result = true;
$event = new PostEvent('setItem.post', $adapter, new ArrayObject([
$event = new PostEvent('setItem.post', $adapter, [
'options' => [],
]), $result);
], $result);
$this->_plugin->clearExpiredByFactor($event);

$this->assertTrue($event->getResult());
Expand Down
9 changes: 4 additions & 5 deletions test/Storage/Plugin/ExceptionHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

namespace ZendTest\Cache\Storage\Plugin;

use ArrayObject;
use Zend\Cache;
use Zend\Cache\Storage\ExceptionEvent;
use Zend\EventManager\Test\EventListenerIntrospectionTrait;
Expand Down Expand Up @@ -114,10 +113,10 @@ public function testOnExceptionCallCallback()

// run onException
$result = null;
$event = new ExceptionEvent('getItem.exception', $this->_adapter, new ArrayObject([
$event = new ExceptionEvent('getItem.exception', $this->_adapter, [
'key' => 'key',
'options' => []
]), $result, $expectedException);
], $result, $expectedException);
$this->_plugin->onException($event);

$this->assertTrue(
Expand All @@ -132,10 +131,10 @@ public function testDontThrowException()

// run onException
$result = 'test';
$event = new ExceptionEvent('getItem.exception', $this->_adapter, new ArrayObject([
$event = new ExceptionEvent('getItem.exception', $this->_adapter, [
'key' => 'key',
'options' => []
]), $result, new \Exception());
], $result, new \Exception());
$this->_plugin->onException($event);

$this->assertFalse($event->getThrowException());
Expand Down
5 changes: 2 additions & 3 deletions test/Storage/Plugin/OptimizeByFactorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

namespace ZendTest\Cache\Storage\Plugin;

use ArrayObject;
use Zend\Cache;
use Zend\Cache\Storage\PostEvent;
use Zend\EventManager\Test\EventListenerIntrospectionTrait;
Expand Down Expand Up @@ -83,9 +82,9 @@ public function testOptimizeByFactor()

// call event callback
$result = true;
$event = new PostEvent('removeItem.post', $adapter, new ArrayObject([
$event = new PostEvent('removeItem.post', $adapter, [
'options' => []
]), $result);
], $result);

$this->_plugin->optimizeByFactor($event);

Expand Down
Loading