1: <?php
2: /**
3: * This file is part of the Autarky package.
4: *
5: * (c) Andreas Lutro <anlutro@gmail.com>
6: *
7: * For the full copyright and license information, please view the LICENSE
8: * file that was distributed with this source code.
9: */
10:
11: namespace Autarky\Routing;
12:
13: use Symfony\Component\HttpFoundation\Request;
14: use Symfony\Component\HttpFoundation\Response;
15: use Symfony\Component\HttpFoundation\RedirectResponse;
16: use Symfony\Component\HttpFoundation\JsonResponse;
17: use Symfony\Component\EventDispatcher\Event;
18:
19: use Autarky\Container\ContainerAwareTrait;
20:
21: /**
22: * Trait for controller functionality. Any class that implements this trait
23: * should also implement the interface Autarky\Container\ContainerAwareInterface
24: * - the interface's methods are implemented by the trait, but you still need to
25: * implement the interface on the class.
26: */
27: trait ControllerTrait
28: {
29: use ContainerAwareTrait;
30:
31: /**
32: * Render a template.
33: *
34: * @param string $name Name of the view.
35: * @param array $data Data to pass to the view.
36: *
37: * @return string
38: */
39: protected function render($name, array $data = array())
40: {
41: return $this->container->resolve('Autarky\TwigTemplating\TemplatingEngine')
42: ->render($name, $data);
43: }
44:
45: /**
46: * Generate the URL to a route.
47: *
48: * @param string $name Name of the route.
49: * @param array $params Route parameters.
50: *
51: * @return string
52: */
53: protected function url($name, array $params = array())
54: {
55: return $this->container->resolve('Autarky\Routing\UrlGenerator')
56: ->getRouteUrl($name, $params);
57: }
58:
59: /**
60: * Get the session manager.
61: *
62: * @return \Symfony\Component\HttpFoundation\Session\Session
63: */
64: protected function getSession()
65: {
66: return $this->container->resolve('Symfony\Component\HttpFoundation\Session\Session');
67: }
68:
69: /**
70: * Flash something to the session.
71: *
72: * @param string $key
73: * @param mixed $value
74: *
75: * @return void
76: */
77: protected function flash($key, $value)
78: {
79: $this->getSession()
80: ->getFlashBag()
81: ->set($key, $value);
82: }
83:
84: /**
85: * Flash an array of messages to the session.
86: *
87: * @param array $messages
88: *
89: * @return void
90: */
91: protected function flashMessages($messages)
92: {
93: $flashBag = $this->getSession()
94: ->getFlashBag();
95:
96: foreach ((array) $messages as $message) {
97: $flashBag->add('_messages', $message);
98: }
99: }
100:
101: /**
102: * Flash input to session.
103: *
104: * @param \Symfony\Component\HttpFoundation\Request $request Optional
105: *
106: * @return void
107: */
108: protected function flashInput(Request $request = null)
109: {
110: if ($request === null) {
111: $request = $this->container
112: ->resolve('Symfony\Component\HttpFoundation\RequestStack')
113: ->getCurrentRequest();
114: }
115:
116: $this->flash('_old_input', $request->request->all());
117: }
118:
119: /**
120: * Get old input flashed to the session.
121: *
122: * @return array
123: */
124: protected function getOldInput()
125: {
126: return $this->getSession()
127: ->getFlashBag()
128: ->peek('_old_input', []);
129: }
130:
131: /**
132: * Get the event dispatcher instance.
133: *
134: * @return \Symfony\Component\EventDispatcher\EventDispatcherInterface
135: */
136: protected function getEventDispatcher()
137: {
138: return $this->container->resolve('Symfony\Component\EventDispatcher\EventDispatcherInterface');
139: }
140:
141: /**
142: * Dispatch an event.
143: *
144: * @param string $name
145: * @param Event $event
146: *
147: * @return mixed
148: */
149: protected function dispatchEvent($name, Event $event)
150: {
151: return $this->getEventDispatcher()
152: ->dispatch($name, $event);
153: }
154:
155: /**
156: * Log a message.
157: *
158: * @param string $level https://github.com/php-fig/log/blob/master/Psr/Log/LogLevel.php
159: * @param string $message
160: * @param array $context
161: *
162: * @return void
163: */
164: protected function log($level, $message, array $context = array())
165: {
166: $this->getLogger()
167: ->log($level, $message, $context);
168: }
169:
170: /**
171: * Get the logger instance.
172: *
173: * @return \Psr\Log\LoggerInterface
174: */
175: protected function getLogger()
176: {
177: return $this->container->resolve('Psr\Log\LoggerInterface');
178: }
179:
180: /**
181: * Create a response.
182: *
183: * @param string $content
184: * @param integer $statusCode
185: *
186: * @return \Symfony\Component\HttpFoundation\Response
187: */
188: protected function response($content, $statusCode = 200)
189: {
190: return new Response($content, $statusCode);
191: }
192:
193: /**
194: * Create a redirect response.
195: *
196: * @param string $name Name of the route to redirect to
197: * @param array $params Route parameters
198: * @param integer $statusCode Default: 302
199: *
200: * @return \Symfony\Component\HttpFoundation\RedirectResponse
201: */
202: protected function redirect($name, array $params = array(), $statusCode = 302)
203: {
204: return new RedirectResponse($this->url($name, $params), $statusCode);
205: }
206:
207: /**
208: * Create a JSON response.
209: *
210: * @param array $data
211: * @param integer $statusCode
212: *
213: * @return \Symfony\Component\HttpFoundation\JsonResponse
214: */
215: protected function json(array $data, $statusCode = 200)
216: {
217: return new JsonResponse($data, $statusCode);
218: }
219: }
220: