Skip to content

Commit

Permalink
Merge pull request #21 from ipuppet/dev
Browse files Browse the repository at this point in the history
修复Reason配置获取的错误
  • Loading branch information
ipuppet committed May 11, 2021
2 parents 59a46bf + badf5b2 commit 01be60c
Showing 1 changed file with 27 additions and 15 deletions.
42 changes: 27 additions & 15 deletions src/Component/Router/Reason/Reason.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ abstract class Reason implements ReasonInterface
/**
* @var string
*/
protected $content;
protected ?string $content;

private ?LoggerInterface $logger;

/**
* Reason constructor.
Expand All @@ -24,24 +26,34 @@ abstract class Reason implements ReasonInterface
*/
public function __construct(Config $config = null, LoggerInterface $logger = null)
{
if ($logger) $this->logger = $logger;
if ($config !== null) {
$errorResponse = $config->get('errorResponse', []);
$httpStatus = $this->getHttpStatus();
$content = array_key_exists($httpStatus, $errorResponse) ? $errorResponse[$httpStatus] : false;
if ($content && $content[0] === '@') {
$content = str_replace('@', $config->get('rootPath'), $content);
if (file_exists($content)) {
$this->content = file_get_contents($content);
} else {
$message = '您在response配置文件中设定的文件不存在,请检查。';
if ($logger !== null) {
$logger->warning($message);
} else {
throw new Exception($message);
}
$content = $config->get($httpStatus, false);
if ($content) {
switch ($content[0]) {
case '@': // 项目路径
$content = str_replace('@', $config->get('rootPath'), $content);
$this->setContent($this->getFileContent($content));
break;
default:
$this->setContent($content);
}
}
}
}

private function getFileContent($path)
{
if (file_exists($path)) {
return file_get_contents($path);
} else {
$message = "您在response配置文件中设定的文件 [{$path}] 不存在,请检查。";
if ($this->logger) {
$this->logger->warning($message);
return false;
} else {
$this->content = $content;
throw new Exception($message);
}
}
}
Expand Down

0 comments on commit 01be60c

Please sign in to comment.