Skip to content

Commit

Permalink
some bug fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Feb 8, 2018
1 parent a64eba1 commit da18372
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 16 deletions.
8 changes: 4 additions & 4 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="./tests/boot.php"
bootstrap="test/boot.php"
colors="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
Expand All @@ -12,13 +12,13 @@
>
<testsuites>
<testsuite name="Web Lib Test Suite">
<directory>./tests/</directory>
<directory>test</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory suffix=".php">./examples</directory>
<directory suffix=".php">src</directory>
</whitelist>
</filter>
</phpunit>
</phpunit>
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
* Time: 14:22
*/

namespace MyLib\Web\Helper;
namespace MyLib\Web\Traits;

/**
* Class RespondUtilTrait
* @package MyLib\Web\Helper
* @package MyLib\Web\Traits
*
* ```php
* class Respond extends ResponseCode {
Expand Down
4 changes: 2 additions & 2 deletions src/Traits/SimpleAssetsLoaderTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
* File: AssetsRendererTrait.php
*/

namespace MyLib\Web;
namespace MyLib\Web\Traits;

/**
* Class AssetsRendererTrait
* @package MyLib\Web
* @package MyLib\Web\Traits
*/
trait SimpleAssetsLoaderTrait
{
Expand Down
6 changes: 4 additions & 2 deletions src/Traits/ViewRendererAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
* Time: 9:12
*/

namespace MyLib\Web;
namespace MyLib\Web\Traits;

use MyLib\Web\ViewRenderer;

/**
* Trait ViewRendererAwareTrait
* @package MyLib\Web
* @package MyLib\Web\Traits
*/
trait ViewRendererAwareTrait
{
Expand Down
4 changes: 2 additions & 2 deletions src/Util/APIAccessChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
* Time: 22:39
*/

namespace MyLib\Web;
namespace MyLib\Web\Util;

/**
* Class APIAccessChecker
* @package MyLib\Web
* @package MyLib\Web\Util
*/
class APIAccessChecker
{
Expand Down
4 changes: 2 additions & 2 deletions src/Util/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
* File: Environment.php
*/

namespace MyLib\Web;
namespace MyLib\Web\Util;

use MyLib\Collection\SimpleCollection;

/**
* mock 环境信息
* Class Environment
* @package MyLib\Web
* @package MyLib\Web\Util
*/
class Environment extends SimpleCollection
{
Expand Down
190 changes: 190 additions & 0 deletions src/Util/RequestUtil.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
<?php
/**
* Created by PhpStorm.
* User: inhere
* Date: 2017/6/10
* Time: 下午12:26
*/

namespace MyLib\Web\Util;

/**
* Class RequestHelper
* @package MyLib\Web\Util
*/
class RequestUtil
{
/**
* 本次请求开始时间
* @param bool $float
* @return mixed
*/
public static function time($float = true)
{
if ((bool)$float) {
return $_SERVER['REQUEST_TIME_FLOAT'];
}

return $_SERVER['REQUEST_TIME'];
}

/**
* Get a value from $_POST / $_GET
* if unavailable, take a default value
* @param string $key Value key
* @param mixed $default (optional)
* @return mixed Value
*/
public static function param($key, $default = null)
{
if (!$key || !\is_string($key)) {
return false;
}

$ret = $_POST[$key] ?? $_GET[$key] ?? $default;

if (\is_string($ret)) {
return stripslashes(urldecode(preg_replace('/((\%5C0+)|(\%00+))/i', '', urlencode($ret))));
}

return $ret;
}

/**
* @param null|string $name
* @param mixed $default
* @return mixed
*/
public static function get($name = null, $default = null)
{
if (null === $name) {
return $_GET;
}

return $_GET[$name] ?? $default;
}

/**
* @param string|null $name
* @param mixed $default
* @return mixed
*/
public static function post($name = null, $default = null)
{
$body = self::getParsedBody();

if (null === $name) {
return $body;
}

return $body[$name] ?? $default;
}

/**
* @var false|array
*/
private static $parsedBody = false;

/**
* @return array
*/
public static function getParsedBody()
{
if (self::$parsedBody === false) {
// post data is json
if (
!isset($_SERVER['HTTP_CONTENT_TYPE']) || !($type = $_SERVER['HTTP_CONTENT_TYPE']) || strpos($type,
'/json') <= 0
) {
self::$parsedBody = &$_POST;
} else {
self::$parsedBody = json_decode(file_get_contents('php://input'), true);
}
}

return self::$parsedBody;
}

/**
* @param $key
* @return bool
*/
public static function hasParam($key)
{
if (!$key || !\is_string($key)) {
return false;
}

return isset($_POST[$key]) ? true : isset($_GET[$key]);
}

public static function safePostVars()
{
if (!$_POST || !\is_array($_POST)) {
$_POST = [];
} else {
$_POST = array_map(array(DataHelper::class, 'htmlentitiesUTF8'), $_POST);
}
}

/**
* Get all values from $_POST/$_GET
* @return mixed
*/
public static function getAll()
{
return $_POST + $_GET;
}

/**
* @param $data
* @param $separator
* @example
* /status/active/id/12
* =>
* [
* 'status' => 'active',
* 'id' => '12',
* ]
* @return array
*/
public static function buildQueryParams($data, $separator = '/')
{
$arrData = \is_string($data) ? explode($separator, $data) : $data;
$arrData = array_values(array_filter($arrData));
$newArr = [];
$count = \count($arrData); #统计

// $arrData 中的 奇数位--变为键,偶数位---变为前一个奇数 键的值 array('前一个奇数'=>'偶数位')
for ($i = 0; $i < $count; $i += 2) {
$newArr[$arrData[$i]] = $arrData[$i + 1] ?? '';
}

unset($arrData);

return $newArr;
}

/**
* @param $name
* @param string $default
* @return mixed
*/
public static function serverParam($name, $default = '')
{
return self::server($name, $default);
}

/**
* get $_SERVER value
* @param string $name
* @param string $default
* @return mixed
*/
public static function server($name, $default = '')
{
$name = strtoupper($name);

return isset($_SERVER[$name]) ? trim($_SERVER[$name]) : $default;
}
}
4 changes: 2 additions & 2 deletions src/Util/SwaggerJSON.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
* Time: 14:32
*/

namespace App\Components;
namespace MyLib\Web\Util;

use Swagger\Annotations\Info;
use Swagger\Annotations\Swagger;

/**
* Class SwaggerJSON
* @package App\Components
* @package MyLib\Web\Util
*/
class SwaggerJSON
{
Expand Down

0 comments on commit da18372

Please sign in to comment.