定制 vdb/uri 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

vdb/uri

Composer 安装命令:

composer require vdb/uri

包简介

A set of URI classes and a URI parser, based on RFC 3986

README 文档

README

Build Status Latest Stable Version Total Downloads License

README

What is VDB\URI?

A set of URI classes and a URI parser, based on RFC 3986, inspired by java.net.URI.

With it, developers can parse, validate, normalize and compare URIs. Once an URI is parsed, developers can use the URI object to get detailed information about the URI.

The parser is a validating parser, that can handle URLs, URNs, and any URI scheme, such as http, mailto, ftp, etc.

It is easy to create your own URI classes for specific URI schemes by extending the Uri, or if you are feeling adventurous, implementing the URI interface.

Installation

The easiest way to install VDB\URI is by downloading vdb-uri.phar. Then you only have to require the Phar file to begin using VDB\URI: require_once '/path/to/vdb-uri.phar';

A more flexible way to install VDB\URI is with composer. That way you can keep control over which version you use. Find it on http://packagist.org under the name vdb/uri.

Usage

Example

The first step is creating and URI object. In this example we will be using an the generic Uri class.

use VDB\Uri\Uri;

$uri = new Uri('http://user:pass@example.com/foo/..?bar#baz');

Alternatively, you could use a relative reference with a base URI to resolve it against:

$uri = new Uri('/foo/..?bar#baz', 'http://user:pass@example.com?ignored');

Or, if no base URI is known, only supply the relative reference:

$relativeUri = new Uri('/foo/..?bar#baz');

Then we can get the validated, recomposed string of the URI:

$parsedUriString = $uri->toString(); // 'http://user:pass@example.com/?bar#baz'
$parsedRelativeUriString = $relativeUri->toString(); // '/foo/..?bar#baz'

Or access its separate components with accessors:

$query = $uri->getQuery(); // 'bar'

As an alternative to calling toString(), you could simple use the URI in a string context because __toString() is implemented as an alias of toString().

Note that normalization doesn't happen automatically, you have to call normalize() for that. Normalization includes the following:

  • dot segements in the path component,
  • the port if it matches the default port for the scheme,
  • percent encoding and character case where applicable according to RFC 3986. The only exception to this: when constructing a relative reference WITH a base URI, the path dot segments get normalized automatically as part of resolving the relative reference against the absolute base URI.

API

The basics:

  • __construct($reference, $baseUri = null)
  • __toString()
  • toString()

Normalization

  • normalize()

Comparison

  • `equals(URI $uri, $normalized)

Accessors

  • getFragment()
  • getHost()
  • getPassword()
  • getPath()
  • getPort()
  • getQuery()
  • getScheme()
  • getUsername()

Subclassing Uri for specific schemes

Although the Uri class can be used to parse any URI, different URI schemes (http, https, etc.) have different rules for what is a valid URI. For example: the HTTP scheme specification states that the path component of a URL, if empty, should be set to '/'. The best way to implement this custom behavior is by subclassing Uri.

There are a few methods you can override in your subclass:

Scheme specific post processing:

  • doSchemeSpecificPostProcessing(). In here you can do anything you like with the parsed component values

Validators. Should throw VDB\URI\UriSyntaxException if the component value is invalid:

  • validateAuthority()
  • validateFragment()
  • validateHost()
  • validateOriginalUrlString()
  • validatePassword()
  • validatePath()
  • validatePort()
  • validateQuery()
  • validateScheme()
  • validateUserInfo()
  • validateUsername()

See the Http class for an example implementation.

Usage tips

If you want to use type hinting (you should) on VDB\Uri classes in your application, you should use the UriInterface for that instead of the Uri class. That way, your application will support switching out different implementations of URI classes without any refactoring.

TODO

For a list of todo's check the issues in milestone feature-complete here.

vdb/uri 适用场景与选型建议

vdb/uri 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 229.98k 次下载、GitHub Stars 达 11, 最近一次更新时间为 2013 年 03 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 vdb/uri 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 229.98k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 12
  • 点击次数: 30
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 11
  • Watchers: 1
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-03-04