From 11fddfdd4b8328fde80b1b0eae03f2d6e22f183b Mon Sep 17 00:00:00 2001 From: Yurun Date: Wed, 15 Nov 2023 11:00:35 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A6=96=E6=AC=A1=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitattributes | 4 + .github/docker-compose.yml | 23 +++++ .github/swoole.dockerfile | 4 + .github/workflows/ci.yml | 31 +++++++ .github/workflows/phpcs.yml | 22 +++++ .github/workflows/phpstan.yml | 29 ++++++ .gitignore | 8 ++ .php-cs-fixer.php | 123 +++++++++++++++++++++++++ LICENSE | 87 +++++++++++++++++ README.md | 128 ++++++++++++++++++++++++++ composer.json | 34 +++++++ init.php | 10 ++ phpstan.neon | 19 ++++ phpunit.xml | 13 +++ src/Annotation/EnumItem.php | 24 +++++ src/Annotation/Parser/EnumParser.php | 23 +++++ src/BaseEnum.php | 100 ++++++++++++++++++++ src/EnumManager.php | 79 ++++++++++++++++ src/Listener/BuildRuntimeListener.php | 28 ++++++ src/Listener/LoadRuntimeListener.php | 27 ++++++ tests/Enum/TestEnum.php | 20 ++++ tests/EnumTest.php | 47 ++++++++++ tests/PHPUnitHook.php | 34 +++++++ 23 files changed, 917 insertions(+) create mode 100644 .gitattributes create mode 100644 .github/docker-compose.yml create mode 100644 .github/swoole.dockerfile create mode 100644 .github/workflows/ci.yml create mode 100644 .github/workflows/phpcs.yml create mode 100644 .github/workflows/phpstan.yml create mode 100644 .gitignore create mode 100644 .php-cs-fixer.php create mode 100644 LICENSE create mode 100644 README.md create mode 100644 composer.json create mode 100644 init.php create mode 100644 phpstan.neon create mode 100644 phpunit.xml create mode 100644 src/Annotation/EnumItem.php create mode 100644 src/Annotation/Parser/EnumParser.php create mode 100644 src/BaseEnum.php create mode 100644 src/EnumManager.php create mode 100644 src/Listener/BuildRuntimeListener.php create mode 100644 src/Listener/LoadRuntimeListener.php create mode 100644 tests/Enum/TestEnum.php create mode 100644 tests/EnumTest.php create mode 100644 tests/PHPUnitHook.php diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..0e302cf --- /dev/null +++ b/.gitattributes @@ -0,0 +1,4 @@ +/.gitattributes export-ignore +/.gitignore export-ignore +/.github export-ignore +/tests export-ignore diff --git a/.github/docker-compose.yml b/.github/docker-compose.yml new file mode 100644 index 0000000..734b559 --- /dev/null +++ b/.github/docker-compose.yml @@ -0,0 +1,23 @@ +version: "3.4" +volumes: + shared-volume: +services: + swoole: + container_name: "swoole" + environment: + REDIS_SERVER_HOST: ${REDIS_SERVER_HOST} + build: + context: . + dockerfile: swoole.dockerfile + args: + SWOOLE_DOCKER_VERSION: ${SWOOLE_DOCKER_VERSION} + volumes: + - "${GITHUB_WORKSPACE}:/imi:rw" + working_dir: /imi + command: tail -f /dev/null + + redis: + image: redis:6-alpine + container_name: redis + ports: + - 6379:6379 diff --git a/.github/swoole.dockerfile b/.github/swoole.dockerfile new file mode 100644 index 0000000..502d768 --- /dev/null +++ b/.github/swoole.dockerfile @@ -0,0 +1,4 @@ +ARG SWOOLE_DOCKER_VERSION + +FROM phpswoole/swoole:${SWOOLE_DOCKER_VERSION} + diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..af63a66 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,31 @@ +name: ci + +on: [push, pull_request] + +jobs: + ci: + name: Linux Swoole-${{ matrix.swoole }} + runs-on: ubuntu-20.04 + strategy: + fail-fast: false + matrix: + swoole: [5.1-php8.2, 5.1-php8.1, 5.0-php8.2, 5.0-php8.1] + env: + SWOOLE_DOCKER_VERSION: ${{ matrix.swoole }} + REDIS_SERVER_HOST: redis + steps: + - name: Checkout + uses: actions/checkout@v2 + - name: Prepare + run: | + docker-compose -f ./.github/docker-compose.yml up -d + docker exec swoole php -v + docker exec swoole php -m + docker exec swoole php --ri swoole + docker exec swoole composer -V + docker exec swoole composer update --no-interaction --prefer-dist --no-progress + - name: Test + run: docker exec swoole composer test + - name: Print logs + if: failure() + run: cat example/.runtime/logs/*.log diff --git a/.github/workflows/phpcs.yml b/.github/workflows/phpcs.yml new file mode 100644 index 0000000..04be608 --- /dev/null +++ b/.github/workflows/phpcs.yml @@ -0,0 +1,22 @@ +name: Coding Standards + +on: [push, pull_request] + +jobs: + tests: + runs-on: ubuntu-20.04 + env: + SWOOLE_DOCKER_VERSION: 4.8-php8.0 + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Prepare + run: | + docker-compose -f ./.github/docker-compose.yml up -d swoole + docker exec swoole php -v + docker exec swoole composer -V + docker exec swoole composer update --no-interaction --prefer-dist --no-progress + + - name: Coding Standards Tests + run: docker exec swoole ./vendor/bin/php-cs-fixer fix --dry-run --diff diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml new file mode 100644 index 0000000..89b95b6 --- /dev/null +++ b/.github/workflows/phpstan.yml @@ -0,0 +1,29 @@ +name: Static Code Analysis + +on: [push, pull_request] + +jobs: + tests: + runs-on: ubuntu-20.04 + strategy: + fail-fast: false + matrix: + swoole-docker: [4.8-php8.0] + env: + SWOOLE_DOCKER_VERSION: ${{ matrix.swoole-docker }} + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Prepare + run: | + docker-compose -f ./.github/docker-compose.yml up -d swoole + docker exec swoole php -v + docker exec swoole php -m + docker exec swoole php --ri swoole + docker exec swoole composer -V + docker exec swoole composer update --no-interaction --prefer-dist --no-progress + + - name: Analyse + run: | + docker exec swoole ./vendor/bin/phpstan analyse --memory-limit 1G diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3a1ef04 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +vendor +composer.lock +/ide-helper +.runtime +*.log +*.pid +*.cache +/.vscode \ No newline at end of file diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php new file mode 100644 index 0000000..c9589af --- /dev/null +++ b/.php-cs-fixer.php @@ -0,0 +1,123 @@ +setRules([ + '@Symfony' => true, + '@Symfony:risky' => true, + 'php_unit_dedicate_assert' => ['target' => '5.6'], + 'array_syntax' => ['syntax' => 'short'], + 'array_indentation' => true, + 'binary_operator_spaces' => [ + 'operators' => [ + '=>' => 'align_single_space', + ], + ], + 'concat_space' => [ + 'spacing' => 'one', + ], + 'fopen_flags' => false, + 'protected_to_private' => false, + 'native_function_invocation' => true, + 'native_constant_invocation' => true, + 'combine_nested_dirname' => true, + 'single_quote' => true, + 'single_space_around_construct' => [ + 'constructs_followed_by_a_single_space' => [ + 'abstract', + 'as', + 'attribute', + 'break', + 'case', + 'catch', + 'class', + 'clone', + 'comment', + 'const', + 'const_import', + 'continue', + 'do', + 'echo', + 'else', + 'elseif', + 'enum', + 'extends', + 'final', + 'finally', + 'for', + 'foreach', + 'function', + 'function_import', + 'global', + 'goto', + 'if', + 'implements', + 'include', + 'include_once', + 'instanceof', + 'insteadof', + 'interface', + 'match', + 'named_argument', + // 'namespace', // 兼容性移除 + 'new', + 'open_tag_with_echo', + 'php_doc', + 'php_open', + 'print', + 'private', + 'protected', + 'public', + 'readonly', + 'require', + 'require_once', + 'return', + 'static', + 'switch', + 'throw', + 'trait', + 'try', + 'type_colon', + 'use', + 'use_lambda', + 'use_trait', + 'var', + 'while', + 'yield', + 'yield_from', + ], + ], + 'control_structure_continuation_position' => [ + 'position' => 'next_line', + ], + 'braces' => [ + 'position_after_control_structures' => 'next', + ], + 'single_line_comment_style' => false, + 'phpdoc_to_comment' => false, + 'declare_strict_types' => true, + 'heredoc_indentation' => [ + 'indentation' => 'same_as_start', + ], + 'no_trailing_whitespace_in_string' => false, + // Symfony 冲突 + 'curly_braces_position' => [ + 'control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end', + ], + 'nullable_type_declaration_for_default_null_value' => false, + 'no_superfluous_phpdoc_tags' => [ + 'allow_mixed' => true, + 'allow_unused_params' => false, + 'remove_inheritdoc' => false, + ], + 'no_null_property_initialization' => false, + // 当支持 php>=8.0 后改配置可以移除 + 'get_class_to_class_keyword' => false, + ]) + ->setRiskyAllowed(true) + ->setFinder( + PhpCsFixer\Finder::create() + ->in(__DIR__) + ) +; diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..ff6118b --- /dev/null +++ b/LICENSE @@ -0,0 +1,87 @@ +木兰宽松许可证, 第2版 + +2020年1月 http://license.coscl.org.cn/MulanPSL2 + +您对“软件”的复制、使用、修改及分发受木兰宽松许可证,第2版(“本许可证”)的如下条款的约束: + +0. 定义 + +“软件” 是指由“贡献”构成的许可在“本许可证”下的程序和相关文档的集合。 + +“贡献” 是指由任一“贡献者”许可在“本许可证”下的受版权法保护的作品。 + +“贡献者” 是指将受版权法保护的作品许可在“本许可证”下的自然人或“法人实体”。 + +“法人实体” 是指提交贡献的机构及其“关联实体”。 + +“关联实体” 是指,对“本许可证”下的行为方而言,控制、受控制或与其共同受控制的机构,此处的控制是指有受控方或共同受控方至少50%直接或间接的投票权、资金或其他有价证券。 + +1. 授予版权许可 + +每个“贡献者”根据“本许可证”授予您永久性的、全球性的、免费的、非独占的、不可撤销的版权许可,您可以复制、使用、修改、分发其“贡献”,不论修改与否。 + +2. 授予专利许可 + +每个“贡献者”根据“本许可证”授予您永久性的、全球性的、免费的、非独占的、不可撤销的(根据本条规定撤销除外)专利许可,供您制造、委托制造、使用、许诺销售、销售、进口其“贡献”或以其他方式转移其“贡献”。前述专利许可仅限于“贡献者”现在或将来拥有或控制的其“贡献”本身或其“贡献”与许可“贡献”时的“软件”结合而将必然会侵犯的专利权利要求,不包括对“贡献”的修改或包含“贡献”的其他结合。如果您或您的“关联实体”直接或间接地,就“软件”或其中的“贡献”对任何人发起专利侵权诉讼(包括反诉或交叉诉讼)或其他专利维权行动,指控其侵犯专利权,则“本许可证”授予您对“软件”的专利许可自您提起诉讼或发起维权行动之日终止。 + +3. 无商标许可 + +“本许可证”不提供对“贡献者”的商品名称、商标、服务标志或产品名称的商标许可,但您为满足第4条规定的声明义务而必须使用除外。 + +4. 分发限制 + +您可以在任何媒介中将“软件”以源程序形式或可执行形式重新分发,不论修改与否,但您必须向接收者提供“本许可证”的副本,并保留“软件”中的版权、商标、专利及免责声明。 + +5. 免责声明与责任限制 + +“软件”及其中的“贡献”在提供时不带任何明示或默示的担保。在任何情况下,“贡献者”或版权所有者不对任何人因使用“软件”或其中的“贡献”而引发的任何直接或间接损失承担责任,不论因何种原因导致或者基于何种法律理论,即使其曾被建议有此种损失的可能性。 + +6. 语言 + +“本许可证”以中英文双语表述,中英文版本具有同等法律效力。如果中英文版本存在任何冲突不一致,以中文版为准。 + +条款结束 + +Mulan Permissive Software License,Version 2 (Mulan PSL v2) + +January 2020 http://license.coscl.org.cn/MulanPSL2 + +Your reproduction, use, modification and distribution of the Software shall be subject to Mulan PSL v2 (this License) with the following terms and conditions: + +0. Definition + +Software means the program and related documents which are licensed under this License and comprise all Contribution(s). + +Contribution means the copyrightable work licensed by a particular Contributor under this License. + +Contributor means the Individual or Legal Entity who licenses its copyrightable work under this License. + +Legal Entity means the entity making a Contribution and all its Affiliates. + +Affiliates means entities that control, are controlled by, or are under common control with the acting entity under this License, ‘control’ means direct or indirect ownership of at least fifty percent (50%) of the voting power, capital or other securities of controlled or commonly controlled entity. + +1. Grant of Copyright License + +Subject to the terms and conditions of this License, each Contributor hereby grants to you a perpetual, worldwide, royalty-free, non-exclusive, irrevocable copyright license to reproduce, use, modify, or distribute its Contribution, with modification or not. + +2. Grant of Patent License + +Subject to the terms and conditions of this License, each Contributor hereby grants to you a perpetual, worldwide, royalty-free, non-exclusive, irrevocable (except for revocation under this Section) patent license to make, have made, use, offer for sale, sell, import or otherwise transfer its Contribution, where such patent license is only limited to the patent claims owned or controlled by such Contributor now or in future which will be necessarily infringed by its Contribution alone, or by combination of the Contribution with the Software to which the Contribution was contributed. The patent license shall not apply to any modification of the Contribution, and any other combination which includes the Contribution. If you or your Affiliates directly or indirectly institute patent litigation (including a cross claim or counterclaim in a litigation) or other patent enforcement activities against any individual or entity by alleging that the Software or any Contribution in it infringes patents, then any patent license granted to you under this License for the Software shall terminate as of the date such litigation or activity is filed or taken. + +3. No Trademark License + +No trademark license is granted to use the trade names, trademarks, service marks, or product names of Contributor, except as required to fulfill notice requirements in section 4. + +4. Distribution Restriction + +You may distribute the Software in any medium with or without modification, whether in source or executable forms, provided that you provide recipients with a copy of this License and retain copyright, patent, trademark and disclaimer statements in the Software. + +5. Disclaimer of Warranty and Limitation of Liability + +THE SOFTWARE AND CONTRIBUTION IN IT ARE PROVIDED WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED. IN NO EVENT SHALL ANY CONTRIBUTOR OR COPYRIGHT HOLDER BE LIABLE TO YOU FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO ANY DIRECT, OR INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES ARISING FROM YOUR USE OR INABILITY TO USE THE SOFTWARE OR THE CONTRIBUTION IN IT, NO MATTER HOW IT’S CAUSED OR BASED ON WHICH LEGAL THEORY, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + +6. Language + +THIS LICENSE IS WRITTEN IN BOTH CHINESE AND ENGLISH, AND THE CHINESE VERSION AND ENGLISH VERSION SHALL HAVE THE SAME LEGAL EFFECT. IN THE CASE OF DIVERGENCE BETWEEN THE CHINESE AND ENGLISH VERSIONS, THE CHINESE VERSION SHALL PREVAIL. + +END OF THE TERMS AND CONDITIONS \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..3951e40 --- /dev/null +++ b/README.md @@ -0,0 +1,128 @@ +# imi-email-blacklist + +[![Latest Version](https://img.shields.io/packagist/v/imiphp/imi-email-blacklist.svg)](https://packagist.org/packages/imiphp/imi-email-blacklist) +[![Php Version](https://img.shields.io/badge/php-%3E=7.4-brightgreen.svg)](https://secure.php.net/) +[![Swoole Version](https://img.shields.io/badge/swoole-%3E=4.8.0-brightgreen.svg)](https://github.com/swoole/swoole-src) +[![imi License](https://img.shields.io/badge/license-MulanPSL%202.0-brightgreen.svg)](https://github.com/imiphp/imi-email-blacklist/blob/master/LICENSE) + +## 介绍 + +imi 框架邮箱域名黑名单组件,支持自动更新临时邮箱域名列表。 + +## 安装 + +`composer require imiphp/imi-email-blacklist:~2.1.0` + +## 使用说明 + +### 数据更新 + +**配置:** + +`@app.beans`: + +```php +[ + \Imi\Email\BlackList\EmailBlackListHandler::class => [ + 'handler' => \Imi\Email\BlackList\Handler\RedisHandler::class, // 数据处理器,目前仅支持 Redis,你也可以实现 \Imi\Email\BlackList\IHandler 接口自定义处理器 + ], + \Imi\Email\BlackList\EmailBlackListCrawler::class => [ + 'enable' => true, // 是否启用 + // 采集器列表 + // 你也可以实现 \Imi\Email\BlackList\Contract\IEmailBlackListCrawler 接口自定义采集器 + 'crawlers' => [ + \Imi\Email\BlackList\IvoloDisposableEmailDomainsCrawler::class, // 数据来源:https://github.com/ivolo/disposable-email-domains/raw/master/index.json + ], + ], + // handler 配置 + \Imi\Email\BlackList\Handler\RedisHandler::class => [ + 'key' => 'imi:email:blacklist', // 存储数据的键名 + 'poolName' => null, // 连接池 + ], +] +``` + +> 上面展示的是默认配置,默认不配置也可以使用 + +**定时自动采集:** + +由于列表频繁更新可能性不大,固定 1 小时采集一次。你必须启用定时任务进程。 + +**手动更新采集:** + +运行命令:`vendor/bin/imi-swoole emailBlackList/crawl` + +> `imi-swoole` 也可以替换为其它容器的启动文件 + +### 自定义管理域名 + +```php +$handler = \Imi\Email\BlackList\Util\EmailBlackListUtil::getHandler(); +$handler->add(['example.com']); // 批量增加 +$handler->remove(['example.com']); // 批量删除 +$handler->clear(); // 清空 +$handler->has('example.com'); // 是否存在 +$handler->count(); // 统计数量 +$handler->list(); // 获取列表 +$handler->list('.com'); // 关键词搜索 +// 分页 +// 注意:RedisHandler 返回数据数量可能不一定等于 $count +$page = 1; +$count = 10; +$handler->list('', $page, $count); +``` + +### 验证器 + +注解:`Imi\Email\BlackList\Validate\Annotation\EmailBlackList` + +```php + + +## 运行环境 + +* [PHP](https://php.net/) >= 7.4 +* [Composer](https://getcomposer.org/) >= 2.0 +* [Swoole](https://www.swoole.com/) >= 4.8.0 +* [imi](https://www.imiphp.com/) >= 2.1 + +## 版权信息 + +`imi-email-blacklist` 遵循 MulanPSL-2.0 开源协议发布,并提供免费使用。 + +## 捐赠 + + + +开源不求盈利,多少都是心意,生活不易,随缘随缘…… diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..28fd759 --- /dev/null +++ b/composer.json @@ -0,0 +1,34 @@ +{ + "name": "imiphp/imi-old-enum", + "type": "library", + "license": "MulanPSL-2.0", + "description": "imi 旧版枚举,仅限 3.0 使用,不推荐,请使用原生枚举!", + "require": { + "imiphp/imi": ">=3.0" + }, + "require-dev": { + "php-cs-fixer/shim": "~3.35.1", + "phpstan/phpstan": "~1.10.39", + "phpunit/phpunit": "~10.4" + }, + "autoload": { + "psr-4": { + "Imi\\Enum\\": "src/" + }, + "files": [ + "init.php" + ] + }, + "autoload-dev": { + "psr-4": { + "Imi\\Enum\\Test\\": "tests/" + } + }, + "scripts": { + "test": "@php vendor/bin/phpunit -c ./phpunit.xml", + "check-format": "./vendor/bin/php-cs-fixer fix --dry-run --diff -vvv", + "phpstan": "./vendor/bin/phpstan" + }, + "minimum-stability": "dev", + "prefer-stable": true +} \ No newline at end of file diff --git a/init.php b/init.php new file mode 100644 index 0000000..f3dc826 --- /dev/null +++ b/init.php @@ -0,0 +1,10 @@ + + + + + ./tests + + + + + + \ No newline at end of file diff --git a/src/Annotation/EnumItem.php b/src/Annotation/EnumItem.php new file mode 100644 index 0000000..725cf4e --- /dev/null +++ b/src/Annotation/EnumItem.php @@ -0,0 +1,24 @@ +toArray()); + } + } +} diff --git a/src/BaseEnum.php b/src/BaseEnum.php new file mode 100644 index 0000000..9feff5c --- /dev/null +++ b/src/BaseEnum.php @@ -0,0 +1,100 @@ +值集合. + * + * @return string[] + */ + public static function getKVMap(string $className): array + { + return self::$map['map'][$className] ?? []; + } + + /** + * 获取所有名称. + * + * @return string[] + */ + public static function getNames(string $className): array + { + return array_keys(self::$map['map'][$className] ?? []); + } + + /** + * 获取所有值 + */ + public static function getValues(string $className): array + { + $data = &self::$map; + if (isset($data['options'][$className])) + { + return array_keys($data['options'][$className]); + } + else + { + return []; + } + } +} diff --git a/src/Listener/BuildRuntimeListener.php b/src/Listener/BuildRuntimeListener.php new file mode 100644 index 0000000..603c127 --- /dev/null +++ b/src/Listener/BuildRuntimeListener.php @@ -0,0 +1,28 @@ +getData(); + $data = []; + $data['enum'] = EnumManager::getMap(); + $eventData['data']['enum'] = $data; + } +} diff --git a/src/Listener/LoadRuntimeListener.php b/src/Listener/LoadRuntimeListener.php new file mode 100644 index 0000000..ac823f1 --- /dev/null +++ b/src/Listener/LoadRuntimeListener.php @@ -0,0 +1,27 @@ +getData()['data']['enum'] ?? []; + EnumManager::setMap($data['enum'] ?? []); + } +} diff --git a/tests/Enum/TestEnum.php b/tests/Enum/TestEnum.php new file mode 100644 index 0000000..c0603ab --- /dev/null +++ b/tests/Enum/TestEnum.php @@ -0,0 +1,20 @@ + TestEnum::A, + 'B' => TestEnum::B, + 'C' => TestEnum::C, + ], TestEnum::getMap()); + + Assert::assertTrue(TestEnum::validate(TestEnum::A)); + Assert::assertFalse(TestEnum::validate(9527)); + TestEnum::assert(TestEnum::A); + $this->expectException(\InvalidArgumentException::class); + TestEnum::assert(9527); + } +} diff --git a/tests/PHPUnitHook.php b/tests/PHPUnitHook.php new file mode 100644 index 0000000..8f9c228 --- /dev/null +++ b/tests/PHPUnitHook.php @@ -0,0 +1,34 @@ +stopPropagation(); + }, 1); + try + { + App::run('Imi\Enum\Test', CliApp::class, static function (): void { + }); + } + catch (\Throwable $th) + { + var_dump((string) $th); // 方便错误调试查看 + throw $th; + } + } +}