/** * Processes a route call, something like `stuff::thing' or just a function name * or even a closure. */ protected function process($proto) { /* We're accepting different types of handler declarations. It can be * anything PHP defines as a 'callable', or in the form class::method. */ $class = ''; $method = ''; $call = $proto['call']; $matches = $proto['params']; if (is_string($call) && preg_match('/^.+::.+$/', trim($call))) { list($class, $method) = explode('::', $call); } else { if (is_array($call)) { $class = $call[0]; $method = $call[1]; } else { if (is_callable($call)) { $method = $call; } } } $response = null; $this->modules->runMethod('preProcess', array('request' => $this->request, 'proto' => $proto, 'response' => $response)); if (!$class) { // Just a function call (or a closure?). Less hooks obviously. // Mounting system stuff into an object and generating the parameters. $params = array_merge(array((object) array('modules' => $this->modules, 'server' => $this->server, 'request' => $this->request, 'sec' => Injector::give('Security'))), array_slice($matches, 1)); $response = call_user_func_array($method, $params); } else { if (class_exists($class)) { $obj = new $class($this->modules, $this->server, $this->request, new Security()); if (method_exists($obj, 'preRequest')) { $obj->preRequest(); } if (method_exists($obj, $method)) { $response = call_user_func_array(array($obj, $method), array_slice($matches, 1)); if (method_exists($obj, 'postRequest')) { $response = $obj->postRequest($response); } } else { throw new \BadMethodCallException("Method, {$method}, not supported."); } } else { throw new NoHandlerException("Class, {$class}, not found."); } } // Cleaning up the response... if (gettype($response) == 'string') { $response = Injector::give('Response', $response); } else { if ($response === null) { $response = Injector::give('Response'); } else { if (gettype($response) != 'object' || gettype($response) == 'object' && (get_class($response) != 'atlatl\\Response' && !is_subclass_of($response, 'atlatl\\Response'))) { throw new IllegalResponseException('Unknown response.'); } } } $this->modules->runMethod('postProcess', array('request' => $this->request, 'proto' => $proto, 'response' => $response)); return $response; }
<?php namespace atlatl; // Core. Injector::register('Core', function ($prefix = '') { $server = Injector::give('Server', $_SERVER, $prefix); $request = Injector::give('Request', $_GET, $_POST); $mc = Injector::give('ModuleContainer', $server); return new Core($prefix, $server, $request, $mc); }); // Server Injector::register('Server', function (array $data, $prefix = '') { return new Server($data, $prefix); }); // Request Injector::register('Request', function (array $get, array $post, array $session = null, array $cookies = null) { return new Request($get, $post, $session ?: array(), $cookies ?: array()); }); // ModuleContainer Injector::register('ModuleContainer', function (Server $server) { return new ModuleContainer($server); }); // Response Injector::register('Response', function ($body = '', $status_code = 200, $content_type = 'text/html; charset=UTF-8') { return new Response($body, $status_code, $content_type); }); // Security Injector::register('Security', function () { return new Security(); });