public function process(Request $request)
 {
     if (!$this->options->rule) {
         return false;
     }
     if (!isset($this->options->targets['module']) || !isset($this->options->targets['controller']) || !isset($this->options->targets['action'])) {
         return false;
     }
     if ($request->getRequestUri() == $this->options->rule && $request->getMethod() == ($this->options->method ?: 'GET')) {
         $this->getPayload()->set('module', $this->options->targets['module']);
         $this->getPayload()->set('controller', $this->options->targets['controller']);
         $this->getPayload()->set('action', $this->options->targets['action']);
         $this->setMatch();
         $this->getPayload()->set('request', $request);
         $this->getPayload()->setPopulated();
     }
     return $this;
 }
예제 #2
0
파일: StaticRoute.php 프로젝트: recck/proem
 public function process(Request $request)
 {
     if (!isset($this->options['rule'])) {
         return false;
     }
     $method = isset($this->options['method']) ? $this->options['method'] : 'GET';
     if (!isset($this->options['targets']) || !isset($this->options['targets']['module']) || !isset($this->options['targets']['controller']) || !isset($this->options['targets']['action'])) {
         return false;
     }
     if ($request->getRequestUri() == $this->options['rule'] && $request->getMethod() == $method) {
         $this->getPayload()->set('module', $this->options['targets']['module']);
         $this->getPayload()->set('controller', $this->options['targets']['controller']);
         $this->getPayload()->set('action', $this->options['targets']['action']);
         $this->setMatch();
         $this->getPayload()->set('request', $request);
         $this->getPayload()->setPopulated();
     }
     return $this;
 }
 /**
  * Process the supplied url.
  *
  * This route takes a simplified series of patterns such as :controller and
  * replaces them with more complex regular expressions which are then used
  * within a preg_match_callback to match against the given uri.
  *
  * A default rule of /:controller/:action/:params is used if no rule is
  * provided through the $options array.
  *
  * If a 'filter' regex is set within the $options array that regex will be
  * used within the preg_match_callback. Otherwise a default regex of
  * ([a-zA-Z0-9_\+\-%]+) is used.
  *
  * If one of the 'simplified' patterns within the rule is :params, this is
  * treated specially and uses a ([a-zA-Z0-9_\+\-%\/]+) regex which will match
  * the same as the default as well as / .
  *
  * This causes the pattern to match entire sections of uri's. Allowing a
  * simple pattern like the default /:controller/:action/:params to match
  * uri's like /foo/bar/a/b/c/d/e/f/g/h and cause everything after /foo/bar
  * to be added to the Payload object as params (which are in turn transformed
  * into key => value pairs).
  *
  * @param Proem\IO\Request\Template $request
  * @return Proem\Api\Routing\Route\Template
  */
 public function process(Request $request)
 {
     if (!$this->options->rule) {
         return false;
     }
     $rule = $this->options->rule;
     $target = $this->options->targets;
     $custom_filters = $this->options->filters;
     $method = $this->options->method ?: 'GET';
     $default_tokens = $this->default_tokens;
     $default_filters = $this->default_filters;
     $uri = $request->getRequestUri();
     $requestMethod = $request->getMethod();
     $keys = [];
     $values = [];
     $params = [];
     preg_match_all('@:([\\w]+)@', $rule, $keys, PREG_PATTERN_ORDER);
     $keys = $keys[0];
     $regex = preg_replace_callback('@:[\\w]+@', function ($matches) use($custom_filters, $default_tokens, $default_filters) {
         $key = str_replace(':', '', $matches[0]);
         if (isset($custom_filters[$key])) {
             if (isset($default_filters[$custom_filters[$key]])) {
                 return '(' . $default_filters[$custom_filters[$key]] . ')';
             } else {
                 return '(' . $custom_filters[$key] . ')';
             }
         } else {
             if (isset($default_tokens[$key])) {
                 return '(' . $default_tokens[$key] . ')';
             } else {
                 return $default_filters[':default'];
             }
         }
     }, $rule) . '/?';
     if (preg_match('@^' . $regex . '$@', $request->getRequestUri(), $values) && $requestMethod == $method) {
         array_shift($values);
         foreach ($keys as $index => $value) {
             if (isset($values[$index])) {
                 $params[substr($value, 1)] = urldecode($values[$index]);
             }
         }
         foreach ($target as $key => $value) {
             $params[$key] = $value;
         }
         foreach ($params as $key => $value) {
             // If the string within $value looks like a / seperated string,
             // parse it into an array and send it to setParams() instead
             // of the singular setParam.
             if ($key == 'params' && strpos($value, '/') !== false) {
                 $this->getPayload()->set('params', $this->createAssocArray(explode('/', trim($value, '/'))));
             } else {
                 $this->getPayload()->set($key, $value);
             }
         }
         $this->setMatch();
         $this->getPayload()->set('request', $request);
         $this->getPayload()->setPopulated();
     }
     return $this;
 }