Exemplo n.º 1
0
 private function createResourceNestedRoute($action, $params, $multiple = true, $to = null)
 {
     $path = '';
     $setAlias = true;
     $resources = $this->resources;
     if (isset($params['as'])) {
         $pre_alias = '';
         // $pre_alias = $params['as'];
         $setAlias = false;
     } elseif (preg_match('/\\A\\w+\\Z/', $action)) {
         $pre_alias = $action;
     } else {
         $pre_alias = '';
         $setAlias = false;
     }
     foreach ($resources as $k => $res) {
         $path .= '/' . $res;
         if ($this->createAsMember || isset($params['on']) && $params['on'] == 'member') {
             $path .= '/:id';
         } elseif (!isset($params['on']) || $params['on'] != 'collection') {
             $path .= '/:' . $res . '_id';
         }
         $pre_alias .= '_' . $res;
     }
     if (!$multiple) {
         $resources = $this->resource;
         foreach ($resources as $res) {
             $path .= '/' . $res;
         }
     }
     $method = !empty($params['via']) ? $params['via'] : null;
     if ($action != 'index') {
         $path .= '/' . $action;
     }
     $path = ltrim($path, '/');
     $controller = end($resources);
     $plural = substr($controller, -1, 1) == 's';
     $inflector = Rails::services()->get('inflector');
     if ($setAlias) {
         $aliasedResources = [];
         foreach ($resources as $resource) {
             $aliasedResources[] = $inflector->singularize($resource);
         }
         if ($action == 'index') {
             $lastIndex = count($resources) - 1;
             $aliasedResources[$lastIndex] = $resources[$lastIndex];
         }
         $pre_alias .= ($pre_alias && $this->routeNames ? '_' : '') . implode($this->routeNames);
         $alias = ($pre_alias ? $pre_alias . '_' : '') . implode('_', $aliasedResources);
         if ($action == 'index') {
             if (!$plural) {
                 $alias .= '_index';
             }
         }
         // elseif ($plural)
         // $alias = substr($alias, 0, -1);
         if (Route\RouteSet::validate_route_alias($alias)) {
             Route\RouteSet::add_route_alias($alias);
             $params['as'] = $alias;
         }
     } elseif (empty($params['as'])) {
         $params['as'] = '';
     }
     if ($method) {
         $params['via'] = $method;
     }
     if (!$to) {
         if (!$multiple) {
             $controller = $inflector->pluralize($controller);
         }
         $to = $controller . '#' . $action;
     }
     return $this->createRoute($path, $to, $params, ['skip_resources' => true]);
 }