1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the jade/jade package. |
5
|
|
|
* |
6
|
|
|
* (c) Slince <[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 Jade\Controller; |
13
|
|
|
|
14
|
|
|
use Jade\ContainerAwareInterface; |
15
|
|
|
use Jade\ContainerAwareTrait; |
16
|
|
|
use Zend\Diactoros\Response; |
17
|
|
|
|
18
|
|
|
class Controller implements ContainerAwareInterface |
19
|
|
|
{ |
20
|
|
|
use ContainerAwareTrait; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* 渲染模板 |
24
|
|
|
* @param string $view |
25
|
|
|
* @param array $parameters |
26
|
|
|
* @return string |
27
|
|
|
*/ |
28
|
|
|
protected function renderView($view, array $parameters = []) |
29
|
|
|
{ |
30
|
|
|
return $this->container->get('twig')->render($view, $parameters); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* 渲染模板 |
35
|
|
|
* |
36
|
|
|
* @param string $view |
37
|
|
|
* @param array $parameters |
38
|
|
|
* @return Response\HtmlResponse |
39
|
|
|
*/ |
40
|
|
|
protected function render($view, array $parameters = []) |
41
|
|
|
{ |
42
|
|
|
$content = $this->renderView($view, $parameters); |
43
|
|
|
return new Response\HtmlResponse($content); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $id |
48
|
|
|
* @return mixed |
49
|
|
|
*/ |
50
|
|
|
protected function get($id) |
51
|
|
|
{ |
52
|
|
|
return $this->container->get($id); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Returns a JsonResponse that uses the serializer component if enabled, or json_encode. |
57
|
|
|
* |
58
|
|
|
* @final |
59
|
|
|
*/ |
60
|
|
|
protected function json($data, int $status = 200, array $headers = [], array $context = []) |
61
|
|
|
{ |
62
|
|
|
if ($this->container->has('serializer')) { |
63
|
|
|
$json = $this->container->get('serializer')->serialize($data, 'json', array_merge([ |
|
|
|
|
64
|
|
|
'json_encode_options' => Response\JsonResponse::DEFAULT_JSON_FLAGS, |
65
|
|
|
], $context)); |
66
|
|
|
} |
67
|
|
|
return new Response\JsonResponse($data, $status, $headers); |
68
|
|
|
} |
69
|
|
|
} |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.