Пример #1
0
 /**
  * Get the final output by String
  * @return string
  * @throws Exception
  */
 public function get()
 {
     $__data = $this->data;
     ob_start() and extract($__data, EXTR_SKIP);
     try {
         include $this->path();
     } catch (Exception $e) {
         ob_get_clean();
         throw FrameworkException::internalError($e->getMessage());
     }
     $content = ob_get_clean();
     return trim($content);
 }
Пример #2
0
 public function then($func)
 {
     if (is_callable($func)) {
         $func();
     }
     if (is_string($func)) {
         $function_array = preg_split("/@/", $func);
         if (!isset($function_array[1])) {
             throw FrameworkException::internalError('Routing Error');
         }
         $class_name = 'App\\Controller\\' . $function_array[0];
         $method_name = $function_array[1];
         $class_name::$method_name();
     }
     return $this;
 }
Пример #3
0
 private function processAsReceiver($func)
 {
     try {
         if (is_callable($func)) {
             $func();
         }
         if (is_string($func)) {
             $function_array = preg_split("/@/", $func);
             if (!isset($function_array[1])) {
                 throw FrameworkException::internalError('Routing Error');
             }
             $class_name = 'App\\Controller\\' . $function_array[0];
             $method_name = $function_array[1];
             $class_name::$method_name();
         }
     } catch (Exception $e) {
         Log::error($e->getTraceAsString());
     }
     return $this;
 }
Пример #4
0
<?php

use Framework\Queue\QueueProcessor;
use Framework\Config;
use Framework\Queue\Driver\Driver;
use Framework\Queue\Driver\RedisDriver;
use Framework\Exception\FrameworkException;
use Framework\Input;
use Framework\Route;
define('__APP__', __DIR__ . "/..");
require __APP__ . "/vendor/autoload.php";
$rs = Config::get('driver', new RedisDriver());
if (!$rs instanceof Driver) {
    throw FrameworkException::internalError('Queue Driver Not Set');
}
QueueProcessor::getInstance()->setDriver($rs)->setAsReceiver();
for ($i = 0; $i < 10; $i++) {
    $message = $rs->receiveMessage('route');
    if (!$message) {
        exit;
    }
    Input::bind($message);
    Route::reset();
    Route::setSkipMain();
    include __APP__ . "/route.php";
}
Пример #5
0
 function set(FrameworkException $e)
 {
     $this->setCode($e->getCode())->setContent("")->setContentType('text/plain');
     return $this;
 }
Пример #6
0
 public function testGet()
 {
     //basic
     Input::set('key', '123');
     $this->assertEquals(123, Input::get('key'));
     //default value
     $this->assertEquals(123, Input::get('key2', 123));
     //default value by function-based default value
     $this->assertEquals(100, Input::get('key2', function () {
         return 100;
     }));
     //default value by throwing Exception explicitly
     $exception = null;
     try {
         $value = Input::get('key2', function () {
             throw new FrameworkException("sample", 500);
         });
     } catch (Exception $e) {
         $exception = $e;
     }
     $this->assertInstanceOf('\\Framework\\Exception\\FrameworkException', $exception);
     try {
         $value = Input::get('key', function () {
             throw new FrameworkException("sample", 500);
         });
     } catch (Exception $e) {
         $value = $e;
     }
     $this->assertEquals(123, $value);
     //default value by specifying Framework Exception
     $exception = null;
     try {
         Input::get("key2", new FrameworkException());
     } catch (Exception $e) {
         $exception = $e;
     }
     $this->assertInstanceOf('\\Framework\\Exception\\FrameworkException', $exception);
     //Validator
     $value = Input::get('key', -1, function ($k) {
         if (is_numeric($k)) {
             return true;
         }
         return false;
     });
     $this->assertEquals(123, $value);
     $value = Input::get('key2', 123, function ($k) {
         if (is_numeric($k)) {
             return true;
         }
         return false;
     });
     $this->assertEquals(123, $value);
     Input::set("key3", "abc");
     $value = Input::get('key3', 123, function ($k) {
         if (is_numeric($k)) {
             return true;
         }
         return false;
     });
     $this->assertEquals(123, $value);
     $exception = null;
     try {
         Input::get('key3', FrameworkException::lackParameter('key 3'), function ($k) {
             if (is_numeric($k)) {
                 return true;
             }
             return false;
         });
     } catch (Exception $e) {
         $exception = $e;
     }
     $this->assertInstanceOf('\\Framework\\Exception\\FrameworkException', $exception);
     $value = Input::get('key', -1, new IsNumericValidator());
     $this->assertEquals(123, $value);
     $value = Input::get('key3', -1, new IsNumericValidator());
     $this->assertEquals(-1, $value);
 }
 function set(FrameworkException $e)
 {
     $message = $e->getTraceAsString();
     $this->setCode($e->getCode())->setContent($message)->setContentType('text/plain');
     return $this;
 }
