Esempio n. 1
0
 public function mustRetry(Handler $handler)
 {
     $mustRetry = false;
     $written = $handler->getLastResult();
     list($fh, $text) = $handler->getArguments();
     if ($written === false) {
         // we try 3 times before abandoning
         $mustRetry = ++$this->nbErrors <= 3;
     } else {
         $this->written += $written;
         if ($written !== strlen($text)) {
             $mustRetry = true;
             $handler->setArguments(array($fh, substr($text, $written)));
         } else {
             // we force the result with the total written length
             $handler->setResult($this->written);
         }
     }
     return $mustRetry;
 }
Esempio n. 2
0
<?php

/**
 * This example shows how it can handle exceptions.
 */
namespace TryAgain\Examples;

use TryAgain\Handler;
use TryAgain\Validator\AnonymousValidator;
require_once __DIR__ . '/../vendor/autoload.php';
function divide($a, $b)
{
    if ($b == 0) {
        throw new \InvalidArgumentException('Cannot divide by zero');
    }
    return $a / $b;
}
$handler = new Handler(new AnonymousValidator(function ($handler) {
    if ($handler->getLastException() instanceof \InvalidArgumentException) {
        $handler->setResult($handler->getLastException()->getMessage());
    }
    return false;
}));
header("Content-Type: text/plain");
$a = 6;
for ($b = -3; $b <= 3; $b++) {
    echo sprintf('%d / %d = %s', $a, $b, $handler->execute('TryAgain\\Examples\\divide', array($a, $b))), PHP_EOL;
}