protonlabs/libsieve-php
Composer 安装命令:
composer require protonlabs/libsieve-php
包简介
libsieve-php is a library to manage and modify sieve (RFC5228) scripts.
README 文档
README
libsieve-php is a library to manage and modify sieve (RFC5228) scripts. It contains a parser for the sieve language (including extensions).
This project is adopted from the discontinued PHP sieve library available at https://sourceforge.net/projects/libsieve-php.
Changes from the RFC
- The
dateand thecurrentdateboth allow forzoneparameter any string to be passed. This allows the user to enter zone names likeEurope/Zurichinstead of+0100. The reason we allow this is because offsets like+0100don't encode information about the daylight saving time, which is often needed.
Usage examples
The libsieve parses a sieve script into a tree. This tree can then be used to interpret its meaning.
Two example will be provided: one basic and one more complex.
Basic Example: Check if an extension is loaded
In this first example, we will check if a specific extension was loaded through a require node:
<?php use Sieve\SieveParser; class ExtensionCheckExample { /** @var \Sieve\SieveTree the tree, obtained from the SieveParser */ protected $tree; /** * Parser constructor. * * @param string $sieve the sieve script to read. */ public function __construct(string $sieve) { $parser = new SieveParser(); try { $parser->parse($sieve); } catch (\Sieve\SieveException $se) { throw new \Exception("The provided sieve script is invalid!"); } // we store the tree, because it contains all the information. $this->tree = $parser->GetParseTree(); } /** * Checks if an extension is loaded. * * @param string $extension * @return bool */ public function isLoaded(string $extension) { /** @var int $root_node_id */ $root_node_id = $this->tree->getRoot(); // The root is not a node, we can only access its children $children = $this->tree->getChildren($root_node_id); foreach ($children as $child) { // The child is an id to a node, which can be access using the following: $node = $this->tree->getNode($child); // is can be used to check the type of node. if ($node->is(\Sieve\SieveToken::IDENTIFIER) && $node->text === "require") { if ($this->checkChildren($this->tree->getChildren($child), $extension)) { return true; } } } return false; } /** * Checks the arguments given to a require node, to know if it includes * * @param $children * @param string $extension * @return bool */ private function checkChildren($children, string $extension): bool { if (is_array($children)) { // it's a string list, let's loop over them. foreach ($children as $child) { if ($this->checkChildren($child, $extension)) { return true; } } return false; } $node = $this->tree->getNode($children); return $node->is(\Sieve\SieveToken::QUOTED_STRING) && $extension === trim($node->text, '"'); } } // load a script, from the tests folder. $sieve = file_get_contents(__DIR__ . './tests/good/currentdate.siv'); $runner = new ExtensionCheckExample($sieve); var_dump($runner->isLoaded("variables"));
Complex Example: Dumping the tree
This second example will print back the sieve script from a parsed tree (note this could simply be done through
the method $tree->dump()).
<?php use Sieve\SieveParser; use Sieve; class UsageExample { /** @var \Sieve\SieveTree the tree, obtained from the SieveParser */ protected $tree; /** * Parser constructor. * * @param string $sieve the sieve script to read. */ public function __construct(string $sieve) { $parser = new SieveParser(); try { $parser->parse($sieve); } catch (\Sieve\SieveException $se) { throw new \Exception("The provided sieve script is invalid!"); } // we store the tree, because it contains all the information. $this->tree = $parser->GetParseTree(); } /** * Displays the tree */ public function display() { /** @var int $root_node_id */ $root_node_id = $this->tree->getRoot(); // The root is not a node, we can only access its children $children = $this->tree->getChildren($root_node_id); $this->displayNodeList($children); } /** * Loop over a list of nodes, and display them. * * @param int[] $nodes a list of node ids. * @param string $indent */ private function displayNodeList(array $nodes, string $indent = '') { foreach ($nodes as $node) { $this->displayNode($node, $indent); } } /** * Display a node and its children. * * @param int $node_id the current node id. * @param string $indent */ private function displayNode(int $node_id, string $indent) { /** * @var \Sieve\SieveToken $node can be used to get info about a specific node. */ $node = $this->tree->getNode($node_id); // All the possible node types are listed as constants in the class SieveToken... switch ($node->type) { case \Sieve\SieveToken::SCRIPT_END: printf($indent . "EOS"); break; case Sieve\SieveToken::WHITESPACE: case Sieve\SieveToken::COMMENT: break; default: // the $node->type is a integer. It can be turned into an explicit string this way... $type = \Sieve\SieveToken::typeString($node->type); $open_color = ''; $end_color = ''; // The type of a node can be checked with the is method. Mask can be used to match several types. if ($node->is(\Sieve\SieveToken::QUOTED_STRING | Sieve\SieveToken::MULTILINE_STRING)) { // we want to put a specific color arround strings... $open_color = "\e[1;32;47m"; $end_color = "\e[0m"; } // The children of a node can be obtain through this method: $children = $this->tree->getChildren($node_id); // do whatever you want with a node and its children :) Here we are going to display them. printf("[%4d, %-10.10s (%5d) ]%s ${open_color}%s$end_color" . PHP_EOL, $node->line, $type, $node->type, $indent, $node->text); $this->displayNodeList($children, $indent . "\t"); } } } $sieve = file_get_contents(__DIR__ . '/tests/good/currentdate.siv'); $parser = new UsageExample($sieve); $parser->display();
protonlabs/libsieve-php 适用场景与选型建议
protonlabs/libsieve-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 78.09k 次下载、GitHub Stars 达 23, 最近一次更新时间为 2018 年 12 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「mail」 「filters」 「sieve」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 protonlabs/libsieve-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 protonlabs/libsieve-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 protonlabs/libsieve-php 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Lightweight Open Source webmail written in PHP and JavaScript
A modern (started in 2022) PHP library for the ManageSieve protocol (RFC5804) to create/edit Sieve scripts (RFC5228). Used by Cypht Webmail.
Simple filters for laravel
Livewire Filters is a series of Livewire components that provide you with the tools to do live filtering of your data from your own Livewire components.
A lightweight WordPress hook helper library. Register hooks before WordPress loads, run callbacks only once, and more.
Courier offers a convenient and painless solution for creating emails tailored for your Kirby website.
统计信息
- 总下载量: 78.09k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 23
- 点击次数: 25
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-3.0-or-later
- 更新时间: 2018-12-13