divineniiquaye/php-rade

此包已被废弃且不再维护。作者建议使用 biurad/flange 包。

一个快速、可编译和可扩展的 PHP 框架

v1.1.3 2019-03-30 05:54 UTC

This package is auto-updated.

Last update: 2024-02-22 09:32:24 UTC


README

PHP Version Latest Version Workflow Status Code Maintainability Coverage Status Quality Score

Flange 是一个基于 PHP 8.0+ 的 PSR-7PSR-11PSR-14PSR-15 的、支持注解/属性的、由 Divine Niiquaye 创建的非常快速、可编译和可扩展的框架。

此库包含了大量适合开发 Web 应用程序的功能。Flange 真正的可扩展,依赖性少,性能高。

Flange v2.0 处于早期开发阶段,不建议用于生产环境。

📦 安装与基本用法

此项目需要 PHP 8.0 或更高版本。推荐的安装方式是通过 Composer。只需运行

$ composer require biurad/flange 2.0.*

Flange 是基于 Flight RoutingBiurad DISymfony 组件Biurad 库 构建的。Flange 是一个完全符合 PSR 的 PHP 框架,可完全自定义,甚至可用于从小型到大型项目的开发。

require_once __DIR__ . '/vendor/autoload.php';

// Boot the application.
$app = new Flange\Application();

// Add a route to application
$app->match('/hello/{name:\w+}', to: fn (string $name): string => 'Hello ' . $app->escape()->escapeHtml($name));

$extensions = [
    [Flange\Extensions\CoreExtension::class, [__DIR__]],
    // You can add more extensions here ...
];

//If you want to use extensions, here is an example:
$app->loadExtensions($extensions, ['config' => '%project_dir%/config']);

// You can set custom pages for caught exceptions, using default event dispatcher, or your custom event dispatcher.
$app->getDispatcher()?->addListener(Flange\Events::EXCEPTION, new ErrorListener(), -8);

$app->run();

在大型项目中工作!建议使用应用程序的可缓存版本。这比使用带有扩展的不可缓存的 Application 类提供了 60% - 100% 的更多性能。

use function Rade\DI\Loader\{param, phpCode, wrap};

$config = [
    'cacheDir' => __DIR__ . '/caches',
    'debug' => $_ENV['APP_DEBUG'] ?? false, // Set the debug mode environment
];

// Setup cache for application.
$app = Flange\AppBuilder::build(static function (Flange\AppBuilder $creator): void {
    // Add resource to re-compile if changes are made to this file.
    $creator->addResource(new FileResource(__FILE__));

    // Adding routes requires the Rade\DI\Extensions\RoutingExtension to be loaded.
    // Routes should always be added before Rade\DI\Extensions\RoutingExtension is booted, else it will not be compiled.
    $creator->match('/hello/{name:\w+}', to: phpCode('fn (string $name): string => \'Hello \' . $this->escape()->escapeHtml($name);'));

    $extensions = [
        [Flange\Extensions\CoreExtension::class, [__DIR__]],
        // You can add more extensions here ...
    ];

    //If you want to use extensions, here is an example as its recommended to use extensions to build your application.
    $creator->loadExtensions($extensions, ['config' => '%project_dir%/config']);

    // You can set custom pages for caught exceptions, using default event dispatcher, or your custom event dispatcher.
    $creator->definition('events.dispatcher')->bind('addListener', [Flange\Events::EXCEPTION, wrap(ErrorListener::class), -8]);
}, $config);

$app->run(); // Boot the application.

以下是一个您可以为您的应用程序使用的自定义错误示例。

use Biurad\Http\Response\HtmlResponse;
use Flange\Event\ExceptionEvent;

class ErrorListener
{
    public function __invoke(ExceptionEvent $event): void
    {
        // If extensions were loaded, the %project_dir% will exist, else replace will absolute path
        $errorsPath = $event->getApplication()->parameter('%project_dir%/errors/');

        $code = $event->getThrowable()->getCode();
        $templates = [
            $errorsPath . \substr($code, 0, 2) . 'x.html.php', // 40x.html.php format ...
            $errorsPath . \substr($code, 0, 1) . 'xx.html.php', // 4xx.html.php format ...
            $errorsPath . $code . '.html.php', // 404.html.php format ...
            $errorsPath . 'default.html.php',
        ];

        // Tries to load a template file from a list of error templates.
        foreach ($template as $template) {
            if (\file_exists($template)) {
                $event->setResponse(
                    (static function () use ($template, $code) {
                        \ob_start();
                        include __DIR__ . $template;

                        return new HtmlResponse(\ob_get_clean(), (int) $code);
                    })()
                );
            }
        }
    }
}

需要注意的是,使用 PSR-15 中间件堆栈时,使用的是 PHP SPL 队列类,其算法为,LAST <- FIRST : FIRST -> LAST。默认情况下,通过优先级堆叠算法(这意味着优先级越高,扩展或事件监听器在链中触发的越早)加载扩展和事件监听器,默认值为 0。

📓 文档

有关如何使用此库的详细文档,请参阅此库的 文档。还建议浏览 tests 目录中的单元测试。

🙌 赞助者

如果这个库已经进入你的项目,或者你对我们感兴趣并想支持我们,请考虑 捐赠 支持未来的开发。

👥 信用 & 致谢

📄 许可证

Flange 完全免费,并按照 BSD 3 许可证 发布。