Skip to content

Commit 8b64f41

Browse files
authored
Merge pull request #9 from SDPM-lab/dev
v1.0.0-beta.2
2 parents c432559 + 79208c5 commit 8b64f41

107 files changed

Lines changed: 6893 additions & 106 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/test-phpunit.yml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
name: PHPUnit
2+
3+
on:
4+
push:
5+
branches:
6+
- dev
7+
paths:
8+
- 'src/**'
9+
- 'test/**'
10+
- composer.json
11+
- '**.php'
12+
- .github/workflows/test-phpunit.yml
13+
pull_request:
14+
branches:
15+
- dev
16+
paths:
17+
- 'src/**'
18+
- 'test/**'
19+
- composer.json
20+
- '**.php'
21+
- .github/workflows/test-phpunit.yml
22+
23+
jobs:
24+
25+
tests:
26+
runs-on: ubuntu-18.04
27+
if: "!contains(github.event.head_commit.message, '[ci skip]')"
28+
name: PHP ${{ matrix.php-ver }}
29+
30+
strategy:
31+
fail-fast: false
32+
matrix:
33+
php-ver: ['7.3','7.4']
34+
35+
steps:
36+
- name: Checkout
37+
uses: actions/checkout@v2
38+
39+
- name: Setup PHP, with composer and extensions
40+
run: |
41+
sudo add-apt-repository ppa:ondrej/php -y
42+
sudo apt update -y
43+
sudo apt-get install php${{ matrix.php-ver }}
44+
sudo apt install php-pear php${{ matrix.php-ver }}-curl php${{ matrix.php-ver }}-dev php${{ matrix.php-ver }}-mbstring php${{ matrix.php-ver }}-zip php${{ matrix.php-ver }}-mysql php${{ matrix.php-ver }}-xml php${{ matrix.php-ver }}-fpm php${{ matrix.php-ver }}-intl -y
45+
sudo apt-get update -y
46+
sudo apt-get install -y php-xdebug
47+
sudo curl -s https://getcomposer.org/installer | php
48+
sudo mv composer.phar /usr/local/bin/composer
49+
50+
- name: Install dependencies
51+
working-directory: ./test
52+
run: |
53+
composer update
54+
env:
55+
COMPOSER_AUTH: ${{ secrets.COMPOSER_AUTH }}
56+
57+
- name: Init roadrunner server
58+
working-directory: ./test
59+
run: |
60+
sudo ./vendor/bin/rr get
61+
cp ../src/Commands/file/psr-worker.php psr-worker.php
62+
sudo ./rr serve -v -d &
63+
64+
- name: Test with PHPUnit
65+
working-directory: ./test
66+
run: script -e -c "vendor/bin/phpunit -v"
67+
env:
68+
TERM: xterm-256color

README.md

Lines changed: 222 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,24 @@
66

