示例#1
0
 /**
  * Rewrites the application routes.
  *
  * @return void
  */
 private static function rewrite()
 {
     foreach (Repository::stored() as $method => $routes) {
         foreach ($routes as $uri => $options) {
             $segments = explode('/', $uri);
             $rewrite = false;
             foreach ($segments as $key => $segment) {
                 $matches = [];
                 // Get route URI segments we need to rewrite.
                 preg_match('/\\(([0-9a-z]+)\\:([a-z]+)\\)/i', $segment, $matches);
                 // Do we have matches?
                 if (!empty($matches)) {
                     // Get the real value for this segment and validate it
                     // against the rule.
                     $value = Uri::segment($key + 1);
                     $rule = $matches[2];
                     $valid = false;
                     // Validate the rule.
                     if ($rule === 'numeric' && is_numeric($value)) {
                         $valid = true;
                     } else {
                         if ($rule === 'any') {
                             $valid = true;
                         }
                     }
                     // If the segment is valid, assign the value.
                     if ($valid === true) {
                         $segments[$key] = $value;
                     }
                     // Add the parameters.
                     if (!isset($options['parameters'])) {
                         $options['parameters'] = [$value];
                     } else {
                         array_push($options['parameters'], $value);
                     }
                     // We will need to rewrite this URL.
                     $rewrite = true;
                 }
             }
             // Do we need to rewrite the URI value.
             if ($rewrite) {
                 // Remove the old URI.
                 Repository::remove($method, $uri);
                 // Add the new one.
                 Repository::store($method, implode('/', $segments), $options);
             }
         }
     }
 }