Skip to content

Commit da18372

Browse files
committed
some bug fixed
1 parent a64eba1 commit da18372

File tree

8 files changed

+208
-16
lines changed

8 files changed

+208
-16
lines changed

phpunit.xml.dist

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<phpunit backupGlobals="false"
44
backupStaticAttributes="false"
5-
bootstrap="./tests/boot.php"
5+
bootstrap="test/boot.php"
66
colors="false"
77
convertErrorsToExceptions="true"
88
convertNoticesToExceptions="true"
@@ -12,13 +12,13 @@
1212
>
1313
<testsuites>
1414
<testsuite name="Web Lib Test Suite">
15-
<directory>./tests/</directory>
15+
<directory>test</directory>
1616
</testsuite>
1717
</testsuites>
1818

1919
<filter>
2020
<whitelist>
21-
<directory suffix=".php">./examples</directory>
21+
<directory suffix=".php">src</directory>
2222
</whitelist>
2323
</filter>
24-
</phpunit>
24+
</phpunit>

src/Helper/RespondUtilTrait.php renamed to src/Traits/RespondUtilTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
* Time: 14:22
77
*/
88

9-
namespace MyLib\Web\Helper;
9+
namespace MyLib\Web\Traits;
1010

1111
/**
1212
* Class RespondUtilTrait
13-
* @package MyLib\Web\Helper
13+
* @package MyLib\Web\Traits
1414
*
1515
* ```php
1616
* class Respond extends ResponseCode {

src/Traits/SimpleAssetsLoaderTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
* File: AssetsRendererTrait.php
88
*/
99

10-
namespace MyLib\Web;
10+
namespace MyLib\Web\Traits;
1111

