Skip to content

Commit f56548e

Browse files
authored
Merge pull request #2 from apisearch-io/feature/static-content
Adding static files server
2 parents adf5c61 + 8a7b50d commit f56548e

16 files changed

+689
-31
lines changed

.circleci/config.yml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
version: 2
2+
jobs:
3+
build:
4+
docker:
5+
- image: circleci/php:7.1-cli
6+
7+
working_directory: ~/project
8+
steps:
9+
- checkout
10+
- run:
11+
name: Run tests
12+
command: |
13+
composer install -n --prefer-dist --no-suggest
14+
php vendor/bin/phpunit

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
vendor
22
.php_cs.cache
3-
composer.lock
3+
composer.lock
4+
var

README.md

+79-13
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
# Symfony ReactPHP Server
22

3+
[![CircleCI](https://circleci.com/gh/apisearch-io/symfony-react-server.svg?style=svg)](https://circleci.com/gh/apisearch-io/symfony-react-server)
4+
[![Join the Slack](https://img.shields.io/badge/join%20us-on%20slack-blue.svg)](https://apisearch.slack.com)
5+
36
This package provides an async server for Symfony kernel based on ReactPHP
47
packages and Promise implementation. The server is distributed with all the
58
Symfony kernel adapters, and can be easily extended for new Kernel
69
modifications.
710

8-
> At the moment, this server cannot serve static files. This feature will be
9-
> added soon.
10-
1111
You can take a look at the
1212
[Symfony + ReactPHP Series](https://medium.com/@apisearch/symfony-and-reactphp-series-82082167f6fb)
1313
in order to understand a little bit better the rationale behind using this
1414
server and Promises in your domain.
1515

16+
> This server is mainly designed to work as a non-blocking symfony server. Used
17+
> as a regular one should be part of an architecture, containing, at least, a
18+
> balancer and several instanced in several ports. Otherwise you may have
19+
> performance issues.
20+
1621
## Installation
1722

1823
In order to use this server, you only have to add the requirement in composer.
@@ -33,7 +38,7 @@ This is a PHP file. This means that the way of starting this server is by, just,
3338
executing it.
3439

3540
```php
36-
php vendor/bin/server
41+
php vendor/bin/server 0.0.0.0:8100
3742
```
3843

3944
You will find that the server starts with a default configuration. You can
@@ -43,22 +48,83 @@ configure how the server starts and what adapters use.
4348
instance in order to start serving Requests. By default, `symfony4`. Can be
4449
overridden with option `--adapter` and the value must be a valid class
4550
namespace of an instance of `KernelAdapter`
46-
51+
52+
```bash
53+
php vendor/bin/server 0.0.0.0:8100 --adapter=symfony4
54+
php vendor/bin/server 0.0.0.0:8100 --adapter=My\Own\Adapter
55+
```
56+
4757
- Bootstrap: How the application is bootstrapped. This would be usually a simple
4858
autoload require, but sometimes, like in symfony, can be some extra actions
4959
before the Kernel is instanced. By default, `symfony4`. Available options are
5060
`Symfony4` and `autoload`. Can be overridden with the option `--bootstrap` and
5161
the value must be a valid path of a file, starting from the project root.
52-
62+
63+
```bash
64+
php vendor/bin/server 0.0.0.0:8100 --bootstrap=symfony4
65+
php vendor/bin/server 0.0.0.0:8100 --bootstrap=autoload
66+
php vendor/bin/server 0.0.0.0:8100 --bootstrap=config/myfile.php
67+
```
68+
5369
- Environment: Kernel environment. By default `prod`, but turns `dev` if the
5470
option `--dev` is found.
55-
71+
72+
```bash
73+
php vendor/bin/server 0.0.0.0:8100 --dev
74+
```
75+
5676
- Debug: Kernel will start with this option is enabled. By default false,
57-
enabled if the option `--debug` is found.
58-
77+
enabled if the option `--debug` is found. Makes sense on development
78+
environment, but is not exclusive.
79+
80+
```bash
81+
php vendor/bin/server 0.0.0.0:8100 --dev --debug
82+
```
83+
5984
- Silent: No information nor any kind of report will be printed in the standard
6085
output. By default disabled, but can be enabled with `--silent`.
61-
62-
- Non Blocking: This option enabled async kernel. If this option is not found,
63-
the server will use the standard `handle` kernel method. Otherwise, will
64-
use the `handleAsync` method, working in that case, as a non-blocking server.
86+
87+
```bash
88+
php vendor/bin/server 0.0.0.0:8100 --silent
89+
```
90+
91+
92+
## Turning the server non-blocking
93+
94+
By default, the server will work as a blocking server with the Symfony HTTP
95+
Kernel. The server will use the method `handle` to properly serve requests. If
96+
your application starts working with the asynchronous kernel, you must ensure
97+
your kernel uses the AsyncKernel implementation.
98+
99+
> Make sure you have the dependency installed in your composer.json file. You
100+
> must include the line `apisearch-io/symfony-async-http-kernel` under require
101+
> section. Then, `composer update`.
102+
103+
To turn on the asynchronous feature, just add this flag
104+
105+
106+
```bash
107+
php vendor/bin/server 0.0.0.0:8100 --non-blocking
108+
```
109+
110+
## Serving static files
111+
112+
Kernel Adapters have already defined the static folder related to the kernel.
113+
For example, Symfony4 adapter will provide static files from folder `/public`.
114+
115+
You can override the static folder with the command option `--static-folder`.
116+
All files inside this defined folder will be served statically in a non-blocking
117+
way
118+
119+
```bash
120+
php vendor/bin/server 0.0.0.0:8100 --static-folder=public
121+
```
122+
123+
You can disable static folder with the option `--no-static-folder`. This can be
124+
useful when working with the adapter value and want to disable the default
125+
value, for example, for an API.
126+
127+
128+
```bash
129+
php vendor/bin/server 0.0.0.0:8100 --no-static-folder
130+
```

bin/server

+8-2
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,11 @@ use Apisearch\SymfonyReactServer\Adapter\Symfony4KernelAdapter;
6969
* Server.
7070
*/
7171
$arguments = buildServerArguments($argv);
72+
$rootPath = getcwd();
7273
$environment = array_key_exists('--dev', $arguments) ? 'dev' : 'prod';
7374
$silent = $arguments['--silent'] ?? false;
75+
$staticFolder = $arguments['--static-folder'] ?? '';
76+
$staticFolder = isset($arguments['--no-static-folder']) ? null : $staticFolder;
7477
$debug = $arguments['--debug'] ?? false;
7578
$nonBlocking = $arguments['--non-blocking'] ?? false;
7679
$adapter = $arguments['--adapter'] ?? 'symfony4';
@@ -84,6 +87,7 @@ $bootstrapFile = [
8487
][$bootstrap] ?? $bootstrap;
8588

8689
if (
90+
!requireIfExists(__DIR__."/../$bootstrapFile") &&
8791
!requireIfExists(__DIR__."/../../$bootstrapFile") &&
8892
!requireIfExists(__DIR__."/../../../../$bootstrapFile")
8993
) {
@@ -99,14 +103,16 @@ if (!is_a($adapter, KernelAdapter::class, true)) {
99103
}
100104

101105
$application = new \Apisearch\SymfonyReactServer\Application(
106+
$rootPath,
102107
$host,
103108
$port,
104109
$environment,
105110
$debug,
106111
$silent,
107112
$nonBlocking,
108113
$adapter,
109-
$bootstrapFile
114+
$bootstrapFile,
115+
$staticFolder
110116
);
111117

112-
$application->run();
118+
$application->run();

composer.json

+16-9
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,17 @@
1616
"react/http": "^0.8",
1717
"react/event-loop": "^1",
1818
"react/socket": "^1",
19-
"react/promise": "^2"
19+
"react/promise": "^2",
20+
"react/filesystem": "*"
21+
},
22+
"require-dev": {
23+
"phpunit/phpunit": "^7.0.0",
24+
"mmoreram/php-formatter": "^1.3.1",
25+
"friendsofphp/php-cs-fixer": "^2.5.0",
26+
"apisearch-io/symfony-async-http-kernel": "dev-master",
27+
"symfony/framework-bundle": "^4.2",
28+
"symfony/process": "^4.2",
29+
"symfony/config": "^4.2"
2030
},
2131
"suggest": {
2232
"apisearch-io/symfony-async-http-kernel": "To enable --non-blocking flag and work with Async Kernel"
@@ -26,18 +36,15 @@
2636
"Apisearch\\SymfonyReactServer\\": "src"
2737
}
2838
},
39+
"autoload-dev": {
40+
"psr-4": {
41+
"Apisearch\\SymfonyReactServer\\Tests\\": "tests"
42+
}
43+
},
2944
"bin": [
3045
"bin/server"
3146
],
3247
"scripts": {
33-
"post-install-cmd": [
34-
"OneBundleApp\\App\\ComposerHook::installConsole",
35-
"OneBundleApp\\App\\ComposerHook::installReactServer"
36-
],
37-
"post-update-cmd": [
38-
"OneBundleApp\\App\\ComposerHook::installConsole",
39-
"OneBundleApp\\App\\ComposerHook::installReactServer"
40-
],
4148
"fix-code": [
4249
"vendor/bin/php-cs-fixer fix --config=.php_cs",
4350
"vendor/bin/php-formatter f:h:f . --exclude=vendor --exclude=web --exclude=bin --exclude=var",

phpunit.xml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
convertErrorsToExceptions="false"
7+
convertNoticesToExceptions="false"
8+
convertWarningsToExceptions="false"
9+
processIsolation="false"
10+
stopOnFailure="true"
11+
bootstrap="vendor/autoload.php"
12+
>
13+
<testsuites>
14+
<testsuite name="Test Suite">
15+
<directory>./tests</directory>
16+
</testsuite>
17+
</testsuites>
18+
19+
</phpunit>

src/Adapter/KernelAdapter.php

+9
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,13 @@ public static function buildKernel(
3434
string $environment,
3535
bool $debug
3636
) : Kernel;
37+
38+
/**
39+
* Get static folder by kernel
40+
*
41+
* @param Kernel $kernel
42+
*
43+
* @return string|null
44+
*/
45+
public static function getStaticFolder(Kernel $kernel) : ? string;
3746
}

src/Adapter/Symfony4KernelAdapter.php

+12
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,16 @@ public static function buildKernel(
3838
{
3939
return new ApplicationKernel($environment, $debug);
4040
}
41+
42+
/**
43+
* Get static folder by kernel
44+
*
45+
* @param Kernel $kernel
46+
*
47+
* @return string|null
48+
*/
49+
public static function getStaticFolder(Kernel $kernel) : ? string
50+
{
51+
return '/public';
52+
}
4153
}

0 commit comments

Comments
 (0)