/** * Add method to definition * * @param array|Rest_Method_Definition $method * @param null|string $name * @return Rest_Definition * @throws Rest_Exception if duplicate or invalid method provided */ public function addMethod($method, $name = null) { if (is_array($method)) { require_once 'classes/rest/method/Definition.php'; $method = new Rest_Method_Definition($method); } elseif (!$method instanceof Rest_Method_Definition) { require_once 'Exception.php'; throw new Rest_Exception('Invalid method provided'); } if (is_numeric($name)) { $name = null; } if (null !== $name) { $method->setName($name); } else { $name = $method->getName(); } if (null === $name) { require_once 'Exception.php'; throw new Rest_Exception('No method name provided'); } if (!$this->_overwriteExistingMethods && array_key_exists($name, $this->_methods)) { require_once 'Exception.php'; throw new Rest_Exception(sprintf('Method by name of "%s" already exists', $name)); } $this->_methods[$name] = $method; return $this; }
/** * Dispatch method * * @param Rest_Method_Definition $invocable * @param array $params * @return mixed */ protected function _dispatch(Rest_Method_Definition $invocable, array $params) { $callback = $invocable->getCallback(); $type = $callback->getType(); if ('function' == $type) { $function = $callback->getFunction(); return call_user_func_array($function, $params); } $class = $callback->getClass(); $method = $callback->getMethod(); if ('static' == $type) { return call_user_func_array(array($class, $method), $params); } $object = $invocable->getObject(); if (!is_object($object)) { $invokeArgs = $invocable->getInvokeArguments(); if (!empty($invokeArgs)) { $reflection = new ReflectionClass($class); $object = $reflection->newInstanceArgs($invokeArgs); } else { $object = new $class(); } } return call_user_func_array(array($object, $method), $params); }