<?php
$arr = array(
1, 2, 3
);
$maxRetries = 5;
class RetryIterator {
protected static $_currentRetries = 0;
public static function iterate($element) {
var_dump($element);
var_dump(func_get_args());
if ($element == 2) {
throw new Exception('Something went wrong');
}
}
public static function handleException($e, $array) {
var_dump(func_get_args());
var_dump('Exception handled');
}
public static function retry(&$array, $maxRetries, $iterateCallback, $handleCallback, $iterateParams = null, $handleParams = null, $iterateCallbackObject = null, $handleCallbackObject = null) {
while (!empty($array)) {
reset($array);
$key = key($array);
try {
if (!is_object($iterateCallbackObject)) {
if (is_array($iterateParams) && !empty($iterateParams)) {
array_unshift($handleParams, $array[$key]);
call_user_func_array($iterateCallback, $iterateParams);
} else {
call_user_func($iterateCallback, ($array[$key]));
}
} else {
if (is_array($iterateParams) && !empty($iterateParams)) {
array_unshift($handleParams, $array[$key]);
call_user_func_array(array($iterateCallbackObject, $iterateCallback), $iterateParams);
} else {
$iterateCallbackObject->$iterateCallback($array[$key]);
}
}
unset($array[$key]);
} catch (Exception $e) {
static::$_currentRetries++;
if (static::$_currentRetries <= $maxRetries) {
if (!is_object($handleCallbackObject)) {
if (is_array($handleParams) && !empty($handleParams)) {
array_unshift($handleParams, $e, $array);
call_user_func_array($handleCallback, $handleParams);
} else {
call_user_func($handleCallback, $e, $array);
}
} else {
if (is_array($handleParams) && !empty($handleParams)) {
array_unshift($handleParams, $e, $array);
call_user_func_array(array($handleCallbackObject, $handleCallback), $handleParams);
} else {
$handleCallbackObject->$handleCallback($e, $array);
}
}
} else {
throw new Exception('maxRetries reached');
}
}
}
}
}
class Foo
{
public function bar($el, $a = null, $b = null, $c = null) {
var_dump(func_get_args());
var_dump($el);
if ($el == 2) {
throw new Exception('Exception');
}
}
public function handle($e, $arr, $a = null, $b = null, $c = null) {
var_dump(func_get_args());
var_dump('Exception handled');
}
}
try {
RetryIterator::retry($arr, 5, 'RetryIterator::iterate', 'RetryIterator::handleException');
} catch (Exception $e) {
}
$arr = array(1, false, 2, 3);
$foo = new Foo();
try {
RetryIterator::retry($arr, 5, 'bar', 'handle', null, null, $foo, $foo);
} catch (Exception $e) {
}
$arr = array(1, false, 2, 3);
$foo = new Foo();
var_dump('objects');
try {
RetryIterator::retry($arr, 5, 'bar', 'handle', array(1, 2, 3), array(1, 2, 3), $foo, $foo);
} catch (Exception $e) {
}
preferences:
43.15 ms | 402 KiB | 5 Q