public function __construct(Site $site, $what, $action, $extra) { $method = 'do' . ucfirst($what) . ucfirst($action); if (!method_exists(__CLASS__, $method)) { throw new BadMethodCallException('no such method'); } parent::__construct($site, 'application/json', false); $this->response = array(); if (array_key_exists('id', $_REQUEST)) { $id = $_REQUEST['id']; } try { if (isset($id)) { switch ($what) { case 'page': $subject = CMSPage::load($id); break; case 'node': $subject = CMSNode::load($id); break; default: throw new RuntimeException('shouldn\'t happen'); break; } } $rclass = new ReflectionClass(get_class($this)); $rmeth = $rclass->getMethod($method); if (isset($subject)) { $this->{$method}($subject, $extra); } else { if ($rmeth->getNumberOfRequiredParameters() > 0) { throw new InvalidArgumentException('subject required'); } $this->{$method}(); } } catch (StopException $e) { // noop } }