|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Stinger Soft Platform package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Oliver Kotte <[email protected]> |
|
7
|
|
|
* (c) Florian Meyer <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
10
|
|
|
* file that was distributed with this source code. |
|
11
|
|
|
*/ |
|
12
|
|
|
namespace StingerSoft\PlatformBundle\Controller; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
15
|
|
|
use Symfony\Component\Stopwatch\Stopwatch; |
|
16
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
|
17
|
|
|
use Doctrine\Common\Persistence\ObjectRepository; |
|
18
|
|
|
|
|
19
|
|
|
class BaseController extends Controller { |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Returns the default locale for the system |
|
23
|
|
|
* |
|
24
|
|
|
* @return string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected function getDefaultLocale() { |
|
27
|
|
|
return 'en'; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Returns all available (i.e. |
|
32
|
|
|
* configured) locales of the system |
|
33
|
|
|
* |
|
34
|
|
|
* @return string[] |
|
35
|
|
|
*/ |
|
36
|
|
|
protected function getLocales() { |
|
37
|
|
|
return array( |
|
38
|
|
|
'en' |
|
39
|
|
|
); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Proxy to use the transChoice method of the translator |
|
44
|
|
|
* |
|
45
|
|
|
* @see TranslatorInterface::transChoice() |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $id |
|
48
|
|
|
* @param int $number |
|
49
|
|
|
* @param array $parameters |
|
50
|
|
|
* @param string $domain |
|
|
|
|
|
|
51
|
|
|
* @param string $locale |
|
|
|
|
|
|
52
|
|
|
* @return string |
|
53
|
|
|
*/ |
|
54
|
|
View Code Duplication |
protected function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null) { |
|
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* |
|
57
|
|
|
* @var TranslatorInterface $translator |
|
58
|
|
|
*/ |
|
59
|
|
|
$translator = $this->getTranslator(); |
|
60
|
|
|
return $translator->transChoice($id, $number, $parameters, $domain, $locale); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Proxy to use the transChoice method of the translator |
|
65
|
|
|
* |
|
66
|
|
|
* @see TranslatorInterface::trans() |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $id |
|
69
|
|
|
* @param array $parameters |
|
70
|
|
|
* @param string $domain |
|
|
|
|
|
|
71
|
|
|
* @param string $locale |
|
|
|
|
|
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
|
View Code Duplication |
protected function trans($id, array $parameters = array(), $domain = null, $locale = null) { |
|
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* |
|
77
|
|
|
* @var TranslatorInterface $translator |
|
78
|
|
|
*/ |
|
79
|
|
|
$translator = $this->getTranslator(); |
|
80
|
|
|
return $translator->trans($id, $parameters, $domain, $locale); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Returns the translator service |
|
85
|
|
|
* |
|
86
|
|
|
* @return TranslatorInterface |
|
87
|
|
|
*/ |
|
88
|
|
|
protected function getTranslator() { |
|
89
|
|
|
return $this->get('translator'); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Returns the paginator service |
|
94
|
|
|
* |
|
95
|
|
|
* @return \Knp\Component\Pager\PaginatorInterface |
|
96
|
|
|
*/ |
|
97
|
|
|
protected function getPaginator() { |
|
98
|
|
|
return $this->get('knp_paginator'); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Returns the stopwatch service if available |
|
103
|
|
|
* |
|
104
|
|
|
* @return Stopwatch|NULL |
|
|
|
|
|
|
105
|
|
|
*/ |
|
106
|
|
|
protected function getStopWatch() { |
|
107
|
|
|
if($this->has('debug.stopwatch')) { |
|
108
|
|
|
return $this->get('debug.stopwatch'); |
|
109
|
|
|
} |
|
110
|
|
|
return null; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Checks whether the current user has the specified role or not |
|
115
|
|
|
* |
|
116
|
|
|
* @param string $role |
|
117
|
|
|
* @throws \LogicException |
|
118
|
|
|
* @return boolean |
|
119
|
|
|
*/ |
|
120
|
|
|
protected function hasRole($role) { |
|
121
|
|
|
if(!$this->container->has('security.authorization_checker')) { |
|
122
|
|
|
throw new \LogicException('The SecurityBundle is not registered in your application.'); |
|
123
|
|
|
} |
|
124
|
|
|
return false !== $this->get('security.authorization_checker')->isGranted($role); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Returns the repository for the given clazz |
|
129
|
|
|
* |
|
130
|
|
|
* @param string $class |
|
131
|
|
|
* @return ObjectRepository |
|
132
|
|
|
*/ |
|
133
|
|
|
protected function getRepository($class) { |
|
134
|
|
|
return $this->getDoctrine()->getManagerForClass($class)->getRepository($class); |
|
135
|
|
|
} |
|
136
|
|
|
} |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.