Skip to content
This repository was archived by the owner on Jan 23, 2019. It is now read-only.

Commit 5466d43

Browse files
committed
some update
1 parent 21ce8bb commit 5466d43

File tree

9 files changed

+165
-21
lines changed

9 files changed

+165
-21
lines changed

src/Collections/Configuration.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace Inhere\Library\Collections;
1010

11-
use Inhere\Library\Helpers\Obj;
11+
use Inhere\Library\Helpers\Req;
1212
use Inhere\Library\Helpers\Str;
1313
use RuntimeException;
1414

@@ -56,31 +56,34 @@ final class Configuration extends Collection
5656
* @param string $locFile
5757
* @param string $baseFile
5858
* @param string $envFile
59+
* @param bool $detectByHost
5960
* @param string $format
6061
* @return Configuration
6162
*/
62-
public static function makeByEnv($locFile, $baseFile, $envFile, $format = self::FORMAT_PHP)
63+
public static function makeByEnv($locFile, $baseFile, $envFile, $detectByHost = false, $format = self::FORMAT_PHP)
6364
{
6465
$local = [
65-
'env' => 'pdt',
66+
'env' => $detectByHost ? Req::getEnvNameByHost() : 'pdt',
6667
];
6768

6869
// if local env file exists. will fetch env name from it.
6970
if (is_file($locFile) && ($localData = self::parseIni($locFile))) {
7071
$local = array_merge($local, $localData);
7172
}
7273

73-
$env = $local['env'];
74-
$envFile = str_replace('{env}', $env, $envFile);
74+
if (!is_file($baseFile)) {
75+
throw new \InvalidArgumentException("The base config file not exists. File: $baseFile");
76+
}
77+
78+
$config = self::make($baseFile, $format);
79+
$envFile = str_replace('{env}', $local['env'], $envFile);
7580

76-
if (!is_file($envFile)) {
77-
throw new \InvalidArgumentException("The env config file not exists. File: $envFile");
81+
if (is_file($envFile)) {
82+
$config->load($envFile, $format);
7883
}
7984

8085
// load config
81-
return self::make($baseFile, $format)
82-
->load($envFile, $format)
83-
->loadArray($local);
86+
return $config->loadArray($local);
8487
}
8588

8689
/**

src/DI/Container.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ public function getService($id, $thrErr = false)
472472
}
473473

474474
if ($thrErr) {
475-
throw new NotFoundException("Service id: $id was not found, has not been registered!");
475+
throw new NotFoundException("The service '$id' was not found, has not been registered!");
476476
}
477477

478478
return null;

src/Files/File.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Inhere\Exceptions\FileReadException;
1515
use Inhere\Exceptions\FileSystemException;
1616
use Inhere\Exceptions\IOException;
17-
use Inhere\Library\Helpers\StringHelper;
17+
use Inhere\Library\Helpers\Str;
1818

1919
/**
2020
* Class File
@@ -368,7 +368,7 @@ public static function margePhp($fileArr, $outFile, $deleteSpace = true)
368368
foreach ($fileArr as $v) {
369369
// 删除注释、空白
370370
if ($deleteSpace) {
371-
$data .= StringHelper::deleteStripSpace($v);
371+
$data .= Str::deleteStripSpace($v);
372372
// 不删除注释、空白
373373
} else {
374374
$o_data = file_get_contents($v);

src/Helpers/FormatHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public static function convertBytes($value)
147147

148148
$value_length = strlen($value);
149149
$qty = (int)substr($value, 0, $value_length - 1);
150-
$unit = StringHelper::strtolower(substr($value, $value_length - 1));
150+
$unit = Str::strtolower(substr($value, $value_length - 1));
151151
switch ($unit) {
152152
case 'k':
153153
$qty *= 1024;

src/Helpers/Http.php

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017-10-19
6+
* Time: 14:35
7+
*/
8+
9+
namespace Inhere\Library\Helpers;
10+
11+
use Psr\Http\Message\ResponseInterface;
12+
13+
/**
14+
* Class Http
15+
* @package Inhere\Library\Helpers
16+
*/
17+
class Http
18+
{
19+
/**
20+
* Send the response the client
21+
* @param ResponseInterface $response
22+
* @param array $options
23+
*/
24+
public static function respond(ResponseInterface $response, array $options = [])
25+
{
26+
$options = array_merge([
27+
'chunkSize' => 4096,
28+
'addContentLengthHeader' => false,
29+
], $options);
30+
31+
// Send response
32+
if (!headers_sent()) {
33+
// Status
34+
header(sprintf(
35+
'HTTP/%s %s %s',
36+
$response->getProtocolVersion(),
37+
$response->getStatusCode(),
38+
$response->getReasonPhrase()
39+
));
40+
41+
// Headers
42+
foreach ($response->getHeaders() as $name => $values) {
43+
/** @var array $values */
44+
foreach ($values as $value) {
45+
header(sprintf('%s: %s', $name, $value), false);
46+
}
47+
}
48+
}
49+
50+
// Body
51+
if (!self::isEmptyResponse($response)) {
52+
$body = $response->getBody();
53+
if ($body->isSeekable()) {
54+
$body->rewind();
55+
}
56+
57+
$chunkSize = $options['chunkSize'];
58+
$contentLength = $response->getHeaderLine('Content-Length');
59+
60+
if (!$contentLength) {
61+
$contentLength = $body->getSize();
62+
}
63+
64+
if (null !== $contentLength) {
65+
$amountToRead = $contentLength;
66+
while ($amountToRead > 0 && !$body->eof()) {
67+
$data = $body->read(min($chunkSize, $amountToRead));
68+
echo $data;
69+
$amountToRead -= strlen($data);
70+
71+
if (connection_status() !== CONNECTION_NORMAL) {
72+
break;
73+
}
74+
}
75+
} else {
76+
while (!$body->eof()) {
77+
echo $body->read($chunkSize);
78+
if (connection_status() !== CONNECTION_NORMAL) {
79+
break;
80+
}
81+
}
82+
}
83+
}
84+
}
85+
86+
/**
87+
* Helper method, which returns true if the provided response must not output a body and false
88+
* if the response could have a body.
89+
* @see https://tools.ietf.org/html/rfc7231
90+
* @param ResponseInterface $response
91+
* @return bool
92+
*/
93+
public static function isEmptyResponse(ResponseInterface $response)
94+
{
95+
if (method_exists($response, 'isEmpty')) {
96+
return $response->isEmpty();
97+
}
98+
99+
return in_array($response->getStatusCode(), [204, 205, 304], true);
100+
}
101+
102+
}

src/Helpers/Req.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
*/
1515
class Req
1616
{
17+
const DEFAULT_ENV_LIST = ['pdt', 'pre', 'test', 'dev'];
18+
1719
/**
1820
* 本次请求开始时间
1921
* @param bool $float
@@ -28,6 +30,25 @@ public static function time($float = true)
2830
return $_SERVER['REQUEST_TIME'];
2931
}
3032

33+
/**
34+
* @param null $host
35+
* @param array $envList
36+
* @param string $defaultEnv
37+
* @return string
38+
*/
39+
public static function getEnvNameByHost($host = null, $defaultEnv = 'pdt', array $envList = self::DEFAULT_ENV_LIST)
40+
{
41+
$host = $host ?: self::serverParam('HTTP_HOST', 'Unknown');
42+
43+
foreach ($envList as $value) {
44+
if (false !== strpos($host, $value)) {
45+
return $value;
46+
}
47+
}
48+
49+
return $defaultEnv;
50+
}
51+
3152
/**
3253
* Get a value from $_POST / $_GET
3354
* if unavailable, take a default value

src/Traits/MiddlewareAwareTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @from Slim 3 framework
88
*/
99

10-
namespace Inhere\Library\Utils;
10+
namespace Inhere\Library\Traits;
1111

1212
use Psr\Http\Message\ResponseInterface;
1313
use Psr\Http\Message\ServerRequestInterface;

src/Utils/LiteLogger.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ class LiteLogger implements LoggerInterface
164164
* 设为 0 则是每次记录都立即写入文件
165165
* @var int
166166
*/
167-
public $logThreshold = 1000;
167+
public $bufferSize = 1000;
168168

169169
/**
170170
* 格式
@@ -201,10 +201,6 @@ public function __construct(array $config = [])
201201
{
202202
Obj::smartConfigure($this, $config);
203203

204-
if (!$this->name) {
205-
throw new \InvalidArgumentException('Logger name is required.');
206-
}
207-
208204
$this->init();
209205
}
210206

@@ -319,6 +315,10 @@ public function log($level, $message, array $context = [], array $extra = [])
319315
return;
320316
}
321317

318+
if (!$this->name) {
319+
throw new \InvalidArgumentException('Logger name is required.');
320+
}
321+
322322
$levelName = self::getLevelName($level);
323323
$record = array(
324324
'message' => trim($message),
@@ -340,7 +340,7 @@ public function log($level, $message, array $context = [], array $extra = [])
340340
$this->recordSize++;
341341

342342
// 检查阀值
343-
if ($this->recordSize > $this->logThreshold) {
343+
if ($this->recordSize > $this->bufferSize) {
344344
$this->flush();
345345
}
346346
}

src/exceptions.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Inhere\Exceptions;
44

5+
use Psr\Http\Message\ServerRequestInterface;
6+
57
/**
68
* {@inheritDoc}
79
*/
@@ -92,6 +94,22 @@ class HttpQueryStringException extends HttpException
9294
{
9395
}
9496

97+
class InvalidMethodException extends \InvalidArgumentException
98+
{
99+
protected $request;
100+
101+
public function __construct(ServerRequestInterface $request, $method)
102+
{
103+
$this->request = $request;
104+
parent::__construct(sprintf('Unsupported HTTP method "%s" provided', $method));
105+
}
106+
107+
public function getRequest()
108+
{
109+
return $this->request;
110+
}
111+
}
112+
95113
//////////////////////////////////// Custom exception ////////////////////////////////////
96114

97115
class UserPromptException extends BaseException

0 commit comments

Comments
 (0)