irfantoor/engine

一个最小化的PHP框架,专注于请求和响应。网络世界的瑞士军刀

4.1.0 2021-05-06 15:49 UTC

README

一个最小化的PHP框架,秉承HTTP的发明精神,专注于请求和响应。网络世界的瑞士军刀。

本库的目标是成为最小化、可嵌入和可教育的。

Irfan的引擎使用IrfanTOOR\Http实现psr/http-message。

注意:这份文档仅作为入门指南,鼓励您研究代码和示例文件夹中的示例,这可能有助于您开始,通过添加、扩展甚至编写自己的类和/或框架。

快速入门

1. 安装

使用以下命令安装最新版本

composer require irfantoor/engine

注意:Irfan的引擎需要PHP 7.0或更高版本。

使用方法

您可以在示例文件夹中找到代码。

hello-world.php

<?php

# php -S localhost:8000 hello-world.php

require ("autoload.php"); # Give the path/to/vendor/autoload.php

use IrfanTOOR\Engine;

$ie = new Engine(
    [
        'debug' => [
            'level' => 2
        ],
        'default' => [
            'name' => 'world',
        ]
    ]
);

# Name passed as get variable: http://localhost:8000/?name=alfa
# or posted through a form
# Check: http://localhost:8000/?name=alfa&debug=1
# Check: http://localhost:8000/?name=alfa&exception=1

$ie->addHandler(function ($request) use($ie) {
    $name = $request->getQueryParams()['name'] ?? $ie->config('default.name');

    $response = $ie->create('Response');
    $response->getBody()->write('Hello ' . ucfirst($name) . '!');

    if ($request->getQueryParams()['exception'] ?? null) {
        throw new Exception("An exception at your service!");
    }

    if ($request->getQueryParams()['debug'] ?? null) {
        # Dump
        d($request);
        d($response);

        # Dump and die!
        dd($ie);
    }

    # A response must be sent back in normal circumstances!
    return $response;
});

$ie->run();

Http Suite提供者

Irfan的引擎默认使用IrfanTOOR\Http suite。您可以通过配置定义使用其他提供者。

下载您想使用的符合Psr的Http suite,并在配置中定义提供者,例如您可以使用slim\psr7;

$ composer require slim/psr7
<?php

require 'path/to/autoload.php';

use IrfanTOOR\Engine;

# Create engine
$ie = new Engine(
    [
        'http' => [
            'provider' => 'Slim\\Psr7',
        ]
    ]
);

# Add handler
$ie->addHandler(function($request) use($ie) {
    # Request received by handle will be a Slim\Psr7 Request

    # Psr\Slim7 Response
    $response = $ie->create('Response');
    $respone->write("Hello world from Slim\Psr7");
    return $response;
});

# Run ...
$ie->run();

环境

环境实例包含由Web服务器传递的环境变量和头信息,这些信息将自动转换为头信息并添加到请求类中。

环境可以通过在配置文件中定义'env'元素或如下方式模拟,如果在不使用引擎的情况下使用

Uri

每当创建服务器请求时,也会存在一个包含请求URL解析信息的Uri,可以按如下方式访问

class RequestHandler
{
    protected $engine;

    function __construct($engine)
    {
        $this->engine = $engine;
    }

    function handle(RequestInterface $request): ResponseInterface
    {
        $uri =  $request->getUri();
        $host = $uri->getHost();
        $port = $uri->getPort();
        $path = $uri->getPath();
        # ...

        $response = $this->engine->create('Response');
        # ...

        return $response;
    }
}

$ie = new Engine();
$ie->addHandler(new RequestHandler($ie));
$ie->run();

Headers

# ...
# Setting a header
$response = $response
    ->withHeader('Content-Type', 'text/plain')
    ->withHeader('keywords', 'hello, world')
;

# Removing a header
$response = $response->withoutHeader('unwanted-header');

# Checking a header
if ($response->hasHeader('content-type')) {
    # Do something ...
}

# Getting a header, note that the key of headers is not case sensitive
$content_type = $response->getHeader('CONTENT-type');
# ...

创建您的配置文件:path/to/config.php

创建一个config.php文件

<?php

return [
    'debug' => [
        # Note: a level 4 sets the reporting_level to E_ALL, and is 0 for other levels.
        'level' => 0, # Or can be 1, 2, 3 or 4
    ],
    'environment'     => [
        'REMOTE_ADDR' => '192.168.1.1',
        'HELLO' => 'WORLD',
    ],
    'site' => [
        'name' => 'mysite.com',
    ]
];

然后可以通过以下方式包含此配置

<?php
$config = require("path/to/config.php");
$ie = new IrfanTOOR\Engine($config));

# OR preferably:
$ie = new IrfanTOOR\Engine([
    # note a debug level, provided while init can help catching the errors
    # of the config file, or any of the classes loaded in config etc.
    'debug' => [
        'level' => 1,
    ],
    'config_file' => "path/to/config.php",
]);

$ie->config('site.name'); # Returns "mysite.com"

调试

您可以在编码应用程序时启用调试,在发生任何异常时将输出简短、简洁、明确的错误描述和跟踪信息。如果您使用Irfan的引擎,可以使用配置启用调试。

<?php
require "path/to/vendor/autoload.php";

use IrfanTOOR\Engine;
$ie = new Engine(
    [
        'debug' => [
            'level'  => 2, # Can be from 0 to 4
        ]
    ]
);
# ...
# If debug level is above 0, you can use the function d() && dd() to dump a
# variable while development.
d($request);
dd($request->getHeaders());

关于

要求 Irfan的引擎与PHP 7.3或更高版本兼容。

许可证

Irfan的引擎在MIT许可证下授权 - 有关详细信息,请参阅LICENSE文件。