Skip to content

Commit ca6705a

Browse files
committed
Add dumper class
1 parent a4fcd9a commit ca6705a

8 files changed

Lines changed: 83 additions & 40 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: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
<?php
22
namespace SDPMlab\Ci4Roadrunner;
33

4-
use Spiral\Debug;
54
use Laminas\Diactoros\ServerRequest;
65
use SDPMlab\Ci4Roadrunner\Ci4UriBridge;
76
use SDPMlab\Ci4Roadrunner\Ci4FileBridge;
87

98
class Ci4RequestBridge
109
{
1110
private $_rRequest;
12-
private $dumper;
1311

1412
public function __construct(ServerRequest $rRequest)
1513
{
1614
$this->_rRequest = $rRequest;
17-
$this->dumper = new Debug\Dumper();
1815
$this->setFile();
1916
$_SERVER['HTTP_USER_AGENT'] = $this->_rRequest->getHeaderLine("User-Agent");
2017
\CodeIgniter\Config\Services::request(new \Config\App(),false);
21-
$this->setURI();
18+
\CodeIgniter\Config\Services::request()->getUserAgent()->parse($_SERVER['HTTP_USER_AGENT']);
19+
$this->setUri();
2220
\CodeIgniter\Config\Services::request()->setBody($this->getBody());
2321
$this->setParams();
2422
$this->setHeader();
@@ -27,6 +25,7 @@ public function __construct(ServerRequest $rRequest)
2725
private function setFile(){
2826
if(count($this->_rRequest->getUploadedFiles()) > 0){
2927
$fileBridge = new Ci4FileBridge($this->_rRequest->getUploadedFiles());
28+
$fileBridge->setFile();
3029
}
3130
}
3231

@@ -73,9 +72,9 @@ private function setHeader(){
7372
}
7473
}
7574

76-
private function setURI(){
75+
private function setUri(){
7776
$uriBridge = new Ci4UriBridge($this->_rRequest->getUri());
78-
return $uriBridge->getURI();
77+
$uriBridge->setUri();
7978
}
8079

8180
public function getRequest(){

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: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,17 @@
11
<?php
22
namespace SDPMlab\Ci4Roadrunner;
33

4-
use Laminas\Diactoros\Uri as RURI;
5-
use Spiral\Debug;
6-
7-
84
class Ci4UriBridge
95
{
106
private $_rURI;
11-
private $dumper;
127

13-
public function __construct(RURI $rURI)
8+
public function __construct(\Laminas\Diactoros\Uri $rURI)
149
{
1510
\CodeIgniter\Config\Services::uri(null,false);
1611
$this->_rURI = $rURI;
17-
$this->dumper = new Debug\Dumper();
18-
$this->transferAll();
19-
2012
}
2113

22-
private function transferAll(){
14+
public function setUri(){
2315
$this->transferPath();
2416
$this->transferQuery();
2517
}
@@ -50,10 +42,6 @@ private function transferQuery(){
5042
\CodeIgniter\Config\Services::uri()->setQuery($this->_rURI->getQuery());
5143
}
5244

53-
public function getURI(){
54-
return \CodeIgniter\Config\Services::uri();
55-
}
56-
5745
}
5846

5947
?>

src/Commands/file/psr-worker.php

Lines changed: 26 additions & 9 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()) {
@@ -52,7 +69,7 @@ function is_cli(){
5269
} catch (
5370
\Throwable $e
5471
){
55-
$dumper->dump((string)$e, Debug\Dumper::ERROR_LOG);
72+
dump((string)$e);
5673
$psr7->getWorker()->error((string)$e);
5774
}
5875

@@ -67,7 +84,7 @@ function is_cli(){
6784
}
6885
}
6986
} catch (\Throwable $e){
70-
$dumper->dump((string)$e, Debug\Dumper::ERROR_LOG);
87+
dump((string)$e);
7188
$psr7->getWorker()->error((string)$e);
7289
}
7390

@@ -94,7 +111,7 @@ function is_cli(){
94111
} catch (
95112
\Throwable $e
96113
){
97-
$dumper->dump((string)$e, Debug\Dumper::ERROR_LOG);
114+
dump((string)$e);
98115
$psr7->getWorker()->error((string)$e);
99116
}
100117
}

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)