Esempio n. 1
0
 /**
  * Execute callback from MatchedRoute and return result.<br>
  * Required callback structure is:
  * <code>
  * Callback:
  *     Class: \Your\Class
  *     Method: handle
  *     Static: true // (Optional, "false" by default)
  *
  *</code>
  * @param MatchedRoute $route
  *
  * @return mixed
  *
  * @throws RouterException
  */
 public function execute(MatchedRoute $route)
 {
     $callback = $route->getCallback();
     if ($this->isString($callback)) {
         throw new RouterException(RouterException::STRING_CALLBACK_NOT_PARSABLE);
     }
     $callback = $this->arr($callback);
     $handlerClass = $callback->key('Class', false, true);
     $handlerMethod = $callback->key('Method', false, true);
     $staticMethod = StdObjectWrapper::toBool($callback->key('Static', false, true));
     if (!class_exists($handlerClass)) {
         throw new RouterException(RouterException::CALLBACK_CLASS_NOT_FOUND, [$handlerClass]);
     }
     if (!method_exists($handlerClass, $handlerMethod)) {
         throw new RouterException(RouterException::CALLBACK_CLASS_METHOD_NOT_FOUND, [$handlerMethod, $handlerClass]);
     }
     if ($staticMethod) {
         return forward_static_call_array([$handlerClass, $handlerMethod], $route->getParams());
     }
     return call_user_func_array([new $handlerClass(), $handlerMethod], $route->getParams());
 }
Esempio n. 2
0
 /**
  * Internal static method that is called when initRest method matches a URL agains the Path.
  * This method then processes that matched response and then creates and returns a Rest instance back to iniRest.
  *
  * @param MatchedRoute $matchedRoute The matched route.
  * @param ConfigObject $config       Current api config.
  * @param string       $api          Current api name.
  *
  * @return Rest
  * @throws \Webiny\Component\StdLib\StdObject\StringObject\StringObjectException
  */
 private static function processRouterResponse(MatchedRoute $matchedRoute, ConfigObject $config, $api)
 {
     // based on the matched route create the class name
     $className = self::str($config->get('Router.Class'))->trimLeft('\\')->prepend('\\');
     $normalize = $config->get('Router.Normalize', false);
     $matchedParams = $matchedRoute->getParams();
     foreach ($matchedParams as $mpName => $mpParam) {
         if ($normalize) {
             $mpParam = self::str($mpParam)->replace('-', ' ')->caseWordUpper()->replace(' ', '');
         }
         $className->replace('{' . $mpName . '}', $mpParam);
     }
     // return the new rest instance
     return new self($api, $className->val());
 }