public static function get($for = '', $activeTrail = null) { if (empty(static::$tree)) { /** @var Restler $restler */ $restler = Scope::get('Restler'); if (static::$addExtension) { static::$extension = isset($restler->responseFormat) ? '.' . $restler->responseFormat->getExtension() : '.html'; } static::$url = $restler->getBaseUrl(); if (empty(static::$url)) { static::$url = ''; } static::$activeTrail = $activeTrail = empty($activeTrail) ? empty($restler->url) || $restler->url == 'index' ? static::$root : $restler->url : $activeTrail; if (static::$addExtension) { static::$extension = isset($restler->responseFormat) ? '.' . $restler->responseFormat->getExtension() : '.html'; } static::addUrls(static::$prepends); $map = Routes::findAll(static::$excludedPaths, array('POST', 'DELETE', 'PUT', 'PATCH'), $restler->getRequestedApiVersion()); foreach ($map as $path => $data) { foreach ($data as $item) { $access = $item['access']; $route = $item['route']; $url = $route['url']; if ($access && !Text::contains($url, '{')) { $label = Util::nestedValue($route, 'metadata', CommentParser::$embeddedDataName, 'label'); if (!empty($url)) { $url .= static::$extension; } static::add($url, $label); } } } static::addUrls(static::$appends); } elseif (empty($activeTrail)) { $activeTrail = static::$activeTrail; } $tree = static::$tree; $activeTrail = explode('/', $activeTrail); $nested =& static::nested($tree, $activeTrail); if (is_array($nested)) { $nested['active'] = true; } if (!empty($for)) { $for = explode('/', $for); $tree = static::nested($tree, $for)['children']; } return array_filter($tree); }
private function apis($version = 1, $resource = false) { $map = Routes::findAll(static::$excludedPaths + array($this->base()), static::$excludedHttpMethods, $version); $r = array(); $a = array(); foreach ($map as $path => $data) { $route = $data[0]['route']; $access = $data[0]['access']; if ($access && !Text::contains($path, '{')) { $r[] = array('path' => empty($path) ? '/root' : "/{$path}"); } if (static::$hideProtected && !$access) { continue; } $grouper = array(); foreach ($data as $item) { $route = $item['route']; $access = $item['access']; if (static::$hideProtected && !$access) { continue; } $url = $route['url']; if (isset($grouper[$url])) { $grouper[$url]['operations'][] = $this->operation($route); } else { $api = array('path' => "/{$url}", 'description' => Util::nestedValue($route, 'metadata', 'classDescription') ?: '', 'operations' => array($this->operation($route))); static::$groupOperations ? $grouper[$url] = $api : ($a[$path][] = $api); } } if (!empty($grouper)) { $a[$path] = array_values($grouper); } else { $order = array('GET' => 1, 'POST' => 2, 'PUT' => 3, 'PATCH' => 4, 'DELETE' => 5); foreach ($a as &$b) { usort($b, function ($x, $y) use($order) { return $x['operations'][0]->method == $y['operations'][0]->method ? $x['path'] > $y['path'] : $order[$x['operations'][0]->method] > $order[$y['operations'][0]->method]; }); } } } if (false !== $resource) { if ($resource == 'root') { $resource = ''; } if (isset($a[$resource])) { return $a[$resource]; } } return $r; }
private function paths($version = 1) { $map = Routes::findAll(static::$excludedPaths + array($this->base()), static::$excludedHttpMethods, $version); $paths = array(); foreach ($map as $path => $data) { $access = $data[0]['access']; if (static::$hideProtected && !$access) { continue; } foreach ($data as $item) { $route = $item['route']; $access = $item['access']; if (static::$hideProtected && !$access) { continue; } $url = $route['url']; $paths["/{$url}"][strtolower($route['httpMethod'])] = $this->operation($route); } } return $paths; }