Skip to content

Commit

Permalink
Hacks, hacks, and more hacks
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobemerick committed Aug 9, 2016
1 parent 4857b63 commit 936e83a
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions src/Talus.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@ public function run()

$this->logger->debug('Talus: walking through swagger doc looking for dispatch');

$result = $this->callStack($request, $response);
try {
$result = $this->callStack($request, $response);
} catch (\Exception $e) {
$result = ($this->errorHandler)($request, $response, $e);
}

$this->outputResponse($result);
}

Expand Down Expand Up @@ -129,8 +134,7 @@ public function __invoke(RequestInterface $request, ResponseInterface $response)
try {
$method = strtolower($request->getMethod());
$operation = $path->getOperation($method);
} catch (Exception $e) {
// todo 404 handler
} catch (\Exception $e) {
throw $e;
}

Expand All @@ -149,6 +153,8 @@ public function __invoke(RequestInterface $request, ResponseInterface $response)
$controller = new $controllerName($this->container);
return $controller->$methodName($request, $response);
}

throw new \Exception('Path not found');
}

/**
Expand All @@ -159,31 +165,27 @@ public function __invoke(RequestInterface $request, ResponseInterface $response)
// todo a better response
protected function matchPath(RequestInterface $request, SwaggerPath $swaggerPath)
{
if ($request->getUri()->getPath() == $swaggerPath->getPath()) {
if ($request->getUri()->getPath() === $swaggerPath->getPath()) {
return $request;
}

// todo what are acceptable path param values, anyways?
$isVariablePath = preg_match_all('/{([a-z_]+)}/', $pathKey, $pathMatches);
$isVariablePath = preg_match_all('/{([a-z_]+)}/', $swaggerPath->getPath(), $pathMatches);
if (!$isVariablePath) {
return false;
}

// var_dump($pathMatches); exit;
// loop da loop

$method = strtolower($request->getMethod());
$operation = $swaggerPath->getOperation($method);

foreach ($pathMatches[1] as $pathParam) {
foreach ($swaggerPath->getParameters() as $parameter) {
// why oh why is this necessary
if ($parameter->hasDocumentProperty('$ref')) {
$resolver = new SchemaResolver($this->swagger);
$pointer = $parameter->getDocumentProperty('$ref');
$pointer = substr($pointer, 2);
$pointer = new SwaggerPointer($pointer);
$parameter = $resolver->findTypeAtPointer($pointer);
}
foreach ($operation->getParameters() as $parameter) {
if ($pathParam == $parameter->getName()) {
// todo extract extract will robinson
if ($parameter->getDocumentProperty('type') == 'string') {
$pathKey = str_replace('{' . $pathParam . '}', '(?P<' . $pathParam . '>\w+)', $pathKey);
if ($parameter->getType() == 'string') {
$pathKey = str_replace('{' . $pathParam . '}', '(?P<' . $pathParam . '>\w+)', $swaggerPath->getPath());
continue 2;
}
}
Expand Down

0 comments on commit 936e83a

Please sign in to comment.