aboutsummaryrefslogtreecommitdiff
path: root/lib/trails/Controller.php
blob: 191838069614395e0d152d7f0640a1c6db4ecb5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
<?php
namespace Trails;

use Flexi\Factory;
use Flexi\Template;
use Flexi\TemplateNotFoundException;
use Trails\Exceptions\DoubleRenderError;
use Trails\Exceptions\UnknownAction;

/**
 * A Controller is responsible for matching the unconsumed part of an URI
 * to an action using the left over words as arguments for that action. The
 * action is then mapped to method of the controller instance which is called
 * with the just mentioned arguments. That method can send the #render_action,
 * #render_template, #render_text, #render_nothing or #redirect method.
 * Otherwise the #render_action is called with the current action as argument.
 * If the action method sets instance variables during performing, they will be
 * be used as attributes for the flexi-template opened by #render_action or
 * #render_template. A controller's response's body is populated with the output
 * of the #render_* methods. The action methods can add additional headers or
 * change the status of that response.
 *
 * @package       trails
 *
 * @author        mlunzena
 * @copyright (c) Authors
 * @version       $Id: trails.php 7001 2008-04-04 11:20:27Z mlunzena $
 */
class Controller
{
    protected Dispatcher $dispatcher;
    protected Response $response;
    protected bool $performed = false;
    protected Template|string|null $layout = null;
    private string $format = 'html';

    /**
     * @param Dispatcher $dispatcher the dispatcher who creates this instance
     */
    public function __construct(Dispatcher $dispatcher)
    {
        $this->dispatcher = $dispatcher;
        $this->erase_response();
    }

    /**
     * Resets the response of the controller
     *
     * @return void
     */
    public function erase_response()
    {
        $this->performed = false;
        $this->response = new Response();
    }

    /**
     * Return this controller's response
     *
     * @return Response the controller's response
     */
    public function get_response()
    {
        return $this->response;
    }

    /**
     * This method extracts an action string and further arguments from it's
     * parameter. The action string is mapped to a method being called afterwards
     * using the said arguments. That method is called and a response object is
     * generated, populated and sent back to the dispatcher.
     *
     * @param string $unconsumed
     *
     * @return Response
     * @throws UnknownAction
     */
    public function perform($unconsumed)
    {
        [$action, $args, $format] = $this->extract_action_and_args($unconsumed);

        $this->format = $format ?? 'html';

        $before_filter_result = $this->before_filter($action, $args);

        # send action to controller
        # TODO (mlunzena) shouldn't the after filter be triggered too?
        if (!($before_filter_result === false || $this->performed)) {

            $callable = $this->map_action($action);

            if (is_callable($callable)) {
                $callable(...$args);
            } else {
                $this->does_not_understand($action, $args);
            }

            if (!$this->performed) {
                $this->render_action($action);
            }

            $this->after_filter($action, $args);
        }

        return $this->response;
    }

    /**
     * Extracts action and args from a string.
     *
     * @param string $string the processed string
     * @return array        an array with two elements - a string containing the
     *                      action and an array of strings representing the args
     */
    public function extract_action_and_args($string)
    {
        if ('' === $string) {
            return $this->default_action_and_args();
        }

        // find optional file extension
        $format = null;
        if (preg_match('/^(.*[^\/.])\.(\w+)$/', $string, $matches)) {
            [, $string, $format] = $matches;
        }

        // TODO this should possibly remove empty tokens
        $args = explode('/', $string);
        $action = array_shift($args);
        return [$action, $args, $format];
    }

    /**
     * Return the default action and arguments
     *
     * @return array containing the action, an array of args and the format
     */
    public function default_action_and_args()
    {
        return ['index', [], null];
    }

    /**
     * Maps the action to an actual method name.
     *
     * @param string $action
     * @return array  the mapped method name
     */
    public function map_action($action)
    {
        return [&$this, $action . '_action'];
    }

    /**
     * Callback function being called before an action is executed. If this
     * function does not return FALSE, the action will be called, otherwise
     * an error will be generated and processing will be aborted. If this function
     * already #rendered or #redirected, further processing of the action is
     * withheld.
     *
     * @param string $action Name of the action to perform.
     * @param array  $args   An array of arguments to the action.
     * @return bool|void
     */
    public function before_filter(&$action, &$args)
    {
    }

    /**
     * Callback function being called after an action is executed.
     *
     * @param string $action Name of the action to perform.
     * @param array  $args   An array of arguments to the action.
     * @return void
     */
    public function after_filter($action, $args)
    {
    }

    /**
     * @param string $action
     * @param array  $args
     * @return void
     * @throws UnknownAction
     */
    public function does_not_understand($action, $args)
    {
        throw new Exceptions\UnknownAction("No action responded to '$action'.");
    }

