此包已废弃,不再维护。未建议替代包。

Siler 是一套通用的高层抽象,旨在为 PHP 提供声明式编程的 API。

资助包维护!
leocavalcante

安装量: 56 619

依赖者: 4

建议者: 0

安全性: 0

星标: 1 124

关注者: 49

分支: 90

开放问题: 28

v1.7.9 2021-01-27 19:18 UTC

README

⚠️ 我很抱歉,我无法像期望的那样保持 Siler 的更新,因此它的仓库已被存档。

作为 Siler 的替代方案,我强烈推荐 Nano!一个轻量级且简单的库,与 Swoole 配合使用:https://nano.hyperf.wiki/#/en/



siler.png





Build codecov Psalm coverage Latest Stable Version Total Downloads License

Siler 是一套通用的高层抽象,旨在为 PHP 提供声明式编程的 API。

  • 💧 文件和函数 作为一等公民
  • 🔋 零依赖,所有内容都建立在 PHP 内置函数之上
  • 闪电般快速,无额外开销 - 基准 1基准 2基准 3

Swoole 一起使用

平面文件和普通的 PHP 函数在一个生产级、高性能、可扩展、并发和非阻塞的 HTTP 服务器上运行。

阅读教程。

入门

安装

composer require leocavalcante/siler

这就是全部。实际上,Siler 是一个库,而不是一个框架(可能是一个微型框架),整体程序流程由你控制。所以,没有隐藏的配置或预定义的目录结构。

你好,世界!

use Siler\Functional as λ; // Just to be cool, don't use non-ASCII identifiers ;)
use Siler\Route;

Route\get('/', λ\puts('Hello, World!'));

没有更多,也没有更少。你甚至不需要告诉 Siler 去运行或类似的东西(puts 的工作方式就像是一个惰性评估的 echo)。

JSON

use Siler\Route;
use Siler\Http\Response;

Route\get('/', fn() => Response\json(['message' => 'Hello, World!']));

Response\json 函数将自动在响应头中添加 Content-type: application/json

Swoole

Siler 为 Swoole 提供了一等支持。你可以在 Swoole HTTP 服务器中定期使用 RouteRequestResponse 模块。

use Siler\Http\Response;
use Siler\Route;
use Siler\Swoole;

$handler = function () {
    Route\get('/', fn() => Response\json('Hello, World!'));
};

$port = 8000;
echo "Listening on port $port\n";
Swoole\http($handler, $port)->start();

GraphQL

安装依赖项

composer require webonyx/graphql-php

Schema-first

type Query {
    hello: String
}
use Siler\Route;
use Siler\GraphQL;

$type_defs = file_get_contents(__DIR__ . '/schema.graphql');
$resolvers = [
    'Query' => [
        'hello' => fn ($root, $args, $context, $info) => 'Hello, World!'
    ]
];

$schema = GraphQL\schema($type_defs, $resolvers);

Route\post('/graphql', fn() => GraphQL\init($schema));

Code-first

另一个依赖项

composer require doctrine/annotations

然后

/**
 * @\Siler\GraphQL\Annotation\ObjectType()
 */
final class Query
{
    /**
     * @\Siler\GraphQL\Annotation\Field()
     */
    public static function hello($root, $args, $context, $info): string
    {
        return 'Hello, World!';
    }
}
use Siler\GraphQL;
use Siler\Route;

$schema = GraphQL\annotated([Query::class]);

Route\post('/graphql', fn() => GraphQL\init($schema));

对象类型名称将从类名中猜测出来,字段名也是如此,以及它的返回类型(即:PHP string 标量 === GraphQL String 标量)。

接下来是什么?

许可证

License