*/ abstract class AbstractApiRoute extends AbstractRoute { /** * @param ServerRequestInterface $request * @return null|string * @throws \ResolverException */ public function resolve(ServerRequestInterface $request) { $relativeUrl = \tao_helpers_Request::getRelativeUrl($request->getRequestTarget()); try { return $this->getController($relativeUrl) . '@' . $this->getAction($request->getMethod()); } catch (RouterException $e) { return null; } } /** * @param $relativeUrl * @return string * @throws RouterException */ protected function getController($relativeUrl) { $parts = explode('/', $relativeUrl); $prefix = $this->getControllerPrefix(); if (strpos($relativeUrl, $this->getId()) !== 0) { throw new RouterException('Path does not match'); } if (!isset($parts[2])) { throw new RouterException('Missed controller name in uri: ' . $relativeUrl); } if (!class_exists($prefix . ucfirst($parts[2]))) { throw new RouterException('Controller ' . $parts[2] . ' does not exists'); } return $prefix . ucfirst($parts[2]); } /** * @param $method * @return string * @throws RouterException */ protected function getAction($method) { switch ($method) { case 'GET': $action = 'get'; break; case 'PUT': $action = 'put'; break; case 'POST': $action = 'post'; break; case 'DELETE': $action = 'delete'; break; default: throw new RouterException('Method `' . $method . ' is not supported'); } return $action; } }