Example #1
0
 public static function dispatch()
 {
     static::$method = Request::method();
     $uri = substr(str_replace('/ws/', '/', $_SERVER['REQUEST_URI']), 1);
     $tab = explode('/', $uri);
     if (count($tab) < 3) {
         Api::forbidden();
     }
     $namespace = current($tab);
     $controller = $tab[1];
     $action = $tab[2];
     $tab = array_slice($tab, 3);
     $count = count($tab);
     if (0 < $count && $count % 2 == 0) {
         for ($i = 0; $i < $count; $i += 2) {
             $_REQUEST[$tab[$i]] = $tab[$i + 1];
         }
     }
     $file = APPLICATION_PATH . DS . 'webservices' . DS . $namespace . DS . $controller . '.php';
     if (!File::exists($file)) {
         Api::NotFound();
     }
     require_once $file;
     $class = 'Thin\\' . ucfirst($controller) . 'Ws';
     $i = new $class();
     $methods = get_class_methods($i);
     $call = strtolower(static::$method) . ucfirst($action);
     if (!Arrays::in($call, $methods)) {
         Api::NotFound();
     }
     if (Arrays::in('init', $methods)) {
         $i->init($call);
     }
     $i->{$call}();
     if (Arrays::in('after', $methods)) {
         $i->after();
     }
 }
Example #2
0
 private function find($args)
 {
     if ($this->isGet()) {
         if (!empty($args)) {
             $id = current($args);
             if (!fnmatch('*,*', $id)) {
                 $row = $this->db->find($id);
                 if ($row) {
                     Api::render(['status' => 200, 'execution_time' => Timer::get(), 'token' => $this->token, 'data' => $row->toArray()]);
                 } else {
                     Api::NotFound();
                 }
             } else {
                 $ids = str_replace(', ', ',', $id);
                 $rows = $this->db->where(['id', 'IN', $ids])->cursor();
                 $collection = [];
                 foreach ($rows as $row) {
                     $collection[] = $row;
                 }
                 Api::render(['status' => 200, 'execution_time' => Timer::get(), 'token' => $this->token, 'data' => $collection]);
             }
         }
     }
     Api::forbidden();
 }
Example #3
0
 public static function controller($dir, $cn)
 {
     static::$method = Request::method();
     $uri = substr(str_replace('/api/', '/', $_SERVER['REQUEST_URI']), 1);
     $tab = explode('/', $uri);
     dd($tab);
     if (count($tab) < 1) {
         Api::forbidden();
     }
     $action = current($tab);
     $tab = array_slice($tab, 1);
     $count = count($tab);
     if (0 < $count && $count % 2 == 0) {
         for ($i = 0; $i < $count; $i += 2) {
             $_REQUEST[$tab[$i]] = $tab[$i + 1];
         }
     }
     $file = $dir . DS . 'controllers' . DS . 'api.php';
     if (!File::exists($file)) {
         Api::NotFound();
     }
     require_once $file;
     $class = 'Thin\\' . $cn;
     $i = new $class();
     $methods = get_class_methods($i);
     $call = strtolower(static::$method) . ucfirst($action);
     if (!Arrays::in($call, $methods)) {
         Api::NotFound();
     }
     if (Arrays::in('init', $methods)) {
         $i->init($call);
     }
     $i->{$call}();
     if (Arrays::in('after', $methods)) {
         $i->after();
     }
 }