1212
/**
1313
* Class AssetsRendererTrait
14-
* @package MyLib\Web
14+
* @package MyLib\Web\Traits
1515
*/
1616
trait SimpleAssetsLoaderTrait
1717
{

src/Traits/ViewRendererAwareTrait.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
* Time: 9:12
77
*/
88

9-
namespace MyLib\Web;
9+
namespace MyLib\Web\Traits;
10+
11+
use MyLib\Web\ViewRenderer;
1012

1113
/**
1214
* Trait ViewRendererAwareTrait
13-
* @package MyLib\Web
15+
* @package MyLib\Web\Traits
1416
*/
1517
trait ViewRendererAwareTrait
1618
{

src/Util/APIAccessChecker.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
* Time: 22:39
77
*/
88

9-
namespace MyLib\Web;
9+
namespace MyLib\Web\Util;
1010

1111
/**
1212
* Class APIAccessChecker
13-
* @package MyLib\Web
13+
* @package MyLib\Web\Util
1414
*/
1515
class APIAccessChecker
1616
{

src/Util/Environment.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
* File: Environment.php
88
*/
99

10-
namespace MyLib\Web;
10+
namespace MyLib\Web\Util;
1111

1212
use MyLib\Collection\SimpleCollection;
1313

1414
/**
1515
* mock 环境信息
1616
* Class Environment
17-
* @package MyLib\Web
17+
* @package MyLib\Web\Util
1818
*/
1919
class Environment extends SimpleCollection
2020
{

src/Util/RequestUtil.php

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017/6/10
6+
* Time: 下午12:26
7+
*/
8+
9+
namespace MyLib\Web\Util;
10+
11+
/**
12+
* Class RequestHelper
13+
* @package MyLib\Web\Util
14+
*/
15+
class RequestUtil
16+
{
17+
/**
18+
* 本次请求开始时间
19+
* @param bool $float
20+
* @return mixed
21+
*/
22+
public static function time($float = true)
23+
{
24+
if ((bool)$float) {
25+
return $_SERVER['REQUEST_TIME_FLOAT'];
26+
}
27+
28+
return $_SERVER['REQUEST_TIME'];
29+
}
30+
31+
/**
32+
* Get a value from $_POST / $_GET
33+
* if unavailable, take a default value
34+
* @param string $key Value key
35+
* @param mixed $default (optional)
36+
* @return mixed Value
37+
*/
38+
public static function param($key, $default = null)
39+
{
40+
if (!$key || !\is_string($key)) {
41+
return false;
42+
}
43+
44+
$ret = $_POST[$key] ?? $_GET[$key] ?? $default;
45+
46+
if (\is_string($ret)) {
47+
return stripslashes(urldecode(preg_replace('/((\%5C0+)|(\%00+))/i', '', urlencode($ret))));
48+
}
49+
50+
return $ret;
51+
}
52+
53+
/**
54+
* @param null|string $name
55+
* @param mixed $default
56+
* @return mixed
57+
*/
58+
public static function get($name = null, $default = null)
59+
{
60+
if (null === $name) {
61+
return $_GET;
62+
}
63+
64+
return $_GET[$name] ?? $default;
65+
}
66+
67+
/**
68+
* @param string|null $name
69+
* @param mixed $default
70+
* @return mixed
71+
*/
72+
public static function post($name = null, $default = null)
73+
{
74+
$body = self::getParsedBody();
75+
76+
if (null === $name) {
77+
return $body;
78+
}
79+
80+
return $body[$name] ?? $default;
81+
}
82+
83+
/**
84+
* @var false|array
85+
*/
86+
private static $parsedBody = false;
87+
88+
/**
89+
* @return array
90+
*/
91+
public static function getParsedBody()
92+
{
93+
if (self::$parsedBody === false) {
94+
// post data is json
95+
if (
96+
!isset($_SERVER['HTTP_CONTENT_TYPE']) || !($type = $_SERVER['HTTP_CONTENT_TYPE']) || strpos($type,
97+
'/json') <= 0
98+
) {
99+
self::$parsedBody = &$_POST;
100+
} else {
101+
self::$parsedBody = json_decode(file_get_contents('php://input'), true);
102+
}
103+
}
104+
105+
return self::$parsedBody;
106+
}
107+
108+
/**
109+
* @param $key
110+
* @return bool
111+
*/
112+
public static function hasParam($key)
113+
{
114+
if (!$key || !\is_string($key)) {
115+
return false;
116+
}
117+
118+
return isset($_POST[$key]) ? true : isset($_GET[$key]);
119+
}
120+
121+
public static function safePostVars()
122+
{
123+
if (!$_POST || !\is_array($_POST)) {
124+
$_POST = [];
125+
} else {
126+
$_POST = array_map(array(DataHelper::class, 'htmlentitiesUTF8'), $_POST);
127+
}
128+
}
129+
130+
/**
131+
* Get all values from $_POST/$_GET
132+
* @return mixed
133+
*/
134+
public static function getAll()
135+
{
136+
return $_POST + $_GET;
137+
}
138+
139+
/**
140+
* @param $data
141+
* @param $separator
142+
* @example
143+
* /status/active/id/12
144+
* =>
145+
* [
146+
* 'status' => 'active',
147+
* 'id' => '12',
148+
* ]
149+
* @return array
150+
*/
151+
public static function buildQueryParams($data, $separator = '/')
152+
{
153+
$arrData = \is_string($data) ? explode($separator, $data) : $data;
154+
$arrData = array_values(array_filter($arrData));
155+
$newArr = [];
156+
$count = \count($arrData); #统计
157+
158+
// $arrData 中的 奇数位--变为键,偶数位---变为前一个奇数 键的值 array('前一个奇数'=>'偶数位')
159+
for ($i = 0; $i < $count; $i += 2) {
160+
$newArr[$arrData[$i]] = $arrData[$i + 1] ?? '';
161+
}
162+
163+
unset($arrData);
164+
165+
return $newArr;
166+
}
167+
168+
/**
169+
* @param $name
170+
* @param string $default
171+
* @return mixed
172+
*/
173+
public static function serverParam($name, $default = '')
174+
{
175+
return self::server($name, $default);
176+
}
177+
178+
/**
179+
* get $_SERVER value
180+
* @param string $name
181+
* @param string $default
182+
* @return mixed
183+
*/
184+
public static function server($name, $default = '')
185+
{
186+
$name = strtoupper($name);
187+
188+
return isset($_SERVER[$name]) ? trim($_SERVER[$name]) : $default;
189+
}
190+
}

src/Util/SwaggerJSON.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
* Time: 14:32
77
*/
88

9-
namespace App\Components;
9+
namespace MyLib\Web\Util;
1010

1111
use Swagger\Annotations\Info;
1212
use Swagger\Annotations\Swagger;
1313

1414
/**
1515
* Class SwaggerJSON
16-
* @package App\Components
16+
* @package MyLib\Web\Util
1717
*/
1818
class SwaggerJSON
1919
{

0 commit comments

Comments
 (0)