Codebench — A benchmarking module.
Class declared in MODPATH/codebench/classes/Controller/Codebench.php on line 12.
boolean $auto_renderauto render template
bool TRUE
Request $requestRequest that created the controller
NULL
Response $responseThe response that will be returned from controller
NULL
$templatestring(9) "codebench"public function action_index()
{
$class = $this->request->param('class');
// Convert submitted class name to URI segment
if (isset($_POST['class'])) {
throw HTTP_Exception::factory(302)->location('codebench/' . trim($_POST['class']));
}
// Pass the class name on to the view
$this->template->class = (string) $class;
// Try to load the class, then run it
if (Kohana::auto_load($class) === true) {
$codebench = new $class;
$this->template->codebench = $codebench->run();
}
}
public function action_media()
{
// Get the file path from the request
$file = $this->request->param('file');
// Find the file extension
$ext = pathinfo($file, PATHINFO_EXTENSION);
// Remove the extension from the filename
$file = substr($file, 0, -(strlen($ext) + 1));
if ($file = Kohana::find_file('media/guide', $file, $ext)) {
// Check if the browser sent an "if-none-match: <etag>" header, and tell if the file hasn't changed
$this->check_cache(sha1($this->request->uri()) . filemtime($file));
// Send the file content as the response
$this->response->body(file_get_contents($file));
// Set the proper headers to allow caching
$this->response->headers('content-type', File::mime_by_ext($ext));
$this->response->headers('last-modified', date('r', filemtime($file)));
} else {
// Return a 404 status
$this->response->status(404);
}
}
Assigns the template View as the request response.
public function after()
{
if ($this->auto_render) {
// Get the media route
$media = Route::get('code-bench/media');
// Add scripts
$this->template->set('scripts', [
$media->uri(['file' => 'js/jquery.min.js']),
]);
}
parent::after();
}
Loads the template View object.
public function before()
{
parent::before();
if ($this->request->action() === 'media') {
// Do not template media files
$this->auto_render = false;
}
}
Creates a new controller instance. Each controller must be constructed with the request object that created it.
Request
$request
required - Request that created the controller Response
$response
required - The request's response voidpublic function __construct(Request $request, Response $response)
{
// Assign the request to the controller
$this->request = $request;
// Assign a response to the controller
$this->response = $response;
}
Executes the given action and calls the Controller::before and Controller::after methods.
Can also be used to catch exceptions from actions in a single place.
Responsepublic function execute()
{
// Execute the "before action" method
$this->before();
// Determine the action to use
$action = 'action_' . $this->request->action();
// If the action doesn't exist, it's a 404
if (!method_exists($this, $action)) {
throw HTTP_Exception::factory(404, 'The requested URL :uri was not found on this server.', [':uri' => $this->request->uri()])->request($this->request);
}
// Execute the action itself
$this->{$action}();
// Execute the "after action" method
$this->after();
// Return the response
return $this->response;
}
Issues an HTTP redirect.
Proxies to the HTTP::redirect method.
string
$uri
= string(0) "" - URI to redirect to int
$code
= integer 302 - HTTP Status code to use for the redirect mixedpublic static function redirect($uri = '', $code = 302)
{
return HTTP::redirect((string) $uri, $code);
}
Checks the browser cache to see the response needs to be returned, execution will halt and a 304 Not Modified will be sent if the browser cache is up-to-date.
$this->check_cache(sha1($content));
string
$etag
= NULL - Resource Etag Responseprotected function check_cache($etag = null)
{
return HTTP::check_cache($this->request, $this->response, $etag);
}