rancoud/application

5.2.8 2024-09-02 12:51 UTC

README

Packagist PHP Version Support Packagist Version Packagist Downloads Composer dependencies Test workflow Codecov

具有严格最小 Router 和 Environment 的应用程序骨架。

依赖关系

环境包: https://github.com/rancoud/Environment
路由器包: https://github.com/rancoud/Router

安装

composer require rancoud/application

如何使用它?

通用

要求

您需要一个 .env 文件和名为例如 routes.php 的路由文件
默认 .env 文件的内容(所有值都是可选的,如果您想使用空 .env 文件,则可以这样做)

# setup timezone, by default use UTC (valid timezones are checked against DateTimeZone::listIdentifiers())
TIMEZONE=null

# file names that contain route configurations, if null it will load all files in current folder provided to the Application constructor
ROUTES=null

# to enable all DEBUG_* parameters
DEBUG=false

# enable error_reporting: show php errors
DEBUG_PHP=false

# save PSR-7 request object for Application->getDebugInfos()
DEBUG_REQUEST=false

# save PSR-7 response object for Application->getDebugInfos()
DEBUG_RESPONSE=false

# save queries for Application->getDebugInfos()
DEBUG_DATABASE=false

# add all values of Session object to Application->getDebugInfos()
DEBUG_SESSION=false

# add memory usage/limit/percentage to Application->getDebugInfos()
DEBUG_MEMORY=false

# save elapsed time of each call of Application->run() for Application->getDebugInfos()
DEBUG_RUN_ELAPSED_TIMES=false

# add all included files included to Application->getDebugInfos()
DEBUG_INCLUDED_FILES=false

DEBUG_* 信息可以通过 Application->getDebugInfos(); 获取(除了 DEBUG_PHP
routes.php 文件的内容

<?php

use Rancoud\Http\Message\Factory\Factory;
use Rancoud\Http\Message\Stream;

/** @var \Rancoud\Router\Router $router */
$router->any('/home', static function ($request, $next) {
    return (new Factory())->createResponse()->withBody(Stream::create('hello world'));
});

用法

// you have to set thoses required folders
$folders = [
    'ROOT' => 'folder/path/of/env',    // ROOT is used to read the .env file (must be a folder)
    'ROUTES' => 'folder/path/of/routes'   // ROUTES is used to initialize the router with routes (must be a folder, so it will read php files inside it)
];

$app = new Application($folders);

// create server request from globals
$request = (new \Rancoud\Http\Message\Factory\Factory())->createServerRequestFromGlobals();

// you can also create request from scratch for example
// $request = new \Rancoud\Http\Message\ServerRequest('GET', '/home');

// $response can be null if no route AND no default404 has been found by the Router
$response = $app->run($request);

// you can send response output directly (if not null of course)
$response->send();

您可以在构造函数中添加更多文件夹

$folders = [
    'ROOT' => 'folder/path/of/env',
    'ROUTES' => 'folder/path/of/routes',
    'EXTRA' => 'folder/path/of/extra'
];

$app = new Application($folders);

// you can access to specific folders
$folderExtra = Application::getFolder('EXTRA');

环境文件

您可以使用其他环境文件而不是在根目录中使用 .env

$env = new Environment(['folder/path/of/env'], 'another.env');
$app = new Application($folders, $env);

// you can access to the environment
$config = Application::getConfig();

路由

默认情况下,它将加载 ROUTES 文件夹中的所有 PHP 文件。
您可以在 .env 文件中指定要使用的路由文件。

# in this example, it will require() 3 files: www.php , backoffice.php and api.php in the routes folder
ROUTES=www,backoffice,api

此示例展示了如何为路由器添加路由

$config = [
    'routes' => [
        [
            'methods'     => ['GET'],
            'url'         => '/',
            'callback'    => function($a,$b){
                return (new MessageFactory())->createResponse(200, null, [], 'home');
            },
            'name'        => 'test_home'
        ]
    ]
];

/* @var \Rancoud\Router\Router $router */
$router->setupRouterAndRoutesWithConfigArray($config);

路由器

$app = new Application($folders, $env);

$router = Application::getRouter();

数据库

您必须使用此 Database 包

$app = new Application($folders, $env);

Application::setDatabase($database);
$db = Application::getDatabase();

$infos = $app->getDebugInfos();
// when enabled, all saved queries will be display in $infos['database']

如果您不使用函数 setDatabasegetDatabase,则可以自由使用其他东西。

会话

您可以使用此 Session 包
它在函数 getDebugInfos() 中使用。
您可以使用其他东西。

时区

默认情况下,使用的时区将是 UTC
您可以在 .env 文件中指定时区

# valid timezones are checked with DateTimeZone::listIdentifiers()
TIMEZONE="Europe/Paris"

调试信息

您必须在 .env 文件中启用它

# to enable all DEBUG_* parameters
DEBUG=true

# enable error_reporting: show php errors
DEBUG_PHP=true

# save PSR-7 request object for Application->getDebugInfos()
DEBUG_REQUEST=true

# save PSR-7 response object for Application->getDebugInfos()
DEBUG_RESPONSE=true

# save queries for Application->getDebugInfos()
DEBUG_DATABASE=true

# add all values of Session object to Application->getDebugInfos()
DEBUG_SESSION=true

# add memory usage/limit/percentage to Application->getDebugInfos()
DEBUG_MEMORY=true

# save elapsed time of each call of Application->run() for Application->getDebugInfos()
DEBUG_RUN_ELAPSED_TIMES=true

# add all included files included to Application->getDebugInfos()
DEBUG_INCLUDED_FILES=true

您可以在 "包" 中放入您想要的任何东西,例如您自己的数据库驱动程序

$app = new Application($folders, $env);

Application::setInBag('db', $mydb);
$dbInBag = Application::getFromBag('db');
Application::removeFromBag('db');

应用程序构造函数

设置

必需

可选

应用程序方法

通用命令

  • run(request: \Psr\Http\Message\ServerRequestInterface): ?\Rancoud\Http\Message\Response
  • getDebugInfos(): array

静态方法

  • getFolder(index: string): string
  • getInstance(): Rancoud\Application\Application
  • getConfig(): Rancoud\Environment\Environment
  • setDatabase(database: Rancoud\Database\Database): void
  • getDatabase(): ?Rancoud\Database\Database
  • getRouter(): Rancoud\Router\Router
  • getFromBag(name: string): mixed
  • removeFromBag(name: string): void
  • setInBag(name: string, object: mixed): void

可选依赖关系

数据库包: https://github.com/rancoud/Database
会话包: https://github.com/rancoud/Session

如何开发

使用 composer ci 为 php-cs-fixer 和 phpunit 以及覆盖率
使用 composer lint 为 php-cs-fixer
使用 composer test 为 phpunit 和覆盖率