Blame | Letzte Änderung | Log anzeigen | RSS feed
<?php namespace Clockwork\DataSource;use Clockwork\DataSource\DataSource;use Clockwork\Request\Request;use Slim\Slim;// Data source for Slim 2 framework, provides controller, request and response informationclass SlimDataSource extends DataSource{// Slim instanceprotected $slim;// Create a new data source, takes Slim instance as an argumentpublic function __construct(Slim $slim){$this->slim = $slim;}// Adds request and response information to the requestpublic function resolve(Request $request){$request->method = $this->getRequestMethod();$request->uri = $this->getRequestUri();$request->controller = $this->getController();$request->headers = $this->getRequestHeaders();$request->responseStatus = $this->getResponseStatus();return $request;}// Get a textual representation of current route's controllerprotected function getController(){$matchedRoutes = $this->slim->router()->getMatchedRoutes($this->slim->request()->getMethod(), $this->slim->request()->getResourceUri());if (! count($matchedRoutes)) return;$controller = end($matchedRoutes)->getCallable();if ($controller instanceof \Closure) {$controller = 'anonymous function';} elseif (is_object($controller)) {$controller = 'instance of ' . get_class($controller);} elseif (is_array($controller) && count($controller) == 2) {if (is_object($controller[0])) {$controller = get_class($controller[0]) . '->' . $controller[1];} else {$controller = $controller[0] . '::' . $controller[1];}} elseif (! is_string($controller)) {$controller = null;}return $controller;}// Get the request headersprotected function getRequestHeaders(){$headers = [];foreach ($_SERVER as $key => $value) {if (substr($key, 0, 5) !== 'HTTP_') continue;$header = substr($key, 5);$header = str_replace('_', ' ', $header);$header = ucwords(strtolower($header));$header = str_replace(' ', '-', $header);$value = $this->slim->request()->headers($header, $value);if (! isset($headers[$header])) {$headers[$header] = [ $value ];} else {$headers[$header][] = $value;}}ksort($headers);return $headers;}// Get the request methodprotected function getRequestMethod(){return $this->slim->request()->getMethod();}// Get the request URIprotected function getRequestUri(){return $this->slim->request()->getPathInfo();}// Get the response status codeprotected function getResponseStatus(){return $this->slim->response()->status();}}