This repository was archived by the owner on Jul 18, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathBase.php
More file actions
executable file
·154 lines (132 loc) · 3.27 KB
/
Base.php
File metadata and controls
executable file
·154 lines (132 loc) · 3.27 KB
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
<?php
/*
* This file is part of the overtrue/yaf-skeleton.
*
* (c) overtrue <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
use App\Presenters\PresenterInterface;
use App\Services\Http\Response;
use Psr\Http\Message\ResponseInterface;
use Yaf\Controller_Abstract as YafController;
/**
* class BaseController.
*
* @author overtrue <[email protected]>
*/
abstract class BaseController extends YafController
{
/**
* Headers.
*
* <pre>
* [
* 'content-type' => 'application/json;charset=utf-8'
* ]
* </pre>
*
* @var array
*/
protected $headers = [];
/**
* 主逻辑.
*/
public function indexAction()
{
$response = $this->handle();
return $this->handleResponse($response);
}
/**
* 业务主逻辑.
*
* @return array
*/
abstract public function handle();
/**
* 添加 header.
*
* @param string $name
* @param mixed $value
*
* @return $this
*/
public function header(string $name, $value)
{
$this->headers[$name] = $value;
return $this;
}
/**
* 获取设置的 headers.
*
* @return array
*/
public function getHeaders()
{
return $this->headers;
}
/**
* 调用其它控制器.
*
* @param string $controller
*
* @return mixed
*/
public function call($controller)
{
$controller = $this->normalizeControllerName($controller);
return (new $controller($this->getRequest(), $this->getResponse(), $this->getView()))->indexAction();
}
/**
* 处理响应内容.
*
* @param callable|array|string|\Psr\Http\Message\ResponseInterface $response
*
* @return mixed
*/
protected function handleResponse($response)
{
if (is_callable($response)) {
$response = $response($this);
}
if ($response instanceof PresenterInterface) {
$response = $response->toArray();
}
if (is_array($response)) {
$response = json_encode($response);
$this->header('Content-Type', 'application/json;charset=utf-8');
}
if (defined('TESTING')) {
return;
}
if (!($response instanceof ResponseInterface)) {
$response = new Response(200, $this->headers, $response);
}
//兼容Yaf的Response输出逻辑
$response->setYafResponse($this->getResponse());
$response->send();
return $response;
}
/**
* 格式化控制器名称.
*
* @param string $controllerName
*
* @return string
*/
public function normalizeControllerName($controllerName)
{
$replacements = [
' ' => '',
'/' => ' ',
'_' => ' ',
'Controller' => '',
];
$controller = str_replace(array_keys($replacements), $replacements, trim($controllerName));
$controller = preg_replace_callback('/([^_\s])([A-Z])/', function ($matches) {
return $matches[1].' '.$matches[2];
}, $controller);
return str_replace(' ', '_', ucwords($controller.'Controller'));
}
}