示例#1
0
文件: Client.php 项目: modera/module
 /**
  * @param $params
  * @return array
  */
 public function apiMethod($params)
 {
     $response = array('success' => false, 'msg' => 'Error');
     $action = isset($params['action']) ? $params['action'] : '';
     switch ($action) {
         case 'list':
             $moduleRepository = new ModuleRepository($this->workingDir);
             $data = $moduleRepository->getAvailable();
             $filter = isset($params['filter']) ? $params['filter'] : null;
             if ($filter) {
                 $data = array_filter($data, function ($name) use($filter) {
                     return stripos($name, $filter) !== false;
                 });
             }
             $total = count($data);
             $result = array();
             if ($total) {
                 $limit = isset($params['limit']) ? $params['limit'] : 5;
                 $page = isset($params['page']) ? $params['page'] : 1;
                 $offset = $limit * $page - $limit;
                 foreach (array_slice($data, $offset, $limit) as $name) {
                     $info = $this->getPackageInfo($name);
                     if ($info) {
                         $result[$info['name']] = $info;
                     }
                 }
             }
             $response = array('success' => true, 'data' => $result, 'total' => $total);
             break;
         case 'info':
             $response = array('success' => true, 'data' => $this->getPackageInfo($params['name']));
             break;
     }
     return $response;
 }