1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Implementation of controller for Slim Framework v3 (https://github.com/orx0r/slim3-controller) |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/orx0r/slim3-controller |
6
|
|
|
* @license https://raw.githubusercontent.com/orx0r/slim3-controller/master/LICENSE |
7
|
|
|
*/ |
8
|
|
|
namespace Orx0r\Slim\Controller; |
9
|
|
|
|
10
|
|
|
use Interop\Container\ContainerInterface as Container; |
11
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
12
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Controller |
16
|
|
|
* @package Orx0r\Slim\Controller |
17
|
|
|
*/ |
18
|
|
|
class Controller |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var Container |
23
|
|
|
*/ |
24
|
|
|
protected $container; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Request |
28
|
|
|
*/ |
29
|
|
|
protected $request; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var Response |
33
|
|
|
*/ |
34
|
|
|
protected $response; |
35
|
|
|
|
36
|
5 |
|
public function __construct(Container $container) |
37
|
|
|
{ |
38
|
5 |
|
$this->container = $container; |
39
|
5 |
|
} |
40
|
|
|
|
41
|
2 |
|
public function __get($property) |
42
|
|
|
{ |
43
|
2 |
|
if ($this->container->has($property)) { |
44
|
1 |
|
return $this->container->get($property); |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
throw new \RuntimeException(sprintf('Property %s does not exist', get_class($this) . "::$property")); |
48
|
|
|
} |
49
|
|
|
|
50
|
3 |
|
public function __call($name, $arguments) |
51
|
|
|
{ |
52
|
3 |
|
$method = 'action' . ucfirst($name); |
53
|
3 |
|
if (method_exists($this, $method)) { |
54
|
2 |
|
list($this->request, $this->response, $params) = $arguments; |
55
|
2 |
|
return call_user_func_array([$this, $method], $params); |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
throw new \RuntimeException(sprintf('Method %s does not exist', get_class($this) . "::$name()")); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Shorthand method for render templates |
63
|
|
|
* |
64
|
|
|
* @link http://www.slimframework.com/docs/features/templates.html |
65
|
|
|
* |
66
|
|
|
* @param string $template Template pathname relative to templates directory |
67
|
|
|
* @param array $data Associative array of template variables |
68
|
|
|
* |
69
|
|
|
* @return mixed |
70
|
|
|
*/ |
71
|
1 |
|
public function render($template, $data = []) |
72
|
|
|
{ |
73
|
1 |
|
return $this->container->get('view')->render($this->response, $template, $data); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|