|
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\Controller; |
|
15
|
|
|
|
|
16
|
|
|
use Sonata\AdminBundle\Action\AppendFormFieldElementAction; |
|
17
|
|
|
use Sonata\AdminBundle\Action\GetShortObjectDescriptionAction; |
|
18
|
|
|
use Sonata\AdminBundle\Action\RetrieveAutocompleteItemsAction; |
|
19
|
|
|
use Sonata\AdminBundle\Action\RetrieveFormFieldElementAction; |
|
20
|
|
|
use Sonata\AdminBundle\Action\SetObjectFieldValueAction; |
|
21
|
|
|
use Sonata\AdminBundle\Admin\AdminHelper; |
|
22
|
|
|
use Sonata\AdminBundle\Admin\Pool; |
|
23
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
24
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
25
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
26
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
27
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
|
28
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
|
29
|
|
|
use Twig\Environment; |
|
30
|
|
|
|
|
31
|
|
|
@trigger_error( |
|
|
|
|
|
|
32
|
|
|
'The '.__NAMESPACE__.'\HelperController class is deprecated since version 3.x and will be removed in 4.0.' |
|
33
|
|
|
.' Use actions inside Sonata\AdminBundle\Action instead.', |
|
34
|
|
|
E_USER_DEPRECATED |
|
35
|
|
|
); |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* NEXT_MAJOR: remove this class. |
|
39
|
|
|
* |
|
40
|
|
|
* @deprecated since version 3.1, to be removed in 4.0. Use Sonata\AdminBundle\AbstractAdmin instead |
|
41
|
|
|
* |
|
42
|
|
|
* @author Thomas Rabaix <[email protected]> |
|
43
|
|
|
*/ |
|
44
|
|
|
class HelperController |
|
45
|
|
|
{ |
|
46
|
|
|
/** |
|
47
|
|
|
* @var \Twig_Environment |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $twig; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var AdminHelper |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $helper; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @var Pool |
|
58
|
|
|
*/ |
|
59
|
|
|
protected $pool; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @var ValidatorInterface |
|
63
|
|
|
*/ |
|
64
|
|
|
protected $validator; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param ValidatorInterface $validator |
|
68
|
|
|
*/ |
|
69
|
|
|
public function __construct(Environment $twig, Pool $pool, AdminHelper $helper, $validator) |
|
70
|
|
|
{ |
|
71
|
|
|
// NEXT_MAJOR: Move ValidatorInterface check to method signature |
|
72
|
|
|
if (!($validator instanceof ValidatorInterface)) { |
|
73
|
|
|
throw new \InvalidArgumentException( |
|
74
|
|
|
'Argument 4 is an instance of '.get_class($validator).', expecting an instance of' |
|
75
|
|
|
.' \Symfony\Component\Validator\Validator\ValidatorInterface' |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$this->twig = $twig; |
|
80
|
|
|
$this->pool = $pool; |
|
81
|
|
|
$this->helper = $helper; |
|
82
|
|
|
$this->validator = $validator; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @throws NotFoundHttpException |
|
87
|
|
|
* |
|
88
|
|
|
* @return Response |
|
89
|
|
|
*/ |
|
90
|
|
|
public function appendFormFieldElementAction(Request $request) |
|
91
|
|
|
{ |
|
92
|
|
|
$action = new AppendFormFieldElementAction($this->twig, $this->pool, $this->helper); |
|
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
return $action($request); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @throws NotFoundHttpException |
|
99
|
|
|
* |
|
100
|
|
|
* @return Response |
|
101
|
|
|
*/ |
|
102
|
|
|
public function retrieveFormFieldElementAction(Request $request) |
|
103
|
|
|
{ |
|
104
|
|
|
$action = new RetrieveFormFieldElementAction($this->twig, $this->pool, $this->helper); |
|
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
return $action($request); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @throws NotFoundHttpException|\RuntimeException |
|
111
|
|
|
* |
|
112
|
|
|
* @return Response |
|
113
|
|
|
*/ |
|
114
|
|
|
public function getShortObjectDescriptionAction(Request $request) |
|
115
|
|
|
{ |
|
116
|
|
|
$action = new GetShortObjectDescriptionAction($this->twig, $this->pool); |
|
|
|
|
|
|
117
|
|
|
|
|
118
|
|
|
return $action($request); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @return Response |
|
123
|
|
|
*/ |
|
124
|
|
|
public function setObjectFieldValueAction(Request $request) |
|
125
|
|
|
{ |
|
126
|
|
|
$action = new SetObjectFieldValueAction($this->twig, $this->pool, $this->validator); |
|
|
|
|
|
|
127
|
|
|
|
|
128
|
|
|
return $action($request); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Retrieve list of items for autocomplete form field. |
|
133
|
|
|
* |
|
134
|
|
|
* @throws \RuntimeException |
|
135
|
|
|
* @throws AccessDeniedException |
|
136
|
|
|
* |
|
137
|
|
|
* @return JsonResponse |
|
138
|
|
|
*/ |
|
139
|
|
|
public function retrieveAutocompleteItemsAction(Request $request) |
|
140
|
|
|
{ |
|
141
|
|
|
$action = new RetrieveAutocompleteItemsAction($this->pool); |
|
142
|
|
|
|
|
143
|
|
|
return $action($request); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.