1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sonata Project package. |
5
|
|
|
* |
6
|
|
|
* (c) Thomas Rabaix <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sonata\AdminBundle\Controller; |
13
|
|
|
|
14
|
|
|
// NEXT_MAJOR: remove this file |
15
|
|
|
|
16
|
|
|
@trigger_error( |
|
|
|
|
17
|
|
|
'The '.__NAMESPACE__.'\CoreController class is deprecated since version 3.x and will be removed in 4.0.' |
18
|
|
|
.' Use '.__NAMESPACE__.'\SearchAction or '.__NAMESPACE__.'\DashboardAction instead.', |
19
|
|
|
E_USER_DEPRECATED |
20
|
|
|
); |
21
|
|
|
|
22
|
|
|
use Sonata\AdminBundle\Action\DashboardAction; |
23
|
|
|
use Sonata\AdminBundle\Action\SearchAction; |
24
|
|
|
use Sonata\AdminBundle\Admin\Pool; |
25
|
|
|
use Sonata\AdminBundle\Search\SearchHandler; |
26
|
|
|
use Sonata\AdminBundle\Templating\TemplateRegistryInterface; |
27
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
28
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
29
|
|
|
use Symfony\Component\HttpFoundation\Request; |
30
|
|
|
use Symfony\Component\HttpFoundation\Response; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @author Thomas Rabaix <[email protected]> |
34
|
|
|
*/ |
35
|
|
|
class CoreController extends Controller |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* @return Response |
39
|
|
|
*/ |
40
|
|
|
public function dashboardAction() |
41
|
|
|
{ |
42
|
|
|
$dashboardAction = $this->container->get(DashboardAction::class); |
43
|
|
|
|
44
|
|
|
return $dashboardAction($this->getCurrentRequest()); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The search action first render an empty page, if the query is set, then the template generates |
49
|
|
|
* some ajax request to retrieve results for each admin. The Ajax query returns a JSON response. |
50
|
|
|
* |
51
|
|
|
* @throws \RuntimeException |
52
|
|
|
* |
53
|
|
|
* @return JsonResponse|Response |
54
|
|
|
*/ |
55
|
|
|
public function searchAction(Request $request) |
56
|
|
|
{ |
57
|
|
|
$searchAction = $this->container->get(SearchAction::class); |
58
|
|
|
|
59
|
|
|
return $searchAction($request); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get the request object from the container. |
64
|
|
|
* |
65
|
|
|
* This method is compatible with both Symfony 2.3 and Symfony 3 |
66
|
|
|
* |
67
|
|
|
* NEXT_MAJOR: remove this method. |
68
|
|
|
* |
69
|
|
|
* @deprecated since 3.0, to be removed in 4.0 and action methods will be adjusted. |
70
|
|
|
* Use Symfony\Component\HttpFoundation\Request as an action argument |
71
|
|
|
* |
72
|
|
|
* @return Request |
73
|
|
|
*/ |
74
|
|
|
public function getRequest() |
75
|
|
|
{ |
76
|
|
|
@trigger_error( |
|
|
|
|
77
|
|
|
'The '.__METHOD__.' method is deprecated since 3.0 and will be removed in 4.0.'. |
78
|
|
|
' Inject the Symfony\Component\HttpFoundation\Request into the actions instead.', |
79
|
|
|
E_USER_DEPRECATED |
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
return $this->getCurrentRequest(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return Pool |
87
|
|
|
*/ |
88
|
|
|
protected function getAdminPool() |
89
|
|
|
{ |
90
|
|
|
return $this->container->get('sonata.admin.pool'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return SearchHandler |
95
|
|
|
*/ |
96
|
|
|
protected function getSearchHandler() |
97
|
|
|
{ |
98
|
|
|
return $this->get('sonata.admin.search.handler'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
protected function getBaseTemplate() |
105
|
|
|
{ |
106
|
|
|
if ($this->getCurrentRequest()->isXmlHttpRequest()) { |
107
|
|
|
return $this->getTemplateRegistry()->getTemplate('ajax'); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $this->getTemplateRegistry()->getTemplate('layout'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return TemplateRegistryInterface |
115
|
|
|
*/ |
116
|
|
|
private function getTemplateRegistry() |
117
|
|
|
{ |
118
|
|
|
return $this->container->get('sonata.admin.global_template_registry'); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get the request object from the container. |
123
|
|
|
* |
124
|
|
|
* @return Request |
125
|
|
|
*/ |
126
|
|
|
private function getCurrentRequest() |
127
|
|
|
{ |
128
|
|
|
return $this->container->get('request_stack')->getCurrentRequest(); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
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.