Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,33 @@ jobs:
name: Run tests / Symfony 5^0
command: |
rm -Rf var/*
composer require seregazhuk/php-watcher:^0.5.2 --no-update
composer require drift/event-bus-bundle:^0.1 --no-update
composer update -n --prefer-dist
php vendor/bin/phpunit
php vendor/bin/phpunit --testsuite=all --exclude-group=without-filesystem-message

test-php74-with-filesystem:
docker:
- image: circleci/php:7.4-cli

working_directory: ~/project
steps:
- checkout

- run:
name: Install PHP dependencies
command: |
sudo docker-php-ext-install pcntl

- run:
name: Run tests / Symfony 5^0
command: |
rm -Rf var/*
composer require react/filesystem --no-update
composer require seregazhuk/php-watcher:^0.5.2 --no-update
composer require drift/event-bus-bundle:^0.1 --no-update
composer update -n --prefer-dist
php vendor/bin/phpunit --testsuite=all --exclude-group=with-filesystem-message

test-php80:
docker:
Expand All @@ -37,12 +62,13 @@ jobs:
name: Run tests / Symfony 5^0
command: |
rm -Rf var/*
composer update -n --prefer-dist --ignore-platform-req=php
php vendor/bin/phpunit
composer update -n --prefer-dist
php vendor/bin/phpunit --testsuite=no-watcher --exclude-group=without-filesystem-message

workflows:
version: 2
test:
jobs:
- test-php74
- test-php74-with-filesystem
- test-php80
1 change: 1 addition & 0 deletions .phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
C:37:"PHPUnit\Runner\DefaultTestResultCache":1489:{a:2:{s:7:"defects";a:2:{s:65:"Drift\Server\Tests\ApplicationStaticFolderTest::testFilesNotFound";i:3;s:49:"Drift\Server\Tests\UploadingFileTest::testRegular";i:3;}s:5:"times";a:18:{s:47:"Drift\Server\Tests\ApplicationTest::testRegular";d:1.017;s:51:"Drift\Server\Tests\ApplicationTest::testDefaultHost";d:1.034;s:49:"Drift\Server\Tests\ApplicationTest::testEmptyHost";d:1.018;s:52:"Drift\Server\Tests\ApplicationTest::testSilentServer";d:1.026;s:53:"Drift\Server\Tests\ApplicationTest::testRouteNotFound";d:1.016;s:47:"Drift\Server\Tests\ApplicationTest::testNonAnsi";d:1.033;s:65:"Drift\Server\Tests\ApplicationTest::testAnotherPSR7Implementation";d:1.019;s:52:"Drift\Server\Tests\ApplicationTest::testServerValues";d:1.014;s:56:"Drift\Server\Tests\ApplicationTest::testBasicAuthHeaders";d:1.02;s:54:"Drift\Server\Tests\ApplicationTest::testSymfonyAdapter";d:1.024;s:59:"Drift\Server\Tests\ApplicationStaticFolderTest::testRegular";d:0.83;s:72:"Drift\Server\Tests\ApplicationStaticFolderTest::testDisabledStaticFolder";d:0.813;s:70:"Drift\Server\Tests\ApplicationStaticFolderTest::testCustomStaticFolder";d:0.829;s:69:"Drift\Server\Tests\ApplicationStaticFolderTest::testStaticFolderAlias";d:0.834;s:65:"Drift\Server\Tests\ApplicationStaticFolderTest::testFilesNotFound";d:0.32;s:49:"Drift\Server\Tests\UploadingFileTest::testRegular";d:0.677;s:51:"Drift\Server\Tests\UploadingFileTest::testEmptyFile";d:0.322;s:60:"Drift\Server\Tests\UploadingFileTest::testDisableFileUploads";d:0.325;}}}
24 changes: 16 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ designed for the watcher feature.
interface KernelAdapter extends ObservableKernel
{
/**
* @param LoopInterface $loop
* @param string $rootPath
* @param ServerContext $serverContext
* @param FilesystemInterface $filesystem
* @param OutputPrinter $outputPrinter
* @param MimeTypeChecker $mimeTypeChecker
* @param LoopInterface $loop
* @param string $rootPath
* @param ServerContext $serverContext
* @param OutputPrinter $outputPrinter
* @param MimeTypeChecker $mimeTypeChecker
* @param FilesystemInterface|null $filesystem
*
* @return PromiseInterface<self>
*
Expand All @@ -95,9 +95,9 @@ interface KernelAdapter extends ObservableKernel
LoopInterface $loop,
string $rootPath,
ServerContext $serverContext,
FilesystemInterface $filesystem,
OutputPrinter $outputPrinter,
MimeTypeChecker $mimeTypeChecker
MimeTypeChecker $mimeTypeChecker,
?FilesystemInterface $filesystem
): PromiseInterface;

/**
Expand Down Expand Up @@ -192,6 +192,14 @@ In this example, a file named `app.js` located under `/internal/public/path/`
folder will be accessible at `http://localhost:8000/public/app.js`. By default,
this feature is disabled.

### Important

By default, this package will not install the `react/filesystem` package. This
means that, if you don't install it by hand in your project, all the disk
operations will be blocking. These operations done synchronously will be much
faster and efficient, but by using large size files could slow down the entire
process.

## Symfony bridge

In order to help you from migrating an application from Symfony to DriftPHP,
Expand Down
11 changes: 5 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,24 @@
"react/event-loop": "^1.0",
"react/socket": "^1.0",
"react/promise": "^2.7",
"react/filesystem": "dev-master as 0.1.3",
"clue/zlib-react": "^0.2.2",
"drift/console-bridge": "0.1.*",
"drift/event-loop-utils": "0.1.*"
},
"require-dev": {
"phpunit/phpunit": "7.5.17",
"seregazhuk/php-watcher": "^0.5.2",
"drift/event-bus-bundle": "0.1.*, >=0.1.2",
"phpunit/phpunit": "^9",
"symfony/framework-bundle": "^5.0",
"symfony/process": "^5.0",
"symfony/config": "^5.0",
"drift/http-kernel": "0.1.*, >=0.1.13",
"drift/http-kernel": "0.1.*, >=0.1.15",
"laminas/laminas-diactoros": "^2",
"vimeo/psalm": "^4",
"ext-fileinfo": "*"
},
"suggest": {
"seregazhuk/php-watcher": "^0.5.2"
"react/filesystem": "*",
"drift/event-bus-bundle": "*",
"seregazhuk/php-watcher": "*"
},
"autoload": {
"psr-4": {
Expand Down
8 changes: 7 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@
bootstrap="vendor/autoload.php"
>
<testsuites>
<testsuite name="Test Suite">
<testsuite name="all">
<directory>./tests</directory>
</testsuite>

<testsuite name="no-watcher">
<directory>./tests</directory>
<exclude>./tests/WatcherTest.php</exclude>
<exclude>./tests/ExchangesTest.php</exclude>
</testsuite>
</testsuites>

</phpunit>
31 changes: 29 additions & 2 deletions src/Adapter/DriftKernel/DriftKernelAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

namespace Drift\Server\Adapter\DriftKernel;

use Drift\EventBus\Subscriber\EventBusSubscriber;
use Drift\HttpKernel\AsyncKernel;
use Drift\Kernel as ApplicationKernel;
use Drift\Server\Adapter\SymfonyKernelBasedAdapter;
Expand Down Expand Up @@ -44,15 +45,41 @@ protected function checkKernel($kernel)
* @param string $environment
* @param bool $debug
*
* @return AsyncKernel
* @return Kernel
*/
protected static function createKernelByEnvironmentAndDebug(
string $environment,
bool $debug
): AsyncKernel {
): Kernel {
return new ApplicationKernel($environment, $debug);
}

