/** * 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); }
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; }
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; }
<?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"; }
/** * 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; }
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(); }