ansas / php-component
PHP 组件 - 跨项目 PHP 类的集合
Requires
- php: ^8.1
- ext-ctype: *
- ext-iconv: *
- ext-intl: *
- ext-json: *
- ext-libxml: *
- ext-mbstring: *
- ext-simplexml: *
Requires (Dev)
- phpunit/phpunit: ^9
Suggests
- monolog/monolog: Needed when using Ansas\Monolog classes
- dev-main
- 1.5.0
- 1.4.11
- 1.4.10
- 1.4.9
- 1.4.8
- 1.4.7
- 1.4.6
- 1.4.5
- 1.4.4
- 1.4.3
- 1.4.2
- 1.4.1
- 1.4.0
- 1.3.0
- 1.2.9
- 1.2.8
- 1.2.7
- 1.2.6
- 1.2.5
- 1.2.4
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.14
- 1.1.13
- 1.1.12
- 1.1.11
- 1.1.10
- 1.1.9
- 1.1.8
- 1.1.7
- 1.1.6
- 1.1.5
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.9
- 1.0.8
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
This package is auto-updated.
Last update: 2024-09-14 09:44:08 UTC
README
跨项目 PHP 类的集合。
安装
$ composer require ansas/php-component
Ansas\Component\Convert\ConvertPrice
将“脏”价格转换为欧元或分
方法(不包括内部方法)
public function __construct($price = null, $format = self::EURO) public function __toString() public function clearPrice() public function getPrice($format = self::EURO) public function setPrice($price, $format = self::EURO) public function sanitize($price, $format = self::EURO) final public static function getInstance()
Ansas\Component\Collection\Collection
使处理上下文数据变得更容易。
使用方法
use Ansas\Component\Collection\Collection; // Create e. g. context $context = new Collection([ 'key1' => 'value1', ]); // Setter $context->key2 = 'value2'; $context['key3'] = 'value3'; $context->set('key4', 'value4'); $context->append([ 'key4', 'value4', 'key5', 'value5', ]); // Getter $key1 = $context->get('key1'); $key2 = $context['key2']; $key3 = $context->key3; $key4 = $context->need('key6'); // throws Exception if key does not exist // Getter (array) $array = $context->all(); $partArray1 = $context->only('key1,key3'); $partArray2 = $context->only(['key2', 'key4']); // Count $elemets = $context->count(); $elemets = count($context); // Delete $context->remove('key1'); unset($context->key1); unset($context['key1']); // Delete (extended) $context->replace([ 'key4', 'value4', 'key5', 'value5', ]); // replaces all existing elements with specified elements $context->clear(); // deletes all elements // Check if ($context->has('key1')) {} if (isset($context->key1)) {} if (isset($context['key1'])) {} // Iterate foreach ($context as $key => $value) {} foreach ($context->getIterator() as $key => $value) {} // Special $context->add('key6', 'value1'); // element key6 is string $context->add('key6', 'value2'); // key6 is converted to array automatically $keyArray = $context->keys(); // new numeric array containing keys only $valueArray = $context->values(); // new numeric array containing values only
Ansas\Component\Convert\ConvertToNull
此特质可以用于“清理值”,通过将空值设置为null。
它包含可以在调用父类设置器方法之前检查字段是否为空并将其设置为“null”的方法。通过这种方式,我们得到了一个更整洁的对象,并且也防止了例如“可版本化”的行为添加新版本(因为 "" !== null)。
方法
protected function convertEmptyToNull($value, array $considerNull = ['']) protected function convertToNull($value, array $considerNull, $trim = false) protected function trimAndConvertEmptyToNull($value, array $considerNull = ['']) protected function trimAndConvertToNull($value, array $considerNull)
Ansas\Component\Session\ThriftyFileSession
您只需像往常一样配置原生 PHP 会话设置即可(请参阅 https://php.ac.cn/manual/de/session.configuration.php)。
之后,加载此类并调用静态 init() 方法:Ansas\Component\Session\ThriftyFileSession::init();
这将自动启动会话,您可以使用原生的会话处理函数和超级全局变量 $_SESSION 如常使用。
此类的优点在于,仅在 $_SESSION 有数据时才设置磁盘上的会话存储和会话 cookie。此外,会话 cookie 也会在每次请求时更新,因此它不会在 session.gc_maxlifetime 之前过期,并且如果您销毁会话,cookie 将会自动删除(这与纯原生 PHP 会话不同)。
方法(不包括内部方法)
public static function getInstance() public static function init() public static function force(boolean $force) public static function ttl(int $ttl) public static function cleanup(callable $cleanup) public static function kill()
Ansas\Monolog\Profiler
一个用于记录到任何 Monolog 日志记录器的不同配置文件的简单分析器(计时器)。
方法(不包括内部方法)
// object handling methods public function __construct(Logger $logger, $level = Logger::DEBUG, callable $formatter = null) public function __destruct() public function __get($profile) public function __toString() // profile methods public function add($profile) public function context(Collection $context = null) public function get($profile) public function has($profile) public function remove($profile) public function removeAll() public function set($profile) // stopwatch methods public function start($message = 'Start', $context = []) public function start($context = []) public function startAll($message = 'Start', $context = []) public function lap($message = 'Lap', $context = []) public function lap($context = []) public function stop($message = 'Stop', $context = []) public function stop($context = []) public function stopAll($message = 'Stop', $context = []) public function note($message = 'Note', $context = []) public function note($context = []) public function clear() public function clearAll() public function restart($message = 'Restart', $context = []) public function restart($context = []) // setter methods public function setLogger(Logger $logger) public function setLevel($level) public function setFormatter(callable $formatter = null) // time methods public function timeCurrent() public function timeStart() public function timeStop() public function timeTotal() public function timeLap($lap = -1) // default methods public function defaultFormatter() // helper methods public function countLaps() public function getFormatter() public function getLaps() public function getName() public function getProfiles() public function isRunning() public function isStarted() public function isStopped()
使用方法
use Ansas\Monolog\Profiler; $profiler = new Profiler($logger); // Starts default profile (must be started before any other profiles) $profiler->start(); sleep(1); // Adds profile "testA" and logs default message "Start" $profiler->add("testA")->start(); sleep(1); // Gets profile "testA" and adds a lap incl. logging it with default message // "Lap" $profiler->get("testA")->lap(); // Adds profile "anotherB" (add always returns new new profile) $anotherB = $profiler->add("anotherB"); // Starts profile "anotherB" with individual message "B is rolling" $anotherB->start("B is rolling"); sleep(1); // Add lap for default profile with individual message $profiler->lap("Lap for main / default profile"); // Stop profile "anotherB" and log with default message "Stop" $anotherB->stop(); // Add subprofile "moreC" to profile "anotherB" and start with individual // message and context array (default Monolog / PSR3 signature) $moreC = $anotherB->add("moreC")->start("Message", ['key' => 1]); // Add profile "lastD" to "anotherB" as well $lastD = $profiler->get("anotherB")->add("lastD"); // Just log a note to profile "lastD" $lastD->note("Starting soon"); sleep(1); // Clear only profile "anotherB" and start it again $anotherB->clear()->start("Restarting after clear"); // Stopping "testA" (will not be restarted with upcomming "startAll") $testA = $profiler->get("testA"); $testA->stop(); // Just add a profile without doing anything (will be started with startAll) $profiler->add("test123"); // Start all profiles incl. default that are not startet $profiler->startAll("Starting all profiles not started yet"); // Add "veryLastE" to "lastD" but do not log it (as message is null) $veryLastE = $lastD->add("veryLastE")->start(null); sleep(1); // Some more profiling $profiler->get("anotherB")->get("moreC")->lap("Lapdance ;P"); $veryLastE->stop("Stopping E"); // Make things easier, just create profiles via set() or magic method __get() $profiler->set("profilX")->start(); $profiler->set("profilX")->lap(); $profiler->profilX->lap(); $profiler->profilX->stop(); sleep(1); // Clear "lastD" all subprofiles (no logging will be done now or on exit) $lastD->clearAll("Clearing D and all subprofiles"); // Stops all counters (done automatically on script end) $anotherB->stopAll(); // Display current profiling status / report echo $profiler; // All running profiles are lapped (if needed), stopped and logged if profiler // is destroyed or program ends $profiler = null; gc_collect_cycles(); sleep(3); exit;
Ansas\Monolog\Processor\ConsoleColorProcessor
通过处理器为 Monolog 添加颜色,以便在控制台输出。此处理器通过 $record
部分对 level_name
和 message
进行着色
使用方法
use Ansas\Monolog\Processor\ConsoleColorProcessor; use Monolog\Logger; use Monolog\Handler\StreamHandler; use Monolog\Formatter\LineFormatter; $loggerFormat = "[%datetime%] %level_name% %message% %context%\n"; $loggerLevel = getenv('DEBUG') ? Logger::DEBUG : Logger::NOTICE; $loggerTimeZone = new DateTimeZone('Europe/Berlin'); $formatter = new LineFormatter($loggerFormat, $loggerTimeFormat); $formatter->ignoreEmptyContextAndExtra(true); $defaultHandler = new StreamHandler('php://stdout', $loggerLevel, $bubble = false); $defaultHandler->setFormatter($formatter); $errorHandler = new StreamHandler('php://stderr', Logger::ERROR, $bubble = false); $errorHandler->setFormatter($formatter); $logger = new Logger('console'); $logger->pushHandler($defaultHandler); $logger->pushHandler($errorHandler); $logger->pushProcessor(new ConsoleColorProcessor()); $logger->useMicrosecondTimestamps(true); $logger->debug(str_repeat("Xx ", rand(5, 40))); $logger->info(str_repeat("Xx ", rand(5, 40))); $logger->notice(str_repeat("Xx ", rand(5, 40))); $logger->warning(str_repeat("Xx ", rand(5, 40))); $logger->error(str_repeat("Xx ", rand(5, 40))); $logger->critical(str_repeat("Xx ", rand(5, 40))); $logger->alert(str_repeat("Xx ", rand(5, 40))); $logger->emergency(str_repeat("Xx ", rand(5, 40)));
待办事项
- 编写测试
贡献
每个人都可以为此包做出贡献。只需
- 将其分叉,
- 进行您的更改,并
- 发送拉取请求。
许可
MIT 许可证(有关更多信息,请参阅 LICENSE 文件)。