77
[![Latest Stable Version](https://poser.pugx.org/sdpmlab/codeigniter4-roadrunner/v)](//packagist.org/packages/sdpmlab/codeigniter4-roadrunner) [![Total Downloads](https://poser.pugx.org/sdpmlab/codeigniter4-roadrunner/downloads)](//packagist.org/packages/sdpmlab/codeigniter4-roadrunner) [![Latest Unstable Version](https://poser.pugx.org/sdpmlab/codeigniter4-roadrunner/v/unstable)](//packagist.org/packages/sdpmlab/codeigniter4-roadrunner) [![License](https://poser.pugx.org/sdpmlab/codeigniter4-roadrunner/license)](//packagist.org/packages/sdpmlab/codeigniter4-roadrunner)
88

9-
Make Codeigniter4 work on Roadrunner Server.
9+
Codeigniter4-RoadRunner provides the synchroniztion of the Request and Response object between Roadrunner-Worker and Codeigniter4. Since Codeigniter4 doesn't implement [PSR-7 standard](https://codeigniter.tw/user_guide/intro/psr.html) completely, you need to use this library to allow your Codeigniter4 project to run using RoadRunner Server.
1010

1111
> This library is currently under development, and its functions are not yet stable. Do not use it in production environment.
1212
13+
[正體中文說明書](https://github.com/SDPM-lab/Codeigniter4-Roadrunner/blob/dev/README_zh-TW.md)
14+
1315
## Install
1416

1517
### Prerequisites
1618
1. CodeIgniter Framework 4.*
1719
2. Composer
20+
3. Enable `php-curl` extension
21+
4. Enable `php-zip` extension
1822

1923
### Composer Install
2024
Use "Composer" to download the library and its dependencies to the project
2125
```
22-
composer require sdpmlab/codeigniter4-roadrunner "v1.0.0-beta.1"
26+
composer require sdpmlab/codeigniter4-roadrunner "v1.0.0-beta.2"
2327
```
2428
Initialize Roadrunner and files using built-in commands in the library
2529

@@ -31,7 +35,7 @@ php spark ciroad:init
3135
Run the command in the root directory of your project:
3236
1. Use Codeigniter4 spark command
3337
```
34-
php spark ciroad:start
38+
php spark ciroad:start -v -d
3539
```
3640
2. Use Roadrunner command in Windows
3741
```
@@ -44,7 +48,7 @@ Run the command in the root directory of your project:
4448

4549
## Server Settings
4650
The server settings are all in the project root directory ".rr.yaml". The default file will look like this:
47-
```
51+
```yaml
4852
http:
4953
address: 0.0.0.0:8080
5054
workers:
@@ -58,4 +62,217 @@ static:
5862
dir: "public"
5963
forbid: [".php", ".htaccess"]
6064
```
61-
You can create your configuration file according to the [Roadrunner document](https://roadrunner.dev/docs).
65+
You can create your configuration file according to the [Roadrunner document](https://roadrunner.dev/docs/intro-config).
66+
67+
## Development Suggestions
68+
69+
### Automatic reload
70+
71+
In the default circumstance of RoadRunner, you must restart the server everytime after you revised any PHP files so that your revision will effective.
72+
It seems not that friendly during development.
73+
74+
You can revise your `.rr.yaml` configuration file, add the settings below and start the development mode with `-v -d`.
75+
RoadRunner Server will detect if the PHP files were revised or not, automatically, and reload the Worker instantly.
76+
77+
```yaml
78+
# reload can reset rr servers when files change
79+
reload:
80+
#refresh interval (default 1s)
81+
interval: 1s
82+
#file extensions to watch, defaults to [.php]
83+
patterns: [".php"]
84+
```
85+
86+
The `reload` function is very resource-intensive, please do not activate the option in the formal environment.
87+
88+
### Using Codeigniter4 Request and Response object
89+
90+
Codeigniter4 does not implement the complete [HTTP message interface](https://www.php-fig.org/psr/psr-7/), hence this library focuses on the synchronize of `PSR-7 interface` and `Codeigniter4 HTTP interface`.
91+
92+
Base on the reasons above, You should use `$this->request`, provided by Codeigniter4, or the global function `/Config/Services::('request')` to fetch the correct request object; Use `$this->response` or `/Config/Services::('response')` to fetch the correct response object.
93+
94+
Please be noticed, while constructing response for the users during developing, you should prevent using PHP built-in methods to conduct `header` or `set-cookies` settings. Using the `setHeader()` and `setCookie()`, provided by the [Codeigniter4 Response Object](https://codeigniter.com/user_guide/outgoing/response.html), to conduct setting.
95+
96+
### Use return to stop controller logic
97+
98+
Inside the Controller, try using return to stop the controller logic. No matter the response of view or API, reduce the `echo` output usage can avoid lets of errors, just like ths:
99+
100+
```php
101+
<?php namespace App\Controllers;
102+
103+
use CodeIgniter\API\ResponseTrait;
104+
105+
class Home extends BaseController
106+
{
107+
use ResponseTrait;
108+
109+
public function index()
110+
{
111+
//Don't use :
112+
//echo view('welcome_message');
113+
return view('welcome_message');
114+
}
115+
116+
/**
117+
* send header
118+
*/
119+
public function sendHeader()
120+
{
121+
$this->response->setHeader("X-Set-Auth-Token", uniqid());
122+
return $this->respond(["status"=>true]);
123+
}
124+
125+
}
126+
```
127+
128+
### Use the built-in Session library
129+
130+
We only focus on supporting the Codeigniter4 built-in [Session library](https://codeigniter.com/user_guide/libraries/sessions.html), and do not guarantee if using `session_start()` and `$_SEEEION` can work as normal. So, you should avoid using the PHP built-in Session method, change to the Codeigniter4 framework built-in library.
131+
132+
### Developing and debugging in a environment with only one Worker
133+
134+
Since the RoadRunner has fundamentally difference with other server software(i.e. Nginx, Apache), every Codeigniter4 will persist inside RAMs as the form of Worker, HTTP requests will reuse these Workers to process. Hence, we have better develop and test stability under the circumstance with only one Worker to prove it can also work properly under serveral Workers in the formal environment.
135+
136+
You can reference the `.rr.yaml` settings below to lower the amount of Worker to the minimum:
137+
138+
```yaml
139+
http:
140+
address: 0.0.0.0:8080
141+
workers:
142+
command: "php psr-worker.php"
143+
pool:
144+
numWorkers: 1
145+
maxJobs: 1
146+
```
147+
148+
# Global Methods
149+
150+
We offer some Global methods to help you develop your projects more smoothly.
151+
152+
### Dealing with the file uploading
153+
154+
Since the RoadRunner Worker can not transfer the correct `$_FILES` context, the Codeigniter4 file upload class will not be able to work properly. To solve this, we offered a file upload class corresponding the PSR-7 standard for you to deal with file uploading correctly within RoadRunner. Even if you switched your project to another server environment(i.e. spark serve, Apache, Nginx), this class can still work properly, and doesn't need any code modification.
155+
156+
You can fetch the uploaded files by means of `SDPMlab\Ci4Roadrunner\UploadedFileBridge::getPsr7UploadedFiles()` in the controller (or any other places). This method will return an array, consist of Uploaded File objects. The available methods of this object is identical as the regulation of [PSR-7 Uploaded File Interface](https://www.php-fig.org/psr/psr-7/#36-psrhttpmessageuploadedfileinterface).
157+
158+
```php
159+
<?php namespace App\Controllers;
160+
161+
use CodeIgniter\API\ResponseTrait;
162+
use SDPMlab\Ci4Roadrunner\UploadedFileBridge;
163+
164+
class FileUploadTest extends BaseController
165+
{
166+
use ResponseTrait;
167+
168+
protected $format = "json";
169+
/**
170+
* form-data
171+
*/
172+
public function fileUpload(){
173+
$files = UploadedFileBridge::getPsr7UploadedFiles();
174+
$data = [];
175+
foreach ($files as $file) {
176+
$fileEx = array_pop(
177+
explode('.', $file->getClientFilename())
178+
);
179+
$newFileName = uniqid(rand()).".".$fileEx;
180+
$newFilePath = WRITEPATH.'uploads'.DIRECTORY_SEPARATOR.$newFileName;
181+
$file->moveTo($newFilePath);
182+
$data[$file->getClientFilename()] = md5_file($newFilePath);
183+
}
184+
return $this->respondCreated($data);
185+
}
186+
187+
/**
188+
* form-data multiple upload
189+
*/
190+
public function fileMultipleUpload(){
191+
$files = UploadedFileBridge::getPsr7UploadedFiles()["data"];
192+
$data = [];
193+
foreach ($files as $file) {
194+
$fileEx = array_pop(
195+
explode('.', $file->getClientFilename())
196+
);
197+
$newFileName = uniqid(rand()).".".$fileEx;
198+
$newFilePath = WRITEPATH.'uploads'.DIRECTORY_SEPARATOR.$newFileName;
199+
$file->moveTo($newFilePath);
200+
$data[$file->getClientFilename()] = md5_file($newFilePath);
201+
}
202+
return $this->respondCreated($data);
203+
}
204+
```
205+
206+
### Dealing with thrown errors
207+
208+
If you encountered some variables or object content that needed to be confirmed in `-v -d` development mode, you can use the global function `dump()` to throw errors onto the terminal no matter where the program is.
209+
210+
```php
211+
/**
212+
* Dump given value into target output.
213+
*
214+
* @param mixed $value Variable
215+
* @param string $target Possible options: OUTPUT, RETURN, ERROR_LOG, LOGGER.
216+
* @return string|null
217+
*/
218+
function dump($value,string $target = "ERROR_LOG") : ?string;
219+
```
220+
221+
## Avaliable commands
222+
223+
### ciroad:init
224+
225+
Initiallize RoadRunner and its needed files.
226+
227+
* Use
228+
```
229+
$ php spark ciroad:init
230+
```
231+
232+
### ciroad:start
233+
234+
Start RoadRunner Server
235+
236+
* Use
237+
```
238+
$ php spark ciroad:start [Options]
239+
```
240+
241+
* Options:
242+
```
243+
-d During debugging mode, HTTP requests details will be listed on the terminal
244+
-b run in the background
245+
-v output details
246+
```
247+
248+
### ciroad:stop
249+
250+
Kill the RoadRunner running in the background.
251+
252+
* Use
253+
```
254+
$ php spark ciroad:stop
255+
```
256+
257+
### ciroad:reset
258+
259+
Force reload all the HTTP Workers.
260+
261+
* Use
262+
```
263+
$ php spark ciroad:reset
264+
```
265+
266+
### ciroad:status
267+
268+
Check the current Worker operating status
269+
270+
* Use
271+
```
272+
$ php spark ciroad:status [Options]
273+
```
274+
275+
* Options:
276+
```
277+
-i output status continuously per second
278+
```

0 commit comments

Comments
 (0)