|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Sonata Project package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Sonata\AdminBundle\Block; |
|
15
|
|
|
|
|
16
|
|
|
use Sonata\AdminBundle\Admin\AdminInterface; |
|
17
|
|
|
use Sonata\AdminBundle\Admin\Pool; |
|
18
|
|
|
use Sonata\AdminBundle\Search\SearchHandler; |
|
19
|
|
|
use Sonata\BlockBundle\Block\BlockContextInterface; |
|
20
|
|
|
use Sonata\BlockBundle\Block\Service\AbstractBlockService; |
|
21
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; |
|
22
|
|
|
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; |
|
23
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
24
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
25
|
|
|
use Twig\Environment; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @final since sonata-project/admin-bundle 3.52 |
|
29
|
|
|
* |
|
30
|
|
|
* @author Thomas Rabaix <[email protected]> |
|
31
|
|
|
*/ |
|
32
|
|
|
class AdminSearchBlockService extends AbstractBlockService |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* @var Pool |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $pool; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var SearchHandler |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $searchHandler; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* NEXT_MAJOR: Change signature for |
|
46
|
|
|
* - Environment $twig |
|
47
|
|
|
* - Pool $pool |
|
48
|
|
|
* - SearchHandler $searchHandler |
|
49
|
|
|
* |
|
50
|
|
|
* @param Environment|string $twigOrName |
|
51
|
|
|
* @param Pool|EngineInterface|null $poolOrTemplating |
|
52
|
|
|
* @param SearchHandler|Pool $searchHandlerOrPool |
|
53
|
|
|
*/ |
|
54
|
|
|
public function __construct($twigOrName, ?object $poolOrTemplating, object $searchHandlerOrPool, ?SearchHandler $searchHandler = null) |
|
55
|
|
|
{ |
|
56
|
|
|
if ($poolOrTemplating instanceof Pool) { |
|
57
|
|
|
if (!$twigOrName instanceof Environment) { |
|
58
|
|
|
throw new \TypeError(sprintf( |
|
|
|
|
|
|
59
|
|
|
'Argument 1 passed to %s() must be either an instance of %s, %s given.', |
|
60
|
|
|
__METHOD__, |
|
61
|
|
|
Environment::class, |
|
62
|
|
|
\is_object($twigOrName) ? 'instance of '.\get_class($twigOrName) : \gettype($twigOrName) |
|
63
|
|
|
)); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
if (!$searchHandlerOrPool instanceof SearchHandler) { |
|
67
|
|
|
throw new \TypeError(sprintf( |
|
|
|
|
|
|
68
|
|
|
'Argument 3 passed to %s() must be either an instance of %s, instance of %s given.', |
|
69
|
|
|
__METHOD__, |
|
70
|
|
|
SearchHandler::class, |
|
71
|
|
|
\get_class($twigOrName) |
|
72
|
|
|
)); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
parent::__construct($twigOrName); |
|
76
|
|
|
|
|
77
|
|
|
$this->pool = $poolOrTemplating; |
|
78
|
|
|
$this->searchHandler = $searchHandlerOrPool; |
|
79
|
|
|
} elseif (null === $poolOrTemplating || $poolOrTemplating instanceof EngineInterface) { |
|
80
|
|
|
@trigger_error(sprintf( |
|
|
|
|
|
|
81
|
|
|
'Passing %s as argument 2 to %s() is deprecated since sonata-project/admin-bundle 3.x' |
|
82
|
|
|
.' and will throw a \TypeError in version 4.0. You must pass an instance of %s instead.', |
|
83
|
|
|
null === $poolOrTemplating ? 'null' : EngineInterface::class, |
|
84
|
|
|
__METHOD__, |
|
85
|
|
|
Pool::class |
|
86
|
|
|
), E_USER_DEPRECATED); |
|
87
|
|
|
|
|
88
|
|
|
if (!$searchHandlerOrPool instanceof Pool) { |
|
89
|
|
|
throw new \TypeError(sprintf( |
|
|
|
|
|
|
90
|
|
|
'Argument 2 passed to %s() must be an instance of %s, instance of %s given.', |
|
91
|
|
|
__METHOD__, |
|
92
|
|
|
Pool::class, |
|
93
|
|
|
\get_class($twigOrName) |
|
94
|
|
|
)); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
if (null === $searchHandler) { |
|
98
|
|
|
throw new \TypeError(sprintf( |
|
|
|
|
|
|
99
|
|
|
'Passing null as argument 3 to %s() is not allowed when %s is passed as argument 2.' |
|
100
|
|
|
.' You must pass an instance of %s instead.', |
|
101
|
|
|
__METHOD__, |
|
102
|
|
|
EngineInterface::class, |
|
103
|
|
|
SearchHandler::class |
|
104
|
|
|
)); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
parent::__construct($twigOrName, $poolOrTemplating); |
|
108
|
|
|
|
|
109
|
|
|
$this->pool = $searchHandlerOrPool; |
|
110
|
|
|
$this->searchHandler = $searchHandler; |
|
111
|
|
|
} else { |
|
112
|
|
|
throw new \TypeError(sprintf( |
|
|
|
|
|
|
113
|
|
|
'Argument 2 passed to %s() must be either null or an instance of %s or preferably %s, instance of %s given.', |
|
114
|
|
|
__METHOD__, |
|
115
|
|
|
EngineInterface::class, |
|
116
|
|
|
Pool::class, |
|
117
|
|
|
\get_class($poolOrTemplating) |
|
118
|
|
|
)); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function execute(BlockContextInterface $blockContext, ?Response $response = null) |
|
123
|
|
|
{ |
|
124
|
|
|
try { |
|
125
|
|
|
$admin = $this->pool->getAdminByAdminCode($blockContext->getSetting('admin_code')); |
|
126
|
|
|
} catch (ServiceNotFoundException $e) { |
|
127
|
|
|
throw new \RuntimeException('Unable to find the Admin instance', $e->getCode(), $e); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
if (!$admin instanceof AdminInterface) { |
|
131
|
|
|
throw new \RuntimeException('The requested service is not an Admin instance'); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
$admin->checkAccess('list'); |
|
135
|
|
|
|
|
136
|
|
|
$pager = $this->searchHandler->search( |
|
137
|
|
|
$admin, |
|
138
|
|
|
$blockContext->getSetting('query'), |
|
139
|
|
|
$blockContext->getSetting('page'), |
|
140
|
|
|
$blockContext->getSetting('per_page') |
|
141
|
|
|
); |
|
142
|
|
|
|
|
143
|
|
|
if (false === $pager) { |
|
144
|
|
|
$response = $response ?: new Response(); |
|
145
|
|
|
|
|
146
|
|
|
return $response->setContent('')->setStatusCode(204); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
return $this->renderPrivateResponse($admin->getTemplate('search_result_block'), [ |
|
|
|
|
|
|
150
|
|
|
'block' => $blockContext->getBlock(), |
|
151
|
|
|
'settings' => $blockContext->getSettings(), |
|
152
|
|
|
'admin_pool' => $this->pool, |
|
153
|
|
|
'pager' => $pager, |
|
154
|
|
|
'admin' => $admin, |
|
155
|
|
|
], $response); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
public function getName() |
|
159
|
|
|
{ |
|
160
|
|
|
return 'Admin Search Result'; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
public function configureSettings(OptionsResolver $resolver) |
|
164
|
|
|
{ |
|
165
|
|
|
$resolver |
|
166
|
|
|
->setDefaults([ |
|
167
|
|
|
'admin_code' => '', |
|
168
|
|
|
'query' => '', |
|
169
|
|
|
'page' => 0, |
|
170
|
|
|
'per_page' => 10, |
|
171
|
|
|
'icon' => '<i class="fa fa-list"></i>', |
|
172
|
|
|
]) |
|
173
|
|
|
->setRequired('admin_code') |
|
174
|
|
|
->setAllowedTypes('admin_code', ['string']); |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignorePhpDoc annotation to the duplicate definition and it will be ignored.