Skip to content

Commit

Permalink
Merge branch 'cache_ignoreUserAbort' of https://github.com/marc-mabe/zf2
Browse files Browse the repository at this point in the history
 into feature/cache-plugin-ignoreuserabort
  • Loading branch information
weierophinney committed Jan 3, 2012
2 parents 2673592 + da4e321 commit 8427ea5
Show file tree
Hide file tree
Showing 5 changed files with 354 additions and 12 deletions.
183 changes: 183 additions & 0 deletions library/Zend/Cache/Storage/Plugin/IgnoreUserAbort.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Cache
* @subpackage Storage
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Cache\Storage\Plugin;

use Zend\Cache\Storage\Adapter,
Zend\Cache\Storage\Event,
Zend\EventManager\EventCollection;

/**
* @category Zend
* @package Zend_Cache
* @subpackage Storage
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class IgnoreUserAbort extends AbstractPlugin
{
/**
* Handles
*
* @var array
*/
protected $handles = array();

/**
* The storage adapter target who activated ignore_user_abort.
*
* @var null|Adapter
*/
protected $activatedTarget = null;

/**
* Attach
*
* @param EventCollection $eventCollection
* @return Serializer
* @throws Exception\LogicException
*/
public function attach(EventCollection $events)
{
$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);
$handles[] = $events->attach('setItem.post', $cbOnAfter);
$handles[] = $events->attach('setItem.exception', $cbOnAfter);

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

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

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

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

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

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

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

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

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

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

return $this;
}

/**
* Detach
*
* @param EventCollection $events
* @return Serializer
* @throws Exception\LogicException
*/
public function detach(EventCollection $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;
}

/**
* Activate ignore_user_abort if not already done
* and save the target who activated it.
*
* @param Event $event
* @return void
*/
public function onBefore(Event $event)
{
if ($this->activatedTarget === null && !ignore_user_abort(true)) {
$this->activatedTarget = $event->getTarget();
}
}

/**
* Reset ignore_user_abort if it's activated and if it's the same target
* who activated it.
*
* If exit_on_abort is enabled and the connection has been aborted
* exit the script.
*
* @param Event $event
* @return void
*/
public function onAfter(Event $event)
{
if ($this->activatedTarget === $event->getTarget()) {
// exit if connection aborted
if ($this->getOptions()->getExitOnAbort() && connection_aborted()) {
exit;
}

// reset ignore_user_abort
ignore_user_abort(false);

// remove activated target
$this->activatedTarget = null;
}
}
}
29 changes: 29 additions & 0 deletions library/Zend/Cache/Storage/Plugin/PluginOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ class PluginOptions extends Options
*/
protected $exceptionCallback;

/**
* Used by:
* - IgnoreUserAbort
* @var boolean
*/
protected $exitOnAbort = true;

/**
* Used by:
* - OptimizeByFactor
Expand Down Expand Up @@ -171,6 +178,28 @@ public function getExceptionCallback()
return $this->exceptionCallback;
}

/**
* Exit if connection aborted and ignore_user_abort is disabled.
*
* @param boolean $exitOnAbort
* @return PluginOptions
*/
public function setExitOnAbort($exitOnAbort)
{
$this->exitOnAbort = (bool) $exitOnAbort;
return $this;
}

/**
* Exit if connection aborted and ignore_user_abort is disabled.
*
* @return boolean
*/
public function getExitOnAbort()
{
return $this->exitOnAbort;
}

/**
* Set automatic optimizing factor
*
Expand Down
16 changes: 8 additions & 8 deletions library/Zend/Cache/Storage/Plugin/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,13 @@ public function onIncrementItemPre(Event $event)
$params = $event->getParams();
$token = null;
$oldValue = $cache->getItem(
$params['key'],
$params['key'],
array('token' => &$token) + $params['options']
);
return $cache->checkAndSetItem(
$token,
$oldValue + $params['value'],
$params['key'],
$token,
$oldValue + $params['value'],
$params['key'],
$params['options']
);
}
Expand Down Expand Up @@ -285,13 +285,13 @@ public function onDecrementItemPre(Event $event)
$params = $event->getParams();
$token = null;
$oldValue = $cache->getItem(
$params['key'],
$params['key'],
array('token' => &$token) + $params['options']
);
return $cache->checkAndSetItem(
$token,
$oldValue - $params['value'],
$params['key'],
$token,
$oldValue - $params['value'],
$params['key'],
$params['options']
);
}
Expand Down
8 changes: 4 additions & 4 deletions library/Zend/Cache/Storage/PluginLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ class PluginLoader extends PluginClassLoader
protected $plugins = array(
'clear_by_factor' => 'Zend\Cache\Storage\Plugin\ClearByFactor',
'clearbyfactor' => 'Zend\Cache\Storage\Plugin\ClearByFactor',
//'exception_handler' => 'Zend\Cache\Storage\Plugin\ExceptionHandler',
//'exceptionhandler' => 'Zend\Cache\Storage\Plugin\ExceptionHandler',
'exception_handler' => 'Zend\Cache\Storage\Plugin\ExceptionHandler',
'exceptionhandler' => 'Zend\Cache\Storage\Plugin\ExceptionHandler',
//'filter' => 'Zend\Cache\Storage\Plugin\Filter',
//'ignore_user_abort' => 'Zend\Cache\Storage\Plugin\IgnoreUserAbort',
//'ignoreuserabort' => 'Zend\Cache\Storage\Plugin\IgnoreUserAbort',
'ignore_user_abort' => 'Zend\Cache\Storage\Plugin\IgnoreUserAbort',
'ignoreuserabort' => 'Zend\Cache\Storage\Plugin\IgnoreUserAbort',
//'key_filter' => 'Zend\Cache\Storage\Plugin\KeyFilter',
//'keyfilter' => 'Zend\Cache\Storage\Plugin\KeyFilter',
//'levels' => 'Zend\Cache\Storage\Plugin\Levels',
Expand Down
Loading

0 comments on commit 8427ea5

Please sign in to comment.