Пример #1
0
 public function __construct(Config $config)
 {
     $this->config = $config;
     $set_names = sprintf("SET NAMES '%s';", str_replace('-', '', $config->encoding));
     $this->db = new \PDO('mysql:host=' . $config->db_host . ';dbname=' . $config->db_db, $config->db_user, $config->db_pwd, array(\PDO::MYSQL_ATTR_INIT_COMMAND => $set_names));
     Services::set($this->db);
 }
Пример #2
0
 public function run($callback)
 {
     try {
         if (is_callable($callback)) {
             Services::inject($callback);
         }
         Services::inject_set(Database::class);
         $response = $this->router->route($this->request);
         if ($response === null) {
             $this->response = response('not_found')->set('Route not found');
         } else {
             $this->response = $response;
         }
     } catch (\Exception $e) {
         $this->response = response('error')->html("\n            <pre style='color: #333; padding: 10px; background: #eee; font-face: monospace;'>{$e}</pre>\n             ");
     }
     echo $this->response;
 }
Пример #3
0
 public function __construct()
 {
     $this->services = Services::getInstance();
     $this->input = $this->services->input();
     $this->output = $this->services->output();
 }
Пример #4
0
 public function call($route, $values, Request $request)
 {
     if (is_array($route['action'])) {
         $controller_class = new \ReflectionClass($route['action'][0]);
         $action_methods = $controller_class->getMethods(\ReflectionMethod::IS_PUBLIC);
         array_walk($action_methods, function (&$v) {
             $v = $v->getName();
         });
         if (($index = array_search($route['action'][1], $action_methods)) === false) {
             return null;
         }
         $action_method = $controller_class->getMethod($action_methods[$index]);
         $controller = Services::injectClass($route['action'][0]);
         $result = Services::injectMethod([$controller, $action_method->name], $values);
         //$parameters = $action_method->getParameters();
         //$arguments = [];
         //foreach($parameters as $param) {
         //$arguments[] = isset($values[$param->getName()]) ?
         //$values[$param->getName()] :
         //Services::get($param->getClass()->getShortName());
         //}
         //fix_types($arguments);
         //$result = call_user_func_array(
         //array($controller, $action_method->name),
         //$arguments
         //);
     } else {
         if (is_callable($route['action'])) {
             $result = Services::injectFunction($route['action'], $values);
             //$action_method = new \ReflectionFunction($route['action']);
             //$parameters = $action_method->getParameters();
             //$arguments = [];
             //foreach($parameters as $param) {
             //$arguments[] = isset($values[$param->getName()]) ?
             //$values[$param->getName()] :
             //Services::get($param->getClass()->name);
             //}
             //fix_types($arguments);
             //$result = call_user_func_array(
             //$route['action'],
             //$arguments
             //);
         }
     }
     if (!$result instanceof Response && (is_array($result) || is_object($result))) {
         $result = response('ok')->json($result);
     }
     return $result;
 }