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: Remove `$deprecatedTemplatingOrNull` argument. |
46
|
|
|
* |
47
|
|
|
* @param Environment|string $twigOrDeprecatedName |
48
|
|
|
*/ |
49
|
|
|
public function __construct( |
50
|
|
|
$twigOrDeprecatedName, |
51
|
|
|
?EngineInterface $deprecatedTemplatingOrNull, |
52
|
|
|
Pool $pool, |
53
|
|
|
SearchHandler $searchHandler |
54
|
|
|
) { |
55
|
|
|
// NEXT_MAJOR: Remove conditions, only pass the Environment |
56
|
|
|
if ($deprecatedTemplatingOrNull instanceof EngineInterface) { |
57
|
|
|
if (!\is_string($twigOrDeprecatedName)) { |
58
|
|
|
throw new \TypeError(sprintf( |
|
|
|
|
59
|
|
|
'Passing other than string as argument 1 to %s() is not allowed when %s is passed as argument 2.' |
60
|
|
|
.' You must pass an string instead.', |
61
|
|
|
__METHOD__, |
62
|
|
|
EngineInterface::class |
63
|
|
|
)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
parent::__construct($twigOrDeprecatedName, $deprecatedTemplatingOrNull); |
|
|
|
|
67
|
|
|
} elseif ($twigOrDeprecatedName instanceof Environment) { |
68
|
|
|
if (null !== $deprecatedTemplatingOrNull) { |
69
|
|
|
throw new \TypeError(sprintf( |
|
|
|
|
70
|
|
|
'Argument 2 passed to %s() must be null if you passing %s as first argument, %s given.', |
71
|
|
|
__METHOD__, |
72
|
|
|
Environment::class, |
73
|
|
|
EngineInterface::class |
74
|
|
|
)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
parent::__construct($twigOrDeprecatedName); |
78
|
|
|
} else { |
79
|
|
|
throw new \TypeError(sprintf( |
|
|
|
|
80
|
|
|
'Argument 1 passed to %s() should be an instance of %s, and you should leave argument 2 as null.', |
81
|
|
|
__METHOD__, |
82
|
|
|
Environment::class |
83
|
|
|
)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$this->pool = $pool; |
87
|
|
|
$this->searchHandler = $searchHandler; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function execute(BlockContextInterface $blockContext, ?Response $response = null): Response |
91
|
|
|
{ |
92
|
|
|
try { |
93
|
|
|
$admin = $this->pool->getAdminByAdminCode($blockContext->getSetting('admin_code')); |
94
|
|
|
} catch (ServiceNotFoundException $e) { |
95
|
|
|
throw new \RuntimeException('Unable to find the Admin instance', $e->getCode(), $e); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if (!$admin instanceof AdminInterface) { |
99
|
|
|
throw new \RuntimeException('The requested service is not an Admin instance'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$admin->checkAccess('list'); |
103
|
|
|
|
104
|
|
|
$pager = $this->searchHandler->search( |
105
|
|
|
$admin, |
106
|
|
|
$blockContext->getSetting('query'), |
107
|
|
|
$blockContext->getSetting('page'), |
108
|
|
|
$blockContext->getSetting('per_page') |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
if (false === $pager) { |
112
|
|
|
$response = $response ?: new Response(); |
113
|
|
|
|
114
|
|
|
return $response->setContent('')->setStatusCode(204); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $this->renderPrivateResponse($admin->getTemplate('search_result_block'), [ |
|
|
|
|
118
|
|
|
'block' => $blockContext->getBlock(), |
119
|
|
|
'settings' => $blockContext->getSettings(), |
120
|
|
|
'admin_pool' => $this->pool, |
121
|
|
|
'pager' => $pager, |
122
|
|
|
'admin' => $admin, |
123
|
|
|
], $response); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function getName() |
127
|
|
|
{ |
128
|
|
|
return 'Admin Search Result'; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function configureSettings(OptionsResolver $resolver): void |
132
|
|
|
{ |
133
|
|
|
$resolver |
134
|
|
|
->setDefaults([ |
135
|
|
|
'admin_code' => '', |
136
|
|
|
'query' => '', |
137
|
|
|
'page' => 0, |
138
|
|
|
'per_page' => 10, |
139
|
|
|
'icon' => '<i class="fa fa-list"></i>', |
140
|
|
|
]) |
141
|
|
|
->setRequired('admin_code') |
142
|
|
|
->setAllowedTypes('admin_code', ['string']); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
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
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.