Skip to content

Commit

Permalink
Merge pull request #12 from bearsunday/php8.1_support
Browse files Browse the repository at this point in the history
PHP 8.2  Support
  • Loading branch information
koriym authored Dec 29, 2022
2 parents abcda9e + b284a4d commit 4e1a610
Show file tree
Hide file tree
Showing 17 changed files with 817 additions and 685 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/coding-standards.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.0
php-version: 8.1
extensions: swoole
tools: cs2pr
coverage: none
Expand All @@ -35,8 +35,5 @@ jobs:
- name: Install dependencies
run: composer install --no-interaction --no-progress --prefer-dist

- name: Validate composer.json
run: composer validate --strict

- name: Run PHP_CodeSniffer
run: ./vendor/bin/phpcs -q --no-colors --report=checkstyle src tests | cs2pr
5 changes: 2 additions & 3 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ on:
push:
pull_request:
workflow_dispatch:
schedule:
- cron: '01 08 * * 4'

jobs:
phpunit:
Expand All @@ -19,8 +17,9 @@ jobs:
- '7.3'
- '7.4'
- '8.0'
- '8.1'
- '8.2'
dependencies:
- lowest
- highest
steps:
- name: Checkout
Expand Down
14 changes: 7 additions & 7 deletions .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.0
php-version: 8.1
extensions: swoole
tools: cs2pr
coverage: none
Expand Down Expand Up @@ -48,7 +48,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 7.4
php-version: 8.1
extensions: swoole

- name: Get composer cache directory
Expand All @@ -66,7 +66,7 @@ jobs:
run: composer install --no-interaction --no-progress --prefer-dist

- name: Run PHP Mess Detector
run: ./vendor/bin/phpmd src text --exclude src/Annotation ./phpmd.xml
run: ./vendor/bin/phpmd src text --exclude src/SwooleServerRequest.php ./phpmd.xml

static-analysis-php-metrics:
name: Static Analysis with PhpMetrics
Expand All @@ -78,7 +78,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.0
php-version: 8.1
extensions: swoole
coverage: none

Expand All @@ -102,7 +102,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 7.4
php-version: 8.1
extensions: swoole
coverage: none

Expand All @@ -113,7 +113,7 @@ jobs:
- name: Install dependencies
run: |
composer install --no-interaction --no-progress --prefer-dist
composer require --dev maglnet/composer-require-checker ^3.0
composer global require --dev maglnet/composer-require-checker ^3.0
- name: Run composer-require-checker
run: ./vendor/bin/composer-require-checker
run: ~/.composer/vendor/bin/composer-require-checker
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2018-2021 Akihito Koriyama
Copyright (c) 2018-2022 Akihito Koriyama

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
13 changes: 9 additions & 4 deletions bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

declare(strict_types=1);

use BEAR\Package\AppInjector;
use BEAR\Resource\ResourceObject;
use BEAR\Swoole\App;
use BEAR\Swoole\SuperGlobals;
use BEAR\Swoole\SwooleModule;
use Ray\Di\Injector;
use Swoole\Http\Request;
use Swoole\Http\Response;
use Swoole\Http\Server;
use BEAR\AppMeta\Meta;
use BEAR\Package\Module;

if (! class_exists(Server::class)) {
throw new \RuntimeException('Swoole is not installed. See https://github.com/swoole/swoole-src/wiki/Installing');
Expand All @@ -28,10 +30,13 @@
$http->set($settings);
$http->on('start', function () use ($ip, $port) {
echo "Swoole http server is started at http://{$ip}:{$port}" . PHP_EOL;

return 1;
});
$injector = new AppInjector($name, $context);
/* @var App $app */
$app = $injector->getOverrideInstance(new SwooleModule, App::class);
$appModule = (new Module)(new Meta($name, $context), $context);
$appModule->override(new SwooleModule());
$injector = new Injector(new SwooleModule($appModule));
$app = $injector->getInstance(App::class);
$superGlobals = new SuperGlobals;
$http->on('request', function (Request $request, Response $response) use ($app, $superGlobals) {
if ($app->httpCache->isNotModified($request->header)) {
Expand Down
23 changes: 17 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,24 @@
"ext-json": "*",
"ext-swoole": ">=4.0.0",
"bear/app-meta": "^1.6",
"bear/query-repository": "^1.6",
"bear/query-repository": "^1.8.4",
"bear/resource": "^1.15",
"bear/sunday": "^1.5",
"doctrine/annotations": "^1.11",
"doctrine/cache": "^1.10",
"fastd/http": "^3.2",
"fastd/http": "5.0.x-dev",
"psr/cache": "^1.0 || ^2.0 || ^3.0",
"psr/http-message": "^1.0",
"ray/di": "^2.11.3",
"ray/psr7-module": "^1.3"
"ray/psr-cache-module": "^1.3",
"ray/psr7-module": "^1.3",
"symfony/cache": "^5.4 || ^6.2"
},
"require-dev": {
"bamarni/composer-bin-plugin": "^1.4",
"bear/package": "^1.9",
"bear/skeleton": "^1.6",
"eaglewu/swoole-ide-helper": "dev-master",
"guzzlehttp/guzzle": "^6.3",
"guzzlehttp/guzzle": "^6.5 || ^7.5",
"josegonzalez/dotenv": "^3.2",
"phpunit/phpunit": "^9.5",
"symfony/process": "^5.0"
Expand Down Expand Up @@ -61,6 +63,15 @@
"build": ["@cs", "@sa", "@pcov", "@metrics"]
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"bamarni/composer-bin-plugin": true
}
},
"extra": {
"bamarni-bin": {
"bin-links": true,
"forward-command": true
}
}
}
12 changes: 6 additions & 6 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
namespace BEAR\Swoole;

use BEAR\Resource\ResourceInterface;
use BEAR\Sunday\Extension\Application\AbstractApp;
use BEAR\Resource\TransferInterface;
use BEAR\Sunday\Extension\Router\RouterInterface;

final class App extends AbstractApp
final class App
{
/**
* @var HttpCache
* @var HttpCacheInterface
*/
public $httpCache;

Expand All @@ -21,7 +21,7 @@ final class App extends AbstractApp
public $router;

/**
* @var Responder
* @var TransferInterface
*/
public $responder;

Expand All @@ -36,9 +36,9 @@ final class App extends AbstractApp
public $error;

public function __construct(
HttpCache $httpCache,
HttpCacheInterface $httpCache,
RouterInterface $router,
Responder $responder,
TransferInterface $responder,
ResourceInterface $resource,
Error $error
) {
Expand Down
36 changes: 6 additions & 30 deletions src/CacheProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,19 @@

namespace BEAR\Swoole;

use BEAR\AppMeta\AbstractAppMeta;
use Doctrine\Common\Cache\ArrayCache;
use Doctrine\Common\Cache\ChainCache;
use Doctrine\Common\Cache\FilesystemCache;
use Ray\Di\Di\Named;
use Ray\Di\ProviderInterface;
use Symfony\Component\Cache\Adapter\ArrayAdapter;

/**
* @implements ProviderInterface<ArrayAdapter>
*/
class CacheProvider implements ProviderInterface
{
/**
* @var string
*/
private $namespace;

/**
* @var string
*/
private $cacheDir;

/**
* @Named("namespace=cache_namespace")
*/
#[Named('namespace=cache_namespace')]
public function __construct(AbstractAppMeta $appMeta, string $namespace)
{
$this->cacheDir = $appMeta->tmpDir . '/cache';
$this->namespace = $namespace;
}

/**
* {@inheritdoc}
*/
public function get()
public function get(): ArrayAdapter
{
$cache = new ChainCache([new ArrayCache, new FilesystemCache($this->cacheDir)]);
$cache->setNamespace($this->namespace);

return $cache;
return new ArrayAdapter(0, false);
}
}
4 changes: 2 additions & 2 deletions src/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use BEAR\Resource\Exception\BadRequestException as BadRequest;
use BEAR\Resource\Exception\ResourceNotFoundException as NotFound;
use BEAR\Resource\Exception\ServerErrorException as ServerError;
use phpDocumentor\Reflection\DocBlock\Tags\Link;
use function array_key_exists;
use function get_class;
use function json_encode;
use const JSON_PRETTY_PRINT;
Expand Down Expand Up @@ -57,6 +57,6 @@ private function isCodeExists(\Exception $e) : bool
return false;
}

return \array_key_exists($e->getCode(), (new Code)->statusText);
return array_key_exists($e->getCode(), (new Code)->statusText);
}
}
3 changes: 1 addition & 2 deletions src/HttpCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
namespace BEAR\Swoole;

use BEAR\QueryRepository\ResourceStorageInterface;
use BEAR\Sunday\Extension\Transfer\HttpCacheInterface;
use Swoole\Http\Response;

final class HttpCache
final class HttpCache implements HttpCacheInterface
{
/**
* @var ResourceStorageInterface
Expand Down
17 changes: 17 additions & 0 deletions src/HttpCacheInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace BEAR\Swoole;

use Swoole\Http\Response;

interface HttpCacheInterface
{
/**
* @param array<string, string> $server
*/
public function isNotModified(array $server) : bool;

public function transfer(Response $response): void;
}
24 changes: 13 additions & 11 deletions src/SwooleModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,27 @@

namespace BEAR\Swoole;

use Doctrine\Common\Annotations\CachedReader;
use Doctrine\Common\Annotations\Reader;
use Doctrine\Common\Cache\Cache;
use BEAR\RepositoryModule\Annotation\EtagPool;
use BEAR\Resource\TransferInterface;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;
use Ray\Di\AbstractModule;
use Ray\Di\Scope;
use Ray\HttpMessage\RequestProviderInterface;
use Ray\PsrCacheModule\Annotation\Shared;
use Symfony\Component\Cache\Adapter\ArrayAdapter;

class SwooleModule extends AbstractModule
{
protected function configure()
{
$this->bind()->annotatedWith('cache_namespace')->toInstance('');
$this->bind(CacheItemPoolInterface::class)->annotatedWith(Shared::class)->toProvider(CacheProvider::class)->in(Scope::SINGLETON);
$this->bind(CacheItemPoolInterface::class)->annotatedWith(EtagPool::class)->toProvider(CacheProvider::class)->in(Scope::SINGLETON);
$this->bind(CacheItemInterface::class)->toProvider(CacheProvider::class);
$this->bind(HttpCacheInterface::class)->to(HttpCache::class);
$this->bind(RequestProviderInterface::class)->to(SwooleRequestProvider::class);
// generic cache for user
$this->bind(Cache::class)->toProvider(CacheProvider::class)->in(Scope::SINGLETON);
// prod annotation reader
$this->bind(Cache::class)->annotatedWith('annotation_cache')->toProvider(CacheProvider::class)->in(Scope::SINGLETON);
$this->bind(Reader::class)->toConstructor(
CachedReader::class,
'reader=annotation_reader'
);
$this->bind(TransferInterface::class)->to(Responder::class);
$this->bind(Error::class);
}
}
1 change: 0 additions & 1 deletion src/SwooleRequestProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace BEAR\Swoole;

use FastD\Http\SwooleServerRequest;
use Psr\Http\Message\ServerRequestInterface;
use Ray\HttpMessage\RequestProviderInterface;

Expand Down
Loading

0 comments on commit 4e1a610

Please sign in to comment.