Exemplo n.º 1
0
 /**
  * Set input and output filters
  *
  * Input filter is nothing but a callable, which accepts 4 parameters:
  * http method, uri, handler callback and handler parameters. Returning
  * anything other than NULL will keep handler from executing, and showing
  * the result to user.
  *
  * Input filter is mean to filter the work flow, so it SHOULD NOT modify
  * these parameters.
  *
  * Output filter is mean to filter data. It is a simple function accepts
  * the result of handler, manipulate on it, then return it back. Anything
  * returned from it will show to user, including NULL.
  */
 public function setFilters(array $input = array(), array $output = array())
 {
     // validate input filters
     foreach ($input as $i) {
         list($f) = Util::reflectionCallable($i);
         if (count($f->getParameters()) !== 4) {
             throw new Exception('Input filter must accepts exactly 4 parameters');
         }
     }
     // validate output filters
     foreach ($output as $o) {
         list($f) = Util::reflectionCallable($o);
         if (count($f->getParameters()) !== 1) {
             throw new Exception('Output filter must accepts exactly 1 parameter');
         }
     }
     $this->currentFilters = array($input, $output);
     return $this;
 }
Exemplo n.º 2
0
 /**
  * @dataProvider cbP
  */
 public function testCompileCallable($cb, $expect, $msg)
 {
     $this->assertEquals($expect . '()', Util::compileCallable($cb), $msg);
     $this->assertEquals($expect . '($a)', Util::compileCallable($cb, ['$a']), $msg);
     $this->assertEquals($expect . '($a,$b)', Util::compileCallable($cb, ['$a', '$b']), $msg);
 }
Exemplo n.º 3
0
 public function exportHandler(array $params, bool $raw = false, Interceptor $int = null)
 {
     if (!is_array($this->handler)) {
         return '';
     }
     list($h, $args) = $this->handler;
     if (is_array($h)) {
         // (new class($args[0], $args[1]...))->method()
         $paramStr = '';
         if (count($params) > 0) {
             $tmp = array();
             foreach ($params as $k => $p) {
                 $tmp[$k] = $raw ? $p : var_export($p, true);
             }
             $paramStr = implode(', ', $tmp);
         }
         $argStr = '';
         if (is_array($args)) {
             $tmp = array();
             foreach ($args as $k => $v) {
                 $tmp[$k] = var_export($v, true);
             }
             $argStr = implode(', ', $tmp);
         }
         $method = var_export($h[1], true);
         $intercept = array();
         if ($int !== null) {
             $intercept[] = '$int->intercept($url, $obj, ' . $method . ');';
         }
         $input = array();
         foreach ($this->inputFilters as $f) {
             $input[] = '$ret = ' . Util::compileCallable($f, array('$method', '$url', '[$obj,' . $method . ']', '$params')) . ';';
             $input[] = 'if ($ret !== null) {';
             $input[] = '    return $ret;';
             $input[] = '}';
         }
         $output = array();
         foreach ($this->outputFilters as $f) {
             $output[] = '$ret = ' . Util::compileCallable($f, array('$ret')) . ';';
         }
         $post = array('return $ret;');
         if (is_object($h[0])) {
             $h[0] = var_export($h[0], true);
             return array_merge(['$obj = ' . $h[0] . ';'], $input, $intercept, [sprintf('$ret = $obj->%s(%s);', $h[1], $paramStr)], $output, $post);
         }
         return array_merge([sprintf('$obj = new %s(%s);', $h[0], $argStr)], $input, $intercept, [sprintf('$ret = $obj->%s(%s);', $h[1], $paramStr)], $output, $post);
     }
     return ['return ' . Util::compileCallable($h, $params) . ';'];
 }