headzoo / utilities
此包已被废弃,不再维护。未建议替代包。
一组PHP实用类和函数。
0.6.3.2
2014-06-25 01:13 UTC
Requires
- php: >=5.5.0
- psr/log: dev-master
README
一组PHP实用类和函数。
概述
这个项目是我多年来创建的一组类、方法和函数的集合,我将其用于大多数其他项目。这个项目的目的是将我所有的有用代码放在一个地方。随着我在其他项目中找到更多有用的类,我会将其添加到这个框架中。
要求
安装
可以使用git或composer安装此库。
Git
使用以下命令克隆项目。
git clone git@github.com:headzoo/core.git
Composer
将项目添加到composer.json作为依赖项。
"require": {
"headzoo/core" : "dev-master"
}
类概述
Core\Arrays
包含处理数组的静态方法。
Core\Strings
包含处理字符串的静态方法。
Core\Objects
包含处理对象和类的静态方法。
Core\ErrorHandler
用于捕获和优雅地处理核心错误和异常。
Core\Errors
用于处理E_ERROR常量的实用类。
Core\Profiler
用于代码分析。
Core\Conversions
用于将一个值转换为另一个值的实用类。
Core\ConstantsTrait
用于反射类常量的特质。
Core\AbstractEnum
用于创建枚举类的抽象类。
Core\SmartCallable
用于在不再需要资源时调用函数。
Core\ConfigurableCallable
创建具有可配置行为的可调用实例。
Core\FunctionsTrait
包含处理函数和方法的方法。
Core\Comparator
包含进行值之间比较的静态方法。
快速入门
本快速入门指南简要介绍了几个类。完整的类API文档可在/docs目录中找到。
Core\Strings
echo Strings::camelCaseToUnderscore("CamelCaseString"); // Outputs: "camel_case_string" echo Strings::camelCaseToUnderscore("MaryHadALittleLamb"); // Outputs: "mary_had_a_little_lamb" $is = Strings::startsUpper("Welcome my son, welcome to the machine."); var_dump($is); // Output: bool(true); $is = Strings::startsUpper("you've been in the pipeline, filling in time"); var_dump($is); // Output: bool(false) // Truncating a string at the end. echo Strings::truncate("Mary had a little lamb, whose fleece was white as snow.", 20, Strings::TRUNC_END); // Outputs: "Mary had a little..." // Truncating a string at the start. echo Strings::truncate("Mary had a little lamb, whose fleece was white as snow.", 20, Strings::TRUNC_START); // Outputs: "...as white as snow." // Truncating a string in the middle. echo Strings::truncate("Mary had a little lamb, whose fleece was white as snow.", 20, Strings::TRUNC_MIDDLE); // Outputs: "Mary ha...e as snow."
Core\Arrays
$array = [ "headzoo", "joe", "sam" ]; echo Arrays::conjunct($array); // Outputs: headzoo, joe, and sam // Using a callback to quote the array values. echo Arrays::conjunct($array, "and", 'Headzoo\Core\Strings::quote'); // Outputs: 'headzoo', 'joe', and 'sam' $arr = [ 0 => [ "username" => "headzoo", "email" => "sean@headzoo.io" ], 1 => [ "username" => "joe", "email" => "joe@headzoo.io" ] ] $ret = Arrays::column($arr, "username"); // Outputs: ["headzoo", "joe"]
Core\Objects
// Testing whether an object is an instance of another. $is = Objects::isInstance(new stdClass(), stdClass); var_dump($is); // Outputs: bool(true) // Unlike the instanceof operator, the second argument can be a string. $is = Objects::isInstance(new stdClass(), 'stdClass'); var_dump($is); // Outputs: bool(true); // You can even test an array of objects. $objects = [ new stdClass(), new stdClass() ]; $is = Objects::isInstance($objects, stdClass); var_dump($is); // Outputs: bool(true)
Core\ErrorHandler
// Capture all errors, and display an error page instead of the usual php // error message. $handler = new ErrorHandler(); $handler->handle(); // Setup your own way of handing errors. $handler = new ErrorHandler(); $handler->setCallback(function($handler) { include("template/error.php"); }); $handler->handle() // Even handle errors different in different environments. $handler = new ErrorHandler(); $handler->setCallback("dev", function($handler) { include("template/error_dev.php"); }); $handler->setCallback("live", function($handler) { include("template/error_live.php"); }); $handler->handle("live");
Core\Profiler
// The most basic profiling. $profiler = new Profiler(); $profiler->start(); ... do something here ... $micro = $profiler->stop(); var_dump($micro); // Outputs: // "Profile time for 'default': 0.00030207633972168" // double(0.00030207633972168) // This example runs the closure 100 times, and displays the profile results. Profiler::run(100, true, function() { ... do something here ... }); // Output: // // Total Runs: 100 // Total Time: 0.099596977234 // Average Time: 0.000981624126 // ------------------------------- // Run #1 0.000479936599 // Run #2 0.000968933105 // Run #3 0.000982999801 // Run #4 0.000988006591 // ...... // Run #97 0.000985145568 // Run #98 0.000983953476 // Run #99 0.000997066497 // Run #100 0.000993013382
Core\AbstractEnum
class DaysEnum extends AbstractEnum { const SUNDAY = "SUNDAY"; const MONDAY = "MONDAY"; const TUESDAY = "TUESDAY"; const WEDNESDAY = "WEDNESDAY"; const THURSDAY = "THURSDAY"; const FRIDAY = "FRIDAY"; const SATURDAY = "SATURDAY"; const __DEFAULT = self::SUNDAY; } $day = new DaysEnum("SUNDAY"); echo $day; echo $day->value(); // Outputs: // "SUNDAY" // "SUNDAY" // The default value is used when not specified. $day = new DaysEnum(); echo $day; // Outputs: "SUNDAY" // The constructor value is not case-sensitive. $day = new DaysEnum("sUndAy"); echo $day; // Outputs: "SUNDAY" // Enum values are easy to compare. $day_tue1 = new DaysEnum(DaysEnum::TUESDAY); $day_fri1 = new DaysEnum(DaysEnum::FRIDAY); $day_tue2 = new DaysEnum(DaysEnum::TUESDAY); $day_fri2 = new DaysEnum($day_fri1); var_dump($day_tue1 == DaysEnum::TUESDAY); var_dump($day_tue1 == $day_tue2); var_dump($day_fri1 == $day_fri2); var_dump($day_tue1 == DaysEnum::FRIDAY); var_dump($day_tue1 == $day_fri1); // Outputs: // bool(true) // bool(true) // bool(true) // bool(false) // bool(false)
Core\SmartCallable
// In this example we create a method which requests a web resource using curl. // We use a SmartCallable instance to ensure the curl resource is closed when // the method returns, or an exception is thrown. public function fetch() { $curl = curl_init("http://some-site.com"); $sc = SmartCallable::factory(function() use($curl) { curl_close($curl); }); $response = curl_exec($curl); if ($e = curl_error()) { throw new Exception($e); } return $response; } // The method could also be written this way. public function fetch() { $curl = curl_init("http://some-site.com"); $sc = SmartCallable::factory("curl_close", $curl); $response = curl_exec($curl); if ($e = curl_error()) { throw new Exception($e); } return $response; }
Core\ConfigurableCallable
// In this example you want to insert a row into the database, which may lead to // a DeadLockException being thrown. The recommended action for dead locks is retrying // the query. We use a ConfigurableCallable instance to keep trying the query until // it succeeds. $link = mysqli_connect("localhost", "my_user", "my_password", "my_db"); $query = new ConfigurableCallable("mysqli_query"); $query->retryOnException(DeadLockException::class); $result = $query($link, "INSERT INTO `members` ('headzoo')"); // In this example we will call a remote web API, which sometimes takes a few tries // depending on how busy the remote server is at the any given moment. The remote // server may return an empty value (null), the API library may thrown an exception, // or PHP may trigger an error. $api = new RemoteApi(); $members = new ConfigurableCallable([$api, "getMembers"]); $members->retryOnException() ->retryOnError() ->retryOnNull(); $rows = $members(0, 10);
Core\Conversions
echo Conversions::bytesToHuman(100); // Outputs: "100B" echo Conversions::bytesToHuman(1024); // Outputs: "1KB" echo Conversions::bytesToHuman(1050); // Outputs: "1.02KB"
变更日志
v0.6.3.2 - 2014/06/24
- 修改了对非对象处理
Objects::isInstance
。
v0.6.3.1 - 2014/05/28
- 创建
FunctionsTrait::throwOnInvalidArgument
。
v0.6.2 - 2014/05/19
Arrays
类的bug修复。
v0.6.1 - 2014/05/13
- 对Psr\Log的少量修复。
v0.6.0 - 2014/04/01
Functions
类现在是一个特质,FunctionsTrait
。- 将类
Complete
重命名为SmartCallable
。 - 创建
ConfigurableCallable
类。 - 创建
Comparator
类。
v0.5.0 - 2014/03/31
- 创建了一个名为
Conversions
的类。 Profiler::run
方法输出内存使用情况。- 重构了
ErrorHandler
类。
v0.4.1 - 2014/03/30
- 创建了一个名为
Functions::swapArgs
的方法。 - 重构了
ErrorsHandler
类中的部分代码。
v0.4.0 - 2014/03/30
- 创建了一个名为
ErrorsHandler
的类。 - 创建了一个名为
Errors
的类。 - 创建了一个名为
Arrays::remove
的方法。
v0.3.2 - 2014/03/29
- 创建了一个名为
Strings::truncate
的方法。 - 移除了
Strings::split
方法。 - 创建了一个名为
Profiler
的类。 - 将
psr/Log
设置为必需。
v0.3.1 - 2014/03/27
- 将
Validator
类合并到Functions
类中。
v0.3 - 2014/03/26
- 将命名空间
Headzoo\Utilities
重命名为Headzoo\Core
。 - 将项目重命名为
headzoo/core
。 - 创建了一个核心类
Obj
。 - 添加了
ConstantsTrait
特性。 - 创建了一个名为
ConstantsTrait
的特性。 - 创建了一个名为
AbstractEnum
的类。 Strings
类现在可以无缝地处理多字节字符串。- 将
Strings::transformCamelCaseToUnderscore
重命名为Strings::camelCaseToUnderscore
。 - 将
Strings::transformUnderscoreToCamelCase
重命名为Strings::underscoreToCamelCase
。 - 向
Strings
类添加了新的方法Strings::startsWith
.Strings::endsWith
.Strings::startsUpper
.Strings::startsLower
.Strings::replace
.Strings::length
.Strings::chars
.Strings::toUpper
.Strings::toLower
.Strings::ucFirst
.Strings::lcFirst
.Strings::title
.Strings::sub
.Strings::split
.Strings::transform
.
v0.2.3 - 2014/03/25
- 将最低 PHP 版本要求提高至 5.5.0。ClassName::class 永远长存!
- 添加了
Strings::quote
方法。 - 添加了
Arrays::conjunct
方法。 - 添加了
Functions::swapCallable
方法。 - 添加了
Validator
类。
v0.2.2 - 2014/03/24
- 添加了
Arrays::findString
方法。
v0.2.1 - 2014/03/24
- 将
Complete::invoke
的可见性改为公共。
v0.2 - 2014/03/24
- 添加了
Complete
类。
v0.1 - 2014/03/23
- 首次以 MIT 许可证发布版本。
待办事项
- 将
Strings
常量替换为枚举。
许可证
此内容根据 MIT 许可证发布。有关更多信息,请参阅包含的 LICENSE 文件。