eureka/kernel-http 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

eureka/kernel-http

Composer 安装命令:

composer require eureka/kernel-http

包简介

Kernel Http for Content PHP application. Use PSR-7, 15 & 17 to build an application.

README 文档

README

Current version Supported PHP version Build Quality Gate Status Coverage

Kernel Http for any Eureka Framework application.

Define global Application, Controller & Component kernel versions

Installation

If you wish to install it in your project, require it via composer:

composer require eureka/kernel-http

Usage

<?php

declare(strict_types=1);

/*
 * Copyright (c) Romain Cottard
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

use Eureka\Kernel\Http\Application\Application;
use Eureka\Kernel\Http\Kernel;

//~ Define Loader & add main classes for config
require_once __DIR__ . '/vendor/autoload.php';

$root  = realpath(__DIR__ . '/');
$env   = 'dev';
$debug = true;

// Run application
// Applications exception should be caught. Try catch useful only when you have a bug in kernel component
try {
    $application = new Application(new Kernel($root, $env, $debug));
    $response    = $application->run();
    $application->send($response);
} catch (\Exception $exception) {
    echo 'Exception: ' . $exception->getMessage() . PHP_EOL;
    echo 'Trace: ' . $exception->getTraceAsString() . PHP_EOL;
    exit(1);
}

Config tree example

config/
|_ packages/
|  |_ domains/
|      - user.yaml
|   -  app.yaml
|   - database.yaml
|   - services.yaml
|_ dev/
|   - app.yaml
|_ test/
|   - app.yaml
|_ secrets/
|   - database.yaml
|_ services.yaml

Config loading order and overrides:

  1. kernel.yaml is loaded
  2. Then services.yaml
  3. Then packages/
  4. Then {current_env}/
  5. Finally secrets/

Kernel config

parameters:

  #~ Debug
  kernel.debug: false

  #~ Environment
  kernel.environment: 'prod' # Overridden in Kernel class, but define here as helper

  #~ Errors
  kernel.error.display:   'false'
  kernel.error.reporting: !php/const E_ALL

  #~ Directories
  kernel.directory.root:   '..' # Overridden in Kernel class, but define here as helper
  kernel.directory.cache:  '%kernel.directory.root%/var/cache'
  kernel.directory.log:    '%kernel.directory.root%/var/log'
  kernel.directory.config: '%kernel.directory.root%/config'
  kernel.directory.vendor: '%kernel.directory.root%/vendor'

  #~ Compiler passes
  kernel.compiler_pass:
    - My\Custom\CompilerPass

Global services config

parameters:
  app.name: 'kernel'

services:
  # Main container
  Psr\Container\ContainerInterface:
    alias: 'service_container'

  # Main Logger
  logger:
    alias: Psr\Log\NullLogger

  # Main cache item pool
  cache:
    alias: Psr\Cache\CacheItemPoolInterface

  #~ PSR-17 Factories public alias (used by Application via container directly)
  response_factory:
    alias: Nyholm\Psr7\Factory\Psr17Factory
    public: true

  request_factory:
    alias: Nyholm\Psr7\Factory\Psr17Factory
    public: true

  server_request_factory:
    alias: Nyholm\Psr7\Factory\Psr17Factory
    public: true

  stream_factory:
    alias: Nyholm\Psr7\Factory\Psr17Factory
    public: true

  uri_factory:
    alias: Nyholm\Psr7\Factory\Psr17Factory
    public: true

Application (controllers) config

services:
  _defaults:
    autowire: true
    public:   true # Important, because Controller middleware use container to get appropriate controller and use it

  #~ Auto discovery kernel controllers (mainly error controller + interfaces)
  Eureka\Kernel\Http\Controller\:
    resource: '../../vendor/eureka/kernel-http/src/Controller/*'
    calls:
      - setLogger:               [ '@logger' ]
      - setRouter:               [ '@router' ]
      - setResponseFactory:      [ '@response_factory' ]
      - setRequestFactory:       [ '@request_factory' ]
      - setServerRequestFactory: [ '@server_request_factory' ]
      - setStreamFactory:        [ '@stream_factory' ]
      - setUriFactory:           [ '@uri_factory' ]

  #~ Auto discovery application controllers
  Application\Controller\:
    resource: '../../src/Controller/*'
    calls:
      - setLogger:               [ '@logger' ]
      - setRouter:               [ '@router' ]
      - setResponseFactory:      [ '@response_factory' ]
      - setRequestFactory:       [ '@request_factory' ]
      - setServerRequestFactory: [ '@server_request_factory' ]
      - setStreamFactory:        [ '@stream_factory' ]
      - setUriFactory:           [ '@uri_factory' ]

  #~ Auto discovery kernel services
  Eureka\Kernel\Http\Service\:
    resource: '../../vendor/eureka/kernel-http/src/Service/*'

Middleware config

parameters:

  # app.middleware Name is important, used by app to get list of middlewares
  # order is important, as the first is process, then the following, etc.
  app.middleware:
    error:      'Eureka\Kernel\Http\Middleware\ErrorMiddleware'
    router:     'Eureka\Kernel\Http\Middleware\RouterMiddleware'
    logger:     'Eureka\Kernel\Http\Middleware\ResponseTimeLoggerMiddleware'
    quota:      'Eureka\Kernel\Http\Middleware\RateLimiterMiddleware'
    controller: 'Eureka\Kernel\Http\Middleware\ControllerMiddleware'

services:
  _defaults:
    autowire: true
    public:   true # Must be true, because Application use container to retrieve each middleware and assign them to handler
    bind:
      $applicationName: '%app.name%'
      $logger: '@logger'
      $cache:  '@cache'

  #~ Global Kernel Services Middleware
  Eureka\Kernel\Http\Middleware\:
    resource: '../../vendor/eureka/kernel-http/src/Middleware'

  #~ Global Application Services Middleware
  Application\Middleware\:
    resource: '../../src/Middleware'

Router config

services:
  _defaults:
    autowire: true

  router:
    alias: Symfony\Component\Routing\Router

  Symfony\Component\Routing\:
    resource: '../../vendor/symfony/routing/*'
    exclude:  '../../vendor/symfony/routing/{Router,Tests}'

  Symfony\Component\Config\:
    resource: '../../vendor/symfony/config/*'
    exclude:  '../../vendor/symfony/config/{FileLocator,Tests}'

  Symfony\Component\Config\Loader\LoaderInterface:
    alias: Symfony\Component\Routing\Loader\YamlFileLoader

  Symfony\Component\Config\FileLocatorInterface:
    alias: Symfony\Component\Config\FileLocator

  Symfony\Component\Config\FileLocator:
    arguments:
      - '%kernel.directory.config%'

  Symfony\Component\Routing\Router:
    arguments:
      $loader:   '@Symfony\Component\Routing\Loader\YamlFileLoader'
      $resource: 'routes.yaml'
      $options:
        cache_dir: '%kernel.directory.cache%/%kernel.environment%'
        debug:     '%kernel.debug%'

Routes configs

services:
  _defaults:
    autowire: true

  router:
    alias: Symfony\Component\Routing\Router

  Symfony\Component\Routing\:
    resource: '../../vendor/symfony/routing/*'
    exclude:  '../../vendor/symfony/routing/{Router,Tests}'

  Symfony\Component\Config\:
    resource: '../../vendor/symfony/config/*'
    exclude:  '../../vendor/symfony/config/{FileLocator,Tests}'

  Symfony\Component\Config\Loader\LoaderInterface:
    alias: Symfony\Component\Routing\Loader\YamlFileLoader

  Symfony\Component\Config\FileLocatorInterface:
    alias: Symfony\Component\Config\FileLocator

  Symfony\Component\Config\FileLocator:
    arguments:
      - '%kernel.directory.config%'

  Symfony\Component\Routing\Router:
    arguments:
      $loader:   '@Symfony\Component\Routing\Loader\YamlFileLoader'
      $resource: 'routes.yaml'
      $options:
        cache_dir: '%kernel.directory.cache%/%kernel.environment%'
        debug:     '%kernel.debug%'

Components configs

services:
  _defaults:
    autowire: true

  Nyholm\Psr7\:
    resource: '%kernel.directory.root%/vendor/nyholm/psr7/src/*'

  #~ PSR
  Eureka\Component\Http\:
    resource: '%kernel.directory.root%/vendor/eureka/component-http/src/*'

  Psr\Log\:
    resource: '%kernel.directory.root%/vendor/psr/log/src/*'
    
  Psr\Cache\CacheItemPoolInterface:
    alias: Symfony\Component\Cache\Adapter\ArrayAdapter

  Symfony\Component\Cache\Adapter\ArrayAdapter: ~

Routes configs

# ===== HOME =====
home:
  path: /
  controller: Application\Controller\ExampleController::home
  methods: [ GET ]
  defaults:
    rateLimiterQuota: 100 # Used to set rate limit on this endpoint
    rateLimiterTTL:   60  # Used to set rate limit on this endpoint

test_url_with_param:
  path: /blog/{id}/{title}
  controller: Application\Controller\ExampleController::viewWithParams
  methods: [ GET ]
  defaults:
    someString: 'value'
    someBool:   true
    someInt:    42
  requirements:
    id:    '\d+'
    title: '[a-zA-Z-]+'

Default value & requirements values are injected in controller actions, with the following order:

  • ServerRequest object (always pass)
  • defaults (in same order as defined in yaml file)
  • requirements (in same order as defined in yaml. Requirement are always injected as string)

Example of controller:

<?php

declare(strict_types=1);

namespace Eureka\Kernel\Http\Tests\Unit\Mock;

use Eureka\Kernel\Http\Controller\Controller;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

class ExampleController extends Controller
{
    public function home(): ResponseInterface
    {
        return $this->getResponse('Hello world!');
    }
    
    public function viewWithParams(
        ServerRequestInterface $serverRequest,
        string $someString,
        bool $someBool,
        int $someInt,
        string $id,
        string $title,
    ): ResponseInterface {
        return $this->getResponse("entity id: $id, title: $title, someString: $someString, someBool: " . ($someBool ? 'true' : 'false') . ", someInt: $someInt");
    }

Contributing

See the CONTRIBUTING file.

Install / update project

You can install project with the following command:

make install

And update with the following command:

make update

NB: For the components, the composer.lock file is not committed.

Testing & CI (Continuous Integration)

Tests

You can run unit tests (with coverage) on your side with following command:

make php/tests

You can run integration tests (without coverage) on your side with following command:

make php/integration

For prettier output (but without coverage), you can use the following command:

make php/testdox # run tests without coverage reports but with prettified output

Code Style

You also can run code style check with following commands:

make php/check

You also can run code style fixes with following commands:

make php/fix

Check for missing explicit dependencies

You can check if any explicit dependency is missing with the following command:

make php/deps

Static Analysis

To perform a static analyze of your code (with phpstan, lvl 9 at default), you can use the following command:

make php/analyse

To ensure you code still compatible with current supported version at Deezer and futures versions of php, you need to run the following commands (both are required for full support):

Minimal supported version:

make php/min-compatibility

Maximal supported version:

make php/max-compatibility

CI Simulation

And the last "helper" commands, you can run before commit and push, is:

make ci  

License

This project is currently under The MIT License (MIT). See LICENSE file for more information.

eureka/kernel-http 适用场景与选型建议

eureka/kernel-http 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 622 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 02 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 eureka/kernel-http 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-02-19