Skip to content

Commit

Permalink
Merge pull request zendframework#3876 from Ocramius/feature/abstract-…
Browse files Browse the repository at this point in the history
…aggregate-listener

Implementing and re-utilizing an abstract aggregate listener
  • Loading branch information
weierophinney committed Mar 25, 2013
2 parents d665ce5 + 311c8a7 commit 127bef9
Show file tree
Hide file tree
Showing 42 changed files with 449 additions and 891 deletions.
4 changes: 3 additions & 1 deletion library/Zend/Cache/Storage/Plugin/AbstractPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

namespace Zend\Cache\Storage\Plugin;

abstract class AbstractPlugin implements PluginInterface
use Zend\EventManager\AbstractListenerAggregate;

abstract class AbstractPlugin extends AbstractListenerAggregate implements PluginInterface
{
/**
* @var PluginOptions
Expand Down
56 changes: 5 additions & 51 deletions library/Zend/Cache/Storage/Plugin/ClearExpiredByFactor.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,62 +17,16 @@
class ClearExpiredByFactor extends AbstractPlugin
{
/**
* Handles
*
* @var array
*/
protected $handles = array();

/**
* Attach
*
* @param EventManagerInterface $events
* @param int $priority
* @return ClearExpiredByFactor
* @throws Exception\LogicException
* {@inheritDoc}
*/
public function attach(EventManagerInterface $events, $priority = 1)
{
$index = spl_object_hash($events);
if (isset($this->handles[$index])) {
throw new Exception\LogicException('Plugin already attached');
}

$handles = array();
$this->handles[$index] = & $handles;

$callback = array($this, 'clearExpiredByFactor');
$handles[] = $events->attach('setItem.post', $callback, $priority);
$handles[] = $events->attach('setItems.post', $callback, $priority);
$handles[] = $events->attach('addItem.post', $callback, $priority);
$handles[] = $events->attach('addItems.post', $callback, $priority);

return $this;
}

/**
* Detach
*
* @param EventManagerInterface $events
* @return ClearExpiredByFactor
* @throws Exception\LogicException
*/
public function detach(EventManagerInterface $events)
{
$index = spl_object_hash($events);
if (!isset($this->handles[$index])) {
throw new Exception\LogicException('Plugin not attached');
}

// detach all handles of this index
foreach ($this->handles[$index] as $handle) {
$events->detach($handle);
}

// remove all detached handles
unset($this->handles[$index]);

return $this;
$this->listeners[] = $events->attach('setItem.post', $callback, $priority);
$this->listeners[] = $events->attach('setItems.post', $callback, $priority);
$this->listeners[] = $events->attach('addItem.post', $callback, $priority);
$this->listeners[] = $events->attach('addItems.post', $callback, $priority);
}

/**
Expand Down
90 changes: 22 additions & 68 deletions library/Zend/Cache/Storage/Plugin/ExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,92 +16,46 @@
class ExceptionHandler extends AbstractPlugin
{
/**
* Handles
*
* @var array
*/
protected $handles = array();

/**
* Attach
*
* @param EventManagerInterface $events
* @param int $priority
* @return ExceptionHandler
* @throws Exception\LogicException
* {@inheritDoc}
*/
public function attach(EventManagerInterface $events, $priority = 1)
{
$index = spl_object_hash($events);
if (isset($this->handles[$index])) {
throw new Exception\LogicException('Plugin already attached');
}

$callback = array($this, 'onException');
$handles = array();
$this->handles[$index] = & $handles;

// read
$handles[] = $events->attach('getItem.exception', $callback, $priority);
$handles[] = $events->attach('getItems.exception', $callback, $priority);
$this->listeners[] = $events->attach('getItem.exception', $callback, $priority);
$this->listeners[] = $events->attach('getItems.exception', $callback, $priority);

$handles[] = $events->attach('hasItem.exception', $callback, $priority);
$handles[] = $events->attach('hasItems.exception', $callback, $priority);
$this->listeners[] = $events->attach('hasItem.exception', $callback, $priority);
$this->listeners[] = $events->attach('hasItems.exception', $callback, $priority);

$handles[] = $events->attach('getMetadata.exception', $callback, $priority);
$handles[] = $events->attach('getMetadatas.exception', $callback, $priority);
$this->listeners[] = $events->attach('getMetadata.exception', $callback, $priority);
$this->listeners[] = $events->attach('getMetadatas.exception', $callback, $priority);

// write
$handles[] = $events->attach('setItem.exception', $callback, $priority);
$handles[] = $events->attach('setItems.exception', $callback, $priority);
$this->listeners[] = $events->attach('setItem.exception', $callback, $priority);
$this->listeners[] = $events->attach('setItems.exception', $callback, $priority);

$handles[] = $events->attach('addItem.exception', $callback, $priority);
$handles[] = $events->attach('addItems.exception', $callback, $priority);
$this->listeners[] = $events->attach('addItem.exception', $callback, $priority);
$this->listeners[] = $events->attach('addItems.exception', $callback, $priority);

$handles[] = $events->attach('replaceItem.exception', $callback, $priority);
$handles[] = $events->attach('replaceItems.exception', $callback, $priority);
$this->listeners[] = $events->attach('replaceItem.exception', $callback, $priority);
$this->listeners[] = $events->attach('replaceItems.exception', $callback, $priority);

$handles[] = $events->attach('touchItem.exception', $callback, $priority);
$handles[] = $events->attach('touchItems.exception', $callback, $priority);
$this->listeners[] = $events->attach('touchItem.exception', $callback, $priority);
$this->listeners[] = $events->attach('touchItems.exception', $callback, $priority);

$handles[] = $events->attach('removeItem.exception', $callback, $priority);
$handles[] = $events->attach('removeItems.exception', $callback, $priority);
$this->listeners[] = $events->attach('removeItem.exception', $callback, $priority);
$this->listeners[] = $events->attach('removeItems.exception', $callback, $priority);

$handles[] = $events->attach('checkAndSetItem.exception', $callback, $priority);
$this->listeners[] = $events->attach('checkAndSetItem.exception', $callback, $priority);

// increment / decrement item(s)
$handles[] = $events->attach('incrementItem.exception', $callback, $priority);
$handles[] = $events->attach('incrementItems.exception', $callback, $priority);

$handles[] = $events->attach('decrementItem.exception', $callback, $priority);
$handles[] = $events->attach('decrementItems.exception', $callback, $priority);

return $this;
}

/**
* Detach
*
* @param EventManagerInterface $events
* @return ExceptionHandler
* @throws Exception\LogicException
*/
public function detach(EventManagerInterface $events)
{
$index = spl_object_hash($events);
if (!isset($this->handles[$index])) {
throw new Exception\LogicException('Plugin not attached');
}

// detach all handles of this index
foreach ($this->handles[$index] as $handle) {
$events->detach($handle);
}

// remove all detached handles
unset($this->handles[$index]);
$this->listeners[] = $events->attach('incrementItem.exception', $callback, $priority);
$this->listeners[] = $events->attach('incrementItems.exception', $callback, $priority);

return $this;
$this->listeners[] = $events->attach('decrementItem.exception', $callback, $priority);
$this->listeners[] = $events->attach('decrementItems.exception', $callback, $priority);
}

/**
Expand Down
115 changes: 34 additions & 81 deletions library/Zend/Cache/Storage/Plugin/IgnoreUserAbort.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@

class IgnoreUserAbort extends AbstractPlugin
{
/**
* Handles
*
* @var array
*/
protected $handles = array();

/**
* The storage who activated ignore_user_abort.
*
Expand All @@ -30,97 +23,57 @@ class IgnoreUserAbort extends AbstractPlugin
protected $activatedTarget = null;

/**
* Attach
*
* @param EventManagerInterface $events
* @param int $priority
* @return Serializer
* @throws Exception\LogicException
* {@inheritDoc}
*/
public function attach(EventManagerInterface $events, $priority = 1)
{
$index = spl_object_hash($events);
if (isset($this->handles[$index])) {
throw new Exception\LogicException('Plugin already attached');
}

$handles = array();
$this->handles[$index] = & $handles;

$cbOnBefore = array($this, 'onBefore');
$cbOnAfter = array($this, 'onAfter');

$handles[] = $events->attach('setItem.pre', $cbOnBefore, $priority);
$handles[] = $events->attach('setItem.post', $cbOnAfter, $priority);
$handles[] = $events->attach('setItem.exception', $cbOnAfter, $priority);
$this->listeners[] = $events->attach('setItem.pre', $cbOnBefore, $priority);
$this->listeners[] = $events->attach('setItem.post', $cbOnAfter, $priority);
$this->listeners[] = $events->attach('setItem.exception', $cbOnAfter, $priority);

$handles[] = $events->attach('setItems.pre', $cbOnBefore, $priority);
$handles[] = $events->attach('setItems.post', $cbOnAfter, $priority);
$handles[] = $events->attach('setItems.exception', $cbOnAfter, $priority);
$this->listeners[] = $events->attach('setItems.pre', $cbOnBefore, $priority);
$this->listeners[] = $events->attach('setItems.post', $cbOnAfter, $priority);
$this->listeners[] = $events->attach('setItems.exception', $cbOnAfter, $priority);

$handles[] = $events->attach('addItem.pre', $cbOnBefore, $priority);
$handles[] = $events->attach('addItem.post', $cbOnAfter, $priority);
$handles[] = $events->attach('addItem.exception', $cbOnAfter, $priority);
$this->listeners[] = $events->attach('addItem.pre', $cbOnBefore, $priority);
$this->listeners[] = $events->attach('addItem.post', $cbOnAfter, $priority);
$this->listeners[] = $events->attach('addItem.exception', $cbOnAfter, $priority);

$handles[] = $events->attach('addItems.pre', $cbOnBefore, $priority);
$handles[] = $events->attach('addItems.post', $cbOnAfter, $priority);
$handles[] = $events->attach('addItems.exception', $cbOnAfter, $priority);
$this->listeners[] = $events->attach('addItems.pre', $cbOnBefore, $priority);
$this->listeners[] = $events->attach('addItems.post', $cbOnAfter, $priority);
$this->listeners[] = $events->attach('addItems.exception', $cbOnAfter, $priority);

$handles[] = $events->attach('replaceItem.pre', $cbOnBefore, $priority);
$handles[] = $events->attach('replaceItem.post', $cbOnAfter, $priority);
$handles[] = $events->attach('replaceItem.exception', $cbOnAfter, $priority);
$this->listeners[] = $events->attach('replaceItem.pre', $cbOnBefore, $priority);
$this->listeners[] = $events->attach('replaceItem.post', $cbOnAfter, $priority);
$this->listeners[] = $events->attach('replaceItem.exception', $cbOnAfter, $priority);

$handles[] = $events->attach('replaceItems.pre', $cbOnBefore, $priority);
$handles[] = $events->attach('replaceItems.post', $cbOnAfter, $priority);
$handles[] = $events->attach('replaceItems.exception', $cbOnAfter, $priority);
$this->listeners[] = $events->attach('replaceItems.pre', $cbOnBefore, $priority);
$this->listeners[] = $events->attach('replaceItems.post', $cbOnAfter, $priority);
$this->listeners[] = $events->attach('replaceItems.exception', $cbOnAfter, $priority);

$handles[] = $events->attach('checkAndSetItem.pre', $cbOnBefore, $priority);
$handles[] = $events->attach('checkAndSetItem.post', $cbOnAfter, $priority);
$handles[] = $events->attach('checkAndSetItem.exception', $cbOnAfter, $priority);
$this->listeners[] = $events->attach('checkAndSetItem.pre', $cbOnBefore, $priority);
$this->listeners[] = $events->attach('checkAndSetItem.post', $cbOnAfter, $priority);
$this->listeners[] = $events->attach('checkAndSetItem.exception', $cbOnAfter, $priority);

// increment / decrement item(s)
$handles[] = $events->attach('incrementItem.pre', $cbOnBefore, $priority);
$handles[] = $events->attach('incrementItem.post', $cbOnAfter, $priority);
$handles[] = $events->attach('incrementItem.exception', $cbOnAfter, $priority);

$handles[] = $events->attach('incrementItems.pre', $cbOnBefore, $priority);
$handles[] = $events->attach('incrementItems.post', $cbOnAfter, $priority);
$handles[] = $events->attach('incrementItems.exception', $cbOnAfter, $priority);

$handles[] = $events->attach('decrementItem.pre', $cbOnBefore, $priority);
$handles[] = $events->attach('decrementItem.post', $cbOnAfter, $priority);
$handles[] = $events->attach('decrementItem.exception', $cbOnAfter, $priority);
$this->listeners[] = $events->attach('incrementItem.pre', $cbOnBefore, $priority);
$this->listeners[] = $events->attach('incrementItem.post', $cbOnAfter, $priority);
$this->listeners[] = $events->attach('incrementItem.exception', $cbOnAfter, $priority);

$handles[] = $events->attach('decrementItems.pre', $cbOnBefore, $priority);
$handles[] = $events->attach('decrementItems.post', $cbOnAfter, $priority);
$handles[] = $events->attach('decrementItems.exception', $cbOnAfter, $priority);

return $this;
}

/**
* Detach
*
* @param EventManagerInterface $events
* @return Serializer
* @throws Exception\LogicException
*/
public function detach(EventManagerInterface $events)
{
$index = spl_object_hash($events);
if (!isset($this->handles[$index])) {
throw new Exception\LogicException('Plugin not attached');
}

// detach all handles of this index
foreach ($this->handles[$index] as $handle) {
$events->detach($handle);
}
$this->listeners[] = $events->attach('incrementItems.pre', $cbOnBefore, $priority);
$this->listeners[] = $events->attach('incrementItems.post', $cbOnAfter, $priority);
$this->listeners[] = $events->attach('incrementItems.exception', $cbOnAfter, $priority);

// remove all detached handles
unset($this->handles[$index]);
$this->listeners[] = $events->attach('decrementItem.pre', $cbOnBefore, $priority);
$this->listeners[] = $events->attach('decrementItem.post', $cbOnAfter, $priority);
$this->listeners[] = $events->attach('decrementItem.exception', $cbOnAfter, $priority);

return $this;
$this->listeners[] = $events->attach('decrementItems.pre', $cbOnBefore, $priority);
$this->listeners[] = $events->attach('decrementItems.post', $cbOnAfter, $priority);
$this->listeners[] = $events->attach('decrementItems.exception', $cbOnAfter, $priority);
}

/**
Expand Down
Loading

0 comments on commit 127bef9

Please sign in to comment.