diff options
author | Joas Schilling <coding@schilljs.com> | 2020-08-05 09:49:18 +0200 |
---|---|---|
committer | npmbuildbot[bot] <npmbuildbot[bot]@users.noreply.github.com> | 2020-08-05 12:52:16 +0000 |
commit | ea8f68bea6957ae459ff1ba6849b25354b3f0093 (patch) | |
tree | b6523d1ad1c01e55622fd31bfcfb27daabec0593 /core/Controller/UnifiedSearchController.php | |
parent | d98f7c1bd83fc03fd297ebeac6279ffe17316950 (diff) | |
download | nextcloud-server-ea8f68bea6957ae459ff1ba6849b25354b3f0093.tar.gz nextcloud-server-ea8f68bea6957ae459ff1ba6849b25354b3f0093.zip |
Hand in the route and the parameters of the request
Signed-off-by: Joas Schilling <coding@schilljs.com>
Signed-off-by: npmbuildbot[bot] <npmbuildbot[bot]@users.noreply.github.com>
Diffstat (limited to 'core/Controller/UnifiedSearchController.php')
-rw-r--r-- | core/Controller/UnifiedSearchController.php | 62 |
1 files changed, 55 insertions, 7 deletions
diff --git a/core/Controller/UnifiedSearchController.php b/core/Controller/UnifiedSearchController.php index 4aaa1b9b067..839bbde1003 100644 --- a/core/Controller/UnifiedSearchController.php +++ b/core/Controller/UnifiedSearchController.php @@ -32,7 +32,9 @@ use OCP\AppFramework\Http; use OCP\AppFramework\Http\JSONResponse; use OCP\IRequest; use OCP\IUserSession; +use OCP\Route\IRouter; use OCP\Search\ISearchQuery; +use Symfony\Component\Routing\Exception\ResourceNotFoundException; class UnifiedSearchController extends Controller { @@ -42,26 +44,33 @@ class UnifiedSearchController extends Controller { /** @var IUserSession */ private $userSession; + /** @var IRouter */ + private $router; + public function __construct(IRequest $request, IUserSession $userSession, - SearchComposer $composer) { + SearchComposer $composer, + IRouter $router) { parent::__construct('core', $request); $this->composer = $composer; $this->userSession = $userSession; + $this->router = $router; } /** * @NoAdminRequired * @NoCSRFRequired - * + * * @param string $from the url the user is currently at - * + * * @return JSONResponse */ - public function getProviders(string $from): JSONResponse { + public function getProviders(string $from = ''): JSONResponse { + [$route, $parameters] = $this->getRouteInformation($from); + return new JSONResponse( - $this->composer->getProviders($from) + $this->composer->getProviders($route, $parameters) ); } @@ -74,6 +83,7 @@ class UnifiedSearchController extends Controller { * @param int|null $sortOrder * @param int|null $limit * @param int|string|null $cursor + * @param string $from * * @return JSONResponse */ @@ -81,10 +91,12 @@ class UnifiedSearchController extends Controller { string $term = '', ?int $sortOrder = null, ?int $limit = null, - $cursor = null): JSONResponse { + $cursor = null, + string $from = ''): JSONResponse { if (empty(trim($term))) { return new JSONResponse(null, Http::STATUS_BAD_REQUEST); } + [$route, $routeParameters] = $this->getRouteInformation($from); return new JSONResponse( $this->composer->search( @@ -94,9 +106,45 @@ class UnifiedSearchController extends Controller { $term, $sortOrder ?? ISearchQuery::SORT_DATE_DESC, $limit ?? SearchQuery::LIMIT_DEFAULT, - $cursor + $cursor, + $route, + $routeParameters ) ) ); } + + protected function getRouteInformation(string $url): array { + $routeStr = ''; + $parameters = []; + + if ($url !== '') { + $urlParts = parse_url($url); + + try { + $parameters = $this->router->findMatchingRoute($urlParts['path']); + + // contacts.PageController.index => contacts.Page.index + $route = $parameters['caller']; + if (substr($route[1], -10) === 'Controller') { + $route[1] = substr($route[1], 0, -10); + } + $routeStr = implode('.', $route); + + // cleanup + unset($parameters['_route'], $parameters['action'], $parameters['caller']); + } catch (ResourceNotFoundException $exception) { + } + + if (isset($urlParts['query'])) { + parse_str($urlParts['query'], $queryParameters); + $parameters = array_merge($parameters, $queryParameters); + } + } + + return [ + $routeStr, + $parameters, + ]; + } } |