oasis/utils
Composer 安装命令:
composer require oasis/utils
包简介
Various utility classes & functions
README 文档
README
This component provides a number of PHP helper classes for common tasks.
- Data Provider
- Streamed Data Packer
- Caesar Cipher
- RC4 encryption/decryption
- String Utilities
- Memory Monitor Tool
Installation
Install the latest version with command below:
$ composer require oasis/utils
Data Provider
Normally we use data provider to create a validatable container.
Oasis\Mlib\Utils\ArrayDataProvider is probably the most used.
An example:
<?php use Oasis\Mlib\Utils\ArrayDataProvider; use Oasis\Mlib\Utils\DataProviderInterface; $data = [ "int-key" => 10, "string-key" => "name", "switch" => "true", "wrong-type" => "hello", ]; $dp = new ArrayDataProvider($data); // $i = 10 $i = $dp->getMandatory('int-key', DataProviderInterface::INT_TYPE); // throws Oasis\Mlib\Utils\Exceptions\MandatoryValueMissingException $i = $dp->getMandatory('int-key2', DataProviderInterface::INT_TYPE); // $i = 5 $i = $dp->getOptional('int-key2', DataProviderInterface::INT_TYPE, 5); // $s = 'name'; $s = $dp->getMandatory('string-key', DataProviderInterface::STRING_TYPE); // $onoff = true; $onoff = $dp->getOptional('switch', DataProviderInterface::BOOL_TYPE, false); // throws Oasis\Mlib\Utils\Exceptions\InvalidDataTypeException $wrongType = $dp->getMandatory('wrong-type', DataProviderInterface::BOOL_TYPE);
Data Packer
When you have some objects that should be write to or read from a stream
(file, network), Oasis\Mlib\Utils\DataPacker is just for you.
Below is an example using Data Packer:
<?php use Oasis\Mlib\Utils\DataPacker; $obj = new stdClass(); $tmpfile = tempnam(sys_get_temp_dir(), ''); $packer = new DataPacker(); $fh = fopen($tmpfile, 'w'); $packer->attachStream($fh); $packer->packToStream($obj); $packer->packToStream($obj); $packer->packToStream($obj); fclose($fh); $fh = fopen($tmpfile, 'r'); $packer->attachStream($fh); while ($obj = $packer->unpackFromStream()) { // we should have 3 $obj unpacked from stream }
Caesar Cipher
Caesar Cipher is one of the earliest known and simplest ciphers. It is a type of substitution cipher in which each letter in the plaintext is 'shifted' a certain number of places down the alphabet.
oasis/utils provides easy cipher class for Caesar Cipher:
<?php use Oasis\Mlib\Utils\CaesarCipher; $cipher = new CaesarCipher(); // encrypt and decrypt integer $enc = $cipher->encrypt(1234); $dec = $cipher->decrypt($enc); // $dec = 1234 // encrypt and decrypt string $enc = $cipher->encrypt("abcdefg"); $dec = $cipher->decrypt($enc); // $dec = "abcdefg" // using stronger cipher $cipher = new CaesarCipher( 32, // bits to use in partition, default to 32, must be divisable by partition size 8, // partition size, even positive number, default to 8 12 // strength, positive number, default to 5 );
RC4
RC4 is a very simple stream cipher. oasis/utils provides quick access to RC4 encryption. Below is an example:
<?php use Oasis\Mlib\Utils\Rc4; $encrypted = Rc4::rc4('random-key', 'abc'); // to decrypt, call the encrypt function with same key $decrypted = Rc4::rc4('random-key', $encrypted); // $decrypted = 'abc'
String Utils
Oasis\Mlib\Utils\StringUtils class provides some simple string related functions:
<?php use Oasis\Mlib\Utils\StringUtils; $str = 'abcdefg'; var_dump(StringUtils::stringStartsWith($str, 'a')); // true var_dump(StringUtils::stringStartsWith($str, 'b')); // false var_dump(StringUtils::stringEndsWith($str, 'g')); // true var_dump(StringUtils::stringEndsWith($str, 'f')); // false var_dump(StringUtils::stringChopdown($str, 4)); // 'abcd'
Memory Usage Monitor
Sometimes, PHP script can run out of memory. It is especially important to monitor memory usage is a long-running script. oasis/utils provides some tools for memory usage monitor and management on-the-fly.
Example:
<?php use Oasis\Mlib\Utils\CommonUtils; // check the current memory usage and increase if needed CommonUtils::monitorMemoryUsage(); // if the script declares tick, this will monitor memory usage every time tick is triggered CommonUtils::registerMemoryMonitorForTick();
Tick is a declare directive which PHP supports to monitor low-level code execution.
oasis/utils 适用场景与选型建议
oasis/utils 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 187.84k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 12 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 oasis/utils 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 oasis/utils 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 187.84k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-12-04