Skip to content

Instantly share code, notes, and snippets.

@icaine
Created October 20, 2015 21:52
Show Gist options
  • Save icaine/c03734c00ff91db713fd to your computer and use it in GitHub Desktop.
Save icaine/c03734c00ff91db713fd to your computer and use it in GitHub Desktop.
class MicroRoute extends Route {
/** @var array */
private $callbacks;
/**
* @param string URL mask, e.g. '<presenter>/<action>/<id \d{1,3}>'
* @param int flags
*/
public function __construct($mask, $flags = 0) {
parent::__construct($mask, function ($presenter) {
/* @var MicroPresenter $presenter */
$request = $presenter->getRequest();
$method = $request->getMethod();
if (isset($this->callbacks[$method])) {
foreach ($this->callbacks[$method] as $callback) {
$callback($request, $presenter);
}
} else {
$presenter->error('NOT_IMPLEMENTED');
}
}, $flags);
}
public function get($callback) {
$this->callbacks['GET'][] = $callback;
return $this;
}
public function post($callback) {
$this->callbacks['POST'][] = $callback;
return $this;
}
public function delete($callback) {
$this->callbacks['DELETE'][] = $callback;
return $this;
}
public function patch($callback) {
$this->callbacks['PATCH'][] = $callback;
return $this;
}
}
//////////////
$rest = new MicroRoute('rest/<domain>/<action>');
$rest->get(function (Request $request, $presenter) {
$parameters = $request->getParameters();
// CODE
});
$rest->post(function (Request $request, $presenter) {
});
$router[] = $rest;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment