@@ -30,7 +30,7 @@ discard block |
||
| 30 | 30 | use XoopsModules\Wggithub; |
| 31 | 31 | use XoopsModules\Wggithub\Github; |
| 32 | 32 | |
| 33 | -require __DIR__ . '/header.php'; |
|
| 33 | +require __DIR__.'/header.php'; |
|
| 34 | 34 | // It recovered the value of argument op in URL$ |
| 35 | 35 | |
| 36 | 36 | |
@@ -41,8 +41,8 @@ discard block |
||
| 41 | 41 | |
| 42 | 42 | $count = 0; |
| 43 | 43 | foreach ($data as $key => $repo) { |
| 44 | - echo "<br>key:" . $key . ' repo:' . $repo['name']; |
|
| 44 | + echo "<br>key:".$key.' repo:'.$repo['name']; |
|
| 45 | 45 | $count++; |
| 46 | - if ($count>5) {break;} |
|
| 46 | + if ($count > 5) {break; } |
|
| 47 | 47 | } |
| 48 | 48 | echo "<br>OK 2"; |
@@ -31,107 +31,107 @@ |
||
| 31 | 31 | // Request log_id |
| 32 | 32 | $logId = Request::getInt('log_id'); |
| 33 | 33 | switch ($op) { |
| 34 | - case 'list': |
|
| 35 | - default: |
|
| 36 | - // Define Stylesheet |
|
| 37 | - $GLOBALS['xoTheme']->addStylesheet($style, null); |
|
| 38 | - $start = Request::getInt('start', 0); |
|
| 39 | - $limit = Request::getInt('limit', $helper->getConfig('adminpager')); |
|
| 40 | - $templateMain = 'wggithub_admin_logs.tpl'; |
|
| 41 | - $GLOBALS['xoopsTpl']->assign('navigation', $adminObject->displayNavigation('logs.php')); |
|
| 42 | - $adminObject->addItemButton(_AM_WGGITHUB_ADD_LOG, 'logs.php?op=new', 'add'); |
|
| 43 | - $GLOBALS['xoopsTpl']->assign('buttons', $adminObject->displayButton('left')); |
|
| 44 | - $logsCount = $logsHandler->getCountLogs(); |
|
| 45 | - $logsAll = $logsHandler->getAllLogs($start, $limit); |
|
| 46 | - $GLOBALS['xoopsTpl']->assign('logs_count', $logsCount); |
|
| 47 | - $GLOBALS['xoopsTpl']->assign('wggithub_url', WGGITHUB_URL); |
|
| 48 | - $GLOBALS['xoopsTpl']->assign('wggithub_upload_url', WGGITHUB_UPLOAD_URL); |
|
| 49 | - // Table view logs |
|
| 50 | - if ($logsCount > 0) { |
|
| 51 | - foreach (\array_keys($logsAll) as $i) { |
|
| 52 | - $log = $logsAll[$i]->getValuesLogs(); |
|
| 53 | - $GLOBALS['xoopsTpl']->append('logs_list', $log); |
|
| 54 | - unset($log); |
|
| 55 | - } |
|
| 56 | - // Display Navigation |
|
| 57 | - if ($logsCount > $limit) { |
|
| 58 | - include_once XOOPS_ROOT_PATH . '/class/pagenav.php'; |
|
| 59 | - $pagenav = new \XoopsPageNav($logsCount, $limit, $start, 'start', 'op=list&limit=' . $limit); |
|
| 60 | - $GLOBALS['xoopsTpl']->assign('pagenav', $pagenav->renderNav(4)); |
|
| 61 | - } |
|
| 62 | - } else { |
|
| 63 | - $GLOBALS['xoopsTpl']->assign('error', _AM_WGGITHUB_THEREARENT_LOGS); |
|
| 64 | - } |
|
| 65 | - break; |
|
| 66 | - case 'new': |
|
| 67 | - $templateMain = 'wggithub_admin_logs.tpl'; |
|
| 68 | - $GLOBALS['xoopsTpl']->assign('navigation', $adminObject->displayNavigation('logs.php')); |
|
| 69 | - $adminObject->addItemButton(_AM_WGGITHUB_LOGS_LIST, 'logs.php', 'list'); |
|
| 70 | - $GLOBALS['xoopsTpl']->assign('buttons', $adminObject->displayButton('left')); |
|
| 71 | - // Form Create |
|
| 72 | - $logsObj = $logsHandler->create(); |
|
| 73 | - $form = $logsObj->getFormLogs(); |
|
| 74 | - $GLOBALS['xoopsTpl']->assign('form', $form->render()); |
|
| 75 | - break; |
|
| 76 | - case 'save': |
|
| 77 | - // Security Check |
|
| 78 | - if (!$GLOBALS['xoopsSecurity']->check()) { |
|
| 79 | - \redirect_header('logs.php', 3, \implode(',', $GLOBALS['xoopsSecurity']->getErrors())); |
|
| 80 | - } |
|
| 81 | - if ($logId > 0) { |
|
| 82 | - $logsObj = $logsHandler->get($logId); |
|
| 83 | - } else { |
|
| 84 | - $logsObj = $logsHandler->create(); |
|
| 85 | - } |
|
| 86 | - // Set Vars |
|
| 34 | + case 'list': |
|
| 35 | + default: |
|
| 36 | + // Define Stylesheet |
|
| 37 | + $GLOBALS['xoTheme']->addStylesheet($style, null); |
|
| 38 | + $start = Request::getInt('start', 0); |
|
| 39 | + $limit = Request::getInt('limit', $helper->getConfig('adminpager')); |
|
| 40 | + $templateMain = 'wggithub_admin_logs.tpl'; |
|
| 41 | + $GLOBALS['xoopsTpl']->assign('navigation', $adminObject->displayNavigation('logs.php')); |
|
| 42 | + $adminObject->addItemButton(_AM_WGGITHUB_ADD_LOG, 'logs.php?op=new', 'add'); |
|
| 43 | + $GLOBALS['xoopsTpl']->assign('buttons', $adminObject->displayButton('left')); |
|
| 44 | + $logsCount = $logsHandler->getCountLogs(); |
|
| 45 | + $logsAll = $logsHandler->getAllLogs($start, $limit); |
|
| 46 | + $GLOBALS['xoopsTpl']->assign('logs_count', $logsCount); |
|
| 47 | + $GLOBALS['xoopsTpl']->assign('wggithub_url', WGGITHUB_URL); |
|
| 48 | + $GLOBALS['xoopsTpl']->assign('wggithub_upload_url', WGGITHUB_UPLOAD_URL); |
|
| 49 | + // Table view logs |
|
| 50 | + if ($logsCount > 0) { |
|
| 51 | + foreach (\array_keys($logsAll) as $i) { |
|
| 52 | + $log = $logsAll[$i]->getValuesLogs(); |
|
| 53 | + $GLOBALS['xoopsTpl']->append('logs_list', $log); |
|
| 54 | + unset($log); |
|
| 55 | + } |
|
| 56 | + // Display Navigation |
|
| 57 | + if ($logsCount > $limit) { |
|
| 58 | + include_once XOOPS_ROOT_PATH . '/class/pagenav.php'; |
|
| 59 | + $pagenav = new \XoopsPageNav($logsCount, $limit, $start, 'start', 'op=list&limit=' . $limit); |
|
| 60 | + $GLOBALS['xoopsTpl']->assign('pagenav', $pagenav->renderNav(4)); |
|
| 61 | + } |
|
| 62 | + } else { |
|
| 63 | + $GLOBALS['xoopsTpl']->assign('error', _AM_WGGITHUB_THEREARENT_LOGS); |
|
| 64 | + } |
|
| 65 | + break; |
|
| 66 | + case 'new': |
|
| 67 | + $templateMain = 'wggithub_admin_logs.tpl'; |
|
| 68 | + $GLOBALS['xoopsTpl']->assign('navigation', $adminObject->displayNavigation('logs.php')); |
|
| 69 | + $adminObject->addItemButton(_AM_WGGITHUB_LOGS_LIST, 'logs.php', 'list'); |
|
| 70 | + $GLOBALS['xoopsTpl']->assign('buttons', $adminObject->displayButton('left')); |
|
| 71 | + // Form Create |
|
| 72 | + $logsObj = $logsHandler->create(); |
|
| 73 | + $form = $logsObj->getFormLogs(); |
|
| 74 | + $GLOBALS['xoopsTpl']->assign('form', $form->render()); |
|
| 75 | + break; |
|
| 76 | + case 'save': |
|
| 77 | + // Security Check |
|
| 78 | + if (!$GLOBALS['xoopsSecurity']->check()) { |
|
| 79 | + \redirect_header('logs.php', 3, \implode(',', $GLOBALS['xoopsSecurity']->getErrors())); |
|
| 80 | + } |
|
| 81 | + if ($logId > 0) { |
|
| 82 | + $logsObj = $logsHandler->get($logId); |
|
| 83 | + } else { |
|
| 84 | + $logsObj = $logsHandler->create(); |
|
| 85 | + } |
|
| 86 | + // Set Vars |
|
| 87 | 87 | $logsObj->setVar('log_type', Request::getInt('log_type', 0)); |
| 88 | - $logsObj->setVar('log_details', Request::getString('log_details', '')); |
|
| 89 | - $logsObj->setVar('log_result', Request::getString('log_result', '')); |
|
| 90 | - $logDatecreatedObj = \DateTime::createFromFormat(_SHORTDATESTRING, Request::getString('log_datecreated')); |
|
| 91 | - $logsObj->setVar('log_datecreated', $logDatecreatedObj->getTimestamp()); |
|
| 92 | - $logsObj->setVar('log_submitter', Request::getInt('log_submitter', 0)); |
|
| 93 | - // Insert Data |
|
| 94 | - if ($logsHandler->insert($logsObj)) { |
|
| 95 | - \redirect_header('logs.php?op=list', 2, _AM_WGGITHUB_FORM_OK); |
|
| 96 | - } |
|
| 97 | - // Get Form |
|
| 98 | - $GLOBALS['xoopsTpl']->assign('error', $logsObj->getHtmlErrors()); |
|
| 99 | - $form = $logsObj->getFormLogs(); |
|
| 100 | - $GLOBALS['xoopsTpl']->assign('form', $form->render()); |
|
| 101 | - break; |
|
| 102 | - case 'edit': |
|
| 103 | - $templateMain = 'wggithub_admin_logs.tpl'; |
|
| 104 | - $GLOBALS['xoopsTpl']->assign('navigation', $adminObject->displayNavigation('logs.php')); |
|
| 105 | - $adminObject->addItemButton(_AM_WGGITHUB_ADD_LOG, 'logs.php?op=new', 'add'); |
|
| 106 | - $adminObject->addItemButton(_AM_WGGITHUB_LOGS_LIST, 'logs.php', 'list'); |
|
| 107 | - $GLOBALS['xoopsTpl']->assign('buttons', $adminObject->displayButton('left')); |
|
| 108 | - // Get Form |
|
| 109 | - $logsObj = $logsHandler->get($logId); |
|
| 110 | - $form = $logsObj->getFormLogs(); |
|
| 111 | - $GLOBALS['xoopsTpl']->assign('form', $form->render()); |
|
| 112 | - break; |
|
| 113 | - case 'delete': |
|
| 114 | - $templateMain = 'wggithub_admin_logs.tpl'; |
|
| 115 | - $GLOBALS['xoopsTpl']->assign('navigation', $adminObject->displayNavigation('logs.php')); |
|
| 116 | - $logsObj = $logsHandler->get($logId); |
|
| 117 | - $reqRequest = $logsObj->getVar('log_details'); |
|
| 118 | - if (isset($_REQUEST['ok']) && 1 == $_REQUEST['ok']) { |
|
| 119 | - if (!$GLOBALS['xoopsSecurity']->check()) { |
|
| 120 | - \redirect_header('logs.php', 3, \implode(', ', $GLOBALS['xoopsSecurity']->getErrors())); |
|
| 121 | - } |
|
| 122 | - if ($logsHandler->delete($logsObj)) { |
|
| 123 | - \redirect_header('logs.php', 3, _AM_WGGITHUB_FORM_DELETE_OK); |
|
| 124 | - } else { |
|
| 125 | - $GLOBALS['xoopsTpl']->assign('error', $logsObj->getHtmlErrors()); |
|
| 126 | - } |
|
| 127 | - } else { |
|
| 128 | - $xoopsconfirm = new Common\XoopsConfirm( |
|
| 129 | - ['ok' => 1, 'log_id' => $logId, 'op' => 'delete'], |
|
| 130 | - $_SERVER['REQUEST_URI'], |
|
| 131 | - \sprintf(_AM_WGGITHUB_FORM_SURE_DELETE, $logsObj->getVar('log_details'))); |
|
| 132 | - $form = $xoopsconfirm->getFormXoopsConfirm(); |
|
| 133 | - $GLOBALS['xoopsTpl']->assign('form', $form->render()); |
|
| 134 | - } |
|
| 135 | - break; |
|
| 88 | + $logsObj->setVar('log_details', Request::getString('log_details', '')); |
|
| 89 | + $logsObj->setVar('log_result', Request::getString('log_result', '')); |
|
| 90 | + $logDatecreatedObj = \DateTime::createFromFormat(_SHORTDATESTRING, Request::getString('log_datecreated')); |
|
| 91 | + $logsObj->setVar('log_datecreated', $logDatecreatedObj->getTimestamp()); |
|
| 92 | + $logsObj->setVar('log_submitter', Request::getInt('log_submitter', 0)); |
|
| 93 | + // Insert Data |
|
| 94 | + if ($logsHandler->insert($logsObj)) { |
|
| 95 | + \redirect_header('logs.php?op=list', 2, _AM_WGGITHUB_FORM_OK); |
|
| 96 | + } |
|
| 97 | + // Get Form |
|
| 98 | + $GLOBALS['xoopsTpl']->assign('error', $logsObj->getHtmlErrors()); |
|
| 99 | + $form = $logsObj->getFormLogs(); |
|
| 100 | + $GLOBALS['xoopsTpl']->assign('form', $form->render()); |
|
| 101 | + break; |
|
| 102 | + case 'edit': |
|
| 103 | + $templateMain = 'wggithub_admin_logs.tpl'; |
|
| 104 | + $GLOBALS['xoopsTpl']->assign('navigation', $adminObject->displayNavigation('logs.php')); |
|
| 105 | + $adminObject->addItemButton(_AM_WGGITHUB_ADD_LOG, 'logs.php?op=new', 'add'); |
|
| 106 | + $adminObject->addItemButton(_AM_WGGITHUB_LOGS_LIST, 'logs.php', 'list'); |
|
| 107 | + $GLOBALS['xoopsTpl']->assign('buttons', $adminObject->displayButton('left')); |
|
| 108 | + // Get Form |
|
| 109 | + $logsObj = $logsHandler->get($logId); |
|
| 110 | + $form = $logsObj->getFormLogs(); |
|
| 111 | + $GLOBALS['xoopsTpl']->assign('form', $form->render()); |
|
| 112 | + break; |
|
| 113 | + case 'delete': |
|
| 114 | + $templateMain = 'wggithub_admin_logs.tpl'; |
|
| 115 | + $GLOBALS['xoopsTpl']->assign('navigation', $adminObject->displayNavigation('logs.php')); |
|
| 116 | + $logsObj = $logsHandler->get($logId); |
|
| 117 | + $reqRequest = $logsObj->getVar('log_details'); |
|
| 118 | + if (isset($_REQUEST['ok']) && 1 == $_REQUEST['ok']) { |
|
| 119 | + if (!$GLOBALS['xoopsSecurity']->check()) { |
|
| 120 | + \redirect_header('logs.php', 3, \implode(', ', $GLOBALS['xoopsSecurity']->getErrors())); |
|
| 121 | + } |
|
| 122 | + if ($logsHandler->delete($logsObj)) { |
|
| 123 | + \redirect_header('logs.php', 3, _AM_WGGITHUB_FORM_DELETE_OK); |
|
| 124 | + } else { |
|
| 125 | + $GLOBALS['xoopsTpl']->assign('error', $logsObj->getHtmlErrors()); |
|
| 126 | + } |
|
| 127 | + } else { |
|
| 128 | + $xoopsconfirm = new Common\XoopsConfirm( |
|
| 129 | + ['ok' => 1, 'log_id' => $logId, 'op' => 'delete'], |
|
| 130 | + $_SERVER['REQUEST_URI'], |
|
| 131 | + \sprintf(_AM_WGGITHUB_FORM_SURE_DELETE, $logsObj->getVar('log_details'))); |
|
| 132 | + $form = $xoopsconfirm->getFormXoopsConfirm(); |
|
| 133 | + $GLOBALS['xoopsTpl']->assign('form', $form->render()); |
|
| 134 | + } |
|
| 135 | + break; |
|
| 136 | 136 | } |
| 137 | 137 | require __DIR__ . '/footer.php'; |
@@ -25,7 +25,7 @@ discard block |
||
| 25 | 25 | use XoopsModules\Wggithub\Constants; |
| 26 | 26 | use XoopsModules\Wggithub\Common; |
| 27 | 27 | |
| 28 | -require __DIR__ . '/header.php'; |
|
| 28 | +require __DIR__.'/header.php'; |
|
| 29 | 29 | // It recovered the value of argument op in URL$ |
| 30 | 30 | $op = Request::getCmd('op', 'list'); |
| 31 | 31 | // Request log_id |
@@ -55,8 +55,8 @@ discard block |
||
| 55 | 55 | } |
| 56 | 56 | // Display Navigation |
| 57 | 57 | if ($logsCount > $limit) { |
| 58 | - include_once XOOPS_ROOT_PATH . '/class/pagenav.php'; |
|
| 59 | - $pagenav = new \XoopsPageNav($logsCount, $limit, $start, 'start', 'op=list&limit=' . $limit); |
|
| 58 | + include_once XOOPS_ROOT_PATH.'/class/pagenav.php'; |
|
| 59 | + $pagenav = new \XoopsPageNav($logsCount, $limit, $start, 'start', 'op=list&limit='.$limit); |
|
| 60 | 60 | $GLOBALS['xoopsTpl']->assign('pagenav', $pagenav->renderNav(4)); |
| 61 | 61 | } |
| 62 | 62 | } else { |
@@ -134,4 +134,4 @@ discard block |
||
| 134 | 134 | } |
| 135 | 135 | break; |
| 136 | 136 | } |
| 137 | -require __DIR__ . '/footer.php'; |
|
| 137 | +require __DIR__.'/footer.php'; |
|
@@ -28,15 +28,15 @@ discard block |
||
| 28 | 28 | Github\GithubClient |
| 29 | 29 | }; |
| 30 | 30 | |
| 31 | -require __DIR__ . '/header.php'; |
|
| 31 | +require __DIR__.'/header.php'; |
|
| 32 | 32 | $GLOBALS['xoopsOption']['template_main'] = 'wggithub_index.tpl'; |
| 33 | -include_once \XOOPS_ROOT_PATH . '/header.php'; |
|
| 33 | +include_once \XOOPS_ROOT_PATH.'/header.php'; |
|
| 34 | 34 | |
| 35 | 35 | // Permissions |
| 36 | 36 | $permGlobalView = $permissionsHandler->getPermGlobalView(); |
| 37 | 37 | if (!$permGlobalView) { |
| 38 | 38 | $GLOBALS['xoopsTpl']->assign('error', _NOPERM); |
| 39 | - require __DIR__ . '/footer.php'; |
|
| 39 | + require __DIR__.'/footer.php'; |
|
| 40 | 40 | } |
| 41 | 41 | $permGlobalRead = $permissionsHandler->getPermGlobalRead(); |
| 42 | 42 | $permReadmeUpdate = $permissionsHandler->getPermReadmeUpdate(); |
@@ -50,7 +50,7 @@ discard block |
||
| 50 | 50 | |
| 51 | 51 | // Define Stylesheet |
| 52 | 52 | $GLOBALS['xoTheme']->addStylesheet($style, null); |
| 53 | -$GLOBALS['xoTheme']->addStylesheet(WGGITHUB_URL . '/assets/css/tabs.css', null); |
|
| 53 | +$GLOBALS['xoTheme']->addStylesheet(WGGITHUB_URL.'/assets/css/tabs.css', null); |
|
| 54 | 54 | $keywords = []; |
| 55 | 55 | // |
| 56 | 56 | $GLOBALS['xoopsTpl']->assign('xoops_icons32_url', XOOPS_ICONS32_URL); |
@@ -120,13 +120,13 @@ discard block |
||
| 120 | 120 | foreach (\array_keys($directoriesAll) as $i) { |
| 121 | 121 | $directories[$i] = $directoriesAll[$i]->getValuesDirectories(); |
| 122 | 122 | $dirName = $directoriesAll[$i]->getVar('dir_name'); |
| 123 | - $dirFilterRelease = (bool)$directoriesAll[$i]->getVar('dir_filterrelease'); |
|
| 123 | + $dirFilterRelease = (bool) $directoriesAll[$i]->getVar('dir_filterrelease'); |
|
| 124 | 124 | $repos = []; |
| 125 | 125 | $crRepositories = new \CriteriaCompo(); |
| 126 | 126 | $crRepositories->add(new \Criteria('repo_user', $dirName)); |
| 127 | 127 | $repositoriesCountTotal = $repositoriesHandler->getCount($crRepositories); |
| 128 | 128 | if ('any' === $filterRelease && $dirFilterRelease) { |
| 129 | - $crRepositories->add(new \Criteria('repo_prerelease', 1) ); |
|
| 129 | + $crRepositories->add(new \Criteria('repo_prerelease', 1)); |
|
| 130 | 130 | $crRepositories->add(new \Criteria('repo_release', 1), 'OR'); |
| 131 | 131 | } |
| 132 | 132 | if ('final' === $filterRelease && $dirFilterRelease) { |
@@ -181,9 +181,9 @@ discard block |
||
| 181 | 181 | } |
| 182 | 182 | $directories[$i]['repos'] = $repos; |
| 183 | 183 | $directories[$i]['previousRepos'] = $start > 0; |
| 184 | - $directories[$i]['previousOp'] = '&start=' . ($start - $limit) . '&limit=' . $limit . '&release=' . $filterRelease . '&sortby=' . $filterSortby; |
|
| 184 | + $directories[$i]['previousOp'] = '&start='.($start - $limit).'&limit='.$limit.'&release='.$filterRelease.'&sortby='.$filterSortby; |
|
| 185 | 185 | $directories[$i]['nextRepos'] = ($repositoriesCount - $start) > $limit; |
| 186 | - $directories[$i]['nextOp'] = '&start=' . ($start + $limit) . '&limit=' . $limit . '&release=' . $filterRelease . '&sortby=' . $filterSortby; |
|
| 186 | + $directories[$i]['nextOp'] = '&start='.($start + $limit).'&limit='.$limit.'&release='.$filterRelease.'&sortby='.$filterSortby; |
|
| 187 | 187 | $GLOBALS['xoopsTpl']->assign('start', $start); |
| 188 | 188 | $GLOBALS['xoopsTpl']->assign('limit', $limit); |
| 189 | 189 | $GLOBALS['xoopsTpl']->assign('menu', $menu); |
@@ -194,8 +194,8 @@ discard block |
||
| 194 | 194 | unset($crDirectories, $directories); |
| 195 | 195 | // Display Navigation |
| 196 | 196 | if ($directoriesCount > $limit) { |
| 197 | - require_once \XOOPS_ROOT_PATH . '/class/pagenav.php'; |
|
| 198 | - $pagenav = new \XoopsPageNav($directoriesCount, $limit, $start, 'start', 'op=list&limit=' . $limit); |
|
| 197 | + require_once \XOOPS_ROOT_PATH.'/class/pagenav.php'; |
|
| 198 | + $pagenav = new \XoopsPageNav($directoriesCount, $limit, $start, 'start', 'op=list&limit='.$limit); |
|
| 199 | 199 | $GLOBALS['xoopsTpl']->assign('pagenav', $pagenav->renderNav(4)); |
| 200 | 200 | } |
| 201 | 201 | $GLOBALS['xoopsTpl']->assign('lang_thereare', \sprintf(\_MA_WGGITHUB_INDEX_THEREARE, $directoriesCount)); |
@@ -208,11 +208,11 @@ discard block |
||
| 208 | 208 | // Permissions |
| 209 | 209 | if (!$permGlobalRead) { |
| 210 | 210 | $GLOBALS['xoopsTpl']->assign('error', \_NOPERM); |
| 211 | - require __DIR__ . '/footer.php'; |
|
| 211 | + require __DIR__.'/footer.php'; |
|
| 212 | 212 | } |
| 213 | 213 | $dirName = Request::getString('dir_name', ''); |
| 214 | 214 | $result = executeUpdate($dirName); |
| 215 | - $redir = 'index.php?op=list_afterupdate&start=' . $start . '&limit=' . $limit . '&release=' . $filterRelease . '&sortby=' . $filterSortby; |
|
| 215 | + $redir = 'index.php?op=list_afterupdate&start='.$start.'&limit='.$limit.'&release='.$filterRelease.'&sortby='.$filterSortby; |
|
| 216 | 216 | if ($result) { |
| 217 | 217 | \redirect_header($redir, 2, \_MA_WGGITHUB_READGH_SUCCESS); |
| 218 | 218 | } else { |
@@ -224,13 +224,13 @@ discard block |
||
| 224 | 224 | // Permissions |
| 225 | 225 | if (!$permReadmeUpdate) { |
| 226 | 226 | $GLOBALS['xoopsTpl']->assign('error', \_NOPERM); |
| 227 | - require __DIR__ . '/footer.php'; |
|
| 227 | + require __DIR__.'/footer.php'; |
|
| 228 | 228 | } |
| 229 | 229 | $repoId = Request::getInt('repo_id', 0); |
| 230 | 230 | $repoUser = Request::getString('repo_user', 'none'); |
| 231 | 231 | $repoName = Request::getString('repo_name', 'none'); |
| 232 | 232 | $res = $helper->getHandler('Readmes')->updateReadmes($repoId, $repoUser, $repoName); |
| 233 | - $redir = 'index.php?op=list_afterupdate&start=' . $start . '&limit=' . $limit . '&release=' . $filterRelease . '&sortby=' . $filterSortby; |
|
| 233 | + $redir = 'index.php?op=list_afterupdate&start='.$start.'&limit='.$limit.'&release='.$filterRelease.'&sortby='.$filterSortby; |
|
| 234 | 234 | if ($result) { |
| 235 | 235 | \redirect_header($redir, 2, \_MA_WGGITHUB_READGH_SUCCESS); |
| 236 | 236 | } else { |
@@ -243,14 +243,14 @@ discard block |
||
| 243 | 243 | // Breadcrumbs |
| 244 | 244 | $xoBreadcrumbs[] = ['title' => \_MA_WGGITHUB_INDEX]; |
| 245 | 245 | // Keywords |
| 246 | -wggithubMetaKeywords($helper->getConfig('keywords') . ', ' . \implode(',', $keywords)); |
|
| 246 | +wggithubMetaKeywords($helper->getConfig('keywords').', '.\implode(',', $keywords)); |
|
| 247 | 247 | unset($keywords); |
| 248 | 248 | // Description |
| 249 | 249 | wggithubMetaDescription(\_MA_WGGITHUB_INDEX_DESC); |
| 250 | 250 | $GLOBALS['xoopsTpl']->assign('xoops_mpageurl', WGGITHUB_URL.'/index.php'); |
| 251 | 251 | $GLOBALS['xoopsTpl']->assign('xoops_icons32_url', XOOPS_ICONS32_URL); |
| 252 | 252 | $GLOBALS['xoopsTpl']->assign('wggithub_upload_url', WGGITHUB_UPLOAD_URL); |
| 253 | -require __DIR__ . '/footer.php'; |
|
| 253 | +require __DIR__.'/footer.php'; |
|
| 254 | 254 | |
| 255 | 255 | |
| 256 | 256 | /** |
@@ -258,7 +258,7 @@ discard block |
||
| 258 | 258 | * @param string $dirName |
| 259 | 259 | * @return bool |
| 260 | 260 | */ |
| 261 | -function executeUpdate($dirName = ''){ |
|
| 261 | +function executeUpdate($dirName = '') { |
|
| 262 | 262 | |
| 263 | 263 | $githubClient = GithubClient::getInstance(); |
| 264 | 264 | $helper = Helper::getInstance(); |
@@ -12,147 +12,147 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class Paginator extends Sanity implements \Iterator |
| 14 | 14 | { |
| 15 | - /** @var Api */ |
|
| 16 | - private $api; |
|
| 17 | - |
|
| 18 | - /** @var Http\Request */ |
|
| 19 | - private $firstRequest; |
|
| 20 | - |
|
| 21 | - /** @var Http\Request|NULL */ |
|
| 22 | - private $request; |
|
| 23 | - |
|
| 24 | - /** @var Http\Response|NULL */ |
|
| 25 | - private $response; |
|
| 26 | - |
|
| 27 | - /** @var int */ |
|
| 28 | - private $limit; |
|
| 29 | - |
|
| 30 | - /** @var int */ |
|
| 31 | - private $counter = 0; |
|
| 32 | - |
|
| 33 | - |
|
| 34 | - public function __construct(Api $api, Http\Request $request) |
|
| 35 | - { |
|
| 36 | - $this->api = $api; |
|
| 37 | - $this->firstRequest = clone $request; |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * Limits maximum steps of iteration. |
|
| 43 | - * |
|
| 44 | - * @param int|NULL |
|
| 45 | - * @return self |
|
| 46 | - */ |
|
| 47 | - public function limit($limit) |
|
| 48 | - { |
|
| 49 | - $this->limit = $limit === NULL |
|
| 50 | - ? NULL |
|
| 51 | - : (int) $limit; |
|
| 52 | - |
|
| 53 | - return $this; |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * @return void |
|
| 59 | - */ |
|
| 60 | - public function rewind() |
|
| 61 | - { |
|
| 62 | - $this->request = $this->firstRequest; |
|
| 63 | - $this->response = NULL; |
|
| 64 | - $this->counter = 0; |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * @return bool |
|
| 70 | - */ |
|
| 71 | - public function valid() |
|
| 72 | - { |
|
| 73 | - return $this->request !== NULL && ($this->limit === NULL || $this->counter < $this->limit); |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * @return Http\Response |
|
| 79 | - */ |
|
| 80 | - public function current() |
|
| 81 | - { |
|
| 82 | - $this->load(); |
|
| 83 | - return $this->response; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * @return int |
|
| 89 | - */ |
|
| 90 | - public function key() |
|
| 91 | - { |
|
| 92 | - return static::parsePage($this->request->getUrl()); |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * @return void |
|
| 98 | - */ |
|
| 99 | - public function next() |
|
| 100 | - { |
|
| 101 | - $this->load(); |
|
| 102 | - |
|
| 103 | - if ($url = static::parseLink($this->response->getHeader('Link'), 'next')) { |
|
| 104 | - $this->request = new Http\Request( |
|
| 105 | - $this->request->getMethod(), |
|
| 106 | - $url, |
|
| 107 | - $this->request->getHeaders(), |
|
| 108 | - $this->request->getContent() |
|
| 109 | - ); |
|
| 110 | - } else { |
|
| 111 | - $this->request = NULL; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - $this->response = NULL; |
|
| 115 | - $this->counter++; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - |
|
| 119 | - private function load() |
|
| 120 | - { |
|
| 121 | - if ($this->response === NULL) { |
|
| 122 | - $this->response = $this->api->request($this->request); |
|
| 123 | - } |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * @param string |
|
| 129 | - * @return int |
|
| 130 | - */ |
|
| 131 | - public static function parsePage($url) |
|
| 132 | - { |
|
| 133 | - list (, $parametersStr) = explode('?', $url, 2) + ['', '']; |
|
| 134 | - parse_str($parametersStr, $parameters); |
|
| 135 | - |
|
| 136 | - return isset($parameters['page']) |
|
| 137 | - ? max(1, (int) $parameters['page']) |
|
| 138 | - : 1; |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * @see https://developer.github.com/guides/traversing-with-pagination/#navigating-through-the-pages |
|
| 144 | - * |
|
| 145 | - * @param string |
|
| 146 | - * @param string |
|
| 147 | - * @return string|NULL |
|
| 148 | - */ |
|
| 149 | - public static function parseLink($link, $rel) |
|
| 150 | - { |
|
| 151 | - if (!preg_match('(<([^>]+)>;\s*rel="' . preg_quote($rel) . '")', $link, $match)) { |
|
| 152 | - return NULL; |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - return $match[1]; |
|
| 156 | - } |
|
| 15 | + /** @var Api */ |
|
| 16 | + private $api; |
|
| 17 | + |
|
| 18 | + /** @var Http\Request */ |
|
| 19 | + private $firstRequest; |
|
| 20 | + |
|
| 21 | + /** @var Http\Request|NULL */ |
|
| 22 | + private $request; |
|
| 23 | + |
|
| 24 | + /** @var Http\Response|NULL */ |
|
| 25 | + private $response; |
|
| 26 | + |
|
| 27 | + /** @var int */ |
|
| 28 | + private $limit; |
|
| 29 | + |
|
| 30 | + /** @var int */ |
|
| 31 | + private $counter = 0; |
|
| 32 | + |
|
| 33 | + |
|
| 34 | + public function __construct(Api $api, Http\Request $request) |
|
| 35 | + { |
|
| 36 | + $this->api = $api; |
|
| 37 | + $this->firstRequest = clone $request; |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * Limits maximum steps of iteration. |
|
| 43 | + * |
|
| 44 | + * @param int|NULL |
|
| 45 | + * @return self |
|
| 46 | + */ |
|
| 47 | + public function limit($limit) |
|
| 48 | + { |
|
| 49 | + $this->limit = $limit === NULL |
|
| 50 | + ? NULL |
|
| 51 | + : (int) $limit; |
|
| 52 | + |
|
| 53 | + return $this; |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * @return void |
|
| 59 | + */ |
|
| 60 | + public function rewind() |
|
| 61 | + { |
|
| 62 | + $this->request = $this->firstRequest; |
|
| 63 | + $this->response = NULL; |
|
| 64 | + $this->counter = 0; |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * @return bool |
|
| 70 | + */ |
|
| 71 | + public function valid() |
|
| 72 | + { |
|
| 73 | + return $this->request !== NULL && ($this->limit === NULL || $this->counter < $this->limit); |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * @return Http\Response |
|
| 79 | + */ |
|
| 80 | + public function current() |
|
| 81 | + { |
|
| 82 | + $this->load(); |
|
| 83 | + return $this->response; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * @return int |
|
| 89 | + */ |
|
| 90 | + public function key() |
|
| 91 | + { |
|
| 92 | + return static::parsePage($this->request->getUrl()); |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * @return void |
|
| 98 | + */ |
|
| 99 | + public function next() |
|
| 100 | + { |
|
| 101 | + $this->load(); |
|
| 102 | + |
|
| 103 | + if ($url = static::parseLink($this->response->getHeader('Link'), 'next')) { |
|
| 104 | + $this->request = new Http\Request( |
|
| 105 | + $this->request->getMethod(), |
|
| 106 | + $url, |
|
| 107 | + $this->request->getHeaders(), |
|
| 108 | + $this->request->getContent() |
|
| 109 | + ); |
|
| 110 | + } else { |
|
| 111 | + $this->request = NULL; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + $this->response = NULL; |
|
| 115 | + $this->counter++; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + |
|
| 119 | + private function load() |
|
| 120 | + { |
|
| 121 | + if ($this->response === NULL) { |
|
| 122 | + $this->response = $this->api->request($this->request); |
|
| 123 | + } |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * @param string |
|
| 129 | + * @return int |
|
| 130 | + */ |
|
| 131 | + public static function parsePage($url) |
|
| 132 | + { |
|
| 133 | + list (, $parametersStr) = explode('?', $url, 2) + ['', '']; |
|
| 134 | + parse_str($parametersStr, $parameters); |
|
| 135 | + |
|
| 136 | + return isset($parameters['page']) |
|
| 137 | + ? max(1, (int) $parameters['page']) |
|
| 138 | + : 1; |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * @see https://developer.github.com/guides/traversing-with-pagination/#navigating-through-the-pages |
|
| 144 | + * |
|
| 145 | + * @param string |
|
| 146 | + * @param string |
|
| 147 | + * @return string|NULL |
|
| 148 | + */ |
|
| 149 | + public static function parseLink($link, $rel) |
|
| 150 | + { |
|
| 151 | + if (!preg_match('(<([^>]+)>;\s*rel="' . preg_quote($rel) . '")', $link, $match)) { |
|
| 152 | + return NULL; |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + return $match[1]; |
|
| 156 | + } |
|
| 157 | 157 | |
| 158 | 158 | } |
@@ -148,7 +148,7 @@ |
||
| 148 | 148 | */ |
| 149 | 149 | public static function parseLink($link, $rel) |
| 150 | 150 | { |
| 151 | - if (!preg_match('(<([^>]+)>;\s*rel="' . preg_quote($rel) . '")', $link, $match)) { |
|
| 151 | + if (!preg_match('(<([^>]+)>;\s*rel="'.preg_quote($rel).'")', $link, $match)) { |
|
| 152 | 152 | return NULL; |
| 153 | 153 | } |
| 154 | 154 | |
@@ -8,21 +8,21 @@ |
||
| 8 | 8 | */ |
| 9 | 9 | abstract class Sanity |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * @throws LogicException |
|
| 13 | - */ |
|
| 14 | - public function & __get($name) |
|
| 15 | - { |
|
| 16 | - throw new LogicException('Cannot read an undeclared property ' . get_class($this) . "::\$$name."); |
|
| 17 | - } |
|
| 11 | + /** |
|
| 12 | + * @throws LogicException |
|
| 13 | + */ |
|
| 14 | + public function & __get($name) |
|
| 15 | + { |
|
| 16 | + throw new LogicException('Cannot read an undeclared property ' . get_class($this) . "::\$$name."); |
|
| 17 | + } |
|
| 18 | 18 | |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * @throws LogicException |
|
| 22 | - */ |
|
| 23 | - public function __set($name, $value) |
|
| 24 | - { |
|
| 25 | - throw new LogicException('Cannot write to an undeclared property ' . get_class($this) . "::\$$name."); |
|
| 26 | - } |
|
| 20 | + /** |
|
| 21 | + * @throws LogicException |
|
| 22 | + */ |
|
| 23 | + public function __set($name, $value) |
|
| 24 | + { |
|
| 25 | + throw new LogicException('Cannot write to an undeclared property ' . get_class($this) . "::\$$name."); |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | 28 | } |
@@ -13,7 +13,7 @@ discard block |
||
| 13 | 13 | */ |
| 14 | 14 | public function & __get($name) |
| 15 | 15 | { |
| 16 | - throw new LogicException('Cannot read an undeclared property ' . get_class($this) . "::\$$name."); |
|
| 16 | + throw new LogicException('Cannot read an undeclared property '.get_class($this)."::\$$name."); |
|
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | |
@@ -22,7 +22,7 @@ discard block |
||
| 22 | 22 | */ |
| 23 | 23 | public function __set($name, $value) |
| 24 | 24 | { |
| 25 | - throw new LogicException('Cannot write to an undeclared property ' . get_class($this) . "::\$$name."); |
|
| 25 | + throw new LogicException('Cannot write to an undeclared property '.get_class($this)."::\$$name."); |
|
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | } |
@@ -12,595 +12,595 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class Api extends Sanity |
| 14 | 14 | { |
| 15 | - /** @var string */ |
|
| 16 | - private $url = 'https://api.github.com'; |
|
| 17 | - |
|
| 18 | - /** @var string */ |
|
| 19 | - private $defaultAccept = 'application/vnd.github.v3+json'; |
|
| 20 | - |
|
| 21 | - /** @var array|NULL */ |
|
| 22 | - private $defaultParameters = []; |
|
| 23 | - |
|
| 24 | - /** @var Http\IClient */ |
|
| 25 | - private $client; |
|
| 26 | - |
|
| 27 | - /** @var OAuth\Token|NULL */ |
|
| 28 | - private $token; |
|
| 29 | - |
|
| 30 | - |
|
| 31 | - public function __construct(Http\IClient $client = NULL) |
|
| 32 | - { |
|
| 33 | - $this->client = $client ?: Helpers::createDefaultClient(); |
|
| 34 | - } |
|
| 35 | - |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * @return self |
|
| 39 | - */ |
|
| 40 | - public function setToken(OAuth\Token $token = NULL) |
|
| 41 | - { |
|
| 42 | - $this->token = $token; |
|
| 43 | - return $this; |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * @return OAuth\Token|NULL |
|
| 49 | - */ |
|
| 50 | - public function getToken() |
|
| 51 | - { |
|
| 52 | - return $this->token; |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * @param array |
|
| 58 | - * @return self |
|
| 59 | - */ |
|
| 60 | - public function setDefaultParameters(array $defaults = NULL) |
|
| 61 | - { |
|
| 62 | - $this->defaultParameters = $defaults ?: []; |
|
| 63 | - return $this; |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * @return array |
|
| 69 | - */ |
|
| 70 | - public function getDefaultParameters() |
|
| 71 | - { |
|
| 72 | - return $this->defaultParameters; |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * @param string |
|
| 78 | - * @return self |
|
| 79 | - */ |
|
| 80 | - public function setDefaultAccept($accept) |
|
| 81 | - { |
|
| 82 | - $this->defaultAccept = $accept; |
|
| 83 | - return $this; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * @return string |
|
| 89 | - */ |
|
| 90 | - public function getDefaultAccept() |
|
| 91 | - { |
|
| 92 | - return $this->defaultAccept; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * @see createRequest() |
|
| 98 | - * @see request() |
|
| 99 | - * |
|
| 100 | - * @param string |
|
| 101 | - * @return Http\Response |
|
| 102 | - * |
|
| 103 | - * @throws MissingParameterException |
|
| 104 | - */ |
|
| 105 | - public function delete($urlPath, array $parameters = [], array $headers = []) |
|
| 106 | - { |
|
| 107 | - return $this->request( |
|
| 108 | - $this->createRequest(Http\Request::DELETE, $urlPath, $parameters, $headers) |
|
| 109 | - ); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * @see createRequest() |
|
| 115 | - * @see request() |
|
| 116 | - * |
|
| 117 | - * @param string |
|
| 118 | - * @return Http\Response |
|
| 119 | - * |
|
| 120 | - * @throws MissingParameterException |
|
| 121 | - */ |
|
| 122 | - public function get($urlPath, array $parameters = [], array $headers = []) |
|
| 123 | - { |
|
| 124 | - return $this->request( |
|
| 125 | - $this->createRequest(Http\Request::GET, $urlPath, $parameters, $headers) |
|
| 126 | - ); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * @see createRequest() |
|
| 132 | - * @see request() |
|
| 133 | - * |
|
| 134 | - * @param string |
|
| 135 | - * @return Http\Response |
|
| 136 | - * |
|
| 137 | - * @throws MissingParameterException |
|
| 138 | - */ |
|
| 139 | - public function head($urlPath, array $parameters = [], array $headers = []) |
|
| 140 | - { |
|
| 141 | - return $this->request( |
|
| 142 | - $this->createRequest(Http\Request::HEAD, $urlPath, $parameters, $headers) |
|
| 143 | - ); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * @see createRequest() |
|
| 149 | - * @see request() |
|
| 150 | - * |
|
| 151 | - * @param string |
|
| 152 | - * @param mixed |
|
| 153 | - * @return Http\Response |
|
| 154 | - * |
|
| 155 | - * @throws MissingParameterException |
|
| 156 | - * @throws JsonException |
|
| 157 | - */ |
|
| 158 | - public function patch($urlPath, $content, array $parameters = [], array $headers = []) |
|
| 159 | - { |
|
| 160 | - return $this->request( |
|
| 161 | - $this->createRequest(Http\Request::PATCH, $urlPath, $parameters, $headers, $content) |
|
| 162 | - ); |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * @see createRequest() |
|
| 168 | - * @see request() |
|
| 169 | - * |
|
| 170 | - * @param string |
|
| 171 | - * @param mixed |
|
| 172 | - * @return Http\Response |
|
| 173 | - * |
|
| 174 | - * @throws MissingParameterException |
|
| 175 | - * @throws JsonException |
|
| 176 | - */ |
|
| 177 | - public function post($urlPath, $content, array $parameters = [], array $headers = []) |
|
| 178 | - { |
|
| 179 | - return $this->request( |
|
| 180 | - $this->createRequest(Http\Request::POST, $urlPath, $parameters, $headers, $content) |
|
| 181 | - ); |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * @see createRequest() |
|
| 187 | - * @see request() |
|
| 188 | - * |
|
| 189 | - * @param string |
|
| 190 | - * @param mixed |
|
| 191 | - * @return Http\Response |
|
| 192 | - * |
|
| 193 | - * @throws MissingParameterException |
|
| 194 | - * @throws JsonException |
|
| 195 | - */ |
|
| 196 | - public function put($urlPath, $content = NULL, array $parameters = [], array $headers = []) |
|
| 197 | - { |
|
| 198 | - return $this->request( |
|
| 199 | - $this->createRequest(Http\Request::PUT, $urlPath, $parameters, $headers, $content) |
|
| 200 | - ); |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - |
|
| 204 | - /** |
|
| 205 | - * @return Http\Response |
|
| 206 | - * |
|
| 207 | - * @throws Http\BadResponseException |
|
| 208 | - */ |
|
| 209 | - public function request(Http\Request $request) |
|
| 210 | - { |
|
| 211 | - $request = clone $request; |
|
| 212 | - |
|
| 213 | - $request->addHeader('Accept', $this->defaultAccept); |
|
| 214 | - $request->addHeader('Time-Zone', date_default_timezone_get()); |
|
| 215 | - $request->addHeader('User-Agent', 'milo/github-api'); |
|
| 216 | - |
|
| 217 | - if ($this->token) { |
|
| 218 | - /** @todo Distinguish token type? */ |
|
| 219 | - $request->addHeader('Authorization', "token {$this->token->getValue()}"); |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - return $this->client->request($request); |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * @param string Http\Request::GET|POST|... |
|
| 228 | - * @param string path like '/users/:user/repos' where ':user' is substitution |
|
| 229 | - * @param array[name => value] replaces substitutions in $urlPath, the rest is appended as query string to URL |
|
| 230 | - * @param array[name => value] name is case-insensitive |
|
| 231 | - * @param mixed|NULL arrays and objects are encoded to JSON and Content-Type is set |
|
| 232 | - * @return Http\Request |
|
| 233 | - * |
|
| 234 | - * @throws MissingParameterException when substitution is used in URL but parameter is missing |
|
| 235 | - * @throws JsonException when encoding to JSON fails |
|
| 236 | - */ |
|
| 237 | - public function createRequest($method, $urlPath, array $parameters = [], array $headers = [], $content = NULL) |
|
| 238 | - { |
|
| 239 | - if (stripos($urlPath, $this->url) === 0) { # Allows non-HTTPS URLs |
|
| 240 | - $baseUrl = $this->url; |
|
| 241 | - $urlPath = substr($urlPath, strlen($this->url)); |
|
| 242 | - |
|
| 243 | - } elseif (preg_match('#^(https://[^/]+)(/.*)?$#', $urlPath, $m)) { |
|
| 244 | - $baseUrl = $m[1]; |
|
| 245 | - $urlPath = isset($m[2]) ? $m[2] : ''; |
|
| 246 | - |
|
| 247 | - } else { |
|
| 248 | - $baseUrl = $this->url; |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - if (strpos($urlPath, '{') === FALSE) { |
|
| 252 | - $urlPath = $this->expandColonParameters($urlPath, $parameters, $this->defaultParameters); |
|
| 253 | - } else { |
|
| 254 | - $urlPath = $this->expandUriTemplate($urlPath, $parameters, $this->defaultParameters); |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - $url = rtrim($baseUrl, '/') . '/' . ltrim($urlPath, '/'); |
|
| 258 | - |
|
| 259 | - if ($content !== NULL && (is_array($content) || is_object($content))) { |
|
| 260 | - $headers['Content-Type'] = 'application/json; charset=utf-8'; |
|
| 261 | - $content = Helpers::jsonEncode($content); |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - return new Http\Request($method, $url, $headers, $content); |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - |
|
| 268 | - /** |
|
| 269 | - * @param Http\Response |
|
| 270 | - * @param array|NULL these codes are treated as success; code < 300 if NULL |
|
| 271 | - * @return mixed |
|
| 272 | - * |
|
| 273 | - * @throws ApiException |
|
| 274 | - */ |
|
| 275 | - public function decode(Http\Response $response, array $okCodes = NULL) |
|
| 276 | - { |
|
| 277 | - $content = $response->getContent(); |
|
| 278 | - if (preg_match('~application/json~i', $response->getHeader('Content-Type', ''))) { |
|
| 279 | - try { |
|
| 280 | - $content = Helpers::jsonDecode($response->getContent(), true); |
|
| 281 | - } catch (JsonException $e) { |
|
| 282 | - throw new InvalidResponseException('JSON decoding failed.', 0, $e, $response); |
|
| 283 | - } |
|
| 284 | - |
|
| 285 | - if (!is_array($content) && !is_object($content)) { |
|
| 286 | - throw new InvalidResponseException('Decoded JSON is not an array or object.', 0, NULL, $response); |
|
| 287 | - } |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - $code = $response->getCode(); |
|
| 291 | - if (($okCodes === NULL && $code >= 300) || (is_array($okCodes) && !in_array($code, $okCodes))) { |
|
| 292 | - /** @var $content \stdClass */ |
|
| 293 | - switch ($code) { |
|
| 294 | - case Http\Response::S400_BAD_REQUEST: |
|
| 295 | - throw new BadRequestException(self::errorMessage($content), $code, NULL, $response); |
|
| 296 | - |
|
| 297 | - case Http\Response::S401_UNAUTHORIZED: |
|
| 298 | - throw new UnauthorizedException(self::errorMessage($content), $code, NULL, $response); |
|
| 299 | - |
|
| 300 | - case Http\Response::S403_FORBIDDEN: |
|
| 301 | - if ($response->getHeader('X-RateLimit-Remaining') === '0') { |
|
| 302 | - throw new RateLimitExceedException(self::errorMessage($content), $code, NULL, $response); |
|
| 303 | - } |
|
| 304 | - throw new ForbiddenException(self::errorMessage($content), $code, NULL, $response); |
|
| 305 | - |
|
| 306 | - case Http\Response::S404_NOT_FOUND: |
|
| 307 | - throw new NotFoundException('Resource not found or not authorized to access.', $code, NULL, $response); |
|
| 308 | - |
|
| 309 | - case Http\Response::S422_UNPROCESSABLE_ENTITY: |
|
| 310 | - throw new UnprocessableEntityException(self::errorMessage($content), $code, NULL, $response); |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - $message = $okCodes === NULL ? '< 300' : implode(' or ', $okCodes); |
|
| 314 | - throw new UnexpectedResponseException("Expected response with code $message.", $code, NULL, $response); |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - return $content; |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - |
|
| 321 | - /** |
|
| 322 | - * Creates paginator for HTTP GET requests. |
|
| 323 | - * |
|
| 324 | - * @see get() |
|
| 325 | - * |
|
| 326 | - * @param string |
|
| 327 | - * @return Paginator |
|
| 328 | - * |
|
| 329 | - * @throws MissingParameterException |
|
| 330 | - */ |
|
| 331 | - public function paginator($urlPath, array $parameters = [], array $headers = []) |
|
| 332 | - { |
|
| 333 | - return new Paginator( |
|
| 334 | - $this, |
|
| 335 | - $this->createRequest(Http\Request::GET, $urlPath, $parameters, $headers) |
|
| 336 | - ); |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - |
|
| 340 | - /** |
|
| 341 | - * @return Http\IClient |
|
| 342 | - */ |
|
| 343 | - public function getClient() |
|
| 344 | - { |
|
| 345 | - return $this->client; |
|
| 346 | - } |
|
| 347 | - |
|
| 348 | - |
|
| 349 | - /** |
|
| 350 | - * @param string |
|
| 351 | - * @return Api |
|
| 352 | - */ |
|
| 353 | - public function withUrl($url) |
|
| 354 | - { |
|
| 355 | - $api = clone $this; |
|
| 356 | - $api->setUrl($url); |
|
| 357 | - return $api; |
|
| 358 | - } |
|
| 359 | - |
|
| 360 | - |
|
| 361 | - /** |
|
| 362 | - * @param string |
|
| 363 | - * @return self |
|
| 364 | - */ |
|
| 365 | - public function setUrl($url) |
|
| 366 | - { |
|
| 367 | - $this->url = $url; |
|
| 368 | - return $this; |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - |
|
| 372 | - /** |
|
| 373 | - * @return string |
|
| 374 | - */ |
|
| 375 | - public function getUrl() |
|
| 376 | - { |
|
| 377 | - return $this->url; |
|
| 378 | - } |
|
| 379 | - |
|
| 380 | - |
|
| 381 | - /** |
|
| 382 | - * @param string |
|
| 383 | - * @return string |
|
| 384 | - * |
|
| 385 | - * @throws MissingParameterException |
|
| 386 | - */ |
|
| 387 | - protected function expandColonParameters($url, array $parameters, array $defaultParameters) |
|
| 388 | - { |
|
| 389 | - $parameters += $defaultParameters; |
|
| 390 | - |
|
| 391 | - $url = preg_replace_callback('#(^|/|\.):([^/.]+)#', function($m) use ($url, & $parameters) { |
|
| 392 | - if (!isset($parameters[$m[2]])) { |
|
| 393 | - throw new MissingParameterException("Missing parameter '$m[2]' for URL path '$url'."); |
|
| 394 | - } |
|
| 395 | - $parameter = $parameters[$m[2]]; |
|
| 396 | - unset($parameters[$m[2]]); |
|
| 397 | - return $m[1] . rawurlencode($parameter); |
|
| 398 | - }, $url); |
|
| 399 | - |
|
| 400 | - $url = rtrim($url, '/'); |
|
| 401 | - |
|
| 402 | - if (count($parameters)) { |
|
| 403 | - $url .= '?' . http_build_query($parameters); |
|
| 404 | - } |
|
| 405 | - |
|
| 406 | - return $url; |
|
| 407 | - } |
|
| 408 | - |
|
| 409 | - |
|
| 410 | - /** |
|
| 411 | - * Expands URI template (RFC 6570). |
|
| 412 | - * |
|
| 413 | - * @see http://tools.ietf.org/html/rfc6570 |
|
| 414 | - * @todo Inject remaining default parameters into query string? |
|
| 415 | - * |
|
| 416 | - * @param string |
|
| 417 | - * @return string |
|
| 418 | - */ |
|
| 419 | - protected function expandUriTemplate($url, array $parameters, array $defaultParameters) |
|
| 420 | - { |
|
| 421 | - $parameters += $defaultParameters; |
|
| 422 | - |
|
| 423 | - static $operatorFlags = [ |
|
| 424 | - '' => ['prefix' => '', 'separator' => ',', 'named' => FALSE, 'ifEmpty' => '', 'reserved' => FALSE], |
|
| 425 | - '+' => ['prefix' => '', 'separator' => ',', 'named' => FALSE, 'ifEmpty' => '', 'reserved' => TRUE], |
|
| 426 | - '#' => ['prefix' => '#', 'separator' => ',', 'named' => FALSE, 'ifEmpty' => '', 'reserved' => TRUE], |
|
| 427 | - '.' => ['prefix' => '.', 'separator' => '.', 'named' => FALSE, 'ifEmpty' => '', 'reserved' => FALSE], |
|
| 428 | - '/' => ['prefix' => '/', 'separator' => '/', 'named' => FALSE, 'ifEmpty' => '', 'reserved' => FALSE], |
|
| 429 | - ';' => ['prefix' => ';', 'separator' => ';', 'named' => TRUE, 'ifEmpty' => '', 'reserved' => FALSE], |
|
| 430 | - '?' => ['prefix' => '?', 'separator' => '&', 'named' => TRUE, 'ifEmpty' => '=', 'reserved' => FALSE], |
|
| 431 | - '&' => ['prefix' => '&', 'separator' => '&', 'named' => TRUE, 'ifEmpty' => '=', 'reserved' => FALSE], |
|
| 432 | - ]; |
|
| 433 | - |
|
| 434 | - return preg_replace_callback('~{([+#./;?&])?([^}]+?)}~', function($m) use ($url, & $parameters, $operatorFlags) { |
|
| 435 | - $flags = $operatorFlags[$m[1]]; |
|
| 436 | - |
|
| 437 | - $translated = []; |
|
| 438 | - foreach (explode(',', $m[2]) as $name) { |
|
| 439 | - $explode = FALSE; |
|
| 440 | - $maxLength = NULL; |
|
| 441 | - if (preg_match('~^(.+)(?:(\*)|:(\d+))$~', $name, $tmp)) { // TODO: Speed up? |
|
| 442 | - $name = $tmp[1]; |
|
| 443 | - if (isset($tmp[3])) { |
|
| 444 | - $maxLength = (int) $tmp[3]; |
|
| 445 | - } else { |
|
| 446 | - $explode = TRUE; |
|
| 447 | - } |
|
| 448 | - } |
|
| 449 | - |
|
| 450 | - if (!isset($parameters[$name])) { // TODO: Throw exception? |
|
| 451 | - continue; |
|
| 452 | - } |
|
| 453 | - |
|
| 454 | - $value = $parameters[$name]; |
|
| 455 | - if (is_scalar($value)) { |
|
| 456 | - $translated[] = $this->prefix($flags, $name, $this->escape($flags, $value, $maxLength)); |
|
| 457 | - |
|
| 458 | - } else { |
|
| 459 | - $value = (array) $value; |
|
| 460 | - $isAssoc = key($value) !== 0; |
|
| 461 | - |
|
| 462 | - // The '*' (explode) modifier |
|
| 463 | - if ($explode) { |
|
| 464 | - $parts = []; |
|
| 465 | - if ($isAssoc) { |
|
| 466 | - $this->walk($value, function ($v, $k) use (& $parts, $flags, $maxLength) { |
|
| 467 | - $parts[] = $this->prefix(['named' => TRUE] + $flags, $k, $this->escape($flags, $v, $maxLength)); |
|
| 468 | - }); |
|
| 469 | - |
|
| 470 | - } elseif ($flags['named']) { |
|
| 471 | - $this->walk($value, function ($v) use (& $parts, $flags, $name, $maxLength) { |
|
| 472 | - $parts[] = $this->prefix($flags, $name, $this->escape($flags, $v, $maxLength)); |
|
| 473 | - }); |
|
| 474 | - |
|
| 475 | - } else { |
|
| 476 | - $this->walk($value, function ($v) use (& $parts, $flags, $maxLength) { |
|
| 477 | - $parts[] = $this->escape($flags, $v, $maxLength); |
|
| 478 | - }); |
|
| 479 | - } |
|
| 480 | - |
|
| 481 | - if (isset($parts[0])) { |
|
| 482 | - if ($flags['named']) { |
|
| 483 | - $translated[] = implode($flags['separator'], $parts); |
|
| 484 | - } else { |
|
| 485 | - $translated[] = $this->prefix($flags, $name, implode($flags['separator'], $parts)); |
|
| 486 | - } |
|
| 487 | - } |
|
| 488 | - |
|
| 489 | - } else { |
|
| 490 | - $parts = []; |
|
| 491 | - $this->walk($value, function($v, $k) use (& $parts, $isAssoc, $flags, $maxLength) { |
|
| 492 | - if ($isAssoc) { |
|
| 493 | - $parts[] = $this->escape($flags, $k); |
|
| 494 | - } |
|
| 495 | - |
|
| 496 | - $parts[] = $this->escape($flags, $v, $maxLength); |
|
| 497 | - }); |
|
| 498 | - |
|
| 499 | - if (isset($parts[0])) { |
|
| 500 | - $translated[] = $this->prefix($flags, $name, implode(',', $parts)); |
|
| 501 | - } |
|
| 502 | - } |
|
| 503 | - } |
|
| 504 | - } |
|
| 505 | - |
|
| 506 | - if (isset($translated[0])) { |
|
| 507 | - return $flags['prefix'] . implode($flags['separator'], $translated); |
|
| 508 | - } |
|
| 509 | - |
|
| 510 | - return ''; |
|
| 511 | - }, $url); |
|
| 512 | - } |
|
| 513 | - |
|
| 514 | - |
|
| 515 | - /** |
|
| 516 | - * @param array |
|
| 517 | - * @param string |
|
| 518 | - * @param string already escaped |
|
| 519 | - * @return string |
|
| 520 | - */ |
|
| 521 | - private function prefix(array $flags, $name, $value) |
|
| 522 | - { |
|
| 523 | - $prefix = ''; |
|
| 524 | - if ($flags['named']) { |
|
| 525 | - $prefix .= $this->escape($flags, $name); |
|
| 526 | - if (isset($value[0])) { |
|
| 527 | - $prefix .= '='; |
|
| 528 | - } else { |
|
| 529 | - $prefix .= $flags['ifEmpty']; |
|
| 530 | - } |
|
| 531 | - } |
|
| 532 | - |
|
| 533 | - return $prefix . $value; |
|
| 534 | - } |
|
| 535 | - |
|
| 536 | - |
|
| 537 | - /** |
|
| 538 | - * @param array |
|
| 539 | - * @param mixed |
|
| 540 | - * @param int|NULL |
|
| 541 | - * @return string |
|
| 542 | - */ |
|
| 543 | - private function escape(array $flags, $value, $maxLength = NULL) |
|
| 544 | - { |
|
| 545 | - $value = (string) $value; |
|
| 546 | - |
|
| 547 | - if ($maxLength !== NULL) { |
|
| 548 | - if (preg_match('~^(.{' . $maxLength . '}).~u', $value, $m)) { |
|
| 549 | - $value = $m[1]; |
|
| 550 | - } elseif (strlen($value) > $maxLength) { # when malformed UTF-8 |
|
| 551 | - $value = substr($value, 0, $maxLength); |
|
| 552 | - } |
|
| 553 | - } |
|
| 554 | - |
|
| 555 | - if ($flags['reserved']) { |
|
| 556 | - $parts = preg_split('~(%[0-9a-fA-F]{2}|[:/?#[\]@!$&\'()*+,;=])~', $value, -1, PREG_SPLIT_DELIM_CAPTURE); |
|
| 557 | - $parts[] = ''; |
|
| 558 | - |
|
| 559 | - $escaped = ''; |
|
| 560 | - for ($i = 0, $count = count($parts); $i < $count; $i += 2) { |
|
| 561 | - $escaped .= rawurlencode($parts[$i]) . $parts[$i + 1]; |
|
| 562 | - } |
|
| 563 | - |
|
| 564 | - return $escaped; |
|
| 565 | - } |
|
| 566 | - |
|
| 567 | - return rawurlencode($value); |
|
| 568 | - } |
|
| 569 | - |
|
| 570 | - |
|
| 571 | - /** |
|
| 572 | - * @param array |
|
| 573 | - * @param callable |
|
| 574 | - */ |
|
| 575 | - private function walk(array $array, $cb) |
|
| 576 | - { |
|
| 577 | - foreach ($array as $k => $v) { |
|
| 578 | - if ($v === NULL) { |
|
| 579 | - continue; |
|
| 580 | - } |
|
| 581 | - |
|
| 582 | - $cb($v, $k); |
|
| 583 | - } |
|
| 584 | - } |
|
| 585 | - |
|
| 586 | - |
|
| 587 | - /** |
|
| 588 | - * @param \stdClass |
|
| 589 | - * @return string |
|
| 590 | - */ |
|
| 591 | - private static function errorMessage($content) |
|
| 592 | - { |
|
| 593 | - $message = isset($content->message) |
|
| 594 | - ? $content->message |
|
| 595 | - : 'Unknown error'; |
|
| 596 | - |
|
| 597 | - if (isset($content->errors)) { |
|
| 598 | - $message .= implode(', ', array_map(function($error) { |
|
| 599 | - return '[' . implode(':', (array) $error) . ']'; |
|
| 600 | - }, $content->errors)); |
|
| 601 | - } |
|
| 602 | - |
|
| 603 | - return $message; |
|
| 604 | - } |
|
| 15 | + /** @var string */ |
|
| 16 | + private $url = 'https://api.github.com'; |
|
| 17 | + |
|
| 18 | + /** @var string */ |
|
| 19 | + private $defaultAccept = 'application/vnd.github.v3+json'; |
|
| 20 | + |
|
| 21 | + /** @var array|NULL */ |
|
| 22 | + private $defaultParameters = []; |
|
| 23 | + |
|
| 24 | + /** @var Http\IClient */ |
|
| 25 | + private $client; |
|
| 26 | + |
|
| 27 | + /** @var OAuth\Token|NULL */ |
|
| 28 | + private $token; |
|
| 29 | + |
|
| 30 | + |
|
| 31 | + public function __construct(Http\IClient $client = NULL) |
|
| 32 | + { |
|
| 33 | + $this->client = $client ?: Helpers::createDefaultClient(); |
|
| 34 | + } |
|
| 35 | + |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * @return self |
|
| 39 | + */ |
|
| 40 | + public function setToken(OAuth\Token $token = NULL) |
|
| 41 | + { |
|
| 42 | + $this->token = $token; |
|
| 43 | + return $this; |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * @return OAuth\Token|NULL |
|
| 49 | + */ |
|
| 50 | + public function getToken() |
|
| 51 | + { |
|
| 52 | + return $this->token; |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * @param array |
|
| 58 | + * @return self |
|
| 59 | + */ |
|
| 60 | + public function setDefaultParameters(array $defaults = NULL) |
|
| 61 | + { |
|
| 62 | + $this->defaultParameters = $defaults ?: []; |
|
| 63 | + return $this; |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * @return array |
|
| 69 | + */ |
|
| 70 | + public function getDefaultParameters() |
|
| 71 | + { |
|
| 72 | + return $this->defaultParameters; |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * @param string |
|
| 78 | + * @return self |
|
| 79 | + */ |
|
| 80 | + public function setDefaultAccept($accept) |
|
| 81 | + { |
|
| 82 | + $this->defaultAccept = $accept; |
|
| 83 | + return $this; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * @return string |
|
| 89 | + */ |
|
| 90 | + public function getDefaultAccept() |
|
| 91 | + { |
|
| 92 | + return $this->defaultAccept; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * @see createRequest() |
|
| 98 | + * @see request() |
|
| 99 | + * |
|
| 100 | + * @param string |
|
| 101 | + * @return Http\Response |
|
| 102 | + * |
|
| 103 | + * @throws MissingParameterException |
|
| 104 | + */ |
|
| 105 | + public function delete($urlPath, array $parameters = [], array $headers = []) |
|
| 106 | + { |
|
| 107 | + return $this->request( |
|
| 108 | + $this->createRequest(Http\Request::DELETE, $urlPath, $parameters, $headers) |
|
| 109 | + ); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * @see createRequest() |
|
| 115 | + * @see request() |
|
| 116 | + * |
|
| 117 | + * @param string |
|
| 118 | + * @return Http\Response |
|
| 119 | + * |
|
| 120 | + * @throws MissingParameterException |
|
| 121 | + */ |
|
| 122 | + public function get($urlPath, array $parameters = [], array $headers = []) |
|
| 123 | + { |
|
| 124 | + return $this->request( |
|
| 125 | + $this->createRequest(Http\Request::GET, $urlPath, $parameters, $headers) |
|
| 126 | + ); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * @see createRequest() |
|
| 132 | + * @see request() |
|
| 133 | + * |
|
| 134 | + * @param string |
|
| 135 | + * @return Http\Response |
|
| 136 | + * |
|
| 137 | + * @throws MissingParameterException |
|
| 138 | + */ |
|
| 139 | + public function head($urlPath, array $parameters = [], array $headers = []) |
|
| 140 | + { |
|
| 141 | + return $this->request( |
|
| 142 | + $this->createRequest(Http\Request::HEAD, $urlPath, $parameters, $headers) |
|
| 143 | + ); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * @see createRequest() |
|
| 149 | + * @see request() |
|
| 150 | + * |
|
| 151 | + * @param string |
|
| 152 | + * @param mixed |
|
| 153 | + * @return Http\Response |
|
| 154 | + * |
|
| 155 | + * @throws MissingParameterException |
|
| 156 | + * @throws JsonException |
|
| 157 | + */ |
|
| 158 | + public function patch($urlPath, $content, array $parameters = [], array $headers = []) |
|
| 159 | + { |
|
| 160 | + return $this->request( |
|
| 161 | + $this->createRequest(Http\Request::PATCH, $urlPath, $parameters, $headers, $content) |
|
| 162 | + ); |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * @see createRequest() |
|
| 168 | + * @see request() |
|
| 169 | + * |
|
| 170 | + * @param string |
|
| 171 | + * @param mixed |
|
| 172 | + * @return Http\Response |
|
| 173 | + * |
|
| 174 | + * @throws MissingParameterException |
|
| 175 | + * @throws JsonException |
|
| 176 | + */ |
|
| 177 | + public function post($urlPath, $content, array $parameters = [], array $headers = []) |
|
| 178 | + { |
|
| 179 | + return $this->request( |
|
| 180 | + $this->createRequest(Http\Request::POST, $urlPath, $parameters, $headers, $content) |
|
| 181 | + ); |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * @see createRequest() |
|
| 187 | + * @see request() |
|
| 188 | + * |
|
| 189 | + * @param string |
|
| 190 | + * @param mixed |
|
| 191 | + * @return Http\Response |
|
| 192 | + * |
|
| 193 | + * @throws MissingParameterException |
|
| 194 | + * @throws JsonException |
|
| 195 | + */ |
|
| 196 | + public function put($urlPath, $content = NULL, array $parameters = [], array $headers = []) |
|
| 197 | + { |
|
| 198 | + return $this->request( |
|
| 199 | + $this->createRequest(Http\Request::PUT, $urlPath, $parameters, $headers, $content) |
|
| 200 | + ); |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + |
|
| 204 | + /** |
|
| 205 | + * @return Http\Response |
|
| 206 | + * |
|
| 207 | + * @throws Http\BadResponseException |
|
| 208 | + */ |
|
| 209 | + public function request(Http\Request $request) |
|
| 210 | + { |
|
| 211 | + $request = clone $request; |
|
| 212 | + |
|
| 213 | + $request->addHeader('Accept', $this->defaultAccept); |
|
| 214 | + $request->addHeader('Time-Zone', date_default_timezone_get()); |
|
| 215 | + $request->addHeader('User-Agent', 'milo/github-api'); |
|
| 216 | + |
|
| 217 | + if ($this->token) { |
|
| 218 | + /** @todo Distinguish token type? */ |
|
| 219 | + $request->addHeader('Authorization', "token {$this->token->getValue()}"); |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + return $this->client->request($request); |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * @param string Http\Request::GET|POST|... |
|
| 228 | + * @param string path like '/users/:user/repos' where ':user' is substitution |
|
| 229 | + * @param array[name => value] replaces substitutions in $urlPath, the rest is appended as query string to URL |
|
| 230 | + * @param array[name => value] name is case-insensitive |
|
| 231 | + * @param mixed|NULL arrays and objects are encoded to JSON and Content-Type is set |
|
| 232 | + * @return Http\Request |
|
| 233 | + * |
|
| 234 | + * @throws MissingParameterException when substitution is used in URL but parameter is missing |
|
| 235 | + * @throws JsonException when encoding to JSON fails |
|
| 236 | + */ |
|
| 237 | + public function createRequest($method, $urlPath, array $parameters = [], array $headers = [], $content = NULL) |
|
| 238 | + { |
|
| 239 | + if (stripos($urlPath, $this->url) === 0) { # Allows non-HTTPS URLs |
|
| 240 | + $baseUrl = $this->url; |
|
| 241 | + $urlPath = substr($urlPath, strlen($this->url)); |
|
| 242 | + |
|
| 243 | + } elseif (preg_match('#^(https://[^/]+)(/.*)?$#', $urlPath, $m)) { |
|
| 244 | + $baseUrl = $m[1]; |
|
| 245 | + $urlPath = isset($m[2]) ? $m[2] : ''; |
|
| 246 | + |
|
| 247 | + } else { |
|
| 248 | + $baseUrl = $this->url; |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + if (strpos($urlPath, '{') === FALSE) { |
|
| 252 | + $urlPath = $this->expandColonParameters($urlPath, $parameters, $this->defaultParameters); |
|
| 253 | + } else { |
|
| 254 | + $urlPath = $this->expandUriTemplate($urlPath, $parameters, $this->defaultParameters); |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + $url = rtrim($baseUrl, '/') . '/' . ltrim($urlPath, '/'); |
|
| 258 | + |
|
| 259 | + if ($content !== NULL && (is_array($content) || is_object($content))) { |
|
| 260 | + $headers['Content-Type'] = 'application/json; charset=utf-8'; |
|
| 261 | + $content = Helpers::jsonEncode($content); |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + return new Http\Request($method, $url, $headers, $content); |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + |
|
| 268 | + /** |
|
| 269 | + * @param Http\Response |
|
| 270 | + * @param array|NULL these codes are treated as success; code < 300 if NULL |
|
| 271 | + * @return mixed |
|
| 272 | + * |
|
| 273 | + * @throws ApiException |
|
| 274 | + */ |
|
| 275 | + public function decode(Http\Response $response, array $okCodes = NULL) |
|
| 276 | + { |
|
| 277 | + $content = $response->getContent(); |
|
| 278 | + if (preg_match('~application/json~i', $response->getHeader('Content-Type', ''))) { |
|
| 279 | + try { |
|
| 280 | + $content = Helpers::jsonDecode($response->getContent(), true); |
|
| 281 | + } catch (JsonException $e) { |
|
| 282 | + throw new InvalidResponseException('JSON decoding failed.', 0, $e, $response); |
|
| 283 | + } |
|
| 284 | + |
|
| 285 | + if (!is_array($content) && !is_object($content)) { |
|
| 286 | + throw new InvalidResponseException('Decoded JSON is not an array or object.', 0, NULL, $response); |
|
| 287 | + } |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + $code = $response->getCode(); |
|
| 291 | + if (($okCodes === NULL && $code >= 300) || (is_array($okCodes) && !in_array($code, $okCodes))) { |
|
| 292 | + /** @var $content \stdClass */ |
|
| 293 | + switch ($code) { |
|
| 294 | + case Http\Response::S400_BAD_REQUEST: |
|
| 295 | + throw new BadRequestException(self::errorMessage($content), $code, NULL, $response); |
|
| 296 | + |
|
| 297 | + case Http\Response::S401_UNAUTHORIZED: |
|
| 298 | + throw new UnauthorizedException(self::errorMessage($content), $code, NULL, $response); |
|
| 299 | + |
|
| 300 | + case Http\Response::S403_FORBIDDEN: |
|
| 301 | + if ($response->getHeader('X-RateLimit-Remaining') === '0') { |
|
| 302 | + throw new RateLimitExceedException(self::errorMessage($content), $code, NULL, $response); |
|
| 303 | + } |
|
| 304 | + throw new ForbiddenException(self::errorMessage($content), $code, NULL, $response); |
|
| 305 | + |
|
| 306 | + case Http\Response::S404_NOT_FOUND: |
|
| 307 | + throw new NotFoundException('Resource not found or not authorized to access.', $code, NULL, $response); |
|
| 308 | + |
|
| 309 | + case Http\Response::S422_UNPROCESSABLE_ENTITY: |
|
| 310 | + throw new UnprocessableEntityException(self::errorMessage($content), $code, NULL, $response); |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + $message = $okCodes === NULL ? '< 300' : implode(' or ', $okCodes); |
|
| 314 | + throw new UnexpectedResponseException("Expected response with code $message.", $code, NULL, $response); |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + return $content; |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + |
|
| 321 | + /** |
|
| 322 | + * Creates paginator for HTTP GET requests. |
|
| 323 | + * |
|
| 324 | + * @see get() |
|
| 325 | + * |
|
| 326 | + * @param string |
|
| 327 | + * @return Paginator |
|
| 328 | + * |
|
| 329 | + * @throws MissingParameterException |
|
| 330 | + */ |
|
| 331 | + public function paginator($urlPath, array $parameters = [], array $headers = []) |
|
| 332 | + { |
|
| 333 | + return new Paginator( |
|
| 334 | + $this, |
|
| 335 | + $this->createRequest(Http\Request::GET, $urlPath, $parameters, $headers) |
|
| 336 | + ); |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + |
|
| 340 | + /** |
|
| 341 | + * @return Http\IClient |
|
| 342 | + */ |
|
| 343 | + public function getClient() |
|
| 344 | + { |
|
| 345 | + return $this->client; |
|
| 346 | + } |
|
| 347 | + |
|
| 348 | + |
|
| 349 | + /** |
|
| 350 | + * @param string |
|
| 351 | + * @return Api |
|
| 352 | + */ |
|
| 353 | + public function withUrl($url) |
|
| 354 | + { |
|
| 355 | + $api = clone $this; |
|
| 356 | + $api->setUrl($url); |
|
| 357 | + return $api; |
|
| 358 | + } |
|
| 359 | + |
|
| 360 | + |
|
| 361 | + /** |
|
| 362 | + * @param string |
|
| 363 | + * @return self |
|
| 364 | + */ |
|
| 365 | + public function setUrl($url) |
|
| 366 | + { |
|
| 367 | + $this->url = $url; |
|
| 368 | + return $this; |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + |
|
| 372 | + /** |
|
| 373 | + * @return string |
|
| 374 | + */ |
|
| 375 | + public function getUrl() |
|
| 376 | + { |
|
| 377 | + return $this->url; |
|
| 378 | + } |
|
| 379 | + |
|
| 380 | + |
|
| 381 | + /** |
|
| 382 | + * @param string |
|
| 383 | + * @return string |
|
| 384 | + * |
|
| 385 | + * @throws MissingParameterException |
|
| 386 | + */ |
|
| 387 | + protected function expandColonParameters($url, array $parameters, array $defaultParameters) |
|
| 388 | + { |
|
| 389 | + $parameters += $defaultParameters; |
|
| 390 | + |
|
| 391 | + $url = preg_replace_callback('#(^|/|\.):([^/.]+)#', function($m) use ($url, & $parameters) { |
|
| 392 | + if (!isset($parameters[$m[2]])) { |
|
| 393 | + throw new MissingParameterException("Missing parameter '$m[2]' for URL path '$url'."); |
|
| 394 | + } |
|
| 395 | + $parameter = $parameters[$m[2]]; |
|
| 396 | + unset($parameters[$m[2]]); |
|
| 397 | + return $m[1] . rawurlencode($parameter); |
|
| 398 | + }, $url); |
|
| 399 | + |
|
| 400 | + $url = rtrim($url, '/'); |
|
| 401 | + |
|
| 402 | + if (count($parameters)) { |
|
| 403 | + $url .= '?' . http_build_query($parameters); |
|
| 404 | + } |
|
| 405 | + |
|
| 406 | + return $url; |
|
| 407 | + } |
|
| 408 | + |
|
| 409 | + |
|
| 410 | + /** |
|
| 411 | + * Expands URI template (RFC 6570). |
|
| 412 | + * |
|
| 413 | + * @see http://tools.ietf.org/html/rfc6570 |
|
| 414 | + * @todo Inject remaining default parameters into query string? |
|
| 415 | + * |
|
| 416 | + * @param string |
|
| 417 | + * @return string |
|
| 418 | + */ |
|
| 419 | + protected function expandUriTemplate($url, array $parameters, array $defaultParameters) |
|
| 420 | + { |
|
| 421 | + $parameters += $defaultParameters; |
|
| 422 | + |
|
| 423 | + static $operatorFlags = [ |
|
| 424 | + '' => ['prefix' => '', 'separator' => ',', 'named' => FALSE, 'ifEmpty' => '', 'reserved' => FALSE], |
|
| 425 | + '+' => ['prefix' => '', 'separator' => ',', 'named' => FALSE, 'ifEmpty' => '', 'reserved' => TRUE], |
|
| 426 | + '#' => ['prefix' => '#', 'separator' => ',', 'named' => FALSE, 'ifEmpty' => '', 'reserved' => TRUE], |
|
| 427 | + '.' => ['prefix' => '.', 'separator' => '.', 'named' => FALSE, 'ifEmpty' => '', 'reserved' => FALSE], |
|
| 428 | + '/' => ['prefix' => '/', 'separator' => '/', 'named' => FALSE, 'ifEmpty' => '', 'reserved' => FALSE], |
|
| 429 | + ';' => ['prefix' => ';', 'separator' => ';', 'named' => TRUE, 'ifEmpty' => '', 'reserved' => FALSE], |
|
| 430 | + '?' => ['prefix' => '?', 'separator' => '&', 'named' => TRUE, 'ifEmpty' => '=', 'reserved' => FALSE], |
|
| 431 | + '&' => ['prefix' => '&', 'separator' => '&', 'named' => TRUE, 'ifEmpty' => '=', 'reserved' => FALSE], |
|
| 432 | + ]; |
|
| 433 | + |
|
| 434 | + return preg_replace_callback('~{([+#./;?&])?([^}]+?)}~', function($m) use ($url, & $parameters, $operatorFlags) { |
|
| 435 | + $flags = $operatorFlags[$m[1]]; |
|
| 436 | + |
|
| 437 | + $translated = []; |
|
| 438 | + foreach (explode(',', $m[2]) as $name) { |
|
| 439 | + $explode = FALSE; |
|
| 440 | + $maxLength = NULL; |
|
| 441 | + if (preg_match('~^(.+)(?:(\*)|:(\d+))$~', $name, $tmp)) { // TODO: Speed up? |
|
| 442 | + $name = $tmp[1]; |
|
| 443 | + if (isset($tmp[3])) { |
|
| 444 | + $maxLength = (int) $tmp[3]; |
|
| 445 | + } else { |
|
| 446 | + $explode = TRUE; |
|
| 447 | + } |
|
| 448 | + } |
|
| 449 | + |
|
| 450 | + if (!isset($parameters[$name])) { // TODO: Throw exception? |
|
| 451 | + continue; |
|
| 452 | + } |
|
| 453 | + |
|
| 454 | + $value = $parameters[$name]; |
|
| 455 | + if (is_scalar($value)) { |
|
| 456 | + $translated[] = $this->prefix($flags, $name, $this->escape($flags, $value, $maxLength)); |
|
| 457 | + |
|
| 458 | + } else { |
|
| 459 | + $value = (array) $value; |
|
| 460 | + $isAssoc = key($value) !== 0; |
|
| 461 | + |
|
| 462 | + // The '*' (explode) modifier |
|
| 463 | + if ($explode) { |
|
| 464 | + $parts = []; |
|
| 465 | + if ($isAssoc) { |
|
| 466 | + $this->walk($value, function ($v, $k) use (& $parts, $flags, $maxLength) { |
|
| 467 | + $parts[] = $this->prefix(['named' => TRUE] + $flags, $k, $this->escape($flags, $v, $maxLength)); |
|
| 468 | + }); |
|
| 469 | + |
|
| 470 | + } elseif ($flags['named']) { |
|
| 471 | + $this->walk($value, function ($v) use (& $parts, $flags, $name, $maxLength) { |
|
| 472 | + $parts[] = $this->prefix($flags, $name, $this->escape($flags, $v, $maxLength)); |
|
| 473 | + }); |
|
| 474 | + |
|
| 475 | + } else { |
|
| 476 | + $this->walk($value, function ($v) use (& $parts, $flags, $maxLength) { |
|
| 477 | + $parts[] = $this->escape($flags, $v, $maxLength); |
|
| 478 | + }); |
|
| 479 | + } |
|
| 480 | + |
|
| 481 | + if (isset($parts[0])) { |
|
| 482 | + if ($flags['named']) { |
|
| 483 | + $translated[] = implode($flags['separator'], $parts); |
|
| 484 | + } else { |
|
| 485 | + $translated[] = $this->prefix($flags, $name, implode($flags['separator'], $parts)); |
|
| 486 | + } |
|
| 487 | + } |
|
| 488 | + |
|
| 489 | + } else { |
|
| 490 | + $parts = []; |
|
| 491 | + $this->walk($value, function($v, $k) use (& $parts, $isAssoc, $flags, $maxLength) { |
|
| 492 | + if ($isAssoc) { |
|
| 493 | + $parts[] = $this->escape($flags, $k); |
|
| 494 | + } |
|
| 495 | + |
|
| 496 | + $parts[] = $this->escape($flags, $v, $maxLength); |
|
| 497 | + }); |
|
| 498 | + |
|
| 499 | + if (isset($parts[0])) { |
|
| 500 | + $translated[] = $this->prefix($flags, $name, implode(',', $parts)); |
|
| 501 | + } |
|
| 502 | + } |
|
| 503 | + } |
|
| 504 | + } |
|
| 505 | + |
|
| 506 | + if (isset($translated[0])) { |
|
| 507 | + return $flags['prefix'] . implode($flags['separator'], $translated); |
|
| 508 | + } |
|
| 509 | + |
|
| 510 | + return ''; |
|
| 511 | + }, $url); |
|
| 512 | + } |
|
| 513 | + |
|
| 514 | + |
|
| 515 | + /** |
|
| 516 | + * @param array |
|
| 517 | + * @param string |
|
| 518 | + * @param string already escaped |
|
| 519 | + * @return string |
|
| 520 | + */ |
|
| 521 | + private function prefix(array $flags, $name, $value) |
|
| 522 | + { |
|
| 523 | + $prefix = ''; |
|
| 524 | + if ($flags['named']) { |
|
| 525 | + $prefix .= $this->escape($flags, $name); |
|
| 526 | + if (isset($value[0])) { |
|
| 527 | + $prefix .= '='; |
|
| 528 | + } else { |
|
| 529 | + $prefix .= $flags['ifEmpty']; |
|
| 530 | + } |
|
| 531 | + } |
|
| 532 | + |
|
| 533 | + return $prefix . $value; |
|
| 534 | + } |
|
| 535 | + |
|
| 536 | + |
|
| 537 | + /** |
|
| 538 | + * @param array |
|
| 539 | + * @param mixed |
|
| 540 | + * @param int|NULL |
|
| 541 | + * @return string |
|
| 542 | + */ |
|
| 543 | + private function escape(array $flags, $value, $maxLength = NULL) |
|
| 544 | + { |
|
| 545 | + $value = (string) $value; |
|
| 546 | + |
|
| 547 | + if ($maxLength !== NULL) { |
|
| 548 | + if (preg_match('~^(.{' . $maxLength . '}).~u', $value, $m)) { |
|
| 549 | + $value = $m[1]; |
|
| 550 | + } elseif (strlen($value) > $maxLength) { # when malformed UTF-8 |
|
| 551 | + $value = substr($value, 0, $maxLength); |
|
| 552 | + } |
|
| 553 | + } |
|
| 554 | + |
|
| 555 | + if ($flags['reserved']) { |
|
| 556 | + $parts = preg_split('~(%[0-9a-fA-F]{2}|[:/?#[\]@!$&\'()*+,;=])~', $value, -1, PREG_SPLIT_DELIM_CAPTURE); |
|
| 557 | + $parts[] = ''; |
|
| 558 | + |
|
| 559 | + $escaped = ''; |
|
| 560 | + for ($i = 0, $count = count($parts); $i < $count; $i += 2) { |
|
| 561 | + $escaped .= rawurlencode($parts[$i]) . $parts[$i + 1]; |
|
| 562 | + } |
|
| 563 | + |
|
| 564 | + return $escaped; |
|
| 565 | + } |
|
| 566 | + |
|
| 567 | + return rawurlencode($value); |
|
| 568 | + } |
|
| 569 | + |
|
| 570 | + |
|
| 571 | + /** |
|
| 572 | + * @param array |
|
| 573 | + * @param callable |
|
| 574 | + */ |
|
| 575 | + private function walk(array $array, $cb) |
|
| 576 | + { |
|
| 577 | + foreach ($array as $k => $v) { |
|
| 578 | + if ($v === NULL) { |
|
| 579 | + continue; |
|
| 580 | + } |
|
| 581 | + |
|
| 582 | + $cb($v, $k); |
|
| 583 | + } |
|
| 584 | + } |
|
| 585 | + |
|
| 586 | + |
|
| 587 | + /** |
|
| 588 | + * @param \stdClass |
|
| 589 | + * @return string |
|
| 590 | + */ |
|
| 591 | + private static function errorMessage($content) |
|
| 592 | + { |
|
| 593 | + $message = isset($content->message) |
|
| 594 | + ? $content->message |
|
| 595 | + : 'Unknown error'; |
|
| 596 | + |
|
| 597 | + if (isset($content->errors)) { |
|
| 598 | + $message .= implode(', ', array_map(function($error) { |
|
| 599 | + return '[' . implode(':', (array) $error) . ']'; |
|
| 600 | + }, $content->errors)); |
|
| 601 | + } |
|
| 602 | + |
|
| 603 | + return $message; |
|
| 604 | + } |
|
| 605 | 605 | |
| 606 | 606 | } |
@@ -254,7 +254,7 @@ discard block |
||
| 254 | 254 | $urlPath = $this->expandUriTemplate($urlPath, $parameters, $this->defaultParameters); |
| 255 | 255 | } |
| 256 | 256 | |
| 257 | - $url = rtrim($baseUrl, '/') . '/' . ltrim($urlPath, '/'); |
|
| 257 | + $url = rtrim($baseUrl, '/').'/'.ltrim($urlPath, '/'); |
|
| 258 | 258 | |
| 259 | 259 | if ($content !== NULL && (is_array($content) || is_object($content))) { |
| 260 | 260 | $headers['Content-Type'] = 'application/json; charset=utf-8'; |
@@ -394,13 +394,13 @@ discard block |
||
| 394 | 394 | } |
| 395 | 395 | $parameter = $parameters[$m[2]]; |
| 396 | 396 | unset($parameters[$m[2]]); |
| 397 | - return $m[1] . rawurlencode($parameter); |
|
| 397 | + return $m[1].rawurlencode($parameter); |
|
| 398 | 398 | }, $url); |
| 399 | 399 | |
| 400 | 400 | $url = rtrim($url, '/'); |
| 401 | 401 | |
| 402 | 402 | if (count($parameters)) { |
| 403 | - $url .= '?' . http_build_query($parameters); |
|
| 403 | + $url .= '?'.http_build_query($parameters); |
|
| 404 | 404 | } |
| 405 | 405 | |
| 406 | 406 | return $url; |
@@ -421,14 +421,14 @@ discard block |
||
| 421 | 421 | $parameters += $defaultParameters; |
| 422 | 422 | |
| 423 | 423 | static $operatorFlags = [ |
| 424 | - '' => ['prefix' => '', 'separator' => ',', 'named' => FALSE, 'ifEmpty' => '', 'reserved' => FALSE], |
|
| 425 | - '+' => ['prefix' => '', 'separator' => ',', 'named' => FALSE, 'ifEmpty' => '', 'reserved' => TRUE], |
|
| 426 | - '#' => ['prefix' => '#', 'separator' => ',', 'named' => FALSE, 'ifEmpty' => '', 'reserved' => TRUE], |
|
| 427 | - '.' => ['prefix' => '.', 'separator' => '.', 'named' => FALSE, 'ifEmpty' => '', 'reserved' => FALSE], |
|
| 428 | - '/' => ['prefix' => '/', 'separator' => '/', 'named' => FALSE, 'ifEmpty' => '', 'reserved' => FALSE], |
|
| 429 | - ';' => ['prefix' => ';', 'separator' => ';', 'named' => TRUE, 'ifEmpty' => '', 'reserved' => FALSE], |
|
| 430 | - '?' => ['prefix' => '?', 'separator' => '&', 'named' => TRUE, 'ifEmpty' => '=', 'reserved' => FALSE], |
|
| 431 | - '&' => ['prefix' => '&', 'separator' => '&', 'named' => TRUE, 'ifEmpty' => '=', 'reserved' => FALSE], |
|
| 424 | + '' => ['prefix' => '', 'separator' => ',', 'named' => FALSE, 'ifEmpty' => '', 'reserved' => FALSE], |
|
| 425 | + '+' => ['prefix' => '', 'separator' => ',', 'named' => FALSE, 'ifEmpty' => '', 'reserved' => TRUE], |
|
| 426 | + '#' => ['prefix' => '#', 'separator' => ',', 'named' => FALSE, 'ifEmpty' => '', 'reserved' => TRUE], |
|
| 427 | + '.' => ['prefix' => '.', 'separator' => '.', 'named' => FALSE, 'ifEmpty' => '', 'reserved' => FALSE], |
|
| 428 | + '/' => ['prefix' => '/', 'separator' => '/', 'named' => FALSE, 'ifEmpty' => '', 'reserved' => FALSE], |
|
| 429 | + ';' => ['prefix' => ';', 'separator' => ';', 'named' => TRUE, 'ifEmpty' => '', 'reserved' => FALSE], |
|
| 430 | + '?' => ['prefix' => '?', 'separator' => '&', 'named' => TRUE, 'ifEmpty' => '=', 'reserved' => FALSE], |
|
| 431 | + '&' => ['prefix' => '&', 'separator' => '&', 'named' => TRUE, 'ifEmpty' => '=', 'reserved' => FALSE], |
|
| 432 | 432 | ]; |
| 433 | 433 | |
| 434 | 434 | return preg_replace_callback('~{([+#./;?&])?([^}]+?)}~', function($m) use ($url, & $parameters, $operatorFlags) { |
@@ -463,17 +463,17 @@ discard block |
||
| 463 | 463 | if ($explode) { |
| 464 | 464 | $parts = []; |
| 465 | 465 | if ($isAssoc) { |
| 466 | - $this->walk($value, function ($v, $k) use (& $parts, $flags, $maxLength) { |
|
| 466 | + $this->walk($value, function($v, $k) use (& $parts, $flags, $maxLength) { |
|
| 467 | 467 | $parts[] = $this->prefix(['named' => TRUE] + $flags, $k, $this->escape($flags, $v, $maxLength)); |
| 468 | 468 | }); |
| 469 | 469 | |
| 470 | 470 | } elseif ($flags['named']) { |
| 471 | - $this->walk($value, function ($v) use (& $parts, $flags, $name, $maxLength) { |
|
| 471 | + $this->walk($value, function($v) use (& $parts, $flags, $name, $maxLength) { |
|
| 472 | 472 | $parts[] = $this->prefix($flags, $name, $this->escape($flags, $v, $maxLength)); |
| 473 | 473 | }); |
| 474 | 474 | |
| 475 | 475 | } else { |
| 476 | - $this->walk($value, function ($v) use (& $parts, $flags, $maxLength) { |
|
| 476 | + $this->walk($value, function($v) use (& $parts, $flags, $maxLength) { |
|
| 477 | 477 | $parts[] = $this->escape($flags, $v, $maxLength); |
| 478 | 478 | }); |
| 479 | 479 | } |
@@ -504,7 +504,7 @@ discard block |
||
| 504 | 504 | } |
| 505 | 505 | |
| 506 | 506 | if (isset($translated[0])) { |
| 507 | - return $flags['prefix'] . implode($flags['separator'], $translated); |
|
| 507 | + return $flags['prefix'].implode($flags['separator'], $translated); |
|
| 508 | 508 | } |
| 509 | 509 | |
| 510 | 510 | return ''; |
@@ -530,7 +530,7 @@ discard block |
||
| 530 | 530 | } |
| 531 | 531 | } |
| 532 | 532 | |
| 533 | - return $prefix . $value; |
|
| 533 | + return $prefix.$value; |
|
| 534 | 534 | } |
| 535 | 535 | |
| 536 | 536 | |
@@ -545,7 +545,7 @@ discard block |
||
| 545 | 545 | $value = (string) $value; |
| 546 | 546 | |
| 547 | 547 | if ($maxLength !== NULL) { |
| 548 | - if (preg_match('~^(.{' . $maxLength . '}).~u', $value, $m)) { |
|
| 548 | + if (preg_match('~^(.{'.$maxLength.'}).~u', $value, $m)) { |
|
| 549 | 549 | $value = $m[1]; |
| 550 | 550 | } elseif (strlen($value) > $maxLength) { # when malformed UTF-8 |
| 551 | 551 | $value = substr($value, 0, $maxLength); |
@@ -558,7 +558,7 @@ discard block |
||
| 558 | 558 | |
| 559 | 559 | $escaped = ''; |
| 560 | 560 | for ($i = 0, $count = count($parts); $i < $count; $i += 2) { |
| 561 | - $escaped .= rawurlencode($parts[$i]) . $parts[$i + 1]; |
|
| 561 | + $escaped .= rawurlencode($parts[$i]).$parts[$i + 1]; |
|
| 562 | 562 | } |
| 563 | 563 | |
| 564 | 564 | return $escaped; |
@@ -596,7 +596,7 @@ discard block |
||
| 596 | 596 | |
| 597 | 597 | if (isset($content->errors)) { |
| 598 | 598 | $message .= implode(', ', array_map(function($error) { |
| 599 | - return '[' . implode(':', (array) $error) . ']'; |
|
| 599 | + return '['.implode(':', (array) $error).']'; |
|
| 600 | 600 | }, $content->errors)); |
| 601 | 601 | } |
| 602 | 602 | |
@@ -8,169 +8,169 @@ |
||
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | 10 | namespace XoopsModules\Wggithub\Github\ { |
| 11 | - /** |
|
| 12 | - * Marker interface. |
|
| 13 | - */ |
|
| 14 | - interface IException |
|
| 15 | - {} |
|
| 11 | + /** |
|
| 12 | + * Marker interface. |
|
| 13 | + */ |
|
| 14 | + interface IException |
|
| 15 | + {} |
|
| 16 | 16 | |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Wrong algorithm. API is used in wrong way. Application code should be changed. |
|
| 20 | - */ |
|
| 21 | - class LogicException extends \LogicException implements IException |
|
| 22 | - {} |
|
| 18 | + /** |
|
| 19 | + * Wrong algorithm. API is used in wrong way. Application code should be changed. |
|
| 20 | + */ |
|
| 21 | + class LogicException extends \LogicException implements IException |
|
| 22 | + {} |
|
| 23 | 23 | |
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * Substitution is used in URL path but corresponding parameter is missing. |
|
| 27 | - */ |
|
| 28 | - class MissingParameterException extends LogicException |
|
| 29 | - {} |
|
| 25 | + /** |
|
| 26 | + * Substitution is used in URL path but corresponding parameter is missing. |
|
| 27 | + */ |
|
| 28 | + class MissingParameterException extends LogicException |
|
| 29 | + {} |
|
| 30 | 30 | |
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * Unpredictable situation occurred. |
|
| 34 | - */ |
|
| 35 | - abstract class RuntimeException extends \RuntimeException implements IException |
|
| 36 | - {} |
|
| 32 | + /** |
|
| 33 | + * Unpredictable situation occurred. |
|
| 34 | + */ |
|
| 35 | + abstract class RuntimeException extends \RuntimeException implements IException |
|
| 36 | + {} |
|
| 37 | 37 | |
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * Github API returned a non-success HTTP code or data are somehow wrong. See all following descendants. |
|
| 41 | - * |
|
| 42 | - * @see Api::decode() |
|
| 43 | - * @see https://developer.github.com/v3/#client-errors |
|
| 44 | - */ |
|
| 45 | - abstract class ApiException extends RuntimeException |
|
| 46 | - { |
|
| 47 | - /** @var Http\Response|NULL */ |
|
| 48 | - private $response; |
|
| 39 | + /** |
|
| 40 | + * Github API returned a non-success HTTP code or data are somehow wrong. See all following descendants. |
|
| 41 | + * |
|
| 42 | + * @see Api::decode() |
|
| 43 | + * @see https://developer.github.com/v3/#client-errors |
|
| 44 | + */ |
|
| 45 | + abstract class ApiException extends RuntimeException |
|
| 46 | + { |
|
| 47 | + /** @var Http\Response|NULL */ |
|
| 48 | + private $response; |
|
| 49 | 49 | |
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * @param string |
|
| 53 | - * @param int |
|
| 54 | - */ |
|
| 55 | - public function __construct($message = '', $code = 0, \Exception $previous = NULL, Http\Response $response = NULL) |
|
| 56 | - { |
|
| 57 | - parent::__construct($message, $code, $previous); |
|
| 58 | - $this->response = clone $response; |
|
| 59 | - } |
|
| 51 | + /** |
|
| 52 | + * @param string |
|
| 53 | + * @param int |
|
| 54 | + */ |
|
| 55 | + public function __construct($message = '', $code = 0, \Exception $previous = NULL, Http\Response $response = NULL) |
|
| 56 | + { |
|
| 57 | + parent::__construct($message, $code, $previous); |
|
| 58 | + $this->response = clone $response; |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * @return Http\Response|NULL |
|
| 64 | - */ |
|
| 65 | - final public function getResponse() |
|
| 66 | - { |
|
| 67 | - return $this->response; |
|
| 68 | - } |
|
| 62 | + /** |
|
| 63 | + * @return Http\Response|NULL |
|
| 64 | + */ |
|
| 65 | + final public function getResponse() |
|
| 66 | + { |
|
| 67 | + return $this->response; |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - } |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | 72 | |
| 73 | - /** |
|
| 74 | - * Invalid credentials (e.g. revoked token). |
|
| 75 | - */ |
|
| 76 | - class UnauthorizedException extends ApiException |
|
| 77 | - {} |
|
| 73 | + /** |
|
| 74 | + * Invalid credentials (e.g. revoked token). |
|
| 75 | + */ |
|
| 76 | + class UnauthorizedException extends ApiException |
|
| 77 | + {} |
|
| 78 | 78 | |
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * Invalid JSON sent to Github API. |
|
| 82 | - */ |
|
| 83 | - class BadRequestException extends ApiException |
|
| 84 | - {} |
|
| 80 | + /** |
|
| 81 | + * Invalid JSON sent to Github API. |
|
| 82 | + */ |
|
| 83 | + class BadRequestException extends ApiException |
|
| 84 | + {} |
|
| 85 | 85 | |
| 86 | 86 | |
| 87 | - /** |
|
| 88 | - * Invalid structure sent to Github API (e.g. some required fields are missing). |
|
| 89 | - */ |
|
| 90 | - class UnprocessableEntityException extends ApiException |
|
| 91 | - {} |
|
| 87 | + /** |
|
| 88 | + * Invalid structure sent to Github API (e.g. some required fields are missing). |
|
| 89 | + */ |
|
| 90 | + class UnprocessableEntityException extends ApiException |
|
| 91 | + {} |
|
| 92 | 92 | |
| 93 | 93 | |
| 94 | - /** |
|
| 95 | - * Access denied. |
|
| 96 | - * @see https://developer.github.com/v3/#authentication |
|
| 97 | - */ |
|
| 98 | - class ForbiddenException extends ApiException |
|
| 99 | - {} |
|
| 94 | + /** |
|
| 95 | + * Access denied. |
|
| 96 | + * @see https://developer.github.com/v3/#authentication |
|
| 97 | + */ |
|
| 98 | + class ForbiddenException extends ApiException |
|
| 99 | + {} |
|
| 100 | 100 | |
| 101 | 101 | |
| 102 | - /** |
|
| 103 | - * Rate limit exceed. |
|
| 104 | - * @see https://developer.github.com/v3/#rate-limiting |
|
| 105 | - */ |
|
| 106 | - class RateLimitExceedException extends ForbiddenException |
|
| 107 | - {} |
|
| 102 | + /** |
|
| 103 | + * Rate limit exceed. |
|
| 104 | + * @see https://developer.github.com/v3/#rate-limiting |
|
| 105 | + */ |
|
| 106 | + class RateLimitExceedException extends ForbiddenException |
|
| 107 | + {} |
|
| 108 | 108 | |
| 109 | 109 | |
| 110 | - /** |
|
| 111 | - * Resource not found. |
|
| 112 | - * @see https://developer.github.com/v3/#authentication |
|
| 113 | - */ |
|
| 114 | - class NotFoundException extends ApiException |
|
| 115 | - {} |
|
| 110 | + /** |
|
| 111 | + * Resource not found. |
|
| 112 | + * @see https://developer.github.com/v3/#authentication |
|
| 113 | + */ |
|
| 114 | + class NotFoundException extends ApiException |
|
| 115 | + {} |
|
| 116 | 116 | |
| 117 | 117 | |
| 118 | - /** |
|
| 119 | - * Response cannot be classified. |
|
| 120 | - */ |
|
| 121 | - class UnexpectedResponseException extends ApiException |
|
| 122 | - {} |
|
| 118 | + /** |
|
| 119 | + * Response cannot be classified. |
|
| 120 | + */ |
|
| 121 | + class UnexpectedResponseException extends ApiException |
|
| 122 | + {} |
|
| 123 | 123 | |
| 124 | 124 | |
| 125 | - /** |
|
| 126 | - * Response from Github is somehow wrong (e.g. invalid JSON decoding). |
|
| 127 | - */ |
|
| 128 | - class InvalidResponseException extends ApiException |
|
| 129 | - {} |
|
| 125 | + /** |
|
| 126 | + * Response from Github is somehow wrong (e.g. invalid JSON decoding). |
|
| 127 | + */ |
|
| 128 | + class InvalidResponseException extends ApiException |
|
| 129 | + {} |
|
| 130 | 130 | |
| 131 | 131 | |
| 132 | - /** |
|
| 133 | - * JSON cannot be decoded, or value cannot be encoded to JSON. |
|
| 134 | - */ |
|
| 135 | - class JsonException extends RuntimeException |
|
| 136 | - { |
|
| 137 | - } |
|
| 132 | + /** |
|
| 133 | + * JSON cannot be decoded, or value cannot be encoded to JSON. |
|
| 134 | + */ |
|
| 135 | + class JsonException extends RuntimeException |
|
| 136 | + { |
|
| 137 | + } |
|
| 138 | 138 | |
| 139 | 139 | } |
| 140 | 140 | |
| 141 | 141 | |
| 142 | 142 | namespace XoopsModules\Wggithub\Github\Http { |
| 143 | - use XoopsModules\Wggithub\Github; |
|
| 143 | + use XoopsModules\Wggithub\Github; |
|
| 144 | 144 | |
| 145 | 145 | |
| 146 | - /** |
|
| 147 | - * HTTP response is somehow wrong and cannot be processed. |
|
| 148 | - */ |
|
| 149 | - class BadResponseException extends Github\RuntimeException |
|
| 150 | - {} |
|
| 146 | + /** |
|
| 147 | + * HTTP response is somehow wrong and cannot be processed. |
|
| 148 | + */ |
|
| 149 | + class BadResponseException extends Github\RuntimeException |
|
| 150 | + {} |
|
| 151 | 151 | |
| 152 | 152 | } |
| 153 | 153 | |
| 154 | 154 | |
| 155 | 155 | namespace XoopsModules\Wggithub\Github\OAuth { |
| 156 | - use XoopsModules\Wggithub\Github; |
|
| 156 | + use XoopsModules\Wggithub\Github; |
|
| 157 | 157 | |
| 158 | - /** |
|
| 159 | - * Something fails during the token obtaining. |
|
| 160 | - */ |
|
| 161 | - class LoginException extends Github\RuntimeException |
|
| 162 | - {} |
|
| 158 | + /** |
|
| 159 | + * Something fails during the token obtaining. |
|
| 160 | + */ |
|
| 161 | + class LoginException extends Github\RuntimeException |
|
| 162 | + {} |
|
| 163 | 163 | |
| 164 | 164 | } |
| 165 | 165 | |
| 166 | 166 | |
| 167 | 167 | namespace XoopsModules\Wggithub\Github\Storages { |
| 168 | - use XoopsModules\Wggithub\Github; |
|
| 168 | + use XoopsModules\Wggithub\Github; |
|
| 169 | 169 | |
| 170 | - /** |
|
| 171 | - * Directory is missing and/or cannot be created. |
|
| 172 | - */ |
|
| 173 | - class MissingDirectoryException extends Github\RuntimeException |
|
| 174 | - {} |
|
| 170 | + /** |
|
| 171 | + * Directory is missing and/or cannot be created. |
|
| 172 | + */ |
|
| 173 | + class MissingDirectoryException extends Github\RuntimeException |
|
| 174 | + {} |
|
| 175 | 175 | |
| 176 | 176 | } |
@@ -13,131 +13,131 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class CachedClient extends Github\Sanity implements IClient |
| 15 | 15 | { |
| 16 | - /** @var Storages\ICache|NULL */ |
|
| 17 | - private $cache; |
|
| 18 | - |
|
| 19 | - /** @var IClient */ |
|
| 20 | - private $client; |
|
| 21 | - |
|
| 22 | - /** @var bool */ |
|
| 23 | - private $forbidRecheck; |
|
| 24 | - |
|
| 25 | - /** @var callable|NULL */ |
|
| 26 | - private $onResponse; |
|
| 27 | - |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * @param Storages\ICache |
|
| 31 | - * @param IClient |
|
| 32 | - * @param bool forbid checking Github for new data; more or less development purpose only |
|
| 33 | - */ |
|
| 34 | - public function __construct(Storages\ICache $cache, IClient $client = NULL, $forbidRecheck = FALSE) |
|
| 35 | - { |
|
| 36 | - $this->cache = $cache; |
|
| 37 | - $this->client = $client ?: Github\Helpers::createDefaultClient(); |
|
| 38 | - $this->forbidRecheck = (bool) $forbidRecheck; |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * @return IClient |
|
| 44 | - */ |
|
| 45 | - public function getInnerClient() |
|
| 46 | - { |
|
| 47 | - return $this->client; |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * @return Response |
|
| 53 | - * |
|
| 54 | - * @throws BadResponseException |
|
| 55 | - */ |
|
| 56 | - public function request(Request $request) |
|
| 57 | - { |
|
| 58 | - $request = clone $request; |
|
| 59 | - |
|
| 60 | - $cacheKey = implode('.', [ |
|
| 61 | - $request->getMethod(), |
|
| 62 | - $request->getUrl(), |
|
| 63 | - |
|
| 64 | - /** @todo This should depend on Vary: header */ |
|
| 65 | - $request->getHeader('Accept'), |
|
| 66 | - $request->getHeader('Accept-Encoding'), |
|
| 67 | - $request->getHeader('Authorization') |
|
| 68 | - ]); |
|
| 69 | - |
|
| 70 | - if ($cached = $this->cache->load($cacheKey)) { |
|
| 71 | - if ($this->forbidRecheck) { |
|
| 72 | - $cached = clone $cached; |
|
| 73 | - $this->onResponse && call_user_func($this->onResponse, $cached); |
|
| 74 | - return $cached; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - /** @var $cached Response */ |
|
| 78 | - if ($cached->hasHeader('Last-Modified')) { |
|
| 79 | - $request->addHeader('If-Modified-Since', $cached->getHeader('Last-Modified')); |
|
| 80 | - } |
|
| 81 | - if ($cached->hasHeader('ETag')) { |
|
| 82 | - $request->addHeader('If-None-Match', $cached->getHeader('ETag')); |
|
| 83 | - } |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - $response = $this->client->request($request); |
|
| 87 | - |
|
| 88 | - if ($this->isCacheable($response)) { |
|
| 89 | - $this->cache->save($cacheKey, clone $response); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - if (isset($cached) && $response->getCode() === Response::S304_NOT_MODIFIED) { |
|
| 93 | - $cached = clone $cached; |
|
| 94 | - |
|
| 95 | - /** @todo Should be responses somehow combined into one? */ |
|
| 96 | - $response = $cached->setPrevious($response); |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - $this->onResponse && call_user_func($this->onResponse, $response); |
|
| 100 | - |
|
| 101 | - return $response; |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * @param callable|NULL function(Request $request) |
|
| 107 | - * @return self |
|
| 108 | - */ |
|
| 109 | - public function onRequest($callback) |
|
| 110 | - { |
|
| 111 | - $this->client->onRequest($callback); |
|
| 112 | - return $this; |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * @param callable|NULL function(Response $response) |
|
| 118 | - * @return self |
|
| 119 | - */ |
|
| 120 | - public function onResponse($callback) |
|
| 121 | - { |
|
| 122 | - $this->client->onResponse(NULL); |
|
| 123 | - $this->onResponse = $callback; |
|
| 124 | - return $this; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * @return bool |
|
| 130 | - */ |
|
| 131 | - protected function isCacheable(Response $response) |
|
| 132 | - { |
|
| 133 | - /** @todo Do it properly. Vary:, Pragma:, TTL... */ |
|
| 134 | - if (!$response->isCode(200)) { |
|
| 135 | - return FALSE; |
|
| 136 | - } elseif (preg_match('#max-age=0|must-revalidate#i', $response->getHeader('Cache-Control', ''))) { |
|
| 137 | - return FALSE; |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - return $response->hasHeader('ETag') || $response->hasHeader('Last-Modified'); |
|
| 141 | - } |
|
| 16 | + /** @var Storages\ICache|NULL */ |
|
| 17 | + private $cache; |
|
| 18 | + |
|
| 19 | + /** @var IClient */ |
|
| 20 | + private $client; |
|
| 21 | + |
|
| 22 | + /** @var bool */ |
|
| 23 | + private $forbidRecheck; |
|
| 24 | + |
|
| 25 | + /** @var callable|NULL */ |
|
| 26 | + private $onResponse; |
|
| 27 | + |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * @param Storages\ICache |
|
| 31 | + * @param IClient |
|
| 32 | + * @param bool forbid checking Github for new data; more or less development purpose only |
|
| 33 | + */ |
|
| 34 | + public function __construct(Storages\ICache $cache, IClient $client = NULL, $forbidRecheck = FALSE) |
|
| 35 | + { |
|
| 36 | + $this->cache = $cache; |
|
| 37 | + $this->client = $client ?: Github\Helpers::createDefaultClient(); |
|
| 38 | + $this->forbidRecheck = (bool) $forbidRecheck; |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * @return IClient |
|
| 44 | + */ |
|
| 45 | + public function getInnerClient() |
|
| 46 | + { |
|
| 47 | + return $this->client; |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * @return Response |
|
| 53 | + * |
|
| 54 | + * @throws BadResponseException |
|
| 55 | + */ |
|
| 56 | + public function request(Request $request) |
|
| 57 | + { |
|
| 58 | + $request = clone $request; |
|
| 59 | + |
|
| 60 | + $cacheKey = implode('.', [ |
|
| 61 | + $request->getMethod(), |
|
| 62 | + $request->getUrl(), |
|
| 63 | + |
|
| 64 | + /** @todo This should depend on Vary: header */ |
|
| 65 | + $request->getHeader('Accept'), |
|
| 66 | + $request->getHeader('Accept-Encoding'), |
|
| 67 | + $request->getHeader('Authorization') |
|
| 68 | + ]); |
|
| 69 | + |
|
| 70 | + if ($cached = $this->cache->load($cacheKey)) { |
|
| 71 | + if ($this->forbidRecheck) { |
|
| 72 | + $cached = clone $cached; |
|
| 73 | + $this->onResponse && call_user_func($this->onResponse, $cached); |
|
| 74 | + return $cached; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + /** @var $cached Response */ |
|
| 78 | + if ($cached->hasHeader('Last-Modified')) { |
|
| 79 | + $request->addHeader('If-Modified-Since', $cached->getHeader('Last-Modified')); |
|
| 80 | + } |
|
| 81 | + if ($cached->hasHeader('ETag')) { |
|
| 82 | + $request->addHeader('If-None-Match', $cached->getHeader('ETag')); |
|
| 83 | + } |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + $response = $this->client->request($request); |
|
| 87 | + |
|
| 88 | + if ($this->isCacheable($response)) { |
|
| 89 | + $this->cache->save($cacheKey, clone $response); |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + if (isset($cached) && $response->getCode() === Response::S304_NOT_MODIFIED) { |
|
| 93 | + $cached = clone $cached; |
|
| 94 | + |
|
| 95 | + /** @todo Should be responses somehow combined into one? */ |
|
| 96 | + $response = $cached->setPrevious($response); |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + $this->onResponse && call_user_func($this->onResponse, $response); |
|
| 100 | + |
|
| 101 | + return $response; |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * @param callable|NULL function(Request $request) |
|
| 107 | + * @return self |
|
| 108 | + */ |
|
| 109 | + public function onRequest($callback) |
|
| 110 | + { |
|
| 111 | + $this->client->onRequest($callback); |
|
| 112 | + return $this; |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * @param callable|NULL function(Response $response) |
|
| 118 | + * @return self |
|
| 119 | + */ |
|
| 120 | + public function onResponse($callback) |
|
| 121 | + { |
|
| 122 | + $this->client->onResponse(NULL); |
|
| 123 | + $this->onResponse = $callback; |
|
| 124 | + return $this; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * @return bool |
|
| 130 | + */ |
|
| 131 | + protected function isCacheable(Response $response) |
|
| 132 | + { |
|
| 133 | + /** @todo Do it properly. Vary:, Pragma:, TTL... */ |
|
| 134 | + if (!$response->isCode(200)) { |
|
| 135 | + return FALSE; |
|
| 136 | + } elseif (preg_match('#max-age=0|must-revalidate#i', $response->getHeader('Cache-Control', ''))) { |
|
| 137 | + return FALSE; |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + return $response->hasHeader('ETag') || $response->hasHeader('Last-Modified'); |
|
| 141 | + } |
|
| 142 | 142 | |
| 143 | 143 | } |
@@ -10,108 +10,108 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | class StreamClient extends AbstractClient |
| 12 | 12 | { |
| 13 | - /** @var array|NULL */ |
|
| 14 | - private $sslOptions; |
|
| 15 | - |
|
| 16 | - |
|
| 17 | - /** |
|
| 18 | - * @param array SSL context options {@link http://php.net/manual/en/context.ssl.php} |
|
| 19 | - */ |
|
| 20 | - public function __construct(array $sslOptions = NULL) |
|
| 21 | - { |
|
| 22 | - $this->sslOptions = $sslOptions; |
|
| 23 | - } |
|
| 24 | - |
|
| 25 | - |
|
| 26 | - protected function setupRequest(Request $request) |
|
| 27 | - { |
|
| 28 | - parent::setupRequest($request); |
|
| 29 | - $request->setHeader('Connection', 'close'); |
|
| 30 | - } |
|
| 31 | - |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * @return Response |
|
| 35 | - * |
|
| 36 | - * @throws BadResponseException |
|
| 37 | - */ |
|
| 38 | - protected function process(Request $request) |
|
| 39 | - { |
|
| 40 | - $headerStr = []; |
|
| 41 | - foreach ($request->getHeaders() as $name => $value) { |
|
| 42 | - foreach ((array) $value as $v) { |
|
| 43 | - $headerStr[] = "$name: $v"; |
|
| 44 | - } |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - $options = [ |
|
| 48 | - 'http' => [ |
|
| 49 | - 'method' => $request->getMethod(), |
|
| 50 | - 'header' => implode("\r\n", $headerStr) . "\r\n", |
|
| 51 | - 'follow_location' => 0, # Github sets the Location header for 201 code too and redirection is not required for us |
|
| 52 | - 'protocol_version' => 1.1, |
|
| 53 | - 'ignore_errors' => TRUE, |
|
| 54 | - ], |
|
| 55 | - 'ssl' => [ |
|
| 56 | - 'verify_peer' => TRUE, |
|
| 57 | - 'cafile' => realpath(__DIR__ . '/../../ca-chain.crt'), |
|
| 58 | - 'disable_compression' => TRUE, # Effective since PHP 5.4.13 |
|
| 59 | - ], |
|
| 60 | - ]; |
|
| 61 | - |
|
| 62 | - if (($content = $request->getContent()) !== NULL) { |
|
| 63 | - $options['http']['content'] = $content; |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - if ($this->sslOptions) { |
|
| 67 | - $options['ssl'] = $this->sslOptions + $options['ssl']; |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - list($code, $headers, $content) = $this->fileGetContents($request->getUrl(), $options); |
|
| 71 | - return new Response($code, $headers, $content); |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * @internal |
|
| 77 | - * @param string |
|
| 78 | - * @param array |
|
| 79 | - * @return array |
|
| 80 | - * |
|
| 81 | - * @throws BadResponseException |
|
| 82 | - */ |
|
| 83 | - protected function fileGetContents($url, array $contextOptions) |
|
| 84 | - { |
|
| 85 | - $context = stream_context_create($contextOptions); |
|
| 86 | - |
|
| 87 | - $e = NULL; |
|
| 88 | - set_error_handler(function($severity, $message, $file, $line) use (& $e) { |
|
| 89 | - $e = new \ErrorException($message, 0, $severity, $file, $line, $e); |
|
| 90 | - }, E_WARNING); |
|
| 91 | - |
|
| 92 | - $content = file_get_contents($url, FALSE, $context); |
|
| 93 | - restore_error_handler(); |
|
| 94 | - |
|
| 95 | - if (!isset($http_response_header)) { |
|
| 96 | - throw new BadResponseException('Missing HTTP headers, request failed.', 0, $e); |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - if (!isset($http_response_header[0]) || !preg_match('~^HTTP/1[.]. (\d{3})~i', $http_response_header[0], $m)) { |
|
| 100 | - throw new BadResponseException('HTTP status code is missing.', 0, $e); |
|
| 101 | - } |
|
| 102 | - unset($http_response_header[0]); |
|
| 103 | - |
|
| 104 | - $headers = []; |
|
| 105 | - foreach ($http_response_header as $header) { |
|
| 106 | - if (in_array(substr($header, 0, 1), [' ', "\t"], TRUE)) { |
|
| 107 | - $headers[$last] .= ' ' . trim($header); # RFC2616, 2.2 |
|
| 108 | - } else { |
|
| 109 | - list($name, $value) = explode(':', $header, 2) + [NULL, NULL]; |
|
| 110 | - $headers[$last = trim($name)] = trim($value); |
|
| 111 | - } |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - return [$m[1], $headers, $content]; |
|
| 115 | - } |
|
| 13 | + /** @var array|NULL */ |
|
| 14 | + private $sslOptions; |
|
| 15 | + |
|
| 16 | + |
|
| 17 | + /** |
|
| 18 | + * @param array SSL context options {@link http://php.net/manual/en/context.ssl.php} |
|
| 19 | + */ |
|
| 20 | + public function __construct(array $sslOptions = NULL) |
|
| 21 | + { |
|
| 22 | + $this->sslOptions = $sslOptions; |
|
| 23 | + } |
|
| 24 | + |
|
| 25 | + |
|
| 26 | + protected function setupRequest(Request $request) |
|
| 27 | + { |
|
| 28 | + parent::setupRequest($request); |
|
| 29 | + $request->setHeader('Connection', 'close'); |
|
| 30 | + } |
|
| 31 | + |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * @return Response |
|
| 35 | + * |
|
| 36 | + * @throws BadResponseException |
|
| 37 | + */ |
|
| 38 | + protected function process(Request $request) |
|
| 39 | + { |
|
| 40 | + $headerStr = []; |
|
| 41 | + foreach ($request->getHeaders() as $name => $value) { |
|
| 42 | + foreach ((array) $value as $v) { |
|
| 43 | + $headerStr[] = "$name: $v"; |
|
| 44 | + } |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + $options = [ |
|
| 48 | + 'http' => [ |
|
| 49 | + 'method' => $request->getMethod(), |
|
| 50 | + 'header' => implode("\r\n", $headerStr) . "\r\n", |
|
| 51 | + 'follow_location' => 0, # Github sets the Location header for 201 code too and redirection is not required for us |
|
| 52 | + 'protocol_version' => 1.1, |
|
| 53 | + 'ignore_errors' => TRUE, |
|
| 54 | + ], |
|
| 55 | + 'ssl' => [ |
|
| 56 | + 'verify_peer' => TRUE, |
|
| 57 | + 'cafile' => realpath(__DIR__ . '/../../ca-chain.crt'), |
|
| 58 | + 'disable_compression' => TRUE, # Effective since PHP 5.4.13 |
|
| 59 | + ], |
|
| 60 | + ]; |
|
| 61 | + |
|
| 62 | + if (($content = $request->getContent()) !== NULL) { |
|
| 63 | + $options['http']['content'] = $content; |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + if ($this->sslOptions) { |
|
| 67 | + $options['ssl'] = $this->sslOptions + $options['ssl']; |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + list($code, $headers, $content) = $this->fileGetContents($request->getUrl(), $options); |
|
| 71 | + return new Response($code, $headers, $content); |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * @internal |
|
| 77 | + * @param string |
|
| 78 | + * @param array |
|
| 79 | + * @return array |
|
| 80 | + * |
|
| 81 | + * @throws BadResponseException |
|
| 82 | + */ |
|
| 83 | + protected function fileGetContents($url, array $contextOptions) |
|
| 84 | + { |
|
| 85 | + $context = stream_context_create($contextOptions); |
|
| 86 | + |
|
| 87 | + $e = NULL; |
|
| 88 | + set_error_handler(function($severity, $message, $file, $line) use (& $e) { |
|
| 89 | + $e = new \ErrorException($message, 0, $severity, $file, $line, $e); |
|
| 90 | + }, E_WARNING); |
|
| 91 | + |
|
| 92 | + $content = file_get_contents($url, FALSE, $context); |
|
| 93 | + restore_error_handler(); |
|
| 94 | + |
|
| 95 | + if (!isset($http_response_header)) { |
|
| 96 | + throw new BadResponseException('Missing HTTP headers, request failed.', 0, $e); |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + if (!isset($http_response_header[0]) || !preg_match('~^HTTP/1[.]. (\d{3})~i', $http_response_header[0], $m)) { |
|
| 100 | + throw new BadResponseException('HTTP status code is missing.', 0, $e); |
|
| 101 | + } |
|
| 102 | + unset($http_response_header[0]); |
|
| 103 | + |
|
| 104 | + $headers = []; |
|
| 105 | + foreach ($http_response_header as $header) { |
|
| 106 | + if (in_array(substr($header, 0, 1), [' ', "\t"], TRUE)) { |
|
| 107 | + $headers[$last] .= ' ' . trim($header); # RFC2616, 2.2 |
|
| 108 | + } else { |
|
| 109 | + list($name, $value) = explode(':', $header, 2) + [NULL, NULL]; |
|
| 110 | + $headers[$last = trim($name)] = trim($value); |
|
| 111 | + } |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + return [$m[1], $headers, $content]; |
|
| 115 | + } |
|
| 116 | 116 | |
| 117 | 117 | } |
@@ -47,15 +47,15 @@ discard block |
||
| 47 | 47 | $options = [ |
| 48 | 48 | 'http' => [ |
| 49 | 49 | 'method' => $request->getMethod(), |
| 50 | - 'header' => implode("\r\n", $headerStr) . "\r\n", |
|
| 51 | - 'follow_location' => 0, # Github sets the Location header for 201 code too and redirection is not required for us |
|
| 50 | + 'header' => implode("\r\n", $headerStr)."\r\n", |
|
| 51 | + 'follow_location' => 0, # Github sets the Location header for 201 code too and redirection is not required for us |
|
| 52 | 52 | 'protocol_version' => 1.1, |
| 53 | 53 | 'ignore_errors' => TRUE, |
| 54 | 54 | ], |
| 55 | 55 | 'ssl' => [ |
| 56 | 56 | 'verify_peer' => TRUE, |
| 57 | - 'cafile' => realpath(__DIR__ . '/../../ca-chain.crt'), |
|
| 58 | - 'disable_compression' => TRUE, # Effective since PHP 5.4.13 |
|
| 57 | + 'cafile' => realpath(__DIR__.'/../../ca-chain.crt'), |
|
| 58 | + 'disable_compression' => TRUE, # Effective since PHP 5.4.13 |
|
| 59 | 59 | ], |
| 60 | 60 | ]; |
| 61 | 61 | |
@@ -104,7 +104,7 @@ discard block |
||
| 104 | 104 | $headers = []; |
| 105 | 105 | foreach ($http_response_header as $header) { |
| 106 | 106 | if (in_array(substr($header, 0, 1), [' ', "\t"], TRUE)) { |
| 107 | - $headers[$last] .= ' ' . trim($header); # RFC2616, 2.2 |
|
| 107 | + $headers[$last] .= ' '.trim($header); # RFC2616, 2.2 |
|
| 108 | 108 | } else { |
| 109 | 109 | list($name, $value) = explode(':', $header, 2) + [NULL, NULL]; |
| 110 | 110 | $headers[$last = trim($name)] = trim($value); |