    /**
     * @param string $to
     *
     * @return void
     * @throws DoubleRenderError
     */
    public function redirect($to)
    {
        if ($this->performed) {
            throw new Exceptions\DoubleRenderError();
        }

        $this->performed = true;

        # get uri; keep absolute URIs
        $url = preg_match('#^(/|\w+://)#', $to)
            ? $to
            : $this->url_for($to);

        $this->response->add_header('Location', $url)->set_status(302);
    }

    /**
     * Renders the given text as the body of the response.
     *
     * @param string $text the text to be rendered
     * @return void
     * @throws DoubleRenderError
     */
    public function render_text($text = ' ')
    {
        if ($this->performed) {
            throw new Exceptions\DoubleRenderError();
        }

        $this->performed = true;

        $this->response->set_body($text);
    }

    /**
     * Renders the empty string as the response's body.
     *
     * @return void
     * @throws DoubleRenderError
     */
    public function render_nothing()
    {
        $this->render_text('');
    }

    /**
     * Renders the template of the given action as the response's body.
     *
     * @param string $action the action
     * @return void
     */
    public function render_action($action)
    {
        $this->render_template(
            $this->get_default_template($action),
            $this->layout
        );
    }

    public function get_default_template($action)
    {
        $controller_name = Inflector::underscore(
            substr(static::class, 0, -10)
        );
        return $controller_name . '/' . $action;
    }

    /**
     * Renders a template using an optional layout template.
     *
     * @param Template|string      $template_name a flexi template
     * @param Template|string|null $layout        a flexi template which is used as layout
     *
     * @return void
     * @throws DoubleRenderError
     * @throws TemplateNotFoundException
     */
    public function render_template($template_name, $layout = null)
    {
        $factory = $this->get_template_factory();
        $template = $factory->open($template_name);

        $template->set_attributes($this->get_assigned_variables());

        if (isset($layout)) {
            $template->set_layout($layout);
        }

        $this->render_text($template->render());
    }

    /**
     * Create and return a template factory for this controller.
     *
     * @return Factory
     */
    public function get_template_factory()
    {
        return new Factory($this->dispatcher->trails_root . '/views/');
    }

    /**
     * This method returns all the set instance variables to be used as attributes
     * for a template. This controller is returned too as value for
     * key 'controller'.
     *
     * @return array  an associative array of variables for the template
     */
    public function get_assigned_variables()
    {
        $assigns = [];
        $protected = get_class_vars(static::class);

        foreach (get_object_vars($this) as $var => $value) {
            if (!array_key_exists($var, $protected)) {
                $assigns[$var] =& $this->$var;
            }
        }

        $assigns['controller'] = $this;

        return $assigns;
    }

    /**
     * Sets the layout to be used by this controller per default.
     *
     * @param Template|string|null $layout a flexi template to be used as layout
     * @return void
     */
    public function set_layout($layout)
    {
        $this->layout = $layout;
    }

    /**
     * Returns a URL to a specified route to your Trails application.
     *
     * Example:
     * Your Trails application is located at 'http://example.com/dispatch.php'.
     * So your dispatcher's trails_uri is set to 'http://example.com/dispatch.php'
     * If you want the URL to your 'wiki' controller with action 'show' and
     * parameter 'page' you should send:
     *
     *   $url = $controller->url_for('wiki/show', 'page');
     *
     * $url should then contain 'http://example.com/dispatch.php/wiki/show/page'.
     *
     * The first parameter is a string containing the controller and optionally an
     * action:
     *
     *   - "{controller}/{action}"
     *   - "path/to/controller/action"
     *   - "controller"
     *
     * This "controller/action" string is not url encoded. You may provide
     * additional parameter which will be urlencoded and concatenated with
     * slashes:
     *
     *     $controller->url_for('wiki/show', 'page');
     *     -> 'wiki/show/page'
     *
     *     $controller->url_for('wiki/show', 'page', 'one and a half');
     *     -> 'wiki/show/page/one+and+a+half'
     *
     * @param string $to a string containing a controller and optionally an action
     * @return string  a URL to this route
     */
    public function url_for($to/*, ...*/)
    {
        # urlencode all but the first argument
        $args = func_get_args();
        $args = array_map('urlencode', $args);
        $args[0] = $to;

        return $this->dispatcher->trails_uri . '/' . implode('/', $args);
    }

    /**
     * @param int $status
     * @return void
     */
    public function set_status($status, $reason_phrase = null)
    {
        $this->response->set_status($status, $reason_phrase);
    }

    /**
     * Sets the content type of the controller's response.
     *
     * @param string $type the content type
     * @return void
     */
    public function set_content_type($type)
    {
        $this->response->add_header('Content-Type', $type);
    }

    /**
     * Exception handler called when the performance of an action raises an
     * exception.
     *
     * @param \Throwable $exception the thrown exception
     * @return Response  a response object
     */
    public function rescue($exception)
    {
        return $this->dispatcher->trails_error($exception);
    }

    public function respond_to($ext)
    {
        return $this->format === $ext;
    }
}