无膨胀/endocore

无膨胀Endocore是一个框架,旨在快速构建遵循Action-Domain-Responder模式的Web应用程序。

v0.4.0 2019-11-18 19:23 UTC

This package is not auto-updated.

Last update: 2024-09-17 18:14:03 UTC


README

Bloatless Endocore

Endocore是一个框架,旨在快速构建遵循Action-Domain-Responder模式的Web应用程序。

安装

开始一个基于Endocore的新应用程序最简单、最推荐的方法是使用Endocore示例应用。此存储库为您提供了包括所有必需的文件和文件夹在内的脚手架应用程序,以启动您的项目。

您可以使用composer安装Endocore App

php composer.phar create-project bloatless/endocore-app my_sample_project

文档

除了此文档外,您还应查看Endocore App的源代码。它包含一些很好的文档示例。

目录结构

app/            Contains your applications basic files (actions, domains, ...)
bootstrap/      Contains the bootstrap file for your application
config/         Contains the configuration for your application
logs/           Contains log files
public/         Contains the entry script and public files of your application
routes/         Contains the routes file(s) of your application
vendor/         Contains the Endocore framework and other libraries

配置

安装Endocore App后,您应检查并调整位于config文件夹中的config.php文件。大多数设置应使用其默认值,但如果您的应用程序需要使用MySQL数据库等,您需要添加或调整一些值。

路由

您的应用程序的路由定义了在请求指定URL时将执行哪个操作。您的应用程序支持的所有URL都需要路由到操作。您可以通过位于routes文件夹中的default.php文件调整路由。

GET路由

return [
    'home' => [
        'method' => 'GET',
        'pattern' => '/about',
        'handler' => 'Bloatless\EndocoreApp\Actions\AboutAction',
    ],
];

此示例将请求路径/about路由到您的应用程序的Actions文件夹中的AboutAction。

POST路由

return [
    'home' => [
        'method' => 'POST',
        'pattern' => '/customers',
        'handler' => 'Bloatless\EndocoreApp\Actions\AddCustomerAction',
    ],
];

此示例处理对/customers的POST请求,并调用您的应用程序中的AddCustomerAction

其他路由类型

当然,您也可以以相同的方式定义PUTDELETE或其他任何有效的请求类型。

路由参数

您可以在路由模式中使用占位符。这些参数将使用$arguments数组传递给您的操作。

return [
    'customer' => [
        'method' => 'GET',
        'pattern' => '/customers/{id}',
        'handler' => 'Bloatless\EndocoreApp\Actions\CustomerAction',
    ],
];

此路由将匹配例如/customers/123之类的URL。CustomerAction将接收一个如下的$arguments数组

[
    'id' => 123
]

您还可以使用正则表达式模式来定义占位符接受的值。

return [
    'customer' => [
        'method' => 'GET',
        'pattern' => '/customers/{id:[0-9]+}',
        'handler' => 'Bloatless\EndocoreApp\Actions\CustomerAction',
    ],
];

例如,此路由将匹配/customers/123但不会匹配customers/abc

括在方括号中的段被视为可选的,例如:

pattern' => '/customers[/{id:[0-9]+}'

操作和响应者

应用程序接收到的每个请求都将根据您的路由中的定义分派到操作。操作处理此请求。通常通过使用请求中的输入数据从域请求数据。然后将域中的数据传递给响应者,该响应者构建HTTP响应。然后,操作将此响应返回到应用程序。

Endocore为HTML和JSON内容提供响应者。您可以通过扩展相应的操作来使用这些响应者。

具有JSON响应的操作

class MyJsonAction extends JsonAction
{
    public function __invoke(array $arguments = []): Response
    {
        $data = [
            'foo' => 'Some data...',
        ];
        return $this->responder->found($data);
    }
}

此示例显示了一个操作如何从JsonAction继承JsonResponder,然后能够仅使用框架提供的方法响应json数据。

具有HTML响应的操作

如果您想以HTML内容响应,则可以继承自HtmlAction并使用HtmlResponder。

class MyHtmlAction extends HtmlAction
{
    public function __invoke(array $arguments = []): Response
    {        
        $data = [
            'body' => '<p>Hello World!</p>',
        ];
        return $this->responder->found($data);
    }
}

此示例将输出一个简单的Hello World段落。

域处理应用程序的逻辑。域可以是简单的类或任何类型的服务。

错误处理和日志记录

Endocore 框架提供了一些基本的工具来处理错误和日志。

使用文件记录器

在任何 ActionDomain 中,您都可以访问一个符合 PSR-3 规范的文件日志记录器。

class MyHtmlAction extends HtmlAction
{
    public function __invoke(array $arguments = []): Response
    {
        $this->logger->warning('Some error occurred');  
    }
}
日志配置

通过使用您的配置文件 config/config.php,您可以定义日志文件的存储目标文件夹以及最小日志级别。

'logger' => [
    'path_logs' => __DIR__ . '/../logs',
    'min_level' => 'warning',
],

日志文件将按天存储,文件名如 2018-12-12_endocore.log

日志级别

符合 PSR-3 规范的日志记录器可以在不同的级别进行日志记录。所有低于您在配置中定义的最小级别的所有事件都将被丢弃。可用的日志级别包括:

$this->logger->debug('Some error occurred');
$this->logger->notice('Some error occurred');
$this->logger->info('Some error occurred');
$this->logger->warning('Some error occurred');
$this->logger->error('Some error occurred');
$this->logger->critial('Some error occurred');
$this->logger->alert('Some error occurred');
$this->logger->emergency('Some error occurred');

还有一个通用的 log 方法可用。

$this->logger->log('warning', 'Some error occurred');

此外,您还可以提供一些上下文信息。

$this->logger->warning('Some error message', [
    'browser' => 'Firefox',
]);

错误响应

当使用 Responder(通常在 Action 中)时,如果您的应用程序中出现错误并且需要停止执行,您可以使用各种方法。

class MyHtmlAction extends HtmlAction
{
    public function __invoke(array $arguments = []): Response
    {
        // some error occurs...
        return $this->responder->error(['data' => 'some additional data...']);
    }
}

此方法将自动以 HTTP 状态码 500 响应并显示一个简单的错误消息。

对于 HTTP 错误,还有一些其他方法可以自动设置相应的 HTTP 状态码。

class MyHtmlAction extends HtmlAction
{
    public function __invoke(array $arguments = []): Response
    {
        return $this->responder->badRequest(); // 400
        return $this->responder->notFound(); // 404
        return $this->responder->methodNotAllowed(); // 405
    }
}

抛出异常

如果您不能使用 Responder,您仍然可以使用异常来响应错误消息。您可以从应用程序的任何位置抛出这些异常。

通用异常
class MyDomain
{
    public function myMethod(): string
    {
        throw new EndocoreException('Something went wrong...');
    }
}

抛出 EndocoreException 将强制应用程序以错误 500 状态码响应。此外,错误将被记录到日志文件中。

HTTP异常

对于 Responder 支持的每个 HTTP 错误,都有一个相应的异常可以在应用程序的任何位置抛出。

class MyDomain
{
    public function myMethod(): string
    {
        throw new BadRequestException(); // 400
        throw new NotFoundException(); // 404
        throw new MethodNotAllowedException(); // 405
    }
}

许可证

MIT