/**
* @return PromiseInterface
*/
protected function preload(): PromiseInterface
{
return $this
->kernel
->preload()
->then(function () {
$container = $this->kernel->getContainer();
$serverContext = $this->serverContext;

if (
class_exists(EventBusSubscriber::class) &&
$serverContext->hasExchanges() &&
$container->has(EventBusSubscriber::class)
) {
$eventBusSubscriber = $container->get(EventBusSubscriber::class);
$eventBusSubscriber->subscribeToExchanges(
$serverContext->getExchanges(),
$this->outputPrinter
);
}
});
}

/**
* @param Kernel|AsyncKernel $kernel
* @param Request $request
Expand Down
16 changes: 8 additions & 8 deletions src/Adapter/KernelAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@
interface KernelAdapter extends ObservableKernel
{
/**
* @param LoopInterface $loop
* @param string $rootPath
* @param ServerContext $serverContext
* @param FilesystemInterface $filesystem
* @param OutputPrinter $outputPrinter
* @param MimeTypeChecker $mimeTypeChecker
* @param LoopInterface $loop
* @param string $rootPath
* @param ServerContext $serverContext
* @param OutputPrinter $outputPrinter
* @param MimeTypeChecker $mimeTypeChecker
* @param FilesystemInterface|null $filesystem
*
* @return PromiseInterface<self>
*
Expand All @@ -47,9 +47,9 @@ public static function create(
LoopInterface $loop,
string $rootPath,
ServerContext $serverContext,
FilesystemInterface $filesystem,
OutputPrinter $outputPrinter,
MimeTypeChecker $mimeTypeChecker
MimeTypeChecker $mimeTypeChecker,
?FilesystemInterface $filesystem
): PromiseInterface;

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Adapter/SymfonyKernel/SymfonyKernelAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ protected function checkKernel($kernel)
* @param string $environment
* @param bool $debug
*
* @return AsyncKernel
* @return Kernel
*/
protected static function createKernelByEnvironmentAndDebug(
string $environment,
bool $debug
): AsyncKernel {
): Kernel {
return new ApplicationKernel($environment, $debug);
}

