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

Commit

Permalink
Merge branch 'master' into rfc/escaper
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 98 changed files with 6,500 additions and 9,910 deletions.
5 changes: 2 additions & 3 deletions src/Exception/BadMethodCallException.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@

namespace Zend\Cache\Exception;

use Zend\Cache\Exception;

/**
* @category Zend
* @package Zend_Cache
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class BadMethodCallException extends \BadMethodCallException implements Exception
class BadMethodCallException extends \BadMethodCallException implements
ExceptionInterface
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*
* @category Zend
* @package Zend_Cache
* @subpackage Exception
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
Expand All @@ -23,9 +24,10 @@
/**
* @category Zend
* @package Zend_Cache
* @subpackage Exception
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class ItemNotFoundException extends RuntimeException
interface ExceptionInterface
{
}
4 changes: 1 addition & 3 deletions src/Exception/ExtensionNotLoadedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@

namespace Zend\Cache\Exception;

use Zend\Cache\Exception;

/**
* @category Zend
* @package Zend_Cache
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class ExtensionNotLoadedException extends RuntimeException implements Exception
class ExtensionNotLoadedException extends RuntimeException
{
}
5 changes: 2 additions & 3 deletions src/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@

namespace Zend\Cache\Exception;

use Zend\Cache\Exception;

/**
* @category Zend
* @package Zend_Cache
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class InvalidArgumentException extends \InvalidArgumentException implements Exception
class InvalidArgumentException extends \InvalidArgumentException implements
ExceptionInterface
{
}
4 changes: 1 addition & 3 deletions src/Exception/LogicException.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@

namespace Zend\Cache\Exception;

use Zend\Cache\Exception;

/**
* @category Zend
* @package Zend_Cache
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class LogicException extends \LogicException implements Exception
class LogicException extends \LogicException implements ExceptionInterface
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class LockedException extends RuntimeException
class OutOfSpaceException extends \OverflowException implements ExceptionInterface
{
}
4 changes: 1 addition & 3 deletions src/Exception/RuntimeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@

namespace Zend\Cache\Exception;

use Zend\Cache\Exception;

/**
* @category Zend
* @package Zend_Cache
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class RuntimeException extends \RuntimeException implements Exception
class RuntimeException extends \RuntimeException implements ExceptionInterface
{
}
5 changes: 2 additions & 3 deletions src/Exception/UnexpectedValueException.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@

namespace Zend\Cache\Exception;

use Zend\Cache\Exception;

/**
* @category Zend
* @package Zend_Cache
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class UnexpectedValueException extends \UnexpectedValueException implements Exception
class UnexpectedValueException extends \UnexpectedValueException implements
ExceptionInterface
{
}
5 changes: 2 additions & 3 deletions src/Exception/UnsupportedMethodCallException.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@

namespace Zend\Cache\Exception;

use Zend\Cache\Exception;

/**
* @category Zend
* @package Zend_Cache
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class UnsupportedMethodCallException extends \BadMethodCallException implements Exception
class UnsupportedMethodCallException extends \BadMethodCallException implements
ExceptionInterface
{
}
3 changes: 1 addition & 2 deletions src/Pattern/AbstractPattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
namespace Zend\Cache\Pattern;

use Zend\Cache\Exception,
Zend\Cache\Pattern,
Traversable;

/**
Expand All @@ -32,7 +31,7 @@
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
abstract class AbstractPattern implements Pattern
abstract class AbstractPattern implements PatternInterface
{
/**
* @var PatternOptions
Expand Down
148 changes: 67 additions & 81 deletions src/Pattern/CallbackCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
namespace Zend\Cache\Pattern;

use Zend\Cache\Exception,
Zend\Cache\StorageFactory,
Zend\Cache\Storage\Adapter as StorageAdapter;
Zend\Cache\StorageFactory;

/**
* @category Zend
Expand Down Expand Up @@ -56,24 +55,26 @@ public function setOptions(PatternOptions $options)
*
* @param callback $callback A valid callback
* @param array $args Callback arguments
* @param array $options Options
* @return mixed Result
* @throws Exception
*/
public function call($callback, array $args = array(), array $options = array())
public function call($callback, array $args = array())
{
$classOptions = $this->getOptions();
$key = $this->_generateKey($callback, $args, $options);
if ( ($rs = $classOptions->getStorage()->getItem($key, $options)) !== false ) {
if (!isset($rs[0])) {
$options = $this->getOptions();
$storage = $options->getStorage();
$success = null;
$key = $this->generateCallbackKey($callback, $args);
$result = $storage->getItem($key, $success);
if ($success) {
if (!isset($result[0])) {
throw new Exception\RuntimeException("Invalid cached data for key '{$key}'");
}

echo isset($rs[1]) ? $rs[1] : '';
return $rs[0];
echo isset($result[1]) ? $result[1] : '';
return $result[0];
}

$cacheOutput = $classOptions->getCacheOutput();
$cacheOutput = $options->getCacheOutput();
if ($cacheOutput) {
ob_start();
ob_implicit_flush(false);
Expand All @@ -100,7 +101,7 @@ public function call($callback, array $args = array(), array $options = array())
$data = array($ret);
}

$classOptions->getStorage()->setItem($key, $data, $options);
$storage->setItem($key, $data);

return $ret;
}
Expand All @@ -120,106 +121,91 @@ public function __call($function, array $args)

/**
* Generate a unique key in base of a key representing the callback part
* and a key representing the arguments part merged using md5($callbackKey.$argumentsKey).
*
* Options:
* callback_key A string representing the callback part of the key
* or NULL to autogenerate the callback key part
* argument_key A string representing the arguments part of the key
* or NULL to autogenerate the arguments key part
* and a key representing the arguments part.
*
* @param callback $callback A valid callback
* @param array $args Callback arguments
* @param array $options Options
* @return string
* @throws Exception
*/
public function generateKey($callback, array $args = array(), array $options = array())
public function generateKey($callback, array $args = array())
{
return $this->_generateKey($callback, $args, $options);
return $this->generateCallbackKey($callback, $args);
}

/**
* Generate a unique key in base of a key representing the callback part
* and a key representing the arguments part merged using md5($callbackKey.$argumentsKey).
* and a key representing the arguments part.
*
* @param callback $callback A valid callback
* @param array $args Callback arguments
* @param array $options Options
* @return string
* @throws Exception\InvalidArgumentException
* @throws Exception\RuntimeException
* @throws Exception
*/
protected function _generateKey($callback, array $args, array $options)
protected function generateCallbackKey($callback, array $args)
{
$callbackKey = '';
$argumentKey = '';

// generate callback key part
if (isset($options['callback_key'])) {
$callbackKey = (string) $options['callback_key'];

if (!is_callable($callback, false)) {
throw new Exception\InvalidArgumentException('Invalid callback');
}
} else {
if (!is_callable($callback, false, $callbackKey)) {
throw new Exception\InvalidArgumentException('Invalid callback');
}
if (!is_callable($callback, false, $callbackKey)) {
throw new Exception\InvalidArgumentException('Invalid callback');
}

// functions, methods and classnames are case-insensitive
$callbackKey = strtolower($callbackKey);
// functions, methods and classnames are case-insensitive
$callbackKey = strtolower($callbackKey);

// generate a unique key of object callbacks
if (is_object($callback)) { // Closures & __invoke
$object = $callback;
} elseif (isset($callback[0])) { // array($object, 'method')
$object = $callback[0];
}
if (isset($object)) {
try {
$serializedObject = @serialize($object);
} catch (\Exception $e) {
throw new Exception\RuntimeException(
"Can't serialize callback: see previous exception"
, 0, $e);
}

if (!$serializedObject) {
$lastErr = error_get_last();
throw new Exception\RuntimeException(
"Can't serialize callback: "
. $lastErr['message']
);
}
$callbackKey.= $serializedObject;
}
// generate a unique key of object callbacks
if (is_object($callback)) { // Closures & __invoke
$object = $callback;
} elseif (isset($callback[0])) { // array($object, 'method')
$object = $callback[0];
}

// generate argument key part
if (isset($options['argument_key'])) {
$argumentKey = (string)$options['argument_key'];
} elseif ($args) {
if (isset($object)) {
try {
$serializedArgs = @serialize(array_values($args));
$serializedObject = @serialize($object);
} catch (\Exception $e) {
throw new Exception\RuntimeException(
"Can't serialize arguments: see previous exception"
, 0, $e);
"Can't serialize callback: see previous exception", 0, $e
);
}

if (!$serializedArgs) {
if (!$serializedObject) {
$lastErr = error_get_last();
throw new Exception\RuntimeException(
"Can't serialize arguments: "
. $lastErr['message']
"Can't serialize callback: " . $lastErr['message']
);
}
$callbackKey.= $serializedObject;
}

return md5($callbackKey) . $this->generateArgumentsKey($args);
}

/**
* Generate a unique key of the argument part.
*
* @param array $args
* @return string
* @throws Exception
*/
protected function generateArgumentsKey(array $args)
{
if (!$args) {
return '';
}

try {
$serializedArgs = @serialize(array_values($args));
} catch (\Exception $e) {
throw new Exception\RuntimeException(
"Can't serialize arguments: see previous exception"
, 0, $e);
}

$argumentKey = $serializedArgs;
if (!$serializedArgs) {
$lastErr = error_get_last();
throw new Exception\RuntimeException(
"Can't serialize arguments: " . $lastErr['message']
);
}

// merge and return the key parts
return md5($callbackKey.$argumentKey);
return md5($serializedArgs);
}
}
3 changes: 1 addition & 2 deletions src/Pattern/CaptureCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@

namespace Zend\Cache\Pattern;

use Zend\Cache\Exception,
Zend\Cache\Storage\Adapter as StorageAdapter;
use Zend\Cache\Exception;

/**
* @category Zend
Expand Down
Loading

0 comments on commit 93487ba

Please sign in to comment.