Exemple #1
0
    /**
     * Add method to definition
     *
     * @param  array|\Zend\Server\Method\Definition $method
     * @param  null|string $name
     * @return \Zend\Server\Definition
     * @throws \Zend\Server\Exception if duplicate or invalid method provided
     */
    public function addMethod($method, $name = null)
    {
        if (is_array($method)) {
            $method = new Method\Definition($method);
        } elseif (!$method instanceof Method\Definition) {
            throw new Exception\InvalidArgumentException('Invalid method provided');
        }

        if (is_numeric($name)) {
            $name = null;
        }
        if (null !== $name) {
            $method->setName($name);
        } else {
            $name = $method->getName();
        }
        if (null === $name) {
            throw new Exception\InvalidArgumentException('No method name provided');
        }

        if (!$this->_overwriteExistingMethods && array_key_exists($name, $this->_methods)) {
            throw new Exception\InvalidArgumentException(sprintf('Method by name of "%s" already exists', $name));
        }
        $this->_methods[$name] = $method;
        return $this;
    }
Exemple #2
0
 /**
  * Dispatch method
  *
  * @param  \Zend\Server\Method\Definition $invocable
  * @param  array $params
  * @return mixed
  */
 protected function _dispatch(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);
 }