Expand Down
81 changes: 41 additions & 40 deletions src/Adapter/SymfonyKernelBasedAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
namespace Drift\Server\Adapter;

use Drift\Console\OutputPrinter;
use Drift\EventBus\Subscriber\EventBusSubscriber;
use Drift\HttpKernel\AsyncKernel;
use Drift\Server\Context\ServerContext;
use Drift\Server\Exception\KernelException;
use Drift\Server\Exception\RouteNotFoundException;
Expand Down Expand Up @@ -45,21 +43,22 @@
abstract class SymfonyKernelBasedAdapter implements KernelAdapter
{
protected Kernel $kernel;
private FilesystemInterface $filesystem;
private ServerContext $serverContext;
private MimeTypeChecker $mimeTypeChecker;
private string $rootPath;
protected ?FilesystemInterface $filesystem;
protected ServerContext $serverContext;
protected MimeTypeChecker $mimeTypeChecker;
protected OutputPrinter $outputPrinter;
protected string $rootPath;

/**
* @param string $environment
* @param bool $debug
*
* @return AsyncKernel
* @return Kernel
*/
abstract protected static function createKernelByEnvironmentAndDebug(
string $environment,
bool $debug
): AsyncKernel;
): Kernel;

/**
* @param $kernel
Expand All @@ -80,12 +79,12 @@ abstract protected function kernelHandle(
): PromiseInterface;

/**
* @param LoopInterface $loop
* @param string $rootPath
* @param ServerContext $serverContext
* @param FilesystemInterface $filesystem
* @param OutputPrinter $outputPrinter
* @param MimeTypeChecker $mimeTypeChecker
* @param LoopInterface $loop
* @param string $rootPath
* @param ServerContext $serverContext
* @param OutputPrinter $outputPrinter
* @param MimeTypeChecker $mimeTypeChecker
* @param FilesystemInterface|null $filesystem
*
* @return PromiseInterface<self>
*
Expand All @@ -95,9 +94,9 @@ public static function create(
LoopInterface $loop,
string $rootPath,
ServerContext $serverContext,
FilesystemInterface $filesystem,
OutputPrinter $outputPrinter,
MimeTypeChecker $mimeTypeChecker
MimeTypeChecker $mimeTypeChecker,
?FilesystemInterface $filesystem
): PromiseInterface {
$adapter = new static();
$kernel = static::createKernelByEnvironmentAndDebug($serverContext->getEnvironment(), $serverContext->isDebug());
Expand All @@ -111,30 +110,24 @@ public static function create(
$adapter->serverContext = $serverContext;
$adapter->filesystem = $filesystem;
$adapter->mimeTypeChecker = $mimeTypeChecker;
$adapter->outputPrinter = $outputPrinter;
$adapter->rootPath = $rootPath;

return $kernel
return $adapter
->preload()
->then(function () use ($adapter, $outputPrinter) {
$container = $adapter->kernel->getContainer();
$serverContext = $adapter->serverContext;

if (
$serverContext->hasExchanges() &&
$container->has(EventBusSubscriber::class)
) {
$eventBusSubscriber = $container->get(EventBusSubscriber::class);
$eventBusSubscriber->subscribeToExchanges(
$serverContext->getExchanges(),
$outputPrinter
);
}
})
->then(function () use ($adapter) {
return $adapter;
});
}

/**
* @return PromiseInterface
*/
protected function preload(): PromiseInterface
{
return resolve();
}

/**
* @param ServerRequestInterface $request
*
Expand Down Expand Up @@ -292,10 +285,14 @@ private function toSymfonyUploadedFile(PsrUploadedFile $file): PromiseInterface
}

$promise = (UPLOAD_ERR_OK == $file->getError())
? $this
->filesystem
->file($tmpFilename)
->putContents($content)
? (
is_null($this->filesystem)
? resolve(file_put_contents($tmpFilename, $content))
: $this
->filesystem
->file($tmpFilename)
->putContents($content)
)
: resolve();

return $promise
Expand All @@ -318,10 +315,14 @@ private function toSymfonyUploadedFile(PsrUploadedFile $file): PromiseInterface
private function cleanTemporaryUploadedFiles(Request $request): array
{
return array_map(function (SymfonyUploadedFile $file) {
return $this
->filesystem
->file($file->getPath().'/'.$file->getFilename())
->remove();
$filePath = $file->getPath().'/'.$file->getFilename();

return (is_null($this->filesystem))
? resolve(unlink($filePath))
: $this
->filesystem
->file($filePath)
->remove();
}, $request->files->all());
}

Expand Down
Loading