Skip to content

Commit c432559

Browse files
authored
Merge pull request #3 from SDPM-lab/fixHeaders
Fix headers
2 parents 1064836 + ca6705a commit c432559

8 files changed

Lines changed: 99 additions & 61 deletions

File tree

src/Ci4FileBridge.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
<?php
22
namespace SDPMlab\Ci4Roadrunner;
33

4-
use Spiral\Debug;
54
use Laminas\Diactoros\UploadedFile;
65

76
class Ci4FileBridge
87
{
98
private $_rFiles;
10-
private $dumper;
119

1210
public function __construct(array $rFiles)
1311
{
14-
$this->dumper = new Debug\Dumper();
1512
$this->_rFiles = $rFiles;
1613
$_FILES = [];
14+
}
15+
16+
public function setFile(){
1717
$this->handleFile();
1818
}
1919

src/Ci4RequestBridge.php

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,31 @@
11
<?php
22
namespace SDPMlab\Ci4Roadrunner;
33

4-
use Spiral\Debug;
54
use Laminas\Diactoros\ServerRequest;
6-
use CodeIgniter\HTTP\IncomingRequest;
7-
use CodeIgniter\HTTP\UserAgent;
8-
use CodeIgniter\HTTP\URI;
95
use SDPMlab\Ci4Roadrunner\Ci4UriBridge;
106
use SDPMlab\Ci4Roadrunner\Ci4FileBridge;
117

128
class Ci4RequestBridge
139
{
1410
private $_rRequest;
15-
private $_cRequest;
16-
private $dumper;
1711

1812
public function __construct(ServerRequest $rRequest)
1913
{
2014
$this->_rRequest = $rRequest;
21-
$this->dumper = new Debug\Dumper();
2215
$this->setFile();
23-
$body = $this->getBody();
2416
$_SERVER['HTTP_USER_AGENT'] = $this->_rRequest->getHeaderLine("User-Agent");
25-
$this->_cRequest = new IncomingRequest(
26-
new \Config\App(),
27-
new URI(),
28-
$body,
29-
new UserAgent()
30-
);
31-
$this->_cRequest->uri = $this->getBridgeURI($this->_cRequest->uri);
17+
\CodeIgniter\Config\Services::request(new \Config\App(),false);
18+
\CodeIgniter\Config\Services::request()->getUserAgent()->parse($_SERVER['HTTP_USER_AGENT']);
19+
$this->setUri();
20+
\CodeIgniter\Config\Services::request()->setBody($this->getBody());
3221
$this->setParams();
3322
$this->setHeader();
3423
}
3524

3625
private function setFile(){
3726
if(count($this->_rRequest->getUploadedFiles()) > 0){
3827
$fileBridge = new Ci4FileBridge($this->_rRequest->getUploadedFiles());
28+
$fileBridge->setFile();
3929
}
4030
}
4131

@@ -57,37 +47,38 @@ private function getBody(){
5747
}
5848

5949
private function setParams(){
60-
$this->_cRequest->setGlobal("get",$this->_rRequest->getQueryParams());
50+
\CodeIgniter\Config\Services::request()->setMethod($this->_rRequest->getMethod());
51+
\CodeIgniter\Config\Services::request()->setGlobal("get",$this->_rRequest->getQueryParams());
6152
if($this->_rRequest->getMethod() == "POST"){
62-
$this->_cRequest->setGlobal("post",$this->_rRequest->getParsedBody());
53+
\CodeIgniter\Config\Services::request()->setGlobal("post",$this->_rRequest->getParsedBody());
6354
}
6455
$_COOKIE = [];
65-
$this->_cRequest->setGlobal("cookie",$this->_rRequest->getCookieParams());
56+
\CodeIgniter\Config\Services::request()->setGlobal("cookie",$this->_rRequest->getCookieParams());
6657
foreach ($this->_rRequest->getCookieParams() as $key => $value) {
6758
$_COOKIE[$key] = $value;
6859
}
6960
if(isset($_COOKIE[config(App::class)->sessionCookieName])){
7061
session_id($_COOKIE[config(App::class)->sessionCookieName]);
7162
}
72-
$this->_cRequest->setGlobal("server",$this->_rRequest->getServerParams());
63+
\CodeIgniter\Config\Services::request()->setGlobal("server",$this->_rRequest->getServerParams());
7364
}
7465

7566
private function setHeader(){
7667
$rHeader = $this->_rRequest->getHeaders();
7768
foreach ($rHeader as $key => $datas) {
7869
foreach ($datas as $values) {
79-
$this->_cRequest->setHeader($key,$values);
70+
\CodeIgniter\Config\Services::request()->setHeader($key,$values);
8071
}
8172
}
8273
}
8374

84-
private function getBridgeURI(URI $cURI){
85-
$uriBridge = new Ci4UriBridge($this->_rRequest->getUri(),$cURI);
86-
return $uriBridge->getURI();
75+
private function setUri(){
76+
$uriBridge = new Ci4UriBridge($this->_rRequest->getUri());
77+
$uriBridge->setUri();
8778
}
8879

8980
public function getRequest(){
90-
return $this->_cRequest;
81+
return \CodeIgniter\Config\Services::request();
9182
}
9283

9384
}

src/Ci4ResponseBridge.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22
namespace SDPMlab\Ci4Roadrunner;
33

4-
use Spiral\Debug;
54
use Laminas\Diactoros\Response;
65
use Laminas\Diactoros\Stream;
76
use Laminas\Diactoros\Response\InjectContentTypeTrait;

src/Ci4UriBridge.php

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
11
<?php
22
namespace SDPMlab\Ci4Roadrunner;
33

4-
use CodeIgniter\HTTP\URI as CURI;
5-
use Laminas\Diactoros\Uri as RURI;
6-
74
class Ci4UriBridge
85
{
9-
private $_cURI;
106
private $_rURI;
117

12-
public function __construct(RURI $rURI,CURI $cURI)
8+
public function __construct(\Laminas\Diactoros\Uri $rURI)
139
{
14-
$this->_cURI = $cURI;
10+
\CodeIgniter\Config\Services::uri(null,false);
1511
$this->_rURI = $rURI;
16-
$this->transferAll();
1712
}
1813

19-
private function transferAll(){
14+
public function setUri(){
2015
$this->transferPath();
2116
$this->transferQuery();
2217
}
@@ -25,7 +20,7 @@ private function transferPath(){
2520
$rPath = $this->_rURI->getPath();
2621

2722
if($rPath == "/"){
28-
$this->_cURI->setPath($rPath);
23+
\CodeIgniter\Config\Services::uri()->setPath($rPath);
2924
return;
3025
}
3126

@@ -39,15 +34,12 @@ private function transferPath(){
3934
array_values($pathArr);
4035
}
4136
$path = "/".implode("/",$pathArr);
42-
$this->_cURI->setPath($path);
43-
}
37+
\CodeIgniter\Config\Services::uri()->setPath($path);
4438

45-
private function transferQuery(){
46-
$this->_cURI->setQuery($this->_rURI->getQuery());
4739
}
4840

49-
public function getURI(){
50-
return $this->_cURI;
41+
private function transferQuery(){
42+
\CodeIgniter\Config\Services::uri()->setQuery($this->_rURI->getQuery());
5143
}
5244

5345
}

src/Commands/file/psr-worker.php

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,33 @@
66
ini_set('display_errors', 'stderr');
77
require 'vendor/autoload.php';
88

9-
use CodeIgniter\CodeIgniter;
10-
use Spiral\Debug;
119
use Spiral\Goridge;
1210
use Spiral\RoadRunner;
1311
use SDPMlab\Ci4Roadrunner\Ci4ResponseBridge;
1412
use SDPMlab\Ci4Roadrunner\Ci4RequestBridge;
1513
use SDPMlab\Ci4Roadrunner\Debug\Exceptions;
1614
use SDPMlab\Ci4Roadrunner\Debug\Toolbar;
15+
use SDPMlab\Ci4Roadrunner\Debug\Dumper;
16+
1717
// codeigniter4 public/index.php
1818
$minPHPVersion = '7.2';
1919
if (phpversion() < $minPHPVersion)
2020
{
2121
die("Your PHP version must be {$minPHPVersion} or higher to run CodeIgniter. Current version: " . phpversion());
2222
}
2323
unset($minPHPVersion);
24-
//強制使codeigniter 認為這是一般請求
25-
function is_cli(){
24+
25+
/**
26+
* Is CLI?
27+
*
28+
* Test to see if a request was made from the command line.
29+
*
30+
* @return boolean
31+
*/
32+
function is_cli(): bool{
2633
return false;
2734
}
35+
2836
define('FCPATH', __DIR__ . DIRECTORY_SEPARATOR);
2937
$pathsPath = FCPATH . 'app/Config/Paths.php';
3038
chdir(__DIR__);
@@ -35,8 +43,17 @@ function is_cli(){
3543
//worker setting
3644
$worker = new RoadRunner\Worker(new Goridge\StreamRelay(STDIN, STDOUT));
3745
$psr7 = new RoadRunner\PSR7Client($worker);
38-
$dumper = new Debug\Dumper();
39-
$dumper->setRenderer(Debug\Dumper::ERROR_LOG, new Debug\Renderer\ConsoleRenderer());
46+
47+
/**
48+
* Dump given value into target output.
49+
*
50+
* @param mixed $value Variable
51+
* @param string $target Possible options: OUTPUT, RETURN, ERROR_LOG, LOGGER.
52+
* @return string|null
53+
*/
54+
function dump($value,string $target = "ERROR_LOG") : ?string{
55+
return Dumper::getInstance()->dump($value,$target);
56+
}
4057

4158
$count = 0;
4259
while ($req = $psr7->acceptRequest()) {
@@ -49,11 +66,10 @@ function is_cli(){
4966
try {
5067
$requestBridge = new Ci4RequestBridge($req);
5168
$ci4Req = $requestBridge->getRequest();
52-
$app->setRequest($ci4Req);
5369
} catch (
5470
\Throwable $e
5571
){
56-
$dumper->dump((string)$e, Debug\Dumper::ERROR_LOG);
72+
dump((string)$e);
5773
$psr7->getWorker()->error((string)$e);
5874
}
5975

@@ -68,13 +84,13 @@ function is_cli(){
6884
}
6985
}
7086
} catch (\Throwable $e){
71-
$dumper->dump((string)$e, Debug\Dumper::ERROR_LOG);
87+
dump((string)$e);
7288
$psr7->getWorker()->error((string)$e);
7389
}
7490

7591
//執行框架邏輯與錯誤處理
7692
try{
77-
$ci4Response = $app->run();
93+
$ci4Response = $app->setRequest($ci4Req)->run();
7894
}catch(
7995
\Throwable $e
8096
){
@@ -95,7 +111,7 @@ function is_cli(){
95111
} catch (
96112
\Throwable $e
97113
){
98-
$dumper->dump((string)$e, Debug\Dumper::ERROR_LOG);
114+
dump((string)$e);
99115
$psr7->getWorker()->error((string)$e);
100116
}
101117
}

src/Commands/initLibrary.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ public function run(array $params)
1919
}else{
2020
$command = ";./vendor/bin/rr get";
2121
}
22-
print("cd ".ROOTPATH.$command);
2322
$init = popen("cd ".ROOTPATH.$command, 'r');
2423
pclose($init);
2524
CLI::write(

src/Debug/Dumper.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
namespace SDPMlab\Ci4Roadrunner\Debug;
3+
4+
use Spiral\Debug;
5+
6+
class Dumper {
7+
8+
private $_directives = [
9+
"OUTPUT" => 0,
10+
"RETURN" => 1,
11+
"LOGGER" => 2,
12+
"ERROR_LOG" => 3,
13+
"OUTPUT_CLI" => 4,
14+
"OUTPUT_CLI_COLORS" => 5,
15+
];
16+
private static $_instance = null;
17+
private $_dumper;
18+
19+
private function __construct()
20+
{
21+
$this->_dumper = new Debug\Dumper();
22+
$this->_dumper->setRenderer(Debug\Dumper::ERROR_LOG, new Debug\Renderer\ConsoleRenderer());
23+
}
24+
25+
public static function getInstance()
26+
{
27+
if (!(self::$_instance instanceof Dumper)) {
28+
self::$_instance = new Dumper();
29+
}
30+
return self::$_instance;
31+
}
32+
33+
/**
34+
* Dump given value into target output.
35+
*
36+
* @param mixed $value
37+
* @param string $target Possible options: OUTPUT, RETURN, ERROR_LOG, LOGGER.
38+
* @return string|null
39+
* @throws DumperException
40+
*/
41+
public function dump($value,string $target = "ERROR_LOG") : ?string {
42+
return $this->_dumper->dump($value, $this->_directives[$target]);
43+
}
44+
45+
}
46+
47+
?>

src/Debug/Exceptions.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,11 @@
33

44
use CodeIgniter\API\ResponseTrait;
55
use CodeIgniter\Exceptions\PageNotFoundException;
6-
use CodeIgniter\HTTP\IncomingRequest;
7-
use CodeIgniter\HTTP\Response;
86
use Config\Paths;
97
use function error_reporting;
108
use ErrorException;
119
use Throwable;
12-
use Spiral\Debug;
1310
use SDPMlab\Ci4Roadrunner\Ci4ResponseBridge;
14-
use SDPMlab\Ci4Roadrunner\Ci4RequestBridge;
15-
use Spiral\RoadRunner;
1611

1712
/**
1813
* Exceptions manager
@@ -224,7 +219,6 @@ protected function render(Throwable $exception, int $statusCode)
224219
{
225220
ob_end_clean();
226221
}
227-
$this->request->getUserAgent()->parse($_SERVER['HTTP_USER_AGENT']);
228222
ob_start();
229223
include($path . $view);
230224
$buffer = ob_get_contents();

0 commit comments

Comments
 (0)