|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics) |
|
6
|
|
|
* |
|
7
|
|
|
* This program is free software; you can redistribute it and/or |
|
8
|
|
|
* modify it under the terms of the GNU General Public License |
|
9
|
|
|
* as published by the Free Software Foundation; either version 2 |
|
10
|
|
|
* of the License, or (at your option) any later version. |
|
11
|
|
|
* |
|
12
|
|
|
* This program is distributed in the hope that it will be useful, |
|
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
* GNU General Public License for more details. |
|
16
|
|
|
* |
|
17
|
|
|
* You should have received a copy of the GNU General Public License |
|
18
|
|
|
* along with this program; if not, write to the Free Software |
|
19
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
namespace App\Controller; |
|
23
|
|
|
|
|
24
|
|
|
use App\Services\Attachments\BuiltinAttachmentsFinder; |
|
25
|
|
|
use App\Services\TagFinder; |
|
26
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
|
27
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
28
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
29
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
|
30
|
|
|
use Symfony\Component\Serializer\Encoder\JsonEncoder; |
|
31
|
|
|
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; |
|
32
|
|
|
use Symfony\Component\Serializer\Serializer; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* In this controller the endpoints for the typeaheads are collected. |
|
36
|
|
|
* |
|
37
|
|
|
* @Route("/typeahead") |
|
38
|
|
|
*/ |
|
39
|
|
|
class TypeaheadController extends AbstractController |
|
40
|
|
|
{ |
|
41
|
|
|
/** |
|
42
|
|
|
* @Route("/builtInResources/search/{query}", name="typeahead_builtInRessources", requirements={"query"= ".+"}) |
|
43
|
|
|
*/ |
|
44
|
|
|
public function builtInResources(Request $request, string $query, BuiltinAttachmentsFinder $finder) |
|
|
|
|
|
|
45
|
|
|
{ |
|
46
|
|
|
$array = $finder->find($query); |
|
47
|
|
|
|
|
48
|
|
|
$normalizers = [ |
|
49
|
|
|
new ObjectNormalizer(), |
|
50
|
|
|
]; |
|
51
|
|
|
$encoders = [ |
|
52
|
|
|
new JsonEncoder(), |
|
53
|
|
|
]; |
|
54
|
|
|
$serializer = new Serializer($normalizers, $encoders); |
|
55
|
|
|
$data = $serializer->serialize($array, 'json'); |
|
56
|
|
|
|
|
57
|
|
|
return new JsonResponse($data, 200, [], true); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @Route("/tags/search/{query}", name="typeahead_tags", requirements={"query"= ".+"}) |
|
62
|
|
|
*/ |
|
63
|
|
|
public function tags(string $query, TagFinder $finder) |
|
64
|
|
|
{ |
|
65
|
|
|
$array = $finder->searchTags($query); |
|
66
|
|
|
|
|
67
|
|
|
$normalizers = [ |
|
68
|
|
|
new ObjectNormalizer(), |
|
69
|
|
|
]; |
|
70
|
|
|
$encoders = [ |
|
71
|
|
|
new JsonEncoder(), |
|
72
|
|
|
]; |
|
73
|
|
|
$serializer = new Serializer($normalizers, $encoders); |
|
74
|
|
|
$data = $serializer->serialize($array, 'json'); |
|
75
|
|
|
|
|
76
|
|
|
return new JsonResponse($data, 200, [], true); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.