承接 danack/slim4-auryn-invoker 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

danack/slim4-auryn-invoker

Composer 安装命令:

composer require danack/slim4-auryn-invoker

包简介

Allows route callables to be dispatched by Auryn.

关键字:

README 文档

README

Build Status

This library allows you to use the Auryn Dependency Injection library as the dispatching layer of a Slim Framework application.

It replaces the built-in dispatcher that executes the callables for routes, and instead executes the callables through Auryn.

It does not affect the dispatching of middlewares.

Quickstart

If you install this library though Composer, all you need to do to link it into a Slim application is to set the 'foundHandler' when creating the Slim app.

<?php

use Danack\SlimAurynInvoker\SlimAurynInvokerFactory;

$container = new \Slim\Container;
$container['foundHandler'] = new SlimAurynInvokerFactory($injector);
$app = new \Slim\App($container);

There is an example app in the example directory

Why you should use this library

There are two main reasons to use Auryn as the tool that executes your controllers in your application; it allows you to use interface segregation for passing in parameters, as well allowing your controllers to return simple 'stub response' types, rather than having to touch the PSR 7 response object.

Inteface segregation for parameters

@todo - explain why interface segration is so awesome for controllers.

I have a talk that I have given at a couple of user groups and PHPNW - https://www.youtube.com/watch?v=YKXfOYTBaI4 for which the slides are available here.

Simple typed return values

I pretty strongly believe that most of the time, it should be left to the framework to connect up a 'stub response' from a controller to the underlying library that will be used to actually send it to a user.

The SlimAurynInvoker allows you to return a 'stub response' object that consists of just the status code, body and a set of headers that the controller wishes to send as the response. The library

This makes life much more pleasant. Compare two equivalent controllers. The first controller directly modifies the Response object.

function psr7JsonController(Request $request, Response $response)
{
    $data = ['foo' => 'bar'];
    $json = json_encode($data);

    $response->getBody()
      ->write($json)
      ->withHeader('Content-type', 'application/json');
      return $response;
}

The second controller simply returns a stub JsonResponse.

function simpleJsonController(Request $request, Response $response)
{
    $data = ['foo' => 'bar'];

    return new JsonResponse($data);
}

By returning a stub response object, it not only remove a lot of tedious, fiddly code from your controllers, but also makes it easier to:

  • Understand what type of data the controller is returning.
  • Manage the details of which headers are sent for which response type in one place, rather than having to alter every controller that returns that type.
  • Test your controllers are returning the correct data type.

Setup and result processing

The steps before and after the controller is dispatched can be controlled by the user. By default the steps are defined as follows:

Setting up before the controller is dispatched

By default the SlimAurynInvoker does these actions.

  • Shares the request object through the injector, so that controllers can have it as a dependency.
  • Shares the response object through the injector, so that controllers can have it as a dependency.
  • Defines the route parameters as named parameters so that controllers can have the route arguments as dependencies by name.
  • Creates and shares a \Danack\SlimAurynInvoker\RouteParams object to that controllers can have the route pamaters as a dependency by type.

Result processing

By default after dispatching the controller, the SlimAurynInvoker will:

  • If an object of type \Danack\Response\StubResponse was returned by the controller, the SlimAurynInvoker will map it into the Psr 7 response object.

  • If an object of type Psr 7 Response is returned, SlimAurynInvoker will just pass it back to Slim.

Customising the setup and result processing

If you want to customise either the setup or result processing functionality, you can do that by passing appropriate callables as the $resultMappers and/or $setupFunction into the \Danack\SlimAurynInvoker\SlimAurynInvokerFactory.

The resultMapper array should be an array, where the keys are the types that are returned by the controller, and the values are callables, with the signature:

function ($builtResponse, ResponseInterface $response);

A setup callable should have this signature:

function(
        Injector $injector,
        ServerRequestInterface $request,
        ResponseInterface $response,
        array $routeArguments
    );

An example that show an alternative resultMapper is in the file ./example/public/index_html.php. It shows how you can configure the SlimAurynInvoker, so that each controller can just return a string, which automatically gets sent as a HTML response.

Running the example

There is a simple example app in the example folder, which can be run by using PHP's built-in webserver. You can run it by running this:

php -S 0.0.0.0:8000 -t example/public

in the root directory of this library, and then going to http://127.0.0.1:8000/ in your browser.

Additionally, there is an example of a controller returning just a string, which is mapped into a HTML response at http://127.0.0.1:8000/index_html.php

Notes

How to run the unit tests

php vendor/bin/phpunit -c test/phpunit.xml

How to run the code style check

php vendor/bin/phpcs --standard=./test/codesniffer.xml --encoding=utf-8 --extensions=php -p -s lib

danack/slim4-auryn-invoker 适用场景与选型建议

danack/slim4-auryn-invoker 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 61 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 07 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「di」 「auryn」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 danack/slim4-auryn-invoker 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 danack/slim4-auryn-invoker 我们能提供哪些服务?
定制开发 / 二次开发

基于 danack/slim4-auryn-invoker 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 61
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 17
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-07-26