germania-kg/client-location
Composer 安装命令:
composer require germania-kg/client-location
包简介
PSR-15 Middleware for storing client location
README 文档
README
Germania KG · ClientLocation
Installation
$ composer require germania-kg/client-location
PSR-15 Middleware
The middleware checks the incoming ServerRequest for a client-ip attribute. The client IP is passed to the factory callable which returns some location information. This client location is then stored in the ServerRequest as client-location attribute.
If the client-ip attribute is not set or empty, the middleware does nothing and passes on to the RequestHandler.
The ClientIpLocationMiddleware implements PSR-15 MiddlewareInterface. The constructor requires
- Factory callable which accepts the client's IP address and actually determines the client location. Proposal: Use the HttpClientLocationCreator below.
- PSR-3 Logger for errors.
- An optional PSR-3 error loglevel name can be added optionally. Default is
LogLevel::ERROR
<?php use Germania\ClientIpLocation\ClientIpLocationMiddleware; use Psr\Log\LogLevel; $fn = function($ip) { return "Berlin"; }; $logger = new Monolog(...); $middleware = new ClientIpLocationMiddleware($fn, $logger); $middleware = new ClientIpLocationMiddleware($fn, $logger, LogLevel::ERROR);
Configuration
Both attribute names can be renamed. Here the defaults:
$middleware->setClientIpAttributeName("client-ip") ->setClientLocationAttributeName("client-location");
HttpClientLocationCreator
the HttpClientLocationCreator is a callable class which accepts the client IP and returns the client location. Its constructor requires an API endpoint, a PSR-18 HTTP client and a PSR-17 Request factory.
The API endpoint should contain a {{ip}} template variable where the client IP is placed in.
<?php use Germania\ClientIpLocation\HttpClientLocationCreator; use Psr\Http\Client\ClientInterface; use Nyholm\Psr7\Factory\Psr17Factory; $api = "https://api.geocoder.test/location?ip={{ip}}"; $http_client = new \Http\Adapter\Guzzle6\Client( new \GuzzleHttp\Client ); $request_factory = new Psr17Factory; $creator = new HttpClientLocationCreator($api, $psr18_client, $request_factory);
Configuration
Set custom API response decoder
Per default, the response will be json_decoded as associative array. You can specify a custom response decoder:
use Psr\Http\Message\ResponseInterface; $decoder = function(ResponseInterface $response) { return json_decode($response->getBody(), "assoc"); }; // Pass with constructor $creator = new HttpClientLocationCreator($api, $psr18_client, $request_factory, $decoder); // ...or $creator->setResponseDecoder( $decoder );
Set default location to return when HTTP client throws exception:
$creator->setDefaultLocation("Berlin");
Set PSR-3 Logger:
$logger = new Monolog(...); // Pass with constructor $creator = new HttpClientLocationCreator($api, $psr18_client, $request_factory, $decoder, $logger); // ...or $creator->setLogger( $logger );
Issues
Development
Grab and go using one of these:
$ git clone git@github.com:GermaniaKG/ClientLocation.git $ gh repo clone GermaniaKG/ClientLocation
Unit tests
Either copy phpunit.xml.dist to phpunit.xml and adapt to your needs, or leave as is. Run PhpUnit test or composer scripts like this:
$ composer test # or $ vendor/bin/phpunit
germania-kg/client-location 适用场景与选型建议
germania-kg/client-location 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 38 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 12 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「location」 「psr15」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 germania-kg/client-location 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 germania-kg/client-location 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 germania-kg/client-location 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Hero for your Laminas and Mezzio application to trap php errors & exceptions
Simple PHP Library for RESTful APIs
The official PHP library for apiip.net that allowing customers to automate IP address validation and geolocation lookup in websites, applications and back-office system. Visit our API docs at https://apiip.net/documentation
Test cases and doubles for use with WellRESTed
A Symfony2 Bundle to handle geographic location of your entities
Provides a client to interact with the Sendcloud v3 API in an object-oriented way.
统计信息
- 总下载量: 38
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-12-01