Пример #8
0
 /**
  * Get Uploaded File
  *
  * @param $key
  * @param null $default
  * @param null $validator
  * @return InputFile
  */
 static function file($key, $default = null, $validator = null)
 {
     if (!isset($_FILES[$key]) || $_FILES[$key]['error'] != UPLOAD_ERR_OK) {
         return self::processError($default);
     }
     $file = new InputFile($_FILES[$key]['tmp_name']);
     $file->setContentType($_FILES[$key]['type']);
     $file->setOriginalName($_FILES[$key]['name']);
     if ($validator instanceof Validator) {
         if ($validator->execute($file)) {
             return $file;
         } else {
             return self::processError($default);
         }
     } else {
         if (is_callable($validator)) {
             try {
                 if ($validator($file)) {
                     return $file;
                 } else {
                     return self::processError($default);
                 }
             } catch (Exception $e) {
                 throw FrameworkException::internalError("Validator error");
             }
         }
     }
     return $file;
 }
Пример #9
0
 static function action($method, $path, $function)
 {
     $restInput = array();
     $me = self::getInstance();
     if (Input::has('__request_method')) {
         $request_method = Input::get('__request_method');
     } else {
         $request_method = $_SERVER['REQUEST_METHOD'];
     }
     if ($me->is_called == true) {
         return IgnoreProcessor::getInstance();
     }
     if ($method != 'otherwise') {
         if ($method != $request_method) {
             return IgnoreProcessor::getInstance();
         }
         $path_array = preg_split("/\\//", $path);
         foreach ($path_array as $key => $path_element) {
             if (strlen(trim($path_element)) == 0) {
                 unset($path_array[$key]);
             }
         }
         $path_array = array_values($path_array);
         if (count($path_array) != count($me->path)) {
             return $me->getPostProcessor();
         }
         foreach ($path_array as $key => $path_element) {
             if (preg_match("/^\\:(.*)\$/", $path_element, $match) || preg_match("/^\\{(.*)\\}\$/", $path_element, $match)) {
                 Input::set($match[1], $me->path[$key]);
                 $restInput[] = $me->path[$key];
             } else {
                 if (!isset($me->path[$key]) || $me->path[$key] != $path_element) {
                     return IgnoreProcessor::getInstance();
                 }
             }
         }
     }
     if ($me->skip) {
         return $me->getPostProcessor();
     }
     try {
         if (is_callable($function)) {
             $response = $function();
         } else {
             $function_array = preg_split("/@/", $function);
             if (!isset($function_array[1])) {
                 throw FrameworkException::internalError('Routing Error');
             }
             $controller_namespace = Config::get("app.controller_namespace", "App\\Controller\\");
             $class_name = $controller_namespace . $function_array[0];
             $method_name = $function_array[1];
             //$response = $class_name::$method_name();
             // Initialization controller object
             $controller = new $class_name();
             $response = call_user_func_array(array($controller, $method_name), $restInput);
             //$response = $controller->$method_name();
         }
         if ($response instanceof Response) {
             $response->display();
         } else {
             $rs = new Response();
             $rs->setContentType('text/html')->setContent($response)->display();
         }
         $me->is_called = true;
     } catch (FrameworkException $e) {
         $me->handleError($e);
         $me->is_called = true;
         return IgnoreProcessor::getInstance();
     } catch (Exception $e) {
         $exception = FrameworkException::internalError('Internal Error: ' . $e->getMessage());
         $me->handleError($exception);
         $me->is_called = true;
         return IgnoreProcessor::getInstance();
     }
     return $me->getPostProcessor();
 }