Commit | Line | Data |
---|---|---|
44d399bc S |
1 | <?php |
2 | /** | |
3 | * Zend Framework (http://framework.zend.com/) | |
4 | * | |
5 | * @link http://github.com/zendframework/zf2 for the canonical source repository | |
6 | * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) | |
7 | * @license http://framework.zend.com/license/new-bsd New BSD License | |
8 | */ | |
9 | ||
10 | namespace Zend\Mvc\Controller\Plugin; | |
11 | ||
12 | use Zend\Http\Response; | |
13 | use Zend\Mvc\Exception; | |
14 | use Zend\Mvc\InjectApplicationEventInterface; | |
15 | use Zend\Mvc\MvcEvent; | |
16 | ||
17 | /** | |
18 | * @todo allow specifying status code as a default, or as an option to methods | |
19 | */ | |
20 | class Redirect extends AbstractPlugin | |
21 | { | |
22 | protected $event; | |
23 | protected $response; | |
24 | ||
25 | /** | |
26 | * Generate redirect response based on given route | |
27 | * | |
28 | * @param string $route RouteInterface name | |
29 | * @param array $params Parameters to use in url generation, if any | |
30 | * @param array $options RouteInterface-specific options to use in url generation, if any | |
31 | * @param bool $reuseMatchedParams Whether to reuse matched parameters | |
32 | * @return Response | |
33 | * @throws Exception\DomainException if composed controller does not implement InjectApplicationEventInterface, or | |
34 | * router cannot be found in controller event | |
35 | */ | |
36 | public function toRoute($route = null, $params = [], $options = [], $reuseMatchedParams = false) | |
37 | { | |
38 | $controller = $this->getController(); | |
39 | if (!$controller || !method_exists($controller, 'plugin')) { | |
40 | throw new Exception\DomainException('Redirect plugin requires a controller that defines the plugin() method'); | |
41 | } | |
42 | ||
43 | $urlPlugin = $controller->plugin('url'); | |
44 | ||
45 | if (is_scalar($options)) { | |
46 | $url = $urlPlugin->fromRoute($route, $params, $options); | |
47 | } else { | |
48 | $url = $urlPlugin->fromRoute($route, $params, $options, $reuseMatchedParams); | |
49 | } | |
50 | ||
51 | return $this->toUrl($url); | |
52 | } | |
53 | ||
54 | /** | |
55 | * Generate redirect response based on given URL | |
56 | * | |
57 | * @param string $url | |
58 | * @return Response | |
59 | */ | |
60 | public function toUrl($url) | |
61 | { | |
62 | $response = $this->getResponse(); | |
63 | $response->getHeaders()->addHeaderLine('Location', $url); | |
64 | $response->setStatusCode(302); | |
65 | return $response; | |
66 | } | |
67 | ||
68 | /** | |
69 | * Refresh to current route | |
70 | * | |
71 | * @return Response | |
72 | */ | |
73 | public function refresh() | |
74 | { | |
75 | return $this->toRoute(null, [], [], true); | |
76 | } | |
77 | ||
78 | /** | |
79 | * Get the response | |
80 | * | |
81 | * @return Response | |
82 | * @throws Exception\DomainException if unable to find response | |
83 | */ | |
84 | protected function getResponse() | |
85 | { | |
86 | if ($this->response) { | |
87 | return $this->response; | |
88 | } | |
89 | ||
90 | $event = $this->getEvent(); | |
91 | $response = $event->getResponse(); | |
92 | if (!$response instanceof Response) { | |
93 | throw new Exception\DomainException('Redirect plugin requires event compose a response'); | |
94 | } | |
95 | $this->response = $response; | |
96 | return $this->response; | |
97 | } | |
98 | ||
99 | /** | |
100 | * Get the event | |
101 | * | |
102 | * @return MvcEvent | |
103 | * @throws Exception\DomainException if unable to find event | |
104 | */ | |
105 | protected function getEvent() | |
106 | { | |
107 | if ($this->event) { | |
108 | return $this->event; | |
109 | } | |
110 | ||
111 | $controller = $this->getController(); | |
112 | if (!$controller instanceof InjectApplicationEventInterface) { | |
113 | throw new Exception\DomainException('Redirect plugin requires a controller that implements InjectApplicationEventInterface'); | |
114 | } | |
115 | ||
116 | $event = $controller->getEvent(); | |
117 | if (!$event instanceof MvcEvent) { | |
118 | $params = $event->getParams(); | |
119 | $event = new MvcEvent(); | |
120 | $event->setParams($params); | |
121 | } | |
122 | $this->event = $event; | |
123 | ||
124 | return $this->event; | |
125 | } | |
126 | } |