Эх сурвалжийг харах

[php/spiral] Add RoadRunner 2.0 support for Spiral Framework (#7118)

* Add RoadRunner 2.0 support
Add CycleORM 2.0 support

* Updates RoadRunner config

* Fix

* Update PHP version up to 8.1.2

* Fixed RR config
Pavel Buchnev 3 жил өмнө
parent
commit
72c3626bf5

+ 3 - 2
frameworks/PHP/spiral/.env

@@ -1,5 +1,6 @@
 # Debug mode disabled view cache and enabled higher verbosity.
-DEBUG = false
+DEBUG=false
+DB_DSN=mysql:host=tfb-database:3306;charset=utf8;dbname=hello_world;user=benchmarkdbuser;password=benchmarkdbpass
 
 # Set to application specific value, used to encrypt/decrypt cookies and etc.
-ENCRYPTER_KEY = def00000f6f989c4ba99b5eec3dcd4f5b0fb7e5fbbf95d3cacb6b7ed049e22b4a931db7ad59085225b36d051fb06530c8a41b83d10761439326656536293473c2472d911
+ENCRYPTER_KEY=def00000f6f989c4ba99b5eec3dcd4f5b0fb7e5fbbf95d3cacb6b7ed049e22b4a931db7ad59085225b36d051fb06530c8a41b83d10761439326656536293473c2472d911

+ 14 - 3
frameworks/PHP/spiral/.rr.yaml

@@ -1,4 +1,15 @@
+version: '2.7'
+
+rpc:
+  listen: tcp://127.0.0.1:6001
+
+server:
+  command: "php app.php"
+  relay: pipes
+
 http:
-  address: :8080
-  workers:
-    command: "php app.php"
+  address: 0.0.0.0:8080
+
+logs:
+  mode: production
+  level: error

+ 4 - 4
frameworks/PHP/spiral/README.md

@@ -8,9 +8,9 @@ Benchmark code is located in `app/src/Controllers/BenchmarkController.php`.
 The tests were run with:
 * [Spiral Framework Version 2](https://github.com/spiral/framework/)
 * [Spiral/Stempler](https://github.com/spiral/stempler) as template engine
-* [Cycle ORM 1.1.*](https://github.com/cycle/orm)
-* [RoadRunner 1.4.*](https://roadrunner.dev/)
-* [PHP Version 7.4.*](http://www.php.net/) in CLI mode with OPCache
+* [Cycle ORM 2.*](https://github.com/cycle/orm)
+* [RoadRunner 2.*](https://roadrunner.dev/)
+* [PHP Version 8.0.*](http://www.php.net/) in CLI mode with OPCache
 
 ## Test URLs
 Test                | URL 
@@ -20,4 +20,4 @@ Data-Store/Database | http://localhost:8080/db
 Variable Query      | http://localhost:8080/db/:queries
 Templating and ORM  | http://localhost:8080/fortunes
 Update ORM          | http://localhost:8080/updates/:queries
-Plain Text          | http://localhost:8080/plaintext
+Plain Text          | http://localhost:8080/plaintext

+ 4 - 3
frameworks/PHP/spiral/app.php

@@ -17,6 +17,7 @@ require __DIR__ . '/vendor/autoload.php';
 //Initiating shared container, bindings, directories and etc
 $app = \App\App::init(['root' => __DIR__]);
 
-if ($app != null) {
-    $app->serve();
-}
+if ($app !== null) {
+    $code = (int)$app->serve();
+    exit($code);
+}

+ 10 - 11
frameworks/PHP/spiral/app/config/database.php

@@ -1,4 +1,5 @@
 <?php
+
 /**
  * Spiral Framework.
  *
@@ -7,19 +8,17 @@
  */
 declare(strict_types=1);
 
-use Spiral\Database\Driver;
+use Cycle\Database\Config;
 
 return [
-    'default'   => 'default',
+    'default' => 'default',
     'databases' => [
         'default' => ['driver' => 'mysql'],
     ],
-    'drivers'   => [
-        'mysql' => [
-            'driver'     => Driver\MySQL\MySQLDriver::class,
-            'connection' => 'mysql:host=tfb-database:3306;charset=utf8;dbname=hello_world',
-            'username'   => 'benchmarkdbuser',
-            'password'   => 'benchmarkdbpass',
-        ],
-    ]
-];
+    'drivers' => [
+        'mysql' => new Config\MySQLDriverConfig(
+            connection: new Config\MySQL\DsnConnectionConfig(env('DB_DSN')),
+            queryCache: true
+        ),
+    ],
+];

+ 13 - 7
frameworks/PHP/spiral/app/src/App.php

@@ -15,6 +15,8 @@ use Spiral\Bootloader;
 use Spiral\DotEnv\Bootloader as DotEnv;
 use Spiral\Framework\Kernel;
 use Spiral\Nyholm\Bootloader as Nyholm;
+use Spiral\Cycle\Bootloader as CycleBridge;
+use Spiral\RoadRunnerBridge\Bootloader as RoadRunnerBridge;
 use Spiral\Stempler\Bootloader as Stempler;
 
 class App extends Kernel
@@ -37,7 +39,8 @@ class App extends Kernel
         Bootloader\Security\FiltersBootloader::class,
         Bootloader\Security\GuardBootloader::class,
 
-        Bootloader\Http\HttpBootloader::class,
+        RoadRunnerBridge\HttpBootloader::class,
+
         DebugBootloader::class,
 
         // HTTP extensions
@@ -46,18 +49,21 @@ class App extends Kernel
         Bootloader\Http\ErrorHandlerBootloader::class,
 
         // Databases
-        Bootloader\Database\DatabaseBootloader::class,
-        Bootloader\Database\MigrationsBootloader::class,
+        CycleBridge\DatabaseBootloader::class,
+        CycleBridge\MigrationsBootloader::class,
 
         // ORM
-        Bootloader\Cycle\CycleBootloader::class,
-        Bootloader\Cycle\AnnotatedBootloader::class,
+        CycleBridge\SchemaBootloader::class,
+        CycleBridge\CycleOrmBootloader::class,
+        CycleBridge\AnnotatedBootloader::class,
+        CycleBridge\CommandBootloader::class,
 
         // Template engine
         Stempler\StemplerBootloader::class,
 
         // Framework commands
-        Bootloader\CommandBootloader::class
+        Bootloader\CommandBootloader::class,
+        RoadRunnerBridge\CommandBootloader::class,
     ];
 
     /*
@@ -66,4 +72,4 @@ class App extends Kernel
     protected const APP = [
         RoutesBootloader::class,
     ];
-}
+}

+ 7 - 11
frameworks/PHP/spiral/app/src/Model/Fortune.php

@@ -9,28 +9,24 @@ declare(strict_types=1);
 
 namespace App\Model;
 
+use App\Model\Repository;
 use Cycle\Annotated\Annotation\Column;
 use Cycle\Annotated\Annotation\Entity;
 
-/**
- * @Entity(
- *     table="Fortune",
- *     repository="Repository/FortuneRepository"
- * )
- */
+#[Entity(table: 'Fortune', repository: Repository\FortuneRepository::class)]
 class Fortune implements \JsonSerializable
 {
-    /** @Column(type="primary") */
+    #[Column(type: 'primary')]
     public $id;
 
-    /** @Column(type="text") */
+    #[Column(type: 'text')]
     public $message;
 
     /**
-     * @return array|mixed
+     * @return array
      */
-    public function jsonSerialize()
+    public function jsonSerialize(): mixed
     {
         return ['id' => $this->id, 'message' => $this->message];
     }
-}
+}

+ 7 - 11
frameworks/PHP/spiral/app/src/Model/World.php

@@ -9,28 +9,24 @@ declare(strict_types=1);
 
 namespace App\Model;
 
+use App\Model\Repository;
 use Cycle\Annotated\Annotation\Column;
 use Cycle\Annotated\Annotation\Entity;
 
-/**
- * @Entity(
- *     table="World",
- *     repository="Repository/WorldRepository"
- * )
- */
+#[Entity(table: 'World', repository: Repository\WorldRepository::class)]
 class World implements \JsonSerializable
 {
-    /** @Column(type="primary") */
+    #[Column(type: 'primary')]
     public $id;
 
-    /** @Column(type="int", name="randomNumber") */
+    #[Column(type: 'int', name: 'randomNumber')]
     public $randomNumber;
 
     /**
-     * @return array|mixed
+     * @return array
      */
-    public function jsonSerialize()
+    public function jsonSerialize(): mixed
     {
         return ['id' => $this->id, 'randomNumber' => $this->randomNumber];
     }
-}
+}

+ 9 - 19
frameworks/PHP/spiral/composer.json

@@ -9,26 +9,11 @@
     }
   ],
   "require": {
-    "php": ">=7.1",
-    "spiral/framework": "^2.0",
-    "spiral/debug": "^1.3",
-    "spiral/snapshots": "^1.0",
-    "spiral/console": "^1.2",
-    "spiral/http": "^1.1",
-    "spiral/router": "^1.1",
-    "spiral/roadrunner": "^1.4",
-    "spiral/security": "^2.1",
-    "spiral/validation": "^1.2",
-    "spiral/filters": "^1.2",
-    "spiral/database": "^2.3",
-    "spiral/migrations": "^2.0",
-    "cycle/orm": "^1.0.10",
-    "cycle/proxy-factory": "^1.0",
-    "cycle/annotated": "^2.0",
-    "cycle/migrations": "^1.0",
-    "spiral/dotenv-bridge": "^1.0",
+    "php": ">=8.0",
+    "spiral/framework": "^2.9",
     "spiral/nyholm-bridge": "^1.0",
-    "spiral/stempler-bridge": "^1.0"
+    "spiral/cycle-bridge": "^1.0",
+    "spiral/roadrunner-bridge": "^1.0"
   },
   "scripts": {
     "post-create-project-cmd": [
@@ -42,5 +27,10 @@
     "psr-4": {
       "App\\": "app/src/"
     }
+  },
+  "config": {
+    "allow-plugins": {
+      "spiral/composer-publish-plugin": true
+    }
   }
 }

+ 8 - 4
frameworks/PHP/spiral/spiral.dockerfile

@@ -1,7 +1,11 @@
-FROM php:7.4
+FROM php:8.1.2
 
 RUN docker-php-ext-install pdo_mysql > /dev/null
 
+# Workaround solution for installing ext-sockets for PHP 8.0
+# See https://github.com/docker-library/php/issues/1245
+RUN CFLAGS="$CFLAGS -D_GNU_SOURCE" docker-php-ext-install sockets > /dev/null
+
 ADD ./ /spiral
 WORKDIR /spiral
 
@@ -11,12 +15,12 @@ RUN chmod +x /usr/local/etc/php/install-composer.sh && /usr/local/etc/php/instal
 
 # install dependencies
 RUN apt-get update -yqq > /dev/null && apt-get install -yqq git unzip > /dev/null
-RUN php composer.phar install --optimize-autoloader --classmap-authoritative --no-dev --quiet
+RUN php composer.phar install --optimize-autoloader --classmap-authoritative --no-dev
 
 # pre-configure
-RUN ./vendor/bin/spiral get > /dev/null 2>&1
+RUN ./vendor/bin/rr get-binary > /dev/null 2>&1
 RUN php app.php configure > /dev/null 2>&1
 
 EXPOSE 8080
 
-CMD php app.php up > /dev/null 2>&1 && ./spiral serve -o "http.workers.pool.numWorkers = 64"
+CMD php app.php up > /dev/null 2>&1 && ./rr serve -o "http.pool.num_workers = 64"