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 feature/math-biginteger
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 78 changed files with 3,375 additions and 5,616 deletions.
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 OutOfCapacityException extends \OverflowException implements ExceptionInterface
class OutOfSpaceException extends \OverflowException implements ExceptionInterface
{
}
141 changes: 62 additions & 79 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\AdapterInterface as StorageAdapter;
Zend\Cache\StorageFactory;

/**
* @category Zend
Expand Down Expand Up @@ -56,17 +55,16 @@ 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();

$options = $this->getOptions();
$storage = $options->getStorage();
$success = null;
$key = $this->_generateKey($callback, $args, $options);
$result = $classOptions->getStorage()->getItem($key, $options, $success);
$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}'");
Expand All @@ -76,7 +74,7 @@ public function call($callback, array $args = array(), array $options = array())
return $result[0];
}

$cacheOutput = $classOptions->getCacheOutput();
$cacheOutput = $options->getCacheOutput();
if ($cacheOutput) {
ob_start();
ob_implicit_flush(false);
Expand All @@ -103,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 @@ -123,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\AdapterInterface as StorageAdapter;
use Zend\Cache\Exception;

/**
* @category Zend
Expand Down
61 changes: 33 additions & 28 deletions src/Pattern/ClassCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,21 @@ public function setOptions(PatternOptions $options)
*
* @param string $method Method name to call
* @param array $args Method arguments
* @param array $options Cache options
* @return mixed
* @throws Exception
*/
public function call($method, array $args = array(), array $options = array())
public function call($method, array $args = array())
{
$classOptions = $this->getOptions();
$classname = $classOptions->getClass();
$method = strtolower($method);
$callback = $classname . '::' . $method;
$options = $this->getOptions();
$classname = $options->getClass();
$method = strtolower($method);
$callback = $classname . '::' . $method;

$cache = $classOptions->getCacheByDefault();
$cache = $options->getCacheByDefault();
if ($cache) {
$cache = !in_array($method, $classOptions->getClassNonCacheMethods());
$cache = !in_array($method, $options->getClassNonCacheMethods());
} else {
$cache = in_array($method, $classOptions->getClassCacheMethods());
$cache = in_array($method, $options->getClassCacheMethods());
}

if (!$cache) {
Expand All @@ -81,34 +80,40 @@ public function call($method, array $args = array(), array $options = array())
}
}

// speed up key generation
if (!isset($options['callback_key'])) {
$options['callback_key'] = $callback;
}

return parent::call($callback, $args, $options);
return parent::call($callback, $args);
}

/**
* Generate a key from the method name and arguments
* Generate a unique key in base of a key representing the callback part
* and a key representing the arguments part.
*
* @param string $method The method name
* @param array $args Method arguments
* @param string $method The method
* @param array $args Callback arguments
* @return string
* @throws Exception
*/
public function generateKey($method, array $args = array(), array $options = array())
public function generateKey($method, array $args = array())
{
// speed up key generation
$classOptions = $this->getOptions();
if (!isset($options['callback_key'])) {
$callback = $classOptions->getClass() . '::' . strtolower($method);
$options['callback_key'] = $callback;
} else {
$callback = $classOptions->getClass() . '::' . $method;
}
return $this->generateCallbackKey(
$this->getOptions()->getClass() . '::' . $method,
$args
);
}

return parent::generateKey($callback, $args, $options);
/**
* Generate a unique key in base of a key representing the callback part
* and a key representing the arguments part.
*
* @param callback $callback A valid callback
* @param array $args Callback arguments
* @return string
* @throws Exception
*/
protected function generateCallbackKey($callback, array $args)
{
$callbackKey = md5(strtolower($callback));
$argumentKey = $this->generateArgumentsKey($args);
return $callbackKey . $argumentKey;
}

/**
Expand Down
Loading

0 comments on commit 04be01e

Please sign in to comment.