leth/ip-address 问题修复 & 功能扩展

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

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

leth/ip-address

Composer 安装命令:

composer require leth/ip-address

包简介

IPv4 and IPv6 address and subnet classes with awesome utility functions.

README 文档

README

A set of utility classes for working with IP addresses in PHP. Supports both IPv4 and IPv6 schemes.

Requirements

  • PHP version 5.3.0 or greater.

  • The PEAR Math_BigInteger class

    Required for add & subtract operations on IPv6 addresses, and finding IPs in IPv6 address blocks.

Examples

<?php
use Leth\IPAddress\IP, Leth\IPAddress\IPv4, Leth\IPAddress\IPv6;
// Creates an instance
$ip = IP\Address::factory('127.0.0.1');

// Prints "127.0.0.1"
echo $ip . "\n";

/**
 * IP\Address::factory(...) will attempt to guess the address version from the arguments
 */
// Returns an instance of IPv4\Address
$ip = IP\Address::factory('127.0.0.1');
$ip = IPv4\Address::factory('127.0.0.1');

// Returns an instance of IPv6\Address
$ip = IP\Address::factory('::1');
$ip = IPv6\Address::factory('::1');

/**
 * IP\NetworkAddress::factory(...) will also attempt to guess protocol versions
 */
// Can either be called with the subnet size encoded in the address string,
$net_addr = IP\NetworkAddress::factory('192.168.0.1/24');
// Or as the second parameter
$net_addr = IP\NetworkAddress::factory('192.168.0.1', 24);

// Prints '192.168.0.0'
echo $net_addr->get_network_address() . "\n";
// Prints '192.168.0.255'
echo $net_addr->get_broadcast_address() . "\n";
// Prints '255.255.255.0'
echo $net_addr->get_subnet_mask() . "\n";

/**
 * For each address of the specified network.
 */
$network = IPv4\NetworkAddress::factory('192.168.0.0/24');
foreach ($network as $ip) {
	// $ip is instance of IPv4\Address with value:
	// 192.168.0.0
	// 192.168.0.1
	// ...
}

$network = IPv4\NetworkAddress::factory('192.168.0.0/24');
// Prints '256'
echo count($network);

/**
 * Merge adjacent NetworkAddress blocks into larger blocks
 */
$small = array(
	IPv4\NetworkAddress::factory('192.168.0.0/24'),
	IPv4\NetworkAddress::factory('192.168.1.0/24')
);
$merged = IP\NetworkAddress::merge($small);
// Prints '1'
echo count($merged);
// Prints '1'
echo $merged[0] == IP\NetworkAddress::factory('192.168.0.0/23');

/**
 * Get specified octet from IP
 */
$ipv4 = IP\Address::factory('192.168.1.102');
// Prints '102'
echo $ipv4->get_octet(-1);
// Prints '168'
echo $ipv4[1];

$ipv6 = IP\Address::factory('2490::fa');
// Prints '250'
echo $ipv6->get_octet(-1);
// Prints '0'
echo $ipv6[5];

Test Cases

To run the test cases, the following commands will do the trick:

  • No-frills tests:

    phpunit -c phpunit.xml.dist

  • Generate code coverage reports into './coverage/':

    phpunit -c phpunit.xml.dist --coverage-html coverage

  • With colours and verbose output:

    phpunit -c phpunit.xml.dist --colors --verbose

  • All together:

    phpunit -c phpunit.xml.dist --coverage-html coverage --colors --verbose

leth/ip-address 适用场景与选型建议

leth/ip-address 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 748.31k 次下载、GitHub Stars 达 62, 最近一次更新时间为 2012 年 08 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 leth/ip-address 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 748.31k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 64
  • 点击次数: 23
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

  • Stars: 62
  • Watchers: 5
  • Forks: 14
  • 开发语言: PHP

其他信息

  • 授权协议: LGPL-3.0-or-later
  • 更新时间: 2012-08-11