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