1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mero\Bundle\BaseBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Mero\Bundle\BaseBundle\Exception\UnsupportedFormatException; |
6
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
7
|
|
|
use Symfony\Component\HttpFoundation\Request; |
8
|
|
|
use Symfony\Component\HttpFoundation\Response; |
9
|
|
|
|
10
|
|
|
class AbstractController extends Controller |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Constant with webservice response type JSON |
15
|
|
|
*/ |
16
|
|
|
const WS_RESPONSE_JSON = 'json'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Constant with webservice response type XML |
20
|
|
|
*/ |
21
|
|
|
const WS_RESPONSE_XML = 'xml'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Gets the current request object. |
25
|
|
|
* |
26
|
|
|
* @return Request |
27
|
|
|
*/ |
28
|
|
|
protected function getCurrentRequest() |
29
|
|
|
{ |
30
|
|
|
return $this->container->get('request_stack')->getCurrentRequest(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Gets the action name. |
35
|
|
|
* |
36
|
|
|
* @param Request $request HTTP request object |
37
|
|
|
* |
38
|
|
|
* @return string Action name |
39
|
|
|
*/ |
40
|
|
|
protected function getActionName(Request &$request) |
41
|
|
|
{ |
42
|
|
|
$action = explode('::', $request->attributes->get('_controller')); |
43
|
|
|
|
44
|
|
|
return $action[1]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Gets the bundle name. |
49
|
|
|
* |
50
|
|
|
* <b>Attention! This method returns the bundle name as the conventions of framewok.</b> |
51
|
|
|
* Example: "Mero/Bundle/BaseBundle" or "Mero/BaseBundle" returns "MeroBaseBundle". |
52
|
|
|
* |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
protected function getBundleName() |
56
|
|
|
{ |
57
|
|
|
$matches = []; |
58
|
|
|
$className = str_replace('\Bundle\\', '', get_class($this)); |
59
|
|
|
preg_match('/(.*)\\\Controller/', $className, $matches); |
60
|
|
|
|
61
|
|
|
return count($matches) != 0 |
62
|
|
|
? $matches[1] |
63
|
|
|
: null; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Gets the route name. |
68
|
|
|
* |
69
|
|
|
* @param Request $request HTTP request object |
70
|
|
|
* |
71
|
|
|
* @return string Route name |
72
|
|
|
*/ |
73
|
|
|
protected function getRouteName(Request &$request) |
74
|
|
|
{ |
75
|
|
|
return $request->attributes->get('_route'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Return webservice(API Rest JSON or SOAP XML) response. |
80
|
|
|
* |
81
|
|
|
* @param mixed $data The response data |
82
|
|
|
* @param int $status The response status code |
83
|
|
|
* @param array $headers An array of response headers |
84
|
|
|
* @param string $format Response format(json or xml) |
85
|
|
|
* |
86
|
|
|
* @throws UnsupportedFormatException When format is not json or xml |
87
|
|
|
* |
88
|
|
|
* @return Response |
89
|
|
|
*/ |
90
|
|
|
protected function wsResponse($data, $status = 200, array $headers = [], $format = self::WS_RESPONSE_JSON) |
91
|
|
|
{ |
92
|
|
|
if (!in_array($format, [self::WS_RESPONSE_JSON, self::WS_RESPONSE_XML])) { |
93
|
|
|
throw new UnsupportedFormatException(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return new Response( |
97
|
|
|
$this->container->get('serializer')->serialize($data, $format), |
98
|
|
|
$status, |
99
|
|
|
$headers |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|