Merge branch 'master' of http://gitlab.posedu.cn:8099/cdd/employed
正在显示
54 个修改的文件
包含
5568 行增加
和
5 行删除
application/extra/queue.php
0 → 100644
| 1 | +<?php | ||
| 2 | +// +---------------------------------------------------------------------- | ||
| 3 | +// | ThinkPHP [ WE CAN DO IT JUST THINK IT ] | ||
| 4 | +// +---------------------------------------------------------------------- | ||
| 5 | +// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved. | ||
| 6 | +// +---------------------------------------------------------------------- | ||
| 7 | +// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) | ||
| 8 | +// +---------------------------------------------------------------------- | ||
| 9 | +// | Author: yunwuxin <448901948@qq.com> | ||
| 10 | +// +---------------------------------------------------------------------- | ||
| 11 | + | ||
| 12 | +//return [ | ||
| 13 | +// 'connector' => 'Sync' | ||
| 14 | +//]; | ||
| 15 | + | ||
| 16 | +return [ | ||
| 17 | + 'connector' => 'Redis', // Redis 驱动 | ||
| 18 | + 'expire' => null, // 任务的过期时间,默认为60秒; 若要禁用,则设置为 null | ||
| 19 | + 'default' => 'default', // 默认的队列名称 | ||
| 20 | + 'host' => 'r-8vb3mizdy07emeuv4upd.redis.zhangbei.rds.aliyuncs.com', // redis 主机ip | ||
| 21 | + 'port' => 6379, // redis 端口 | ||
| 22 | + 'password' => 'Yundai2019', // redis 密码 | ||
| 23 | + 'select' => 8, // 使用哪一个 db,默认为 db0 | ||
| 24 | + 'timeout' => 0, // redis连接的超时时间 | ||
| 25 | + 'persistent' => false, // 是否是长连接 | ||
| 26 | +]; |
application/index/controller/job/Jobtest.php
0 → 100644
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +namespace app\index\controller\job; | ||
| 4 | +use think\Queue; | ||
| 5 | + | ||
| 6 | +class Jobtest | ||
| 7 | +{ | ||
| 8 | + //生产者测试 | ||
| 9 | + public function test() | ||
| 10 | + { | ||
| 11 | + // 1.当前任务将由哪个类来负责处理。 | ||
| 12 | + // 当轮到该任务时,系统将生成一个该类的实例,并调用其 fire 方法 | ||
| 13 | + $jobHandlerClassName = 'app\index\controller\job\Project'; | ||
| 14 | + | ||
| 15 | + // 2.当前任务归属的队列名称,如果为新队列,会自动创建 | ||
| 16 | + $jobQueueName = "checkItem"; | ||
| 17 | + | ||
| 18 | + // 3.当前任务所需的业务数据 . 不能为 resource 类型,其他类型最终将转化为json形式的字符串 | ||
| 19 | + $jobData = ['name' => 'test_01'] ; | ||
| 20 | + // 4.将该任务推送到消息队列,等待对应的消费者去执行 | ||
| 21 | +// $isPushed = Queue::push( $jobHandlerClassName , $jobData , $jobQueueName ); | ||
| 22 | + $isPushed = Queue::later(30,$jobHandlerClassName,$jobData,$jobQueueName); //把任务分配到队列中,延迟10s后执行 | ||
| 23 | + | ||
| 24 | + // database 驱动时,返回值为 1|false ; redis 驱动时,返回值为 随机字符串|false | ||
| 25 | + if( $isPushed !== false ){ | ||
| 26 | + echo date('Y-m-d H:i:s') . " a new Hello Job is Pushed to the MQ"."<br>"; | ||
| 27 | + }else{ | ||
| 28 | + echo 'something went wrong.'; | ||
| 29 | + } | ||
| 30 | + } | ||
| 31 | + | ||
| 32 | +} | ||
| 33 | + | ||
| 34 | + | ||
| 35 | + | ||
| 36 | + | ||
| 37 | + | ||
| 38 | + | ||
| 39 | + | ||
| 40 | + | ||
| 41 | + | ||
| 42 | + | ||
| 43 | + | ||
| 44 | + | ||
| 45 | + | ||
| 46 | + | ||
| 47 | + | ||
| 48 | + | ||
| 49 | + | ||
| 50 | + | ||
| 51 | + | ||
| 52 | + | ||
| 53 | + | ||
| 54 | + |
application/index/controller/job/Project.php
0 → 100644
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +namespace app\index\controller\job; | ||
| 4 | +use think\queue\Job; | ||
| 5 | +use think\Db; | ||
| 6 | + | ||
| 7 | +class Project | ||
| 8 | +{ | ||
| 9 | + //消费者 | ||
| 10 | + public function fire(Job $job,$data) { | ||
| 11 | +// 有些消息在到达消费者时,可能已经不再需要执行了 | ||
| 12 | + $isJobStillNeedToBeDone = $this->checkDatabaseToSeeIfJobNeedToBeDone($data); | ||
| 13 | + if(!$isJobStillNeedToBeDone){ | ||
| 14 | + $job->delete(); | ||
| 15 | + return; | ||
| 16 | + } | ||
| 17 | + | ||
| 18 | + $isJobDone = $this->doHelloJob($data); | ||
| 19 | + | ||
| 20 | + if ($isJobDone) { | ||
| 21 | + // 如果任务执行成功, 记得删除任务 | ||
| 22 | + $job->delete(); | ||
| 23 | + print("<info>Hello Job has been done and deleted"."</info>\n"); | ||
| 24 | + }else{ | ||
| 25 | + if ($job->attempts() > 3) { | ||
| 26 | + //通过这个方法可以检查这个任务已经重试了几次了 | ||
| 27 | + print("<warn>Hello Job has been retried more than 3 times!"."</warn>\n"); | ||
| 28 | + $job->delete(); | ||
| 29 | + // 也可以重新发布这个任务 | ||
| 30 | + //print("<info>Hello Job will be availabe again after 2s."."</info>\n"); | ||
| 31 | + //$job->release(2); //$delay为延迟时间,表示该任务延迟2秒后再执行 | ||
| 32 | + } | ||
| 33 | + } | ||
| 34 | + } | ||
| 35 | + | ||
| 36 | + /** | ||
| 37 | + * 有些消息在到达消费者时,可能已经不再需要执行了 | ||
| 38 | + * @param array|mixed $data 发布任务时自定义的数据 | ||
| 39 | + * @return boolean 任务执行的结果 | ||
| 40 | + */ | ||
| 41 | + private function checkDatabaseToSeeIfJobNeedToBeDone($data){ | ||
| 42 | + return true; | ||
| 43 | + } | ||
| 44 | + | ||
| 45 | + /** | ||
| 46 | + * 根据消息中的数据进行实际的业务处理... | ||
| 47 | + */ | ||
| 48 | + private function doHelloJob($data) | ||
| 49 | + { | ||
| 50 | +// print("<info>item Job has been exec"."</info>\n"); | ||
| 51 | +// return true; | ||
| 52 | + $result = Db::name('job_test')->insert(['name' => $data['name'],'data' => json_encode($data,true),'create_time' => time()]); | ||
| 53 | + if($result) { | ||
| 54 | + return true; | ||
| 55 | + }else { | ||
| 56 | + return false; | ||
| 57 | + } | ||
| 58 | + | ||
| 59 | + } | ||
| 60 | + | ||
| 61 | +} |
| @@ -29,7 +29,8 @@ | @@ -29,7 +29,8 @@ | ||
| 29 | "ext-curl": "*", | 29 | "ext-curl": "*", |
| 30 | "ext-pdo": "*", | 30 | "ext-pdo": "*", |
| 31 | "endroid/qrcode": "^2.1", | 31 | "endroid/qrcode": "^2.1", |
| 32 | - "alibabacloud/sdk": "^1.8" | 32 | + "alibabacloud/sdk": "^1.8", |
| 33 | + "topthink/think-queue": "1.1.4" | ||
| 33 | }, | 34 | }, |
| 34 | "config": { | 35 | "config": { |
| 35 | "preferred-install": "dist", | 36 | "preferred-install": "dist", |
| @@ -4,7 +4,7 @@ | @@ -4,7 +4,7 @@ | ||
| 4 | "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", | 4 | "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", |
| 5 | "This file is @generated automatically" | 5 | "This file is @generated automatically" |
| 6 | ], | 6 | ], |
| 7 | - "content-hash": "d541833c7fe7a8e6726e0a391587b419", | 7 | + "content-hash": "6ff2165648c741dd2106ef45f054aece", |
| 8 | "packages": [ | 8 | "packages": [ |
| 9 | { | 9 | { |
| 10 | "name": "adbario/php-dot-notation", | 10 | "name": "adbario/php-dot-notation", |
| @@ -4457,6 +4457,58 @@ | @@ -4457,6 +4457,58 @@ | ||
| 4457 | "time": "2019-01-28T04:48:36+00:00" | 4457 | "time": "2019-01-28T04:48:36+00:00" |
| 4458 | }, | 4458 | }, |
| 4459 | { | 4459 | { |
| 4460 | + "name": "topthink/think-helper", | ||
| 4461 | + "version": "v3.1.6", | ||
| 4462 | + "source": { | ||
| 4463 | + "type": "git", | ||
| 4464 | + "url": "https://github.com/top-think/think-helper.git", | ||
| 4465 | + "reference": "769acbe50a4274327162f9c68ec2e89a38eb2aff" | ||
| 4466 | + }, | ||
| 4467 | + "dist": { | ||
| 4468 | + "type": "zip", | ||
| 4469 | + "url": "https://api.github.com/repos/top-think/think-helper/zipball/769acbe50a4274327162f9c68ec2e89a38eb2aff", | ||
| 4470 | + "reference": "769acbe50a4274327162f9c68ec2e89a38eb2aff", | ||
| 4471 | + "shasum": "", | ||
| 4472 | + "mirrors": [ | ||
| 4473 | + { | ||
| 4474 | + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", | ||
| 4475 | + "preferred": true | ||
| 4476 | + } | ||
| 4477 | + ] | ||
| 4478 | + }, | ||
| 4479 | + "require": { | ||
| 4480 | + "php": ">=7.1.0" | ||
| 4481 | + }, | ||
| 4482 | + "require-dev": { | ||
| 4483 | + "phpunit/phpunit": "^9.5" | ||
| 4484 | + }, | ||
| 4485 | + "type": "library", | ||
| 4486 | + "autoload": { | ||
| 4487 | + "files": [ | ||
| 4488 | + "src/helper.php" | ||
| 4489 | + ], | ||
| 4490 | + "psr-4": { | ||
| 4491 | + "think\\": "src" | ||
| 4492 | + } | ||
| 4493 | + }, | ||
| 4494 | + "notification-url": "https://packagist.org/downloads/", | ||
| 4495 | + "license": [ | ||
| 4496 | + "Apache-2.0" | ||
| 4497 | + ], | ||
| 4498 | + "authors": [ | ||
| 4499 | + { | ||
| 4500 | + "name": "yunwuxin", | ||
| 4501 | + "email": "448901948@qq.com" | ||
| 4502 | + } | ||
| 4503 | + ], | ||
| 4504 | + "description": "The ThinkPHP6 Helper Package", | ||
| 4505 | + "support": { | ||
| 4506 | + "issues": "https://github.com/top-think/think-helper/issues", | ||
| 4507 | + "source": "https://github.com/top-think/think-helper/tree/v3.1.6" | ||
| 4508 | + }, | ||
| 4509 | + "time": "2021-12-15T04:27:55+00:00" | ||
| 4510 | + }, | ||
| 4511 | + { | ||
| 4460 | "name": "topthink/think-installer", | 4512 | "name": "topthink/think-installer", |
| 4461 | "version": "v1.0.14", | 4513 | "version": "v1.0.14", |
| 4462 | "source": { | 4514 | "source": { |
| @@ -4506,6 +4558,61 @@ | @@ -4506,6 +4558,61 @@ | ||
| 4506 | "source": "https://github.com/top-think/think-installer/tree/v1.0.14" | 4558 | "source": "https://github.com/top-think/think-installer/tree/v1.0.14" |
| 4507 | }, | 4559 | }, |
| 4508 | "time": "2021-03-25T08:34:02+00:00" | 4560 | "time": "2021-03-25T08:34:02+00:00" |
| 4561 | + }, | ||
| 4562 | + { | ||
| 4563 | + "name": "topthink/think-queue", | ||
| 4564 | + "version": "v1.1.4", | ||
| 4565 | + "source": { | ||
| 4566 | + "type": "git", | ||
| 4567 | + "url": "https://github.com/top-think/think-queue.git", | ||
| 4568 | + "reference": "ad709611d516e13d6760234bc98e91faa901cae8" | ||
| 4569 | + }, | ||
| 4570 | + "dist": { | ||
| 4571 | + "type": "zip", | ||
| 4572 | + "url": "https://api.github.com/repos/top-think/think-queue/zipball/ad709611d516e13d6760234bc98e91faa901cae8", | ||
| 4573 | + "reference": "ad709611d516e13d6760234bc98e91faa901cae8", | ||
| 4574 | + "shasum": "", | ||
| 4575 | + "mirrors": [ | ||
| 4576 | + { | ||
| 4577 | + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", | ||
| 4578 | + "preferred": true | ||
| 4579 | + } | ||
| 4580 | + ] | ||
| 4581 | + }, | ||
| 4582 | + "require": { | ||
| 4583 | + "topthink/think-helper": ">=1.0.4", | ||
| 4584 | + "topthink/think-installer": ">=1.0.10" | ||
| 4585 | + }, | ||
| 4586 | + "type": "think-extend", | ||
| 4587 | + "extra": { | ||
| 4588 | + "think-config": { | ||
| 4589 | + "queue": "src/config.php" | ||
| 4590 | + } | ||
| 4591 | + }, | ||
| 4592 | + "autoload": { | ||
| 4593 | + "files": [ | ||
| 4594 | + "src/common.php" | ||
| 4595 | + ], | ||
| 4596 | + "psr-4": { | ||
| 4597 | + "think\\": "src" | ||
| 4598 | + } | ||
| 4599 | + }, | ||
| 4600 | + "notification-url": "https://packagist.org/downloads/", | ||
| 4601 | + "license": [ | ||
| 4602 | + "Apache-2.0" | ||
| 4603 | + ], | ||
| 4604 | + "authors": [ | ||
| 4605 | + { | ||
| 4606 | + "name": "yunwuxin", | ||
| 4607 | + "email": "448901948@qq.com" | ||
| 4608 | + } | ||
| 4609 | + ], | ||
| 4610 | + "description": "The ThinkPHP5 Queue Package", | ||
| 4611 | + "support": { | ||
| 4612 | + "issues": "https://github.com/top-think/think-queue/issues", | ||
| 4613 | + "source": "https://github.com/top-think/think-queue/tree/master" | ||
| 4614 | + }, | ||
| 4615 | + "time": "2017-06-25T00:49:56+00:00" | ||
| 4509 | } | 4616 | } |
| 4510 | ], | 4617 | ], |
| 4511 | "packages-dev": [], | 4618 | "packages-dev": [], |
think
0 → 100644
| 1 | +#!/usr/bin/env php | ||
| 2 | +<?php | ||
| 3 | +// +---------------------------------------------------------------------- | ||
| 4 | +// | ThinkPHP [ WE CAN DO IT JUST THINK ] | ||
| 5 | +// +---------------------------------------------------------------------- | ||
| 6 | +// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved. | ||
| 7 | +// +---------------------------------------------------------------------- | ||
| 8 | +// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) | ||
| 9 | +// +---------------------------------------------------------------------- | ||
| 10 | +// | Author: yunwuxin <448901948@qq.com> | ||
| 11 | +// +---------------------------------------------------------------------- | ||
| 12 | + | ||
| 13 | +// 定义项目路径 | ||
| 14 | +define('APP_PATH', __DIR__ . '/application/'); | ||
| 15 | + | ||
| 16 | +// 加载框架引导文件 | ||
| 17 | +require './thinkphp/console.php'; |
| @@ -24,8 +24,10 @@ return array( | @@ -24,8 +24,10 @@ return array( | ||
| 24 | '66453932bc1be9fb2f910a27947d11b6' => $vendorDir . '/alibabacloud/client/src/Functions.php', | 24 | '66453932bc1be9fb2f910a27947d11b6' => $vendorDir . '/alibabacloud/client/src/Functions.php', |
| 25 | '2cffec82183ee1cea088009cef9a6fc3' => $vendorDir . '/ezyang/htmlpurifier/library/HTMLPurifier.composer.php', | 25 | '2cffec82183ee1cea088009cef9a6fc3' => $vendorDir . '/ezyang/htmlpurifier/library/HTMLPurifier.composer.php', |
| 26 | '626dcc41390ebdaa619faa02d99943b0' => $vendorDir . '/khanamiryan/qrcode-detector-decoder/lib/common/customFunctions.php', | 26 | '626dcc41390ebdaa619faa02d99943b0' => $vendorDir . '/khanamiryan/qrcode-detector-decoder/lib/common/customFunctions.php', |
| 27 | + '9b552a3cc426e3287cc811caefa3cf53' => $vendorDir . '/topthink/think-helper/src/helper.php', | ||
| 27 | '488987c28e9b5e95a1ce6b6bcb94606c' => $vendorDir . '/karsonzhang/fastadmin-addons/src/common.php', | 28 | '488987c28e9b5e95a1ce6b6bcb94606c' => $vendorDir . '/karsonzhang/fastadmin-addons/src/common.php', |
| 28 | 'f0e7e63bbb278a92db02393536748c5f' => $vendorDir . '/overtrue/wechat/src/Kernel/Support/Helpers.php', | 29 | 'f0e7e63bbb278a92db02393536748c5f' => $vendorDir . '/overtrue/wechat/src/Kernel/Support/Helpers.php', |
| 29 | '6747f579ad6817f318cc3a7e7a0abb93' => $vendorDir . '/overtrue/wechat/src/Kernel/Helpers.php', | 30 | '6747f579ad6817f318cc3a7e7a0abb93' => $vendorDir . '/overtrue/wechat/src/Kernel/Helpers.php', |
| 30 | '1cfd2761b63b0a29ed23657ea394cb2d' => $vendorDir . '/topthink/think-captcha/src/helper.php', | 31 | '1cfd2761b63b0a29ed23657ea394cb2d' => $vendorDir . '/topthink/think-captcha/src/helper.php', |
| 32 | + 'cc56288302d9df745d97c934d6a6e5f0' => $vendorDir . '/topthink/think-queue/src/common.php', | ||
| 31 | ); | 33 | ); |
| @@ -8,7 +8,7 @@ $baseDir = dirname($vendorDir); | @@ -8,7 +8,7 @@ $baseDir = dirname($vendorDir); | ||
| 8 | return array( | 8 | return array( |
| 9 | 'think\\composer\\' => array($vendorDir . '/topthink/think-installer/src'), | 9 | 'think\\composer\\' => array($vendorDir . '/topthink/think-installer/src'), |
| 10 | 'think\\captcha\\' => array($vendorDir . '/topthink/think-captcha/src'), | 10 | 'think\\captcha\\' => array($vendorDir . '/topthink/think-captcha/src'), |
| 11 | - 'think\\' => array($vendorDir . '/karsonzhang/fastadmin-addons/src', $baseDir . '/thinkphp/library/think'), | 11 | + 'think\\' => array($vendorDir . '/karsonzhang/fastadmin-addons/src', $baseDir . '/thinkphp/library/think', $vendorDir . '/topthink/think-helper/src', $vendorDir . '/topthink/think-queue/src'), |
| 12 | 'clagiordano\\weblibs\\configmanager\\' => array($vendorDir . '/clagiordano/weblibs-configmanager/src'), | 12 | 'clagiordano\\weblibs\\configmanager\\' => array($vendorDir . '/clagiordano/weblibs-configmanager/src'), |
| 13 | 'ZipStream\\' => array($vendorDir . '/maennchen/zipstream-php/src'), | 13 | 'ZipStream\\' => array($vendorDir . '/maennchen/zipstream-php/src'), |
| 14 | 'Symfony\\Polyfill\\Php80\\' => array($vendorDir . '/symfony/polyfill-php80'), | 14 | 'Symfony\\Polyfill\\Php80\\' => array($vendorDir . '/symfony/polyfill-php80'), |
| @@ -25,10 +25,12 @@ class ComposerStaticInitbbe846bc22704b0a31b1a4a1369e02e4 | @@ -25,10 +25,12 @@ class ComposerStaticInitbbe846bc22704b0a31b1a4a1369e02e4 | ||
| 25 | '66453932bc1be9fb2f910a27947d11b6' => __DIR__ . '/..' . '/alibabacloud/client/src/Functions.php', | 25 | '66453932bc1be9fb2f910a27947d11b6' => __DIR__ . '/..' . '/alibabacloud/client/src/Functions.php', |
| 26 | '2cffec82183ee1cea088009cef9a6fc3' => __DIR__ . '/..' . '/ezyang/htmlpurifier/library/HTMLPurifier.composer.php', | 26 | '2cffec82183ee1cea088009cef9a6fc3' => __DIR__ . '/..' . '/ezyang/htmlpurifier/library/HTMLPurifier.composer.php', |
| 27 | '626dcc41390ebdaa619faa02d99943b0' => __DIR__ . '/..' . '/khanamiryan/qrcode-detector-decoder/lib/common/customFunctions.php', | 27 | '626dcc41390ebdaa619faa02d99943b0' => __DIR__ . '/..' . '/khanamiryan/qrcode-detector-decoder/lib/common/customFunctions.php', |
| 28 | + '9b552a3cc426e3287cc811caefa3cf53' => __DIR__ . '/..' . '/topthink/think-helper/src/helper.php', | ||
| 28 | '488987c28e9b5e95a1ce6b6bcb94606c' => __DIR__ . '/..' . '/karsonzhang/fastadmin-addons/src/common.php', | 29 | '488987c28e9b5e95a1ce6b6bcb94606c' => __DIR__ . '/..' . '/karsonzhang/fastadmin-addons/src/common.php', |
| 29 | 'f0e7e63bbb278a92db02393536748c5f' => __DIR__ . '/..' . '/overtrue/wechat/src/Kernel/Support/Helpers.php', | 30 | 'f0e7e63bbb278a92db02393536748c5f' => __DIR__ . '/..' . '/overtrue/wechat/src/Kernel/Support/Helpers.php', |
| 30 | '6747f579ad6817f318cc3a7e7a0abb93' => __DIR__ . '/..' . '/overtrue/wechat/src/Kernel/Helpers.php', | 31 | '6747f579ad6817f318cc3a7e7a0abb93' => __DIR__ . '/..' . '/overtrue/wechat/src/Kernel/Helpers.php', |
| 31 | '1cfd2761b63b0a29ed23657ea394cb2d' => __DIR__ . '/..' . '/topthink/think-captcha/src/helper.php', | 32 | '1cfd2761b63b0a29ed23657ea394cb2d' => __DIR__ . '/..' . '/topthink/think-captcha/src/helper.php', |
| 33 | + 'cc56288302d9df745d97c934d6a6e5f0' => __DIR__ . '/..' . '/topthink/think-queue/src/common.php', | ||
| 32 | ); | 34 | ); |
| 33 | 35 | ||
| 34 | public static $prefixLengthsPsr4 = array ( | 36 | public static $prefixLengthsPsr4 = array ( |
| @@ -134,6 +136,8 @@ class ComposerStaticInitbbe846bc22704b0a31b1a4a1369e02e4 | @@ -134,6 +136,8 @@ class ComposerStaticInitbbe846bc22704b0a31b1a4a1369e02e4 | ||
| 134 | array ( | 136 | array ( |
| 135 | 0 => __DIR__ . '/..' . '/karsonzhang/fastadmin-addons/src', | 137 | 0 => __DIR__ . '/..' . '/karsonzhang/fastadmin-addons/src', |
| 136 | 1 => __DIR__ . '/../..' . '/thinkphp/library/think', | 138 | 1 => __DIR__ . '/../..' . '/thinkphp/library/think', |
| 139 | + 2 => __DIR__ . '/..' . '/topthink/think-helper/src', | ||
| 140 | + 3 => __DIR__ . '/..' . '/topthink/think-queue/src', | ||
| 137 | ), | 141 | ), |
| 138 | 'clagiordano\\weblibs\\configmanager\\' => | 142 | 'clagiordano\\weblibs\\configmanager\\' => |
| 139 | array ( | 143 | array ( |
| @@ -4431,6 +4431,61 @@ | @@ -4431,6 +4431,61 @@ | ||
| 4431 | "install-path": "../topthink/think-captcha" | 4431 | "install-path": "../topthink/think-captcha" |
| 4432 | }, | 4432 | }, |
| 4433 | { | 4433 | { |
| 4434 | + "name": "topthink/think-helper", | ||
| 4435 | + "version": "v3.1.6", | ||
| 4436 | + "version_normalized": "3.1.6.0", | ||
| 4437 | + "source": { | ||
| 4438 | + "type": "git", | ||
| 4439 | + "url": "https://github.com/top-think/think-helper.git", | ||
| 4440 | + "reference": "769acbe50a4274327162f9c68ec2e89a38eb2aff" | ||
| 4441 | + }, | ||
| 4442 | + "dist": { | ||
| 4443 | + "type": "zip", | ||
| 4444 | + "url": "https://api.github.com/repos/top-think/think-helper/zipball/769acbe50a4274327162f9c68ec2e89a38eb2aff", | ||
| 4445 | + "reference": "769acbe50a4274327162f9c68ec2e89a38eb2aff", | ||
| 4446 | + "shasum": "", | ||
| 4447 | + "mirrors": [ | ||
| 4448 | + { | ||
| 4449 | + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", | ||
| 4450 | + "preferred": true | ||
| 4451 | + } | ||
| 4452 | + ] | ||
| 4453 | + }, | ||
| 4454 | + "require": { | ||
| 4455 | + "php": ">=7.1.0" | ||
| 4456 | + }, | ||
| 4457 | + "require-dev": { | ||
| 4458 | + "phpunit/phpunit": "^9.5" | ||
| 4459 | + }, | ||
| 4460 | + "time": "2021-12-15T04:27:55+00:00", | ||
| 4461 | + "type": "library", | ||
| 4462 | + "installation-source": "dist", | ||
| 4463 | + "autoload": { | ||
| 4464 | + "files": [ | ||
| 4465 | + "src/helper.php" | ||
| 4466 | + ], | ||
| 4467 | + "psr-4": { | ||
| 4468 | + "think\\": "src" | ||
| 4469 | + } | ||
| 4470 | + }, | ||
| 4471 | + "notification-url": "https://packagist.org/downloads/", | ||
| 4472 | + "license": [ | ||
| 4473 | + "Apache-2.0" | ||
| 4474 | + ], | ||
| 4475 | + "authors": [ | ||
| 4476 | + { | ||
| 4477 | + "name": "yunwuxin", | ||
| 4478 | + "email": "448901948@qq.com" | ||
| 4479 | + } | ||
| 4480 | + ], | ||
| 4481 | + "description": "The ThinkPHP6 Helper Package", | ||
| 4482 | + "support": { | ||
| 4483 | + "issues": "https://github.com/top-think/think-helper/issues", | ||
| 4484 | + "source": "https://github.com/top-think/think-helper/tree/v3.1.6" | ||
| 4485 | + }, | ||
| 4486 | + "install-path": "../topthink/think-helper" | ||
| 4487 | + }, | ||
| 4488 | + { | ||
| 4434 | "name": "topthink/think-installer", | 4489 | "name": "topthink/think-installer", |
| 4435 | "version": "v1.0.14", | 4490 | "version": "v1.0.14", |
| 4436 | "version_normalized": "1.0.14.0", | 4491 | "version_normalized": "1.0.14.0", |
| @@ -4483,6 +4538,64 @@ | @@ -4483,6 +4538,64 @@ | ||
| 4483 | "source": "https://github.com/top-think/think-installer/tree/v1.0.14" | 4538 | "source": "https://github.com/top-think/think-installer/tree/v1.0.14" |
| 4484 | }, | 4539 | }, |
| 4485 | "install-path": "../topthink/think-installer" | 4540 | "install-path": "../topthink/think-installer" |
| 4541 | + }, | ||
| 4542 | + { | ||
| 4543 | + "name": "topthink/think-queue", | ||
| 4544 | + "version": "v1.1.4", | ||
| 4545 | + "version_normalized": "1.1.4.0", | ||
| 4546 | + "source": { | ||
| 4547 | + "type": "git", | ||
| 4548 | + "url": "https://github.com/top-think/think-queue.git", | ||
| 4549 | + "reference": "ad709611d516e13d6760234bc98e91faa901cae8" | ||
| 4550 | + }, | ||
| 4551 | + "dist": { | ||
| 4552 | + "type": "zip", | ||
| 4553 | + "url": "https://api.github.com/repos/top-think/think-queue/zipball/ad709611d516e13d6760234bc98e91faa901cae8", | ||
| 4554 | + "reference": "ad709611d516e13d6760234bc98e91faa901cae8", | ||
| 4555 | + "shasum": "", | ||
| 4556 | + "mirrors": [ | ||
| 4557 | + { | ||
| 4558 | + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", | ||
| 4559 | + "preferred": true | ||
| 4560 | + } | ||
| 4561 | + ] | ||
| 4562 | + }, | ||
| 4563 | + "require": { | ||
| 4564 | + "topthink/think-helper": ">=1.0.4", | ||
| 4565 | + "topthink/think-installer": ">=1.0.10" | ||
| 4566 | + }, | ||
| 4567 | + "time": "2017-06-25T00:49:56+00:00", | ||
| 4568 | + "type": "think-extend", | ||
| 4569 | + "extra": { | ||
| 4570 | + "think-config": { | ||
| 4571 | + "queue": "src/config.php" | ||
| 4572 | + } | ||
| 4573 | + }, | ||
| 4574 | + "installation-source": "dist", | ||
| 4575 | + "autoload": { | ||
| 4576 | + "files": [ | ||
| 4577 | + "src/common.php" | ||
| 4578 | + ], | ||
| 4579 | + "psr-4": { | ||
| 4580 | + "think\\": "src" | ||
| 4581 | + } | ||
| 4582 | + }, | ||
| 4583 | + "notification-url": "https://packagist.org/downloads/", | ||
| 4584 | + "license": [ | ||
| 4585 | + "Apache-2.0" | ||
| 4586 | + ], | ||
| 4587 | + "authors": [ | ||
| 4588 | + { | ||
| 4589 | + "name": "yunwuxin", | ||
| 4590 | + "email": "448901948@qq.com" | ||
| 4591 | + } | ||
| 4592 | + ], | ||
| 4593 | + "description": "The ThinkPHP5 Queue Package", | ||
| 4594 | + "support": { | ||
| 4595 | + "issues": "https://github.com/top-think/think-queue/issues", | ||
| 4596 | + "source": "https://github.com/top-think/think-queue/tree/master" | ||
| 4597 | + }, | ||
| 4598 | + "install-path": "../topthink/think-queue" | ||
| 4486 | } | 4599 | } |
| 4487 | ], | 4600 | ], |
| 4488 | "dev": true, | 4601 | "dev": true, |
| @@ -5,7 +5,7 @@ | @@ -5,7 +5,7 @@ | ||
| 5 | 'type' => 'project', | 5 | 'type' => 'project', |
| 6 | 'install_path' => __DIR__ . '/../../', | 6 | 'install_path' => __DIR__ . '/../../', |
| 7 | 'aliases' => array(), | 7 | 'aliases' => array(), |
| 8 | - 'reference' => 'ddbb1b86cbfeb9d33de449f7b3839e345d4b72a5', | 8 | + 'reference' => 'cb9b0017c6c4e6f18f312b3d801ab0e018e91dd4', |
| 9 | 'name' => 'karsonzhang/fastadmin', | 9 | 'name' => 'karsonzhang/fastadmin', |
| 10 | 'dev' => true, | 10 | 'dev' => true, |
| 11 | ), | 11 | ), |
| @@ -1132,7 +1132,7 @@ | @@ -1132,7 +1132,7 @@ | ||
| 1132 | 'type' => 'project', | 1132 | 'type' => 'project', |
| 1133 | 'install_path' => __DIR__ . '/../../', | 1133 | 'install_path' => __DIR__ . '/../../', |
| 1134 | 'aliases' => array(), | 1134 | 'aliases' => array(), |
| 1135 | - 'reference' => 'ddbb1b86cbfeb9d33de449f7b3839e345d4b72a5', | 1135 | + 'reference' => 'cb9b0017c6c4e6f18f312b3d801ab0e018e91dd4', |
| 1136 | 'dev_requirement' => false, | 1136 | 'dev_requirement' => false, |
| 1137 | ), | 1137 | ), |
| 1138 | 'karsonzhang/fastadmin-addons' => array( | 1138 | 'karsonzhang/fastadmin-addons' => array( |
| @@ -1600,6 +1600,15 @@ | @@ -1600,6 +1600,15 @@ | ||
| 1600 | 'reference' => '1d64363c814c92f6086c4fa5e3223fe7e23db09d', | 1600 | 'reference' => '1d64363c814c92f6086c4fa5e3223fe7e23db09d', |
| 1601 | 'dev_requirement' => false, | 1601 | 'dev_requirement' => false, |
| 1602 | ), | 1602 | ), |
| 1603 | + 'topthink/think-helper' => array( | ||
| 1604 | + 'pretty_version' => 'v3.1.6', | ||
| 1605 | + 'version' => '3.1.6.0', | ||
| 1606 | + 'type' => 'library', | ||
| 1607 | + 'install_path' => __DIR__ . '/../topthink/think-helper', | ||
| 1608 | + 'aliases' => array(), | ||
| 1609 | + 'reference' => '769acbe50a4274327162f9c68ec2e89a38eb2aff', | ||
| 1610 | + 'dev_requirement' => false, | ||
| 1611 | + ), | ||
| 1603 | 'topthink/think-installer' => array( | 1612 | 'topthink/think-installer' => array( |
| 1604 | 'pretty_version' => 'v1.0.14', | 1613 | 'pretty_version' => 'v1.0.14', |
| 1605 | 'version' => '1.0.14.0', | 1614 | 'version' => '1.0.14.0', |
| @@ -1609,5 +1618,14 @@ | @@ -1609,5 +1618,14 @@ | ||
| 1609 | 'reference' => 'eae1740ac264a55c06134b6685dfb9f837d004d1', | 1618 | 'reference' => 'eae1740ac264a55c06134b6685dfb9f837d004d1', |
| 1610 | 'dev_requirement' => false, | 1619 | 'dev_requirement' => false, |
| 1611 | ), | 1620 | ), |
| 1621 | + 'topthink/think-queue' => array( | ||
| 1622 | + 'pretty_version' => 'v1.1.4', | ||
| 1623 | + 'version' => '1.1.4.0', | ||
| 1624 | + 'type' => 'think-extend', | ||
| 1625 | + 'install_path' => __DIR__ . '/../topthink/think-queue', | ||
| 1626 | + 'aliases' => array(), | ||
| 1627 | + 'reference' => 'ad709611d516e13d6760234bc98e91faa901cae8', | ||
| 1628 | + 'dev_requirement' => false, | ||
| 1629 | + ), | ||
| 1612 | ), | 1630 | ), |
| 1613 | ); | 1631 | ); |
| 1 | +name: CI | ||
| 2 | + | ||
| 3 | +on: | ||
| 4 | + push: | ||
| 5 | + branches: [ master ] | ||
| 6 | + pull_request: | ||
| 7 | + branches: [ master ] | ||
| 8 | + | ||
| 9 | + | ||
| 10 | +jobs: | ||
| 11 | + phpcs: | ||
| 12 | + runs-on: ubuntu-latest | ||
| 13 | + steps: | ||
| 14 | + - uses: actions/checkout@v2 | ||
| 15 | + - name: Setup PHP environment | ||
| 16 | + uses: shivammathur/setup-php@v2 | ||
| 17 | + - name: Install dependencies | ||
| 18 | + run: composer install | ||
| 19 | + - name: PHPCSFixer check | ||
| 20 | + run: composer check-style | ||
| 21 | + phpunit: | ||
| 22 | + strategy: | ||
| 23 | + matrix: | ||
| 24 | + php_version: [7.3, 7.4, 8.0] | ||
| 25 | + runs-on: ubuntu-latest | ||
| 26 | + steps: | ||
| 27 | + - uses: actions/checkout@v2 | ||
| 28 | + - name: Setup PHP environment | ||
| 29 | + uses: shivammathur/setup-php@v2 | ||
| 30 | + with: | ||
| 31 | + php-version: ${{ matrix.php_version }} | ||
| 32 | + coverage: xdebug | ||
| 33 | + - name: Install dependencies | ||
| 34 | + run: composer install | ||
| 35 | + - name: PHPUnit check | ||
| 36 | + run: ./vendor/bin/phpunit --coverage-text |
| 1 | +name: PHP Composer | ||
| 2 | + | ||
| 3 | +on: | ||
| 4 | + push: | ||
| 5 | + branches: [ 3.0 ] | ||
| 6 | + pull_request: | ||
| 7 | + branches: [ 3.0 ] | ||
| 8 | + | ||
| 9 | +jobs: | ||
| 10 | + build: | ||
| 11 | + | ||
| 12 | + runs-on: ubuntu-latest | ||
| 13 | + | ||
| 14 | + steps: | ||
| 15 | + - uses: actions/checkout@v2 | ||
| 16 | + | ||
| 17 | + - name: Validate composer.json and composer.lock | ||
| 18 | + run: composer validate --strict | ||
| 19 | + | ||
| 20 | + - name: Cache Composer packages | ||
| 21 | + id: composer-cache | ||
| 22 | + uses: actions/cache@v2 | ||
| 23 | + with: | ||
| 24 | + path: vendor | ||
| 25 | + key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }} | ||
| 26 | + restore-keys: | | ||
| 27 | + ${{ runner.os }}-php- | ||
| 28 | + | ||
| 29 | + - name: Install dependencies | ||
| 30 | + run: composer install --prefer-dist --no-progress | ||
| 31 | + | ||
| 32 | + # Add a test script to composer.json, for instance: "test": "vendor/bin/phpunit" | ||
| 33 | + # Docs: https://getcomposer.org/doc/articles/scripts.md | ||
| 34 | + | ||
| 35 | + - name: Run test suite | ||
| 36 | + run: composer test |
vendor/topthink/think-helper/.gitignore
0 → 100644
vendor/topthink/think-helper/LICENSE
0 → 100644
| 1 | + Apache License | ||
| 2 | + Version 2.0, January 2004 | ||
| 3 | + http://www.apache.org/licenses/ | ||
| 4 | + | ||
| 5 | + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION | ||
| 6 | + | ||
| 7 | + 1. Definitions. | ||
| 8 | + | ||
| 9 | + "License" shall mean the terms and conditions for use, reproduction, | ||
| 10 | + and distribution as defined by Sections 1 through 9 of this document. | ||
| 11 | + | ||
| 12 | + "Licensor" shall mean the copyright owner or entity authorized by | ||
| 13 | + the copyright owner that is granting the License. | ||
| 14 | + | ||
| 15 | + "Legal Entity" shall mean the union of the acting entity and all | ||
| 16 | + other entities that control, are controlled by, or are under common | ||
| 17 | + control with that entity. For the purposes of this definition, | ||
| 18 | + "control" means (i) the power, direct or indirect, to cause the | ||
| 19 | + direction or management of such entity, whether by contract or | ||
| 20 | + otherwise, or (ii) ownership of fifty percent (50%) or more of the | ||
| 21 | + outstanding shares, or (iii) beneficial ownership of such entity. | ||
| 22 | + | ||
| 23 | + "You" (or "Your") shall mean an individual or Legal Entity | ||
| 24 | + exercising permissions granted by this License. | ||
| 25 | + | ||
| 26 | + "Source" form shall mean the preferred form for making modifications, | ||
| 27 | + including but not limited to software source code, documentation | ||
| 28 | + source, and configuration files. | ||
| 29 | + | ||
| 30 | + "Object" form shall mean any form resulting from mechanical | ||
| 31 | + transformation or translation of a Source form, including but | ||
| 32 | + not limited to compiled object code, generated documentation, | ||
| 33 | + and conversions to other media types. | ||
| 34 | + | ||
| 35 | + "Work" shall mean the work of authorship, whether in Source or | ||
| 36 | + Object form, made available under the License, as indicated by a | ||
| 37 | + copyright notice that is included in or attached to the work | ||
| 38 | + (an example is provided in the Appendix below). | ||
| 39 | + | ||
| 40 | + "Derivative Works" shall mean any work, whether in Source or Object | ||
| 41 | + form, that is based on (or derived from) the Work and for which the | ||
| 42 | + editorial revisions, annotations, elaborations, or other modifications | ||
| 43 | + represent, as a whole, an original work of authorship. For the purposes | ||
| 44 | + of this License, Derivative Works shall not include works that remain | ||
| 45 | + separable from, or merely link (or bind by name) to the interfaces of, | ||
| 46 | + the Work and Derivative Works thereof. | ||
| 47 | + | ||
| 48 | + "Contribution" shall mean any work of authorship, including | ||
| 49 | + the original version of the Work and any modifications or additions | ||
| 50 | + to that Work or Derivative Works thereof, that is intentionally | ||
| 51 | + submitted to Licensor for inclusion in the Work by the copyright owner | ||
| 52 | + or by an individual or Legal Entity authorized to submit on behalf of | ||
| 53 | + the copyright owner. For the purposes of this definition, "submitted" | ||
| 54 | + means any form of electronic, verbal, or written communication sent | ||
| 55 | + to the Licensor or its representatives, including but not limited to | ||
| 56 | + communication on electronic mailing lists, source code control systems, | ||
| 57 | + and issue tracking systems that are managed by, or on behalf of, the | ||
| 58 | + Licensor for the purpose of discussing and improving the Work, but | ||
| 59 | + excluding communication that is conspicuously marked or otherwise | ||
| 60 | + designated in writing by the copyright owner as "Not a Contribution." | ||
| 61 | + | ||
| 62 | + "Contributor" shall mean Licensor and any individual or Legal Entity | ||
| 63 | + on behalf of whom a Contribution has been received by Licensor and | ||
| 64 | + subsequently incorporated within the Work. | ||
| 65 | + | ||
| 66 | + 2. Grant of Copyright License. Subject to the terms and conditions of | ||
| 67 | + this License, each Contributor hereby grants to You a perpetual, | ||
| 68 | + worldwide, non-exclusive, no-charge, royalty-free, irrevocable | ||
| 69 | + copyright license to reproduce, prepare Derivative Works of, | ||
| 70 | + publicly display, publicly perform, sublicense, and distribute the | ||
| 71 | + Work and such Derivative Works in Source or Object form. | ||
| 72 | + | ||
| 73 | + 3. Grant of Patent License. Subject to the terms and conditions of | ||
| 74 | + this License, each Contributor hereby grants to You a perpetual, | ||
| 75 | + worldwide, non-exclusive, no-charge, royalty-free, irrevocable | ||
| 76 | + (except as stated in this section) patent license to make, have made, | ||
| 77 | + use, offer to sell, sell, import, and otherwise transfer the Work, | ||
| 78 | + where such license applies only to those patent claims licensable | ||
| 79 | + by such Contributor that are necessarily infringed by their | ||
| 80 | + Contribution(s) alone or by combination of their Contribution(s) | ||
| 81 | + with the Work to which such Contribution(s) was submitted. If You | ||
| 82 | + institute patent litigation against any entity (including a | ||
| 83 | + cross-claim or counterclaim in a lawsuit) alleging that the Work | ||
| 84 | + or a Contribution incorporated within the Work constitutes direct | ||
| 85 | + or contributory patent infringement, then any patent licenses | ||
| 86 | + granted to You under this License for that Work shall terminate | ||
| 87 | + as of the date such litigation is filed. | ||
| 88 | + | ||
| 89 | + 4. Redistribution. You may reproduce and distribute copies of the | ||
| 90 | + Work or Derivative Works thereof in any medium, with or without | ||
| 91 | + modifications, and in Source or Object form, provided that You | ||
| 92 | + meet the following conditions: | ||
| 93 | + | ||
| 94 | + (a) You must give any other recipients of the Work or | ||
| 95 | + Derivative Works a copy of this License; and | ||
| 96 | + | ||
| 97 | + (b) You must cause any modified files to carry prominent notices | ||
| 98 | + stating that You changed the files; and | ||
| 99 | + | ||
| 100 | + (c) You must retain, in the Source form of any Derivative Works | ||
| 101 | + that You distribute, all copyright, patent, trademark, and | ||
| 102 | + attribution notices from the Source form of the Work, | ||
| 103 | + excluding those notices that do not pertain to any part of | ||
| 104 | + the Derivative Works; and | ||
| 105 | + | ||
| 106 | + (d) If the Work includes a "NOTICE" text file as part of its | ||
| 107 | + distribution, then any Derivative Works that You distribute must | ||
| 108 | + include a readable copy of the attribution notices contained | ||
| 109 | + within such NOTICE file, excluding those notices that do not | ||
| 110 | + pertain to any part of the Derivative Works, in at least one | ||
| 111 | + of the following places: within a NOTICE text file distributed | ||
| 112 | + as part of the Derivative Works; within the Source form or | ||
| 113 | + documentation, if provided along with the Derivative Works; or, | ||
| 114 | + within a display generated by the Derivative Works, if and | ||
| 115 | + wherever such third-party notices normally appear. The contents | ||
| 116 | + of the NOTICE file are for informational purposes only and | ||
| 117 | + do not modify the License. You may add Your own attribution | ||
| 118 | + notices within Derivative Works that You distribute, alongside | ||
| 119 | + or as an addendum to the NOTICE text from the Work, provided | ||
| 120 | + that such additional attribution notices cannot be construed | ||
| 121 | + as modifying the License. | ||
| 122 | + | ||
| 123 | + You may add Your own copyright statement to Your modifications and | ||
| 124 | + may provide additional or different license terms and conditions | ||
| 125 | + for use, reproduction, or distribution of Your modifications, or | ||
| 126 | + for any such Derivative Works as a whole, provided Your use, | ||
| 127 | + reproduction, and distribution of the Work otherwise complies with | ||
| 128 | + the conditions stated in this License. | ||
| 129 | + | ||
| 130 | + 5. Submission of Contributions. Unless You explicitly state otherwise, | ||
| 131 | + any Contribution intentionally submitted for inclusion in the Work | ||
| 132 | + by You to the Licensor shall be under the terms and conditions of | ||
| 133 | + this License, without any additional terms or conditions. | ||
| 134 | + Notwithstanding the above, nothing herein shall supersede or modify | ||
| 135 | + the terms of any separate license agreement you may have executed | ||
| 136 | + with Licensor regarding such Contributions. | ||
| 137 | + | ||
| 138 | + 6. Trademarks. This License does not grant permission to use the trade | ||
| 139 | + names, trademarks, service marks, or product names of the Licensor, | ||
| 140 | + except as required for reasonable and customary use in describing the | ||
| 141 | + origin of the Work and reproducing the content of the NOTICE file. | ||
| 142 | + | ||
| 143 | + 7. Disclaimer of Warranty. Unless required by applicable law or | ||
| 144 | + agreed to in writing, Licensor provides the Work (and each | ||
| 145 | + Contributor provides its Contributions) on an "AS IS" BASIS, | ||
| 146 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
| 147 | + implied, including, without limitation, any warranties or conditions | ||
| 148 | + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A | ||
| 149 | + PARTICULAR PURPOSE. You are solely responsible for determining the | ||
| 150 | + appropriateness of using or redistributing the Work and assume any | ||
| 151 | + risks associated with Your exercise of permissions under this License. | ||
| 152 | + | ||
| 153 | + 8. Limitation of Liability. In no event and under no legal theory, | ||
| 154 | + whether in tort (including negligence), contract, or otherwise, | ||
| 155 | + unless required by applicable law (such as deliberate and grossly | ||
| 156 | + negligent acts) or agreed to in writing, shall any Contributor be | ||
| 157 | + liable to You for damages, including any direct, indirect, special, | ||
| 158 | + incidental, or consequential damages of any character arising as a | ||
| 159 | + result of this License or out of the use or inability to use the | ||
| 160 | + Work (including but not limited to damages for loss of goodwill, | ||
| 161 | + work stoppage, computer failure or malfunction, or any and all | ||
| 162 | + other commercial damages or losses), even if such Contributor | ||
| 163 | + has been advised of the possibility of such damages. | ||
| 164 | + | ||
| 165 | + 9. Accepting Warranty or Additional Liability. While redistributing | ||
| 166 | + the Work or Derivative Works thereof, You may choose to offer, | ||
| 167 | + and charge a fee for, acceptance of support, warranty, indemnity, | ||
| 168 | + or other liability obligations and/or rights consistent with this | ||
| 169 | + License. However, in accepting such obligations, You may act only | ||
| 170 | + on Your own behalf and on Your sole responsibility, not on behalf | ||
| 171 | + of any other Contributor, and only if You agree to indemnify, | ||
| 172 | + defend, and hold each Contributor harmless for any liability | ||
| 173 | + incurred by, or claims asserted against, such Contributor by reason | ||
| 174 | + of your accepting any such warranty or additional liability. | ||
| 175 | + | ||
| 176 | + END OF TERMS AND CONDITIONS | ||
| 177 | + | ||
| 178 | + APPENDIX: How to apply the Apache License to your work. | ||
| 179 | + | ||
| 180 | + To apply the Apache License to your work, attach the following | ||
| 181 | + boilerplate notice, with the fields enclosed by brackets "{}" | ||
| 182 | + replaced with your own identifying information. (Don't include | ||
| 183 | + the brackets!) The text should be enclosed in the appropriate | ||
| 184 | + comment syntax for the file format. We also recommend that a | ||
| 185 | + file or class name and description of purpose be included on the | ||
| 186 | + same "printed page" as the copyright notice for easier | ||
| 187 | + identification within third-party archives. | ||
| 188 | + | ||
| 189 | + Copyright {yyyy} {name of copyright owner} | ||
| 190 | + | ||
| 191 | + Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 192 | + you may not use this file except in compliance with the License. | ||
| 193 | + You may obtain a copy of the License at | ||
| 194 | + | ||
| 195 | + http://www.apache.org/licenses/LICENSE-2.0 | ||
| 196 | + | ||
| 197 | + Unless required by applicable law or agreed to in writing, software | ||
| 198 | + distributed under the License is distributed on an "AS IS" BASIS, | ||
| 199 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 200 | + See the License for the specific language governing permissions and | ||
| 201 | + limitations under the License. |
vendor/topthink/think-helper/README.md
0 → 100644
| 1 | +# thinkphp6 常用的一些扩展类库 | ||
| 2 | + | ||
| 3 | +基于PHP7.1+ | ||
| 4 | + | ||
| 5 | +[](https://github.com/larvatecn/think-helper/actions/workflows/php.yml) | ||
| 6 | + | ||
| 7 | +> 以下类库都在`\\think\\helper`命名空间下 | ||
| 8 | + | ||
| 9 | +## Str | ||
| 10 | + | ||
| 11 | +> 字符串操作 | ||
| 12 | + | ||
| 13 | +``` | ||
| 14 | +// 检查字符串中是否包含某些字符串 | ||
| 15 | +Str::contains($haystack, $needles) | ||
| 16 | + | ||
| 17 | +// 检查字符串是否以某些字符串结尾 | ||
| 18 | +Str::endsWith($haystack, $needles) | ||
| 19 | + | ||
| 20 | +// 获取指定长度的随机字母数字组合的字符串 | ||
| 21 | +Str::random($length = 16) | ||
| 22 | + | ||
| 23 | +// 字符串转小写 | ||
| 24 | +Str::lower($value) | ||
| 25 | + | ||
| 26 | +// 字符串转大写 | ||
| 27 | +Str::upper($value) | ||
| 28 | + | ||
| 29 | +// 获取字符串的长度 | ||
| 30 | +Str::length($value) | ||
| 31 | + | ||
| 32 | +// 截取字符串 | ||
| 33 | +Str::substr($string, $start, $length = null) | ||
| 34 | + | ||
| 35 | +``` |
vendor/topthink/think-helper/composer.json
0 → 100644
| 1 | +{ | ||
| 2 | + "name": "topthink/think-helper", | ||
| 3 | + "description": "The ThinkPHP6 Helper Package", | ||
| 4 | + "license": "Apache-2.0", | ||
| 5 | + "authors": [ | ||
| 6 | + { | ||
| 7 | + "name": "yunwuxin", | ||
| 8 | + "email": "448901948@qq.com" | ||
| 9 | + } | ||
| 10 | + ], | ||
| 11 | + "require": { | ||
| 12 | + "php": ">=7.1.0" | ||
| 13 | + }, | ||
| 14 | + "require-dev": { | ||
| 15 | + "phpunit/phpunit": "^9.5" | ||
| 16 | + }, | ||
| 17 | + "autoload": { | ||
| 18 | + "psr-4": { | ||
| 19 | + "think\\": "src" | ||
| 20 | + }, | ||
| 21 | + "files": [ | ||
| 22 | + "src/helper.php" | ||
| 23 | + ] | ||
| 24 | + }, | ||
| 25 | + "autoload-dev": { | ||
| 26 | + "psr-4": { | ||
| 27 | + "Tests\\": "tests" | ||
| 28 | + } | ||
| 29 | + }, | ||
| 30 | + "scripts": { | ||
| 31 | + "test": "./vendor/bin/phpunit --colors" | ||
| 32 | + }, | ||
| 33 | + "scripts-descriptions": { | ||
| 34 | + "test": "Run all tests." | ||
| 35 | + } | ||
| 36 | +} |
| 1 | +<?xml version="1.0" encoding="UTF-8"?> | ||
| 2 | +<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| 3 | + xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd" | ||
| 4 | + bootstrap="vendor/autoload.php" | ||
| 5 | + colors="true" | ||
| 6 | +> | ||
| 7 | + <testsuites> | ||
| 8 | + <testsuite name="Application Test Suite"> | ||
| 9 | + <directory>./tests/</directory> | ||
| 10 | + </testsuite> | ||
| 11 | + </testsuites> | ||
| 12 | + <coverage processUncoveredFiles="true"> | ||
| 13 | + <include> | ||
| 14 | + <directory suffix=".php">./src</directory> | ||
| 15 | + </include> | ||
| 16 | + </coverage> | ||
| 17 | +</phpunit> |
| 1 | +<?php | ||
| 2 | +// +---------------------------------------------------------------------- | ||
| 3 | +// | ThinkPHP [ WE CAN DO IT JUST THINK ] | ||
| 4 | +// +---------------------------------------------------------------------- | ||
| 5 | +// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved. | ||
| 6 | +// +---------------------------------------------------------------------- | ||
| 7 | +// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) | ||
| 8 | +// +---------------------------------------------------------------------- | ||
| 9 | +// | Author: zhangyajun <448901948@qq.com> | ||
| 10 | +// +---------------------------------------------------------------------- | ||
| 11 | +declare (strict_types = 1); | ||
| 12 | + | ||
| 13 | +namespace think; | ||
| 14 | + | ||
| 15 | +use ArrayAccess; | ||
| 16 | +use ArrayIterator; | ||
| 17 | +use Countable; | ||
| 18 | +use IteratorAggregate; | ||
| 19 | +use JsonSerializable; | ||
| 20 | +use think\contract\Arrayable; | ||
| 21 | +use think\contract\Jsonable; | ||
| 22 | +use think\helper\Arr; | ||
| 23 | +use Traversable; | ||
| 24 | + | ||
| 25 | +/** | ||
| 26 | + * 数据集管理类 | ||
| 27 | + */ | ||
| 28 | +class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable, Arrayable, Jsonable | ||
| 29 | +{ | ||
| 30 | + /** | ||
| 31 | + * 数据集数据 | ||
| 32 | + * @var array | ||
| 33 | + */ | ||
| 34 | + protected $items = []; | ||
| 35 | + | ||
| 36 | + public function __construct($items = []) | ||
| 37 | + { | ||
| 38 | + $this->items = $this->convertToArray($items); | ||
| 39 | + } | ||
| 40 | + | ||
| 41 | + public static function make($items = []) | ||
| 42 | + { | ||
| 43 | + return new static($items); | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + /** | ||
| 47 | + * 是否为空 | ||
| 48 | + * @access public | ||
| 49 | + * @return bool | ||
| 50 | + */ | ||
| 51 | + public function isEmpty(): bool | ||
| 52 | + { | ||
| 53 | + return empty($this->items); | ||
| 54 | + } | ||
| 55 | + | ||
| 56 | + public function toArray(): array | ||
| 57 | + { | ||
| 58 | + return array_map(function ($value) { | ||
| 59 | + return $value instanceof Arrayable ? $value->toArray() : $value; | ||
| 60 | + }, $this->items); | ||
| 61 | + } | ||
| 62 | + | ||
| 63 | + public function all(): array | ||
| 64 | + { | ||
| 65 | + return $this->items; | ||
| 66 | + } | ||
| 67 | + | ||
| 68 | + /** | ||
| 69 | + * 合并数组 | ||
| 70 | + * | ||
| 71 | + * @access public | ||
| 72 | + * @param mixed $items 数据 | ||
| 73 | + * @return static | ||
| 74 | + */ | ||
| 75 | + public function merge($items) | ||
| 76 | + { | ||
| 77 | + return new static(array_merge($this->items, $this->convertToArray($items))); | ||
| 78 | + } | ||
| 79 | + | ||
| 80 | + /** | ||
| 81 | + * 按指定键整理数据 | ||
| 82 | + * | ||
| 83 | + * @access public | ||
| 84 | + * @param mixed $items 数据 | ||
| 85 | + * @param string $indexKey 键名 | ||
| 86 | + * @return array | ||
| 87 | + */ | ||
| 88 | + public function dictionary($items = null, string &$indexKey = null) | ||
| 89 | + { | ||
| 90 | + if ($items instanceof self) { | ||
| 91 | + $items = $items->all(); | ||
| 92 | + } | ||
| 93 | + | ||
| 94 | + $items = is_null($items) ? $this->items : $items; | ||
| 95 | + | ||
| 96 | + if ($items && empty($indexKey)) { | ||
| 97 | + $indexKey = is_array($items[0]) ? 'id' : $items[0]->getPk(); | ||
| 98 | + } | ||
| 99 | + | ||
| 100 | + if (isset($indexKey) && is_string($indexKey)) { | ||
| 101 | + return array_column($items, null, $indexKey); | ||
| 102 | + } | ||
| 103 | + | ||
| 104 | + return $items; | ||
| 105 | + } | ||
| 106 | + | ||
| 107 | + /** | ||
| 108 | + * 比较数组,返回差集 | ||
| 109 | + * | ||
| 110 | + * @access public | ||
| 111 | + * @param mixed $items 数据 | ||
| 112 | + * @param string $indexKey 指定比较的键名 | ||
| 113 | + * @return static | ||
| 114 | + */ | ||
| 115 | + public function diff($items, string $indexKey = null) | ||
| 116 | + { | ||
| 117 | + if ($this->isEmpty() || is_scalar($this->items[0])) { | ||
| 118 | + return new static(array_diff($this->items, $this->convertToArray($items))); | ||
| 119 | + } | ||
| 120 | + | ||
| 121 | + $diff = []; | ||
| 122 | + $dictionary = $this->dictionary($items, $indexKey); | ||
| 123 | + | ||
| 124 | + if (is_string($indexKey)) { | ||
| 125 | + foreach ($this->items as $item) { | ||
| 126 | + if (!isset($dictionary[$item[$indexKey]])) { | ||
| 127 | + $diff[] = $item; | ||
| 128 | + } | ||
| 129 | + } | ||
| 130 | + } | ||
| 131 | + | ||
| 132 | + return new static($diff); | ||
| 133 | + } | ||
| 134 | + | ||
| 135 | + /** | ||
| 136 | + * 比较数组,返回交集 | ||
| 137 | + * | ||
| 138 | + * @access public | ||
| 139 | + * @param mixed $items 数据 | ||
| 140 | + * @param string $indexKey 指定比较的键名 | ||
| 141 | + * @return static | ||
| 142 | + */ | ||
| 143 | + public function intersect($items, string $indexKey = null) | ||
| 144 | + { | ||
| 145 | + if ($this->isEmpty() || is_scalar($this->items[0])) { | ||
| 146 | + return new static(array_intersect($this->items, $this->convertToArray($items))); | ||
| 147 | + } | ||
| 148 | + | ||
| 149 | + $intersect = []; | ||
| 150 | + $dictionary = $this->dictionary($items, $indexKey); | ||
| 151 | + | ||
| 152 | + if (is_string($indexKey)) { | ||
| 153 | + foreach ($this->items as $item) { | ||
| 154 | + if (isset($dictionary[$item[$indexKey]])) { | ||
| 155 | + $intersect[] = $item; | ||
| 156 | + } | ||
| 157 | + } | ||
| 158 | + } | ||
| 159 | + | ||
| 160 | + return new static($intersect); | ||
| 161 | + } | ||
| 162 | + | ||
| 163 | + /** | ||
| 164 | + * 交换数组中的键和值 | ||
| 165 | + * | ||
| 166 | + * @access public | ||
| 167 | + * @return static | ||
| 168 | + */ | ||
| 169 | + public function flip() | ||
| 170 | + { | ||
| 171 | + return new static(array_flip($this->items)); | ||
| 172 | + } | ||
| 173 | + | ||
| 174 | + /** | ||
| 175 | + * 返回数组中所有的键名 | ||
| 176 | + * | ||
| 177 | + * @access public | ||
| 178 | + * @return static | ||
| 179 | + */ | ||
| 180 | + public function keys() | ||
| 181 | + { | ||
| 182 | + return new static(array_keys($this->items)); | ||
| 183 | + } | ||
| 184 | + | ||
| 185 | + /** | ||
| 186 | + * 返回数组中所有的值组成的新 Collection 实例 | ||
| 187 | + * @access public | ||
| 188 | + * @return static | ||
| 189 | + */ | ||
| 190 | + public function values() | ||
| 191 | + { | ||
| 192 | + return new static(array_values($this->items)); | ||
| 193 | + } | ||
| 194 | + | ||
| 195 | + /** | ||
| 196 | + * 删除数组的最后一个元素(出栈) | ||
| 197 | + * | ||
| 198 | + * @access public | ||
| 199 | + * @return mixed | ||
| 200 | + */ | ||
| 201 | + public function pop() | ||
| 202 | + { | ||
| 203 | + return array_pop($this->items); | ||
| 204 | + } | ||
| 205 | + | ||
| 206 | + /** | ||
| 207 | + * 通过使用用户自定义函数,以字符串返回数组 | ||
| 208 | + * | ||
| 209 | + * @access public | ||
| 210 | + * @param callable $callback 调用方法 | ||
| 211 | + * @param mixed $initial | ||
| 212 | + * @return mixed | ||
| 213 | + */ | ||
| 214 | + public function reduce(callable $callback, $initial = null) | ||
| 215 | + { | ||
| 216 | + return array_reduce($this->items, $callback, $initial); | ||
| 217 | + } | ||
| 218 | + | ||
| 219 | + /** | ||
| 220 | + * 以相反的顺序返回数组。 | ||
| 221 | + * | ||
| 222 | + * @access public | ||
| 223 | + * @return static | ||
| 224 | + */ | ||
| 225 | + public function reverse() | ||
| 226 | + { | ||
| 227 | + return new static(array_reverse($this->items)); | ||
| 228 | + } | ||
| 229 | + | ||
| 230 | + /** | ||
| 231 | + * 删除数组中首个元素,并返回被删除元素的值 | ||
| 232 | + * | ||
| 233 | + * @access public | ||
| 234 | + * @return mixed | ||
| 235 | + */ | ||
| 236 | + public function shift() | ||
| 237 | + { | ||
| 238 | + return array_shift($this->items); | ||
| 239 | + } | ||
| 240 | + | ||
| 241 | + /** | ||
| 242 | + * 在数组结尾插入一个元素 | ||
| 243 | + * @access public | ||
| 244 | + * @param mixed $value 元素 | ||
| 245 | + * @param string $key KEY | ||
| 246 | + * @return $this | ||
| 247 | + */ | ||
| 248 | + public function push($value, string $key = null) | ||
| 249 | + { | ||
| 250 | + if (is_null($key)) { | ||
| 251 | + $this->items[] = $value; | ||
| 252 | + } else { | ||
| 253 | + $this->items[$key] = $value; | ||
| 254 | + } | ||
| 255 | + | ||
| 256 | + return $this; | ||
| 257 | + } | ||
| 258 | + | ||
| 259 | + /** | ||
| 260 | + * 把一个数组分割为新的数组块. | ||
| 261 | + * | ||
| 262 | + * @access public | ||
| 263 | + * @param int $size 块大小 | ||
| 264 | + * @param bool $preserveKeys | ||
| 265 | + * @return static | ||
| 266 | + */ | ||
| 267 | + public function chunk(int $size, bool $preserveKeys = false) | ||
| 268 | + { | ||
| 269 | + $chunks = []; | ||
| 270 | + | ||
| 271 | + foreach (array_chunk($this->items, $size, $preserveKeys) as $chunk) { | ||
| 272 | + $chunks[] = new static($chunk); | ||
| 273 | + } | ||
| 274 | + | ||
| 275 | + return new static($chunks); | ||
| 276 | + } | ||
| 277 | + | ||
| 278 | + /** | ||
| 279 | + * 在数组开头插入一个元素 | ||
| 280 | + * @access public | ||
| 281 | + * @param mixed $value 元素 | ||
| 282 | + * @param string $key KEY | ||
| 283 | + * @return $this | ||
| 284 | + */ | ||
| 285 | + public function unshift($value, string $key = null) | ||
| 286 | + { | ||
| 287 | + if (is_null($key)) { | ||
| 288 | + array_unshift($this->items, $value); | ||
| 289 | + } else { | ||
| 290 | + $this->items = [$key => $value] + $this->items; | ||
| 291 | + } | ||
| 292 | + | ||
| 293 | + return $this; | ||
| 294 | + } | ||
| 295 | + | ||
| 296 | + /** | ||
| 297 | + * 给每个元素执行个回调 | ||
| 298 | + * | ||
| 299 | + * @access public | ||
| 300 | + * @param callable $callback 回调 | ||
| 301 | + * @return $this | ||
| 302 | + */ | ||
| 303 | + public function each(callable $callback) | ||
| 304 | + { | ||
| 305 | + foreach ($this->items as $key => $item) { | ||
| 306 | + $result = $callback($item, $key); | ||
| 307 | + | ||
| 308 | + if (false === $result) { | ||
| 309 | + break; | ||
| 310 | + } elseif (!is_object($item)) { | ||
| 311 | + $this->items[$key] = $result; | ||
| 312 | + } | ||
| 313 | + } | ||
| 314 | + | ||
| 315 | + return $this; | ||
| 316 | + } | ||
| 317 | + | ||
| 318 | + /** | ||
| 319 | + * 用回调函数处理数组中的元素 | ||
| 320 | + * @access public | ||
| 321 | + * @param callable|null $callback 回调 | ||
| 322 | + * @return static | ||
| 323 | + */ | ||
| 324 | + public function map(callable $callback) | ||
| 325 | + { | ||
| 326 | + return new static(array_map($callback, $this->items)); | ||
| 327 | + } | ||
| 328 | + | ||
| 329 | + /** | ||
| 330 | + * 用回调函数过滤数组中的元素 | ||
| 331 | + * @access public | ||
| 332 | + * @param callable|null $callback 回调 | ||
| 333 | + * @return static | ||
| 334 | + */ | ||
| 335 | + public function filter(callable $callback = null) | ||
| 336 | + { | ||
| 337 | + if ($callback) { | ||
| 338 | + return new static(array_filter($this->items, $callback)); | ||
| 339 | + } | ||
| 340 | + | ||
| 341 | + return new static(array_filter($this->items)); | ||
| 342 | + } | ||
| 343 | + | ||
| 344 | + /** | ||
| 345 | + * 根据字段条件过滤数组中的元素 | ||
| 346 | + * @access public | ||
| 347 | + * @param string $field 字段名 | ||
| 348 | + * @param mixed $operator 操作符 | ||
| 349 | + * @param mixed $value 数据 | ||
| 350 | + * @return static | ||
| 351 | + */ | ||
| 352 | + public function where(string $field, $operator, $value = null) | ||
| 353 | + { | ||
| 354 | + if (is_null($value)) { | ||
| 355 | + $value = $operator; | ||
| 356 | + $operator = '='; | ||
| 357 | + } | ||
| 358 | + | ||
| 359 | + return $this->filter(function ($data) use ($field, $operator, $value) { | ||
| 360 | + if (strpos($field, '.')) { | ||
| 361 | + [$field, $relation] = explode('.', $field); | ||
| 362 | + | ||
| 363 | + $result = $data[$field][$relation] ?? null; | ||
| 364 | + } else { | ||
| 365 | + $result = $data[$field] ?? null; | ||
| 366 | + } | ||
| 367 | + | ||
| 368 | + switch (strtolower($operator)) { | ||
| 369 | + case '===': | ||
| 370 | + return $result === $value; | ||
| 371 | + case '!==': | ||
| 372 | + return $result !== $value; | ||
| 373 | + case '!=': | ||
| 374 | + case '<>': | ||
| 375 | + return $result != $value; | ||
| 376 | + case '>': | ||
| 377 | + return $result > $value; | ||
| 378 | + case '>=': | ||
| 379 | + return $result >= $value; | ||
| 380 | + case '<': | ||
| 381 | + return $result < $value; | ||
| 382 | + case '<=': | ||
| 383 | + return $result <= $value; | ||
| 384 | + case 'like': | ||
| 385 | + return is_string($result) && false !== strpos($result, $value); | ||
| 386 | + case 'not like': | ||
| 387 | + return is_string($result) && false === strpos($result, $value); | ||
| 388 | + case 'in': | ||
| 389 | + return is_scalar($result) && in_array($result, $value, true); | ||
| 390 | + case 'not in': | ||
| 391 | + return is_scalar($result) && !in_array($result, $value, true); | ||
| 392 | + case 'between': | ||
| 393 | + [$min, $max] = is_string($value) ? explode(',', $value) : $value; | ||
| 394 | + return is_scalar($result) && $result >= $min && $result <= $max; | ||
| 395 | + case 'not between': | ||
| 396 | + [$min, $max] = is_string($value) ? explode(',', $value) : $value; | ||
| 397 | + return is_scalar($result) && $result > $max || $result < $min; | ||
| 398 | + case '==': | ||
| 399 | + case '=': | ||
| 400 | + default: | ||
| 401 | + return $result == $value; | ||
| 402 | + } | ||
| 403 | + }); | ||
| 404 | + } | ||
| 405 | + | ||
| 406 | + /** | ||
| 407 | + * LIKE过滤 | ||
| 408 | + * @access public | ||
| 409 | + * @param string $field 字段名 | ||
| 410 | + * @param string $value 数据 | ||
| 411 | + * @return static | ||
| 412 | + */ | ||
| 413 | + public function whereLike(string $field, string $value) | ||
| 414 | + { | ||
| 415 | + return $this->where($field, 'like', $value); | ||
| 416 | + } | ||
| 417 | + | ||
| 418 | + /** | ||
| 419 | + * NOT LIKE过滤 | ||
| 420 | + * @access public | ||
| 421 | + * @param string $field 字段名 | ||
| 422 | + * @param string $value 数据 | ||
| 423 | + * @return static | ||
| 424 | + */ | ||
| 425 | + public function whereNotLike(string $field, string $value) | ||
| 426 | + { | ||
| 427 | + return $this->where($field, 'not like', $value); | ||
| 428 | + } | ||
| 429 | + | ||
| 430 | + /** | ||
| 431 | + * IN过滤 | ||
| 432 | + * @access public | ||
| 433 | + * @param string $field 字段名 | ||
| 434 | + * @param array $value 数据 | ||
| 435 | + * @return static | ||
| 436 | + */ | ||
| 437 | + public function whereIn(string $field, array $value) | ||
| 438 | + { | ||
| 439 | + return $this->where($field, 'in', $value); | ||
| 440 | + } | ||
| 441 | + | ||
| 442 | + /** | ||
| 443 | + * NOT IN过滤 | ||
| 444 | + * @access public | ||
| 445 | + * @param string $field 字段名 | ||
| 446 | + * @param array $value 数据 | ||
| 447 | + * @return static | ||
| 448 | + */ | ||
| 449 | + public function whereNotIn(string $field, array $value) | ||
| 450 | + { | ||
| 451 | + return $this->where($field, 'not in', $value); | ||
| 452 | + } | ||
| 453 | + | ||
| 454 | + /** | ||
| 455 | + * BETWEEN 过滤 | ||
| 456 | + * @access public | ||
| 457 | + * @param string $field 字段名 | ||
| 458 | + * @param mixed $value 数据 | ||
| 459 | + * @return static | ||
| 460 | + */ | ||
| 461 | + public function whereBetween(string $field, $value) | ||
| 462 | + { | ||
| 463 | + return $this->where($field, 'between', $value); | ||
| 464 | + } | ||
| 465 | + | ||
| 466 | + /** | ||
| 467 | + * NOT BETWEEN 过滤 | ||
| 468 | + * @access public | ||
| 469 | + * @param string $field 字段名 | ||
| 470 | + * @param mixed $value 数据 | ||
| 471 | + * @return static | ||
| 472 | + */ | ||
| 473 | + public function whereNotBetween(string $field, $value) | ||
| 474 | + { | ||
| 475 | + return $this->where($field, 'not between', $value); | ||
| 476 | + } | ||
| 477 | + | ||
| 478 | + /** | ||
| 479 | + * 返回数据中指定的一列 | ||
| 480 | + * @access public | ||
| 481 | + * @param string|null $columnKey 键名 | ||
| 482 | + * @param string|null $indexKey 作为索引值的列 | ||
| 483 | + * @return array | ||
| 484 | + */ | ||
| 485 | + public function column( ? string $columnKey, string $indexKey = null) | ||
| 486 | + { | ||
| 487 | + return array_column($this->items, $columnKey, $indexKey); | ||
| 488 | + } | ||
| 489 | + | ||
| 490 | + /** | ||
| 491 | + * 对数组排序 | ||
| 492 | + * | ||
| 493 | + * @access public | ||
| 494 | + * @param callable|null $callback 回调 | ||
| 495 | + * @return static | ||
| 496 | + */ | ||
| 497 | + public function sort(callable $callback = null) | ||
| 498 | + { | ||
| 499 | + $items = $this->items; | ||
| 500 | + | ||
| 501 | + $callback = $callback ?: function ($a, $b) { | ||
| 502 | + return $a == $b ? 0 : (($a < $b) ? -1 : 1); | ||
| 503 | + }; | ||
| 504 | + | ||
| 505 | + uasort($items, $callback); | ||
| 506 | + | ||
| 507 | + return new static($items); | ||
| 508 | + } | ||
| 509 | + | ||
| 510 | + /** | ||
| 511 | + * 指定字段排序 | ||
| 512 | + * @access public | ||
| 513 | + * @param string $field 排序字段 | ||
| 514 | + * @param string $order 排序 | ||
| 515 | + * @return $this | ||
| 516 | + */ | ||
| 517 | + public function order(string $field, string $order = 'asc') | ||
| 518 | + { | ||
| 519 | + return $this->sort(function ($a, $b) use ($field, $order) { | ||
| 520 | + $fieldA = $a[$field] ?? null; | ||
| 521 | + $fieldB = $b[$field] ?? null; | ||
| 522 | + | ||
| 523 | + return 'desc' == strtolower($order) ? intval($fieldB > $fieldA) : intval($fieldA > $fieldB); | ||
| 524 | + }); | ||
| 525 | + } | ||
| 526 | + | ||
| 527 | + /** | ||
| 528 | + * 将数组打乱 | ||
| 529 | + * | ||
| 530 | + * @access public | ||
| 531 | + * @return static | ||
| 532 | + */ | ||
| 533 | + public function shuffle() | ||
| 534 | + { | ||
| 535 | + $items = $this->items; | ||
| 536 | + | ||
| 537 | + shuffle($items); | ||
| 538 | + | ||
| 539 | + return new static($items); | ||
| 540 | + } | ||
| 541 | + | ||
| 542 | + /** | ||
| 543 | + * 获取第一个单元数据 | ||
| 544 | + * | ||
| 545 | + * @access public | ||
| 546 | + * @param callable|null $callback | ||
| 547 | + * @param null $default | ||
| 548 | + * @return mixed | ||
| 549 | + */ | ||
| 550 | + public function first(callable $callback = null, $default = null) | ||
| 551 | + { | ||
| 552 | + return Arr::first($this->items, $callback, $default); | ||
| 553 | + } | ||
| 554 | + | ||
| 555 | + /** | ||
| 556 | + * 获取最后一个单元数据 | ||
| 557 | + * | ||
| 558 | + * @access public | ||
| 559 | + * @param callable|null $callback | ||
| 560 | + * @param null $default | ||
| 561 | + * @return mixed | ||
| 562 | + */ | ||
| 563 | + public function last(callable $callback = null, $default = null) | ||
| 564 | + { | ||
| 565 | + return Arr::last($this->items, $callback, $default); | ||
| 566 | + } | ||
| 567 | + | ||
| 568 | + /** | ||
| 569 | + * 截取数组 | ||
| 570 | + * | ||
| 571 | + * @access public | ||
| 572 | + * @param int $offset 起始位置 | ||
| 573 | + * @param int $length 截取长度 | ||
| 574 | + * @param bool $preserveKeys preserveKeys | ||
| 575 | + * @return static | ||
| 576 | + */ | ||
| 577 | + public function slice(int $offset, int $length = null, bool $preserveKeys = false) | ||
| 578 | + { | ||
| 579 | + return new static(array_slice($this->items, $offset, $length, $preserveKeys)); | ||
| 580 | + } | ||
| 581 | + | ||
| 582 | + // ArrayAccess | ||
| 583 | + #[\ReturnTypeWillChange] | ||
| 584 | + public function offsetExists($offset) : bool | ||
| 585 | + { | ||
| 586 | + return array_key_exists($offset, $this->items); | ||
| 587 | + } | ||
| 588 | + | ||
| 589 | + #[\ReturnTypeWillChange] | ||
| 590 | + public function offsetGet($offset) | ||
| 591 | + { | ||
| 592 | + return $this->items[$offset]; | ||
| 593 | + } | ||
| 594 | + | ||
| 595 | + #[\ReturnTypeWillChange] | ||
| 596 | + public function offsetSet($offset, $value) | ||
| 597 | + { | ||
| 598 | + if (is_null($offset)) { | ||
| 599 | + $this->items[] = $value; | ||
| 600 | + } else { | ||
| 601 | + $this->items[$offset] = $value; | ||
| 602 | + } | ||
| 603 | + } | ||
| 604 | + | ||
| 605 | + #[\ReturnTypeWillChange] | ||
| 606 | + public function offsetUnset($offset) | ||
| 607 | + { | ||
| 608 | + unset($this->items[$offset]); | ||
| 609 | + } | ||
| 610 | + | ||
| 611 | + //Countable | ||
| 612 | + public function count(): int | ||
| 613 | + { | ||
| 614 | + return count($this->items); | ||
| 615 | + } | ||
| 616 | + | ||
| 617 | + //IteratorAggregate | ||
| 618 | + #[\ReturnTypeWillChange] | ||
| 619 | + public function getIterator(): Traversable | ||
| 620 | + { | ||
| 621 | + return new ArrayIterator($this->items); | ||
| 622 | + } | ||
| 623 | + | ||
| 624 | + //JsonSerializable | ||
| 625 | + #[\ReturnTypeWillChange] | ||
| 626 | + public function jsonSerialize() | ||
| 627 | + { | ||
| 628 | + return $this->toArray(); | ||
| 629 | + } | ||
| 630 | + | ||
| 631 | + /** | ||
| 632 | + * 转换当前数据集为JSON字符串 | ||
| 633 | + * @access public | ||
| 634 | + * @param integer $options json参数 | ||
| 635 | + * @return string | ||
| 636 | + */ | ||
| 637 | + public function toJson(int $options = JSON_UNESCAPED_UNICODE): string | ||
| 638 | + { | ||
| 639 | + return json_encode($this->toArray(), $options); | ||
| 640 | + } | ||
| 641 | + | ||
| 642 | + public function __toString() | ||
| 643 | + { | ||
| 644 | + return $this->toJson(); | ||
| 645 | + } | ||
| 646 | + | ||
| 647 | + /** | ||
| 648 | + * 转换成数组 | ||
| 649 | + * | ||
| 650 | + * @access public | ||
| 651 | + * @param mixed $items 数据 | ||
| 652 | + * @return array | ||
| 653 | + */ | ||
| 654 | + protected function convertToArray($items): array | ||
| 655 | + { | ||
| 656 | + if ($items instanceof self) { | ||
| 657 | + return $items->all(); | ||
| 658 | + } | ||
| 659 | + | ||
| 660 | + return (array) $items; | ||
| 661 | + } | ||
| 662 | +} |
vendor/topthink/think-helper/src/helper.php
0 → 100644
| 1 | +<?php | ||
| 2 | +// +---------------------------------------------------------------------- | ||
| 3 | +// | ThinkPHP [ WE CAN DO IT JUST THINK IT ] | ||
| 4 | +// +---------------------------------------------------------------------- | ||
| 5 | +// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved. | ||
| 6 | +// +---------------------------------------------------------------------- | ||
| 7 | +// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) | ||
| 8 | +// +---------------------------------------------------------------------- | ||
| 9 | +// | Author: yunwuxin <448901948@qq.com> | ||
| 10 | +// +---------------------------------------------------------------------- | ||
| 11 | + | ||
| 12 | +use think\Collection; | ||
| 13 | +use think\helper\Arr; | ||
| 14 | + | ||
| 15 | +if (!function_exists('throw_if')) { | ||
| 16 | + /** | ||
| 17 | + * 按条件抛异常 | ||
| 18 | + * | ||
| 19 | + * @param mixed $condition | ||
| 20 | + * @param Throwable|string $exception | ||
| 21 | + * @param array ...$parameters | ||
| 22 | + * @return mixed | ||
| 23 | + * | ||
| 24 | + * @throws Throwable | ||
| 25 | + */ | ||
| 26 | + function throw_if($condition, $exception, ...$parameters) | ||
| 27 | + { | ||
| 28 | + if ($condition) { | ||
| 29 | + throw (is_string($exception) ? new $exception(...$parameters) : $exception); | ||
| 30 | + } | ||
| 31 | + | ||
| 32 | + return $condition; | ||
| 33 | + } | ||
| 34 | +} | ||
| 35 | + | ||
| 36 | +if (!function_exists('throw_unless')) { | ||
| 37 | + /** | ||
| 38 | + * 按条件抛异常 | ||
| 39 | + * | ||
| 40 | + * @param mixed $condition | ||
| 41 | + * @param Throwable|string $exception | ||
| 42 | + * @param array ...$parameters | ||
| 43 | + * @return mixed | ||
| 44 | + * @throws Throwable | ||
| 45 | + */ | ||
| 46 | + function throw_unless($condition, $exception, ...$parameters) | ||
| 47 | + { | ||
| 48 | + if (!$condition) { | ||
| 49 | + throw (is_string($exception) ? new $exception(...$parameters) : $exception); | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + return $condition; | ||
| 53 | + } | ||
| 54 | +} | ||
| 55 | + | ||
| 56 | +if (!function_exists('tap')) { | ||
| 57 | + /** | ||
| 58 | + * 对一个值调用给定的闭包,然后返回该值 | ||
| 59 | + * | ||
| 60 | + * @param mixed $value | ||
| 61 | + * @param callable|null $callback | ||
| 62 | + * @return mixed | ||
| 63 | + */ | ||
| 64 | + function tap($value, $callback = null) | ||
| 65 | + { | ||
| 66 | + if (is_null($callback)) { | ||
| 67 | + return $value; | ||
| 68 | + } | ||
| 69 | + | ||
| 70 | + $callback($value); | ||
| 71 | + | ||
| 72 | + return $value; | ||
| 73 | + } | ||
| 74 | +} | ||
| 75 | + | ||
| 76 | +if (!function_exists('value')) { | ||
| 77 | + /** | ||
| 78 | + * Return the default value of the given value. | ||
| 79 | + * | ||
| 80 | + * @param mixed $value | ||
| 81 | + * @return mixed | ||
| 82 | + */ | ||
| 83 | + function value($value) | ||
| 84 | + { | ||
| 85 | + return $value instanceof Closure ? $value() : $value; | ||
| 86 | + } | ||
| 87 | +} | ||
| 88 | + | ||
| 89 | +if (!function_exists('collect')) { | ||
| 90 | + /** | ||
| 91 | + * Create a collection from the given value. | ||
| 92 | + * | ||
| 93 | + * @param mixed $value | ||
| 94 | + * @return Collection | ||
| 95 | + */ | ||
| 96 | + function collect($value = null) | ||
| 97 | + { | ||
| 98 | + return new Collection($value); | ||
| 99 | + } | ||
| 100 | +} | ||
| 101 | + | ||
| 102 | +if (!function_exists('data_fill')) { | ||
| 103 | + /** | ||
| 104 | + * Fill in data where it's missing. | ||
| 105 | + * | ||
| 106 | + * @param mixed $target | ||
| 107 | + * @param string|array $key | ||
| 108 | + * @param mixed $value | ||
| 109 | + * @return mixed | ||
| 110 | + */ | ||
| 111 | + function data_fill(&$target, $key, $value) | ||
| 112 | + { | ||
| 113 | + return data_set($target, $key, $value, false); | ||
| 114 | + } | ||
| 115 | +} | ||
| 116 | + | ||
| 117 | +if (!function_exists('data_get')) { | ||
| 118 | + /** | ||
| 119 | + * Get an item from an array or object using "dot" notation. | ||
| 120 | + * | ||
| 121 | + * @param mixed $target | ||
| 122 | + * @param string|array|int $key | ||
| 123 | + * @param mixed $default | ||
| 124 | + * @return mixed | ||
| 125 | + */ | ||
| 126 | + function data_get($target, $key, $default = null) | ||
| 127 | + { | ||
| 128 | + if (is_null($key)) { | ||
| 129 | + return $target; | ||
| 130 | + } | ||
| 131 | + | ||
| 132 | + $key = is_array($key) ? $key : explode('.', $key); | ||
| 133 | + | ||
| 134 | + while (!is_null($segment = array_shift($key))) { | ||
| 135 | + if ('*' === $segment) { | ||
| 136 | + if ($target instanceof Collection) { | ||
| 137 | + $target = $target->all(); | ||
| 138 | + } elseif (!is_array($target)) { | ||
| 139 | + return value($default); | ||
| 140 | + } | ||
| 141 | + | ||
| 142 | + $result = []; | ||
| 143 | + | ||
| 144 | + foreach ($target as $item) { | ||
| 145 | + $result[] = data_get($item, $key); | ||
| 146 | + } | ||
| 147 | + | ||
| 148 | + return in_array('*', $key) ? Arr::collapse($result) : $result; | ||
| 149 | + } | ||
| 150 | + | ||
| 151 | + if (Arr::accessible($target) && Arr::exists($target, $segment)) { | ||
| 152 | + $target = $target[$segment]; | ||
| 153 | + } elseif (is_object($target) && isset($target->{$segment})) { | ||
| 154 | + $target = $target->{$segment}; | ||
| 155 | + } else { | ||
| 156 | + return value($default); | ||
| 157 | + } | ||
| 158 | + } | ||
| 159 | + | ||
| 160 | + return $target; | ||
| 161 | + } | ||
| 162 | +} | ||
| 163 | + | ||
| 164 | +if (!function_exists('data_set')) { | ||
| 165 | + /** | ||
| 166 | + * Set an item on an array or object using dot notation. | ||
| 167 | + * | ||
| 168 | + * @param mixed $target | ||
| 169 | + * @param string|array $key | ||
| 170 | + * @param mixed $value | ||
| 171 | + * @param bool $overwrite | ||
| 172 | + * @return mixed | ||
| 173 | + */ | ||
| 174 | + function data_set(&$target, $key, $value, $overwrite = true) | ||
| 175 | + { | ||
| 176 | + $segments = is_array($key) ? $key : explode('.', $key); | ||
| 177 | + | ||
| 178 | + if (($segment = array_shift($segments)) === '*') { | ||
| 179 | + if (!Arr::accessible($target)) { | ||
| 180 | + $target = []; | ||
| 181 | + } | ||
| 182 | + | ||
| 183 | + if ($segments) { | ||
| 184 | + foreach ($target as &$inner) { | ||
| 185 | + data_set($inner, $segments, $value, $overwrite); | ||
| 186 | + } | ||
| 187 | + } elseif ($overwrite) { | ||
| 188 | + foreach ($target as &$inner) { | ||
| 189 | + $inner = $value; | ||
| 190 | + } | ||
| 191 | + } | ||
| 192 | + } elseif (Arr::accessible($target)) { | ||
| 193 | + if ($segments) { | ||
| 194 | + if (!Arr::exists($target, $segment)) { | ||
| 195 | + $target[$segment] = []; | ||
| 196 | + } | ||
| 197 | + | ||
| 198 | + data_set($target[$segment], $segments, $value, $overwrite); | ||
| 199 | + } elseif ($overwrite || !Arr::exists($target, $segment)) { | ||
| 200 | + $target[$segment] = $value; | ||
| 201 | + } | ||
| 202 | + } elseif (is_object($target)) { | ||
| 203 | + if ($segments) { | ||
| 204 | + if (!isset($target->{$segment})) { | ||
| 205 | + $target->{$segment} = []; | ||
| 206 | + } | ||
| 207 | + | ||
| 208 | + data_set($target->{$segment}, $segments, $value, $overwrite); | ||
| 209 | + } elseif ($overwrite || !isset($target->{$segment})) { | ||
| 210 | + $target->{$segment} = $value; | ||
| 211 | + } | ||
| 212 | + } else { | ||
| 213 | + $target = []; | ||
| 214 | + | ||
| 215 | + if ($segments) { | ||
| 216 | + data_set($target[$segment], $segments, $value, $overwrite); | ||
| 217 | + } elseif ($overwrite) { | ||
| 218 | + $target[$segment] = $value; | ||
| 219 | + } | ||
| 220 | + } | ||
| 221 | + | ||
| 222 | + return $target; | ||
| 223 | + } | ||
| 224 | +} | ||
| 225 | + | ||
| 226 | +if (!function_exists('trait_uses_recursive')) { | ||
| 227 | + /** | ||
| 228 | + * 获取一个trait里所有引用到的trait | ||
| 229 | + * | ||
| 230 | + * @param string $trait Trait | ||
| 231 | + * @return array | ||
| 232 | + */ | ||
| 233 | + function trait_uses_recursive(string $trait): array | ||
| 234 | + { | ||
| 235 | + $traits = class_uses($trait); | ||
| 236 | + foreach ($traits as $trait) { | ||
| 237 | + $traits += trait_uses_recursive($trait); | ||
| 238 | + } | ||
| 239 | + | ||
| 240 | + return $traits; | ||
| 241 | + } | ||
| 242 | +} | ||
| 243 | + | ||
| 244 | +if (!function_exists('class_basename')) { | ||
| 245 | + /** | ||
| 246 | + * 获取类名(不包含命名空间) | ||
| 247 | + * | ||
| 248 | + * @param mixed $class 类名 | ||
| 249 | + * @return string | ||
| 250 | + */ | ||
| 251 | + function class_basename($class): string | ||
| 252 | + { | ||
| 253 | + $class = is_object($class) ? get_class($class) : $class; | ||
| 254 | + return basename(str_replace('\\', '/', $class)); | ||
| 255 | + } | ||
| 256 | +} | ||
| 257 | + | ||
| 258 | +if (!function_exists('class_uses_recursive')) { | ||
| 259 | + /** | ||
| 260 | + *获取一个类里所有用到的trait,包括父类的 | ||
| 261 | + * | ||
| 262 | + * @param mixed $class 类名 | ||
| 263 | + * @return array | ||
| 264 | + */ | ||
| 265 | + function class_uses_recursive($class): array | ||
| 266 | + { | ||
| 267 | + if (is_object($class)) { | ||
| 268 | + $class = get_class($class); | ||
| 269 | + } | ||
| 270 | + | ||
| 271 | + $results = []; | ||
| 272 | + $classes = array_merge([$class => $class], class_parents($class)); | ||
| 273 | + foreach ($classes as $class) { | ||
| 274 | + $results += trait_uses_recursive($class); | ||
| 275 | + } | ||
| 276 | + | ||
| 277 | + return array_unique($results); | ||
| 278 | + } | ||
| 279 | +} |
| 1 | +<?php | ||
| 2 | +// +---------------------------------------------------------------------- | ||
| 3 | +// | ThinkPHP [ WE CAN DO IT JUST THINK IT ] | ||
| 4 | +// +---------------------------------------------------------------------- | ||
| 5 | +// | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved. | ||
| 6 | +// +---------------------------------------------------------------------- | ||
| 7 | +// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) | ||
| 8 | +// +---------------------------------------------------------------------- | ||
| 9 | +// | Author: yunwuxin <448901948@qq.com> | ||
| 10 | +// +---------------------------------------------------------------------- | ||
| 11 | + | ||
| 12 | +namespace think\helper; | ||
| 13 | + | ||
| 14 | +use ArrayAccess; | ||
| 15 | +use InvalidArgumentException; | ||
| 16 | +use think\Collection; | ||
| 17 | + | ||
| 18 | +class Arr | ||
| 19 | +{ | ||
| 20 | + | ||
| 21 | + /** | ||
| 22 | + * Determine whether the given value is array accessible. | ||
| 23 | + * | ||
| 24 | + * @param mixed $value | ||
| 25 | + * @return bool | ||
| 26 | + */ | ||
| 27 | + public static function accessible($value) | ||
| 28 | + { | ||
| 29 | + return is_array($value) || $value instanceof ArrayAccess; | ||
| 30 | + } | ||
| 31 | + | ||
| 32 | + /** | ||
| 33 | + * Add an element to an array using "dot" notation if it doesn't exist. | ||
| 34 | + * | ||
| 35 | + * @param array $array | ||
| 36 | + * @param string $key | ||
| 37 | + * @param mixed $value | ||
| 38 | + * @return array | ||
| 39 | + */ | ||
| 40 | + public static function add($array, $key, $value) | ||
| 41 | + { | ||
| 42 | + if (is_null(static::get($array, $key))) { | ||
| 43 | + static::set($array, $key, $value); | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + return $array; | ||
| 47 | + } | ||
| 48 | + | ||
| 49 | + /** | ||
| 50 | + * Collapse an array of arrays into a single array. | ||
| 51 | + * | ||
| 52 | + * @param array $array | ||
| 53 | + * @return array | ||
| 54 | + */ | ||
| 55 | + public static function collapse($array) | ||
| 56 | + { | ||
| 57 | + $results = []; | ||
| 58 | + | ||
| 59 | + foreach ($array as $values) { | ||
| 60 | + if ($values instanceof Collection) { | ||
| 61 | + $values = $values->all(); | ||
| 62 | + } elseif (!is_array($values)) { | ||
| 63 | + continue; | ||
| 64 | + } | ||
| 65 | + | ||
| 66 | + $results = array_merge($results, $values); | ||
| 67 | + } | ||
| 68 | + | ||
| 69 | + return $results; | ||
| 70 | + } | ||
| 71 | + | ||
| 72 | + /** | ||
| 73 | + * Cross join the given arrays, returning all possible permutations. | ||
| 74 | + * | ||
| 75 | + * @param array ...$arrays | ||
| 76 | + * @return array | ||
| 77 | + */ | ||
| 78 | + public static function crossJoin(...$arrays) | ||
| 79 | + { | ||
| 80 | + $results = [[]]; | ||
| 81 | + | ||
| 82 | + foreach ($arrays as $index => $array) { | ||
| 83 | + $append = []; | ||
| 84 | + | ||
| 85 | + foreach ($results as $product) { | ||
| 86 | + foreach ($array as $item) { | ||
| 87 | + $product[$index] = $item; | ||
| 88 | + | ||
| 89 | + $append[] = $product; | ||
| 90 | + } | ||
| 91 | + } | ||
| 92 | + | ||
| 93 | + $results = $append; | ||
| 94 | + } | ||
| 95 | + | ||
| 96 | + return $results; | ||
| 97 | + } | ||
| 98 | + | ||
| 99 | + /** | ||
| 100 | + * Divide an array into two arrays. One with keys and the other with values. | ||
| 101 | + * | ||
| 102 | + * @param array $array | ||
| 103 | + * @return array | ||
| 104 | + */ | ||
| 105 | + public static function divide($array) | ||
| 106 | + { | ||
| 107 | + return [array_keys($array), array_values($array)]; | ||
| 108 | + } | ||
| 109 | + | ||
| 110 | + /** | ||
| 111 | + * Flatten a multi-dimensional associative array with dots. | ||
| 112 | + * | ||
| 113 | + * @param array $array | ||
| 114 | + * @param string $prepend | ||
| 115 | + * @return array | ||
| 116 | + */ | ||
| 117 | + public static function dot($array, $prepend = '') | ||
| 118 | + { | ||
| 119 | + $results = []; | ||
| 120 | + | ||
| 121 | + foreach ($array as $key => $value) { | ||
| 122 | + if (is_array($value) && !empty($value)) { | ||
| 123 | + $results = array_merge($results, static::dot($value, $prepend . $key . '.')); | ||
| 124 | + } else { | ||
| 125 | + $results[$prepend . $key] = $value; | ||
| 126 | + } | ||
| 127 | + } | ||
| 128 | + | ||
| 129 | + return $results; | ||
| 130 | + } | ||
| 131 | + | ||
| 132 | + /** | ||
| 133 | + * Get all of the given array except for a specified array of keys. | ||
| 134 | + * | ||
| 135 | + * @param array $array | ||
| 136 | + * @param array|string $keys | ||
| 137 | + * @return array | ||
| 138 | + */ | ||
| 139 | + public static function except($array, $keys) | ||
| 140 | + { | ||
| 141 | + static::forget($array, $keys); | ||
| 142 | + | ||
| 143 | + return $array; | ||
| 144 | + } | ||
| 145 | + | ||
| 146 | + /** | ||
| 147 | + * Determine if the given key exists in the provided array. | ||
| 148 | + * | ||
| 149 | + * @param \ArrayAccess|array $array | ||
| 150 | + * @param string|int $key | ||
| 151 | + * @return bool | ||
| 152 | + */ | ||
| 153 | + public static function exists($array, $key) | ||
| 154 | + { | ||
| 155 | + if ($array instanceof ArrayAccess) { | ||
| 156 | + return $array->offsetExists($key); | ||
| 157 | + } | ||
| 158 | + | ||
| 159 | + return array_key_exists($key, $array); | ||
| 160 | + } | ||
| 161 | + | ||
| 162 | + /** | ||
| 163 | + * Return the first element in an array passing a given truth test. | ||
| 164 | + * | ||
| 165 | + * @param array $array | ||
| 166 | + * @param callable|null $callback | ||
| 167 | + * @param mixed $default | ||
| 168 | + * @return mixed | ||
| 169 | + */ | ||
| 170 | + public static function first($array, callable $callback = null, $default = null) | ||
| 171 | + { | ||
| 172 | + if (is_null($callback)) { | ||
| 173 | + if (empty($array)) { | ||
| 174 | + return value($default); | ||
| 175 | + } | ||
| 176 | + | ||
| 177 | + foreach ($array as $item) { | ||
| 178 | + return $item; | ||
| 179 | + } | ||
| 180 | + } | ||
| 181 | + | ||
| 182 | + foreach ($array as $key => $value) { | ||
| 183 | + if (call_user_func($callback, $value, $key)) { | ||
| 184 | + return $value; | ||
| 185 | + } | ||
| 186 | + } | ||
| 187 | + | ||
| 188 | + return value($default); | ||
| 189 | + } | ||
| 190 | + | ||
| 191 | + /** | ||
| 192 | + * Return the last element in an array passing a given truth test. | ||
| 193 | + * | ||
| 194 | + * @param array $array | ||
| 195 | + * @param callable|null $callback | ||
| 196 | + * @param mixed $default | ||
| 197 | + * @return mixed | ||
| 198 | + */ | ||
| 199 | + public static function last($array, callable $callback = null, $default = null) | ||
| 200 | + { | ||
| 201 | + if (is_null($callback)) { | ||
| 202 | + return empty($array) ? value($default) : end($array); | ||
| 203 | + } | ||
| 204 | + | ||
| 205 | + return static::first(array_reverse($array, true), $callback, $default); | ||
| 206 | + } | ||
| 207 | + | ||
| 208 | + /** | ||
| 209 | + * Flatten a multi-dimensional array into a single level. | ||
| 210 | + * | ||
| 211 | + * @param array $array | ||
| 212 | + * @param int $depth | ||
| 213 | + * @return array | ||
| 214 | + */ | ||
| 215 | + public static function flatten($array, $depth = INF) | ||
| 216 | + { | ||
| 217 | + $result = []; | ||
| 218 | + | ||
| 219 | + foreach ($array as $item) { | ||
| 220 | + $item = $item instanceof Collection ? $item->all() : $item; | ||
| 221 | + | ||
| 222 | + if (!is_array($item)) { | ||
| 223 | + $result[] = $item; | ||
| 224 | + } elseif ($depth === 1) { | ||
| 225 | + $result = array_merge($result, array_values($item)); | ||
| 226 | + } else { | ||
| 227 | + $result = array_merge($result, static::flatten($item, $depth - 1)); | ||
| 228 | + } | ||
| 229 | + } | ||
| 230 | + | ||
| 231 | + return $result; | ||
| 232 | + } | ||
| 233 | + | ||
| 234 | + /** | ||
| 235 | + * Remove one or many array items from a given array using "dot" notation. | ||
| 236 | + * | ||
| 237 | + * @param array $array | ||
| 238 | + * @param array|string $keys | ||
| 239 | + * @return void | ||
| 240 | + */ | ||
| 241 | + public static function forget(&$array, $keys) | ||
| 242 | + { | ||
| 243 | + $original = &$array; | ||
| 244 | + | ||
| 245 | + $keys = (array) $keys; | ||
| 246 | + | ||
| 247 | + if (count($keys) === 0) { | ||
| 248 | + return; | ||
| 249 | + } | ||
| 250 | + | ||
| 251 | + foreach ($keys as $key) { | ||
| 252 | + // if the exact key exists in the top-level, remove it | ||
| 253 | + if (static::exists($array, $key)) { | ||
| 254 | + unset($array[$key]); | ||
| 255 | + | ||
| 256 | + continue; | ||
| 257 | + } | ||
| 258 | + | ||
| 259 | + $parts = explode('.', $key); | ||
| 260 | + | ||
| 261 | + // clean up before each pass | ||
| 262 | + $array = &$original; | ||
| 263 | + | ||
| 264 | + while (count($parts) > 1) { | ||
| 265 | + $part = array_shift($parts); | ||
| 266 | + | ||
| 267 | + if (isset($array[$part]) && is_array($array[$part])) { | ||
| 268 | + $array = &$array[$part]; | ||
| 269 | + } else { | ||
| 270 | + continue 2; | ||
| 271 | + } | ||
| 272 | + } | ||
| 273 | + | ||
| 274 | + unset($array[array_shift($parts)]); | ||
| 275 | + } | ||
| 276 | + } | ||
| 277 | + | ||
| 278 | + /** | ||
| 279 | + * Get an item from an array using "dot" notation. | ||
| 280 | + * | ||
| 281 | + * @param \ArrayAccess|array $array | ||
| 282 | + * @param string $key | ||
| 283 | + * @param mixed $default | ||
| 284 | + * @return mixed | ||
| 285 | + */ | ||
| 286 | + public static function get($array, $key, $default = null) | ||
| 287 | + { | ||
| 288 | + if (!static::accessible($array)) { | ||
| 289 | + return value($default); | ||
| 290 | + } | ||
| 291 | + | ||
| 292 | + if (is_null($key)) { | ||
| 293 | + return $array; | ||
| 294 | + } | ||
| 295 | + | ||
| 296 | + if (static::exists($array, $key)) { | ||
| 297 | + return $array[$key]; | ||
| 298 | + } | ||
| 299 | + | ||
| 300 | + if (strpos($key, '.') === false) { | ||
| 301 | + return $array[$key] ?? value($default); | ||
| 302 | + } | ||
| 303 | + | ||
| 304 | + foreach (explode('.', $key) as $segment) { | ||
| 305 | + if (static::accessible($array) && static::exists($array, $segment)) { | ||
| 306 | + $array = $array[$segment]; | ||
| 307 | + } else { | ||
| 308 | + return value($default); | ||
| 309 | + } | ||
| 310 | + } | ||
| 311 | + | ||
| 312 | + return $array; | ||
| 313 | + } | ||
| 314 | + | ||
| 315 | + /** | ||
| 316 | + * Check if an item or items exist in an array using "dot" notation. | ||
| 317 | + * | ||
| 318 | + * @param \ArrayAccess|array $array | ||
| 319 | + * @param string|array $keys | ||
| 320 | + * @return bool | ||
| 321 | + */ | ||
| 322 | + public static function has($array, $keys) | ||
| 323 | + { | ||
| 324 | + $keys = (array) $keys; | ||
| 325 | + | ||
| 326 | + if (!$array || $keys === []) { | ||
| 327 | + return false; | ||
| 328 | + } | ||
| 329 | + | ||
| 330 | + foreach ($keys as $key) { | ||
| 331 | + $subKeyArray = $array; | ||
| 332 | + | ||
| 333 | + if (static::exists($array, $key)) { | ||
| 334 | + continue; | ||
| 335 | + } | ||
| 336 | + | ||
| 337 | + foreach (explode('.', $key) as $segment) { | ||
| 338 | + if (static::accessible($subKeyArray) && static::exists($subKeyArray, $segment)) { | ||
| 339 | + $subKeyArray = $subKeyArray[$segment]; | ||
| 340 | + } else { | ||
| 341 | + return false; | ||
| 342 | + } | ||
| 343 | + } | ||
| 344 | + } | ||
| 345 | + | ||
| 346 | + return true; | ||
| 347 | + } | ||
| 348 | + | ||
| 349 | + /** | ||
| 350 | + * Determines if an array is associative. | ||
| 351 | + * | ||
| 352 | + * An array is "associative" if it doesn't have sequential numerical keys beginning with zero. | ||
| 353 | + * | ||
| 354 | + * @param array $array | ||
| 355 | + * @return bool | ||
| 356 | + */ | ||
| 357 | + public static function isAssoc(array $array) | ||
| 358 | + { | ||
| 359 | + $keys = array_keys($array); | ||
| 360 | + | ||
| 361 | + return array_keys($keys) !== $keys; | ||
| 362 | + } | ||
| 363 | + | ||
| 364 | + /** | ||
| 365 | + * Get a subset of the items from the given array. | ||
| 366 | + * | ||
| 367 | + * @param array $array | ||
| 368 | + * @param array|string $keys | ||
| 369 | + * @return array | ||
| 370 | + */ | ||
| 371 | + public static function only($array, $keys) | ||
| 372 | + { | ||
| 373 | + return array_intersect_key($array, array_flip((array) $keys)); | ||
| 374 | + } | ||
| 375 | + | ||
| 376 | + /** | ||
| 377 | + * Pluck an array of values from an array. | ||
| 378 | + * | ||
| 379 | + * @param array $array | ||
| 380 | + * @param string|array $value | ||
| 381 | + * @param string|array|null $key | ||
| 382 | + * @return array | ||
| 383 | + */ | ||
| 384 | + public static function pluck($array, $value, $key = null) | ||
| 385 | + { | ||
| 386 | + $results = []; | ||
| 387 | + | ||
| 388 | + [$value, $key] = static::explodePluckParameters($value, $key); | ||
| 389 | + | ||
| 390 | + foreach ($array as $item) { | ||
| 391 | + $itemValue = data_get($item, $value); | ||
| 392 | + | ||
| 393 | + // If the key is "null", we will just append the value to the array and keep | ||
| 394 | + // looping. Otherwise we will key the array using the value of the key we | ||
| 395 | + // received from the developer. Then we'll return the final array form. | ||
| 396 | + if (is_null($key)) { | ||
| 397 | + $results[] = $itemValue; | ||
| 398 | + } else { | ||
| 399 | + $itemKey = data_get($item, $key); | ||
| 400 | + | ||
| 401 | + if (is_object($itemKey) && method_exists($itemKey, '__toString')) { | ||
| 402 | + $itemKey = (string) $itemKey; | ||
| 403 | + } | ||
| 404 | + | ||
| 405 | + $results[$itemKey] = $itemValue; | ||
| 406 | + } | ||
| 407 | + } | ||
| 408 | + | ||
| 409 | + return $results; | ||
| 410 | + } | ||
| 411 | + | ||
| 412 | + /** | ||
| 413 | + * Explode the "value" and "key" arguments passed to "pluck". | ||
| 414 | + * | ||
| 415 | + * @param string|array $value | ||
| 416 | + * @param string|array|null $key | ||
| 417 | + * @return array | ||
| 418 | + */ | ||
| 419 | + protected static function explodePluckParameters($value, $key) | ||
| 420 | + { | ||
| 421 | + $value = is_string($value) ? explode('.', $value) : $value; | ||
| 422 | + | ||
| 423 | + $key = is_null($key) || is_array($key) ? $key : explode('.', $key); | ||
| 424 | + | ||
| 425 | + return [$value, $key]; | ||
| 426 | + } | ||
| 427 | + | ||
| 428 | + /** | ||
| 429 | + * Push an item onto the beginning of an array. | ||
| 430 | + * | ||
| 431 | + * @param array $array | ||
| 432 | + * @param mixed $value | ||
| 433 | + * @param mixed $key | ||
| 434 | + * @return array | ||
| 435 | + */ | ||
| 436 | + public static function prepend($array, $value, $key = null) | ||
| 437 | + { | ||
| 438 | + if (is_null($key)) { | ||
| 439 | + array_unshift($array, $value); | ||
| 440 | + } else { | ||
| 441 | + $array = [$key => $value] + $array; | ||
| 442 | + } | ||
| 443 | + | ||
| 444 | + return $array; | ||
| 445 | + } | ||
| 446 | + | ||
| 447 | + /** | ||
| 448 | + * Get a value from the array, and remove it. | ||
| 449 | + * | ||
| 450 | + * @param array $array | ||
| 451 | + * @param string $key | ||
| 452 | + * @param mixed $default | ||
| 453 | + * @return mixed | ||
| 454 | + */ | ||
| 455 | + public static function pull(&$array, $key, $default = null) | ||
| 456 | + { | ||
| 457 | + $value = static::get($array, $key, $default); | ||
| 458 | + | ||
| 459 | + static::forget($array, $key); | ||
| 460 | + | ||
| 461 | + return $value; | ||
| 462 | + } | ||
| 463 | + | ||
| 464 | + /** | ||
| 465 | + * Get one or a specified number of random values from an array. | ||
| 466 | + * | ||
| 467 | + * @param array $array | ||
| 468 | + * @param int|null $number | ||
| 469 | + * @return mixed | ||
| 470 | + * | ||
| 471 | + * @throws \InvalidArgumentException | ||
| 472 | + */ | ||
| 473 | + public static function random($array, $number = null) | ||
| 474 | + { | ||
| 475 | + $requested = is_null($number) ? 1 : $number; | ||
| 476 | + | ||
| 477 | + $count = count($array); | ||
| 478 | + | ||
| 479 | + if ($requested > $count) { | ||
| 480 | + throw new InvalidArgumentException( | ||
| 481 | + "You requested {$requested} items, but there are only {$count} items available." | ||
| 482 | + ); | ||
| 483 | + } | ||
| 484 | + | ||
| 485 | + if (is_null($number)) { | ||
| 486 | + return $array[array_rand($array)]; | ||
| 487 | + } | ||
| 488 | + | ||
| 489 | + if ((int) $number === 0) { | ||
| 490 | + return []; | ||
| 491 | + } | ||
| 492 | + | ||
| 493 | + $keys = array_rand($array, $number); | ||
| 494 | + | ||
| 495 | + $results = []; | ||
| 496 | + | ||
| 497 | + foreach ((array) $keys as $key) { | ||
| 498 | + $results[] = $array[$key]; | ||
| 499 | + } | ||
| 500 | + | ||
| 501 | + return $results; | ||
| 502 | + } | ||
| 503 | + | ||
| 504 | + /** | ||
| 505 | + * Set an array item to a given value using "dot" notation. | ||
| 506 | + * | ||
| 507 | + * If no key is given to the method, the entire array will be replaced. | ||
| 508 | + * | ||
| 509 | + * @param array $array | ||
| 510 | + * @param string $key | ||
| 511 | + * @param mixed $value | ||
| 512 | + * @return array | ||
| 513 | + */ | ||
| 514 | + public static function set(&$array, $key, $value) | ||
| 515 | + { | ||
| 516 | + if (is_null($key)) { | ||
| 517 | + return $array = $value; | ||
| 518 | + } | ||
| 519 | + | ||
| 520 | + $keys = explode('.', $key); | ||
| 521 | + | ||
| 522 | + while (count($keys) > 1) { | ||
| 523 | + $key = array_shift($keys); | ||
| 524 | + | ||
| 525 | + // If the key doesn't exist at this depth, we will just create an empty array | ||
| 526 | + // to hold the next value, allowing us to create the arrays to hold final | ||
| 527 | + // values at the correct depth. Then we'll keep digging into the array. | ||
| 528 | + if (!isset($array[$key]) || !is_array($array[$key])) { | ||
| 529 | + $array[$key] = []; | ||
| 530 | + } | ||
| 531 | + | ||
| 532 | + $array = &$array[$key]; | ||
| 533 | + } | ||
| 534 | + | ||
| 535 | + $array[array_shift($keys)] = $value; | ||
| 536 | + | ||
| 537 | + return $array; | ||
| 538 | + } | ||
| 539 | + | ||
| 540 | + /** | ||
| 541 | + * Shuffle the given array and return the result. | ||
| 542 | + * | ||
| 543 | + * @param array $array | ||
| 544 | + * @param int|null $seed | ||
| 545 | + * @return array | ||
| 546 | + */ | ||
| 547 | + public static function shuffle($array, $seed = null) | ||
| 548 | + { | ||
| 549 | + if (is_null($seed)) { | ||
| 550 | + shuffle($array); | ||
| 551 | + } else { | ||
| 552 | + srand($seed); | ||
| 553 | + | ||
| 554 | + usort($array, function () { | ||
| 555 | + return rand(-1, 1); | ||
| 556 | + }); | ||
| 557 | + } | ||
| 558 | + | ||
| 559 | + return $array; | ||
| 560 | + } | ||
| 561 | + | ||
| 562 | + /** | ||
| 563 | + * Sort the array using the given callback or "dot" notation. | ||
| 564 | + * | ||
| 565 | + * @param array $array | ||
| 566 | + * @param callable|string|null $callback | ||
| 567 | + * @return array | ||
| 568 | + */ | ||
| 569 | + public static function sort($array, $callback = null) | ||
| 570 | + { | ||
| 571 | + return Collection::make($array)->sort($callback)->all(); | ||
| 572 | + } | ||
| 573 | + | ||
| 574 | + /** | ||
| 575 | + * Recursively sort an array by keys and values. | ||
| 576 | + * | ||
| 577 | + * @param array $array | ||
| 578 | + * @return array | ||
| 579 | + */ | ||
| 580 | + public static function sortRecursive($array) | ||
| 581 | + { | ||
| 582 | + foreach ($array as &$value) { | ||
| 583 | + if (is_array($value)) { | ||
| 584 | + $value = static::sortRecursive($value); | ||
| 585 | + } | ||
| 586 | + } | ||
| 587 | + | ||
| 588 | + if (static::isAssoc($array)) { | ||
| 589 | + ksort($array); | ||
| 590 | + } else { | ||
| 591 | + sort($array); | ||
| 592 | + } | ||
| 593 | + | ||
| 594 | + return $array; | ||
| 595 | + } | ||
| 596 | + | ||
| 597 | + /** | ||
| 598 | + * Convert the array into a query string. | ||
| 599 | + * | ||
| 600 | + * @param array $array | ||
| 601 | + * @return string | ||
| 602 | + */ | ||
| 603 | + public static function query($array) | ||
| 604 | + { | ||
| 605 | + return http_build_query($array, null, '&', PHP_QUERY_RFC3986); | ||
| 606 | + } | ||
| 607 | + | ||
| 608 | + /** | ||
| 609 | + * Filter the array using the given callback. | ||
| 610 | + * | ||
| 611 | + * @param array $array | ||
| 612 | + * @param callable $callback | ||
| 613 | + * @return array | ||
| 614 | + */ | ||
| 615 | + public static function where($array, callable $callback) | ||
| 616 | + { | ||
| 617 | + return array_filter($array, $callback, ARRAY_FILTER_USE_BOTH); | ||
| 618 | + } | ||
| 619 | + | ||
| 620 | + /** | ||
| 621 | + * If the given value is not an array and not null, wrap it in one. | ||
| 622 | + * | ||
| 623 | + * @param mixed $value | ||
| 624 | + * @return array | ||
| 625 | + */ | ||
| 626 | + public static function wrap($value) | ||
| 627 | + { | ||
| 628 | + if (is_null($value)) { | ||
| 629 | + return []; | ||
| 630 | + } | ||
| 631 | + | ||
| 632 | + return is_array($value) ? $value : [$value]; | ||
| 633 | + } | ||
| 634 | +} |
| 1 | +<?php | ||
| 2 | +// +---------------------------------------------------------------------- | ||
| 3 | +// | ThinkPHP [ WE CAN DO IT JUST THINK IT ] | ||
| 4 | +// +---------------------------------------------------------------------- | ||
| 5 | +// | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved. | ||
| 6 | +// +---------------------------------------------------------------------- | ||
| 7 | +// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) | ||
| 8 | +// +---------------------------------------------------------------------- | ||
| 9 | +// | Author: yunwuxin <448901948@qq.com> | ||
| 10 | +// +---------------------------------------------------------------------- | ||
| 11 | +namespace think\helper; | ||
| 12 | + | ||
| 13 | +class Str | ||
| 14 | +{ | ||
| 15 | + | ||
| 16 | + protected static $snakeCache = []; | ||
| 17 | + | ||
| 18 | + protected static $camelCache = []; | ||
| 19 | + | ||
| 20 | + protected static $studlyCache = []; | ||
| 21 | + | ||
| 22 | + /** | ||
| 23 | + * 检查字符串中是否包含某些字符串 | ||
| 24 | + * @param string $haystack | ||
| 25 | + * @param string|array $needles | ||
| 26 | + * @return bool | ||
| 27 | + */ | ||
| 28 | + public static function contains(string $haystack, $needles): bool | ||
| 29 | + { | ||
| 30 | + foreach ((array) $needles as $needle) { | ||
| 31 | + if ('' != $needle && mb_strpos($haystack, $needle) !== false) { | ||
| 32 | + return true; | ||
| 33 | + } | ||
| 34 | + } | ||
| 35 | + | ||
| 36 | + return false; | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + /** | ||
| 40 | + * 检查字符串是否以某些字符串结尾 | ||
| 41 | + * | ||
| 42 | + * @param string $haystack | ||
| 43 | + * @param string|array $needles | ||
| 44 | + * @return bool | ||
| 45 | + */ | ||
| 46 | + public static function endsWith(string $haystack, $needles): bool | ||
| 47 | + { | ||
| 48 | + foreach ((array) $needles as $needle) { | ||
| 49 | + if ((string) $needle === static::substr($haystack, -static::length($needle))) { | ||
| 50 | + return true; | ||
| 51 | + } | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + return false; | ||
| 55 | + } | ||
| 56 | + | ||
| 57 | + /** | ||
| 58 | + * 检查字符串是否以某些字符串开头 | ||
| 59 | + * | ||
| 60 | + * @param string $haystack | ||
| 61 | + * @param string|array $needles | ||
| 62 | + * @return bool | ||
| 63 | + */ | ||
| 64 | + public static function startsWith(string $haystack, $needles): bool | ||
| 65 | + { | ||
| 66 | + foreach ((array) $needles as $needle) { | ||
| 67 | + if ('' != $needle && mb_strpos($haystack, $needle) === 0) { | ||
| 68 | + return true; | ||
| 69 | + } | ||
| 70 | + } | ||
| 71 | + | ||
| 72 | + return false; | ||
| 73 | + } | ||
| 74 | + | ||
| 75 | + /** | ||
| 76 | + * 获取指定长度的随机字母数字组合的字符串 | ||
| 77 | + * | ||
| 78 | + * @param int $length | ||
| 79 | + * @param int $type | ||
| 80 | + * @param string $addChars | ||
| 81 | + * @return string | ||
| 82 | + */ | ||
| 83 | + public static function random(int $length = 6, int $type = null, string $addChars = ''): string | ||
| 84 | + { | ||
| 85 | + $str = ''; | ||
| 86 | + switch ($type) { | ||
| 87 | + case 0: | ||
| 88 | + $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' . $addChars; | ||
| 89 | + break; | ||
| 90 | + case 1: | ||
| 91 | + $chars = str_repeat('0123456789', 3); | ||
| 92 | + break; | ||
| 93 | + case 2: | ||
| 94 | + $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' . $addChars; | ||
| 95 | + break; | ||
| 96 | + case 3: | ||
| 97 | + $chars = 'abcdefghijklmnopqrstuvwxyz' . $addChars; | ||
| 98 | + break; | ||
| 99 | + case 4: | ||
| 100 | + $chars = "们以我到他会作时要动国产的一是工就年阶义发成部民可出能方进在了不和有大这主中人上为来分生对于学下级地个用同行面说种过命度革而多子后自社加小机也经力线本电高量长党得实家定深法表着水理化争现所二起政三好十战无农使性前等反体合斗路图把结第里正新开论之物从当两些还天资事队批点育重其思与间内去因件日利相由压员气业代全组数果期导平各基或月毛然如应形想制心样干都向变关问比展那它最及外没看治提五解系林者米群头意只明四道马认次文通但条较克又公孔领军流入接席位情运器并飞原油放立题质指建区验活众很教决特此常石强极土少已根共直团统式转别造切九你取西持总料连任志观调七么山程百报更见必真保热委手改管处己将修支识病象几先老光专什六型具示复安带每东增则完风回南广劳轮科北打积车计给节做务被整联步类集号列温装即毫知轴研单色坚据速防史拉世设达尔场织历花受求传口断况采精金界品判参层止边清至万确究书" . $addChars; | ||
| 101 | + break; | ||
| 102 | + default: | ||
| 103 | + $chars = 'ABCDEFGHIJKMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789' . $addChars; | ||
| 104 | + break; | ||
| 105 | + } | ||
| 106 | + if ($length > 10) { | ||
| 107 | + $chars = $type == 1 ? str_repeat($chars, $length) : str_repeat($chars, 5); | ||
| 108 | + } | ||
| 109 | + if ($type != 4) { | ||
| 110 | + $chars = str_shuffle($chars); | ||
| 111 | + $str = substr($chars, 0, $length); | ||
| 112 | + } else { | ||
| 113 | + for ($i = 0; $i < $length; $i++) { | ||
| 114 | + $str .= mb_substr($chars, floor(mt_rand(0, mb_strlen($chars, 'utf-8') - 1)), 1); | ||
| 115 | + } | ||
| 116 | + } | ||
| 117 | + return $str; | ||
| 118 | + } | ||
| 119 | + | ||
| 120 | + /** | ||
| 121 | + * 字符串转小写 | ||
| 122 | + * | ||
| 123 | + * @param string $value | ||
| 124 | + * @return string | ||
| 125 | + */ | ||
| 126 | + public static function lower(string $value): string | ||
| 127 | + { | ||
| 128 | + return mb_strtolower($value, 'UTF-8'); | ||
| 129 | + } | ||
| 130 | + | ||
| 131 | + /** | ||
| 132 | + * 字符串转大写 | ||
| 133 | + * | ||
| 134 | + * @param string $value | ||
| 135 | + * @return string | ||
| 136 | + */ | ||
| 137 | + public static function upper(string $value): string | ||
| 138 | + { | ||
| 139 | + return mb_strtoupper($value, 'UTF-8'); | ||
| 140 | + } | ||
| 141 | + | ||
| 142 | + /** | ||
| 143 | + * 获取字符串的长度 | ||
| 144 | + * | ||
| 145 | + * @param string $value | ||
| 146 | + * @return int | ||
| 147 | + */ | ||
| 148 | + public static function length(string $value): int | ||
| 149 | + { | ||
| 150 | + return mb_strlen($value); | ||
| 151 | + } | ||
| 152 | + | ||
| 153 | + /** | ||
| 154 | + * 截取字符串 | ||
| 155 | + * | ||
| 156 | + * @param string $string | ||
| 157 | + * @param int $start | ||
| 158 | + * @param int|null $length | ||
| 159 | + * @return string | ||
| 160 | + */ | ||
| 161 | + public static function substr(string $string, int $start, int $length = null): string | ||
| 162 | + { | ||
| 163 | + return mb_substr($string, $start, $length, 'UTF-8'); | ||
| 164 | + } | ||
| 165 | + | ||
| 166 | + /** | ||
| 167 | + * 驼峰转下划线 | ||
| 168 | + * | ||
| 169 | + * @param string $value | ||
| 170 | + * @param string $delimiter | ||
| 171 | + * @return string | ||
| 172 | + */ | ||
| 173 | + public static function snake(string $value, string $delimiter = '_'): string | ||
| 174 | + { | ||
| 175 | + $key = $value; | ||
| 176 | + | ||
| 177 | + if (isset(static::$snakeCache[$key][$delimiter])) { | ||
| 178 | + return static::$snakeCache[$key][$delimiter]; | ||
| 179 | + } | ||
| 180 | + | ||
| 181 | + if (!ctype_lower($value)) { | ||
| 182 | + $value = preg_replace('/\s+/u', '', ucwords($value)); | ||
| 183 | + | ||
| 184 | + $value = static::lower(preg_replace('/(.)(?=[A-Z])/u', '$1' . $delimiter, $value)); | ||
| 185 | + } | ||
| 186 | + | ||
| 187 | + return static::$snakeCache[$key][$delimiter] = $value; | ||
| 188 | + } | ||
| 189 | + | ||
| 190 | + /** | ||
| 191 | + * 下划线转驼峰(首字母小写) | ||
| 192 | + * | ||
| 193 | + * @param string $value | ||
| 194 | + * @return string | ||
| 195 | + */ | ||
| 196 | + public static function camel(string $value): string | ||
| 197 | + { | ||
| 198 | + if (isset(static::$camelCache[$value])) { | ||
| 199 | + return static::$camelCache[$value]; | ||
| 200 | + } | ||
| 201 | + | ||
| 202 | + return static::$camelCache[$value] = lcfirst(static::studly($value)); | ||
| 203 | + } | ||
| 204 | + | ||
| 205 | + /** | ||
| 206 | + * 下划线转驼峰(首字母大写) | ||
| 207 | + * | ||
| 208 | + * @param string $value | ||
| 209 | + * @return string | ||
| 210 | + */ | ||
| 211 | + public static function studly(string $value): string | ||
| 212 | + { | ||
| 213 | + $key = $value; | ||
| 214 | + | ||
| 215 | + if (isset(static::$studlyCache[$key])) { | ||
| 216 | + return static::$studlyCache[$key]; | ||
| 217 | + } | ||
| 218 | + | ||
| 219 | + $value = ucwords(str_replace(['-', '_'], ' ', $value)); | ||
| 220 | + | ||
| 221 | + return static::$studlyCache[$key] = str_replace(' ', '', $value); | ||
| 222 | + } | ||
| 223 | + | ||
| 224 | + /** | ||
| 225 | + * 转为首字母大写的标题格式 | ||
| 226 | + * | ||
| 227 | + * @param string $value | ||
| 228 | + * @return string | ||
| 229 | + */ | ||
| 230 | + public static function title(string $value): string | ||
| 231 | + { | ||
| 232 | + return mb_convert_case($value, MB_CASE_TITLE, 'UTF-8'); | ||
| 233 | + } | ||
| 234 | +} |
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +namespace Tests; | ||
| 4 | + | ||
| 5 | +use stdClass; | ||
| 6 | +use think\Collection; | ||
| 7 | +use think\helper\Arr; | ||
| 8 | + | ||
| 9 | +class ArrTest extends TestCase | ||
| 10 | +{ | ||
| 11 | + public function testAdd() | ||
| 12 | + { | ||
| 13 | + $array = Arr::add(['name' => 'ThinkPHP'], 'price', 100); | ||
| 14 | + $this->assertSame(['name' => 'ThinkPHP', 'price' => 100], $array); | ||
| 15 | + } | ||
| 16 | + | ||
| 17 | + public function testCrossJoin() | ||
| 18 | + { | ||
| 19 | + // Single dimension | ||
| 20 | + $this->assertSame( | ||
| 21 | + [[1, 'a'], [1, 'b'], [1, 'c']], | ||
| 22 | + Arr::crossJoin([1], ['a', 'b', 'c']) | ||
| 23 | + ); | ||
| 24 | + // Square matrix | ||
| 25 | + $this->assertSame( | ||
| 26 | + [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']], | ||
| 27 | + Arr::crossJoin([1, 2], ['a', 'b']) | ||
| 28 | + ); | ||
| 29 | + // Rectangular matrix | ||
| 30 | + $this->assertSame( | ||
| 31 | + [[1, 'a'], [1, 'b'], [1, 'c'], [2, 'a'], [2, 'b'], [2, 'c']], | ||
| 32 | + Arr::crossJoin([1, 2], ['a', 'b', 'c']) | ||
| 33 | + ); | ||
| 34 | + // 3D matrix | ||
| 35 | + $this->assertSame( | ||
| 36 | + [ | ||
| 37 | + [1, 'a', 'I'], [1, 'a', 'II'], [1, 'a', 'III'], | ||
| 38 | + [1, 'b', 'I'], [1, 'b', 'II'], [1, 'b', 'III'], | ||
| 39 | + [2, 'a', 'I'], [2, 'a', 'II'], [2, 'a', 'III'], | ||
| 40 | + [2, 'b', 'I'], [2, 'b', 'II'], [2, 'b', 'III'], | ||
| 41 | + ], | ||
| 42 | + Arr::crossJoin([1, 2], ['a', 'b'], ['I', 'II', 'III']) | ||
| 43 | + ); | ||
| 44 | + // With 1 empty dimension | ||
| 45 | + $this->assertSame([], Arr::crossJoin([], ['a', 'b'], ['I', 'II', 'III'])); | ||
| 46 | + $this->assertSame([], Arr::crossJoin([1, 2], [], ['I', 'II', 'III'])); | ||
| 47 | + $this->assertSame([], Arr::crossJoin([1, 2], ['a', 'b'], [])); | ||
| 48 | + // With empty arrays | ||
| 49 | + $this->assertSame([], Arr::crossJoin([], [], [])); | ||
| 50 | + $this->assertSame([], Arr::crossJoin([], [])); | ||
| 51 | + $this->assertSame([], Arr::crossJoin([])); | ||
| 52 | + // Not really a proper usage, still, test for preserving BC | ||
| 53 | + $this->assertSame([[]], Arr::crossJoin()); | ||
| 54 | + } | ||
| 55 | + | ||
| 56 | + public function testDivide() | ||
| 57 | + { | ||
| 58 | + list($keys, $values) = Arr::divide(['name' => 'ThinkPHP']); | ||
| 59 | + $this->assertSame(['name'], $keys); | ||
| 60 | + $this->assertSame(['ThinkPHP'], $values); | ||
| 61 | + } | ||
| 62 | + | ||
| 63 | + public function testDot() | ||
| 64 | + { | ||
| 65 | + $array = Arr::dot(['foo' => ['bar' => 'baz']]); | ||
| 66 | + $this->assertSame(['foo.bar' => 'baz'], $array); | ||
| 67 | + $array = Arr::dot([]); | ||
| 68 | + $this->assertSame([], $array); | ||
| 69 | + $array = Arr::dot(['foo' => []]); | ||
| 70 | + $this->assertSame(['foo' => []], $array); | ||
| 71 | + $array = Arr::dot(['foo' => ['bar' => []]]); | ||
| 72 | + $this->assertSame(['foo.bar' => []], $array); | ||
| 73 | + } | ||
| 74 | + | ||
| 75 | + public function testExcept() | ||
| 76 | + { | ||
| 77 | + $array = ['name' => 'ThinkPHP', 'price' => 100]; | ||
| 78 | + $array = Arr::except($array, ['price']); | ||
| 79 | + $this->assertSame(['name' => 'ThinkPHP'], $array); | ||
| 80 | + } | ||
| 81 | + | ||
| 82 | + public function testExists() | ||
| 83 | + { | ||
| 84 | + $this->assertTrue(Arr::exists([1], 0)); | ||
| 85 | + $this->assertTrue(Arr::exists([null], 0)); | ||
| 86 | + $this->assertTrue(Arr::exists(['a' => 1], 'a')); | ||
| 87 | + $this->assertTrue(Arr::exists(['a' => null], 'a')); | ||
| 88 | + $this->assertFalse(Arr::exists([1], 1)); | ||
| 89 | + $this->assertFalse(Arr::exists([null], 1)); | ||
| 90 | + $this->assertFalse(Arr::exists(['a' => 1], 0)); | ||
| 91 | + } | ||
| 92 | + | ||
| 93 | + public function testFirst() | ||
| 94 | + { | ||
| 95 | + $array = [100, 200, 300]; | ||
| 96 | + $value = Arr::first($array, function ($value) { | ||
| 97 | + return $value >= 150; | ||
| 98 | + }); | ||
| 99 | + $this->assertSame(200, $value); | ||
| 100 | + $this->assertSame(100, Arr::first($array)); | ||
| 101 | + | ||
| 102 | + $this->assertSame('default', Arr::first([], null, 'default')); | ||
| 103 | + | ||
| 104 | + $this->assertSame('default', Arr::first([], function () { | ||
| 105 | + return false; | ||
| 106 | + }, 'default')); | ||
| 107 | + } | ||
| 108 | + | ||
| 109 | + public function testLast() | ||
| 110 | + { | ||
| 111 | + $array = [100, 200, 300]; | ||
| 112 | + $last = Arr::last($array, function ($value) { | ||
| 113 | + return $value < 250; | ||
| 114 | + }); | ||
| 115 | + $this->assertSame(200, $last); | ||
| 116 | + $last = Arr::last($array, function ($value, $key) { | ||
| 117 | + return $key < 2; | ||
| 118 | + }); | ||
| 119 | + $this->assertSame(200, $last); | ||
| 120 | + $this->assertSame(300, Arr::last($array)); | ||
| 121 | + } | ||
| 122 | + | ||
| 123 | + public function testFlatten() | ||
| 124 | + { | ||
| 125 | + // Flat arrays are unaffected | ||
| 126 | + $array = ['#foo', '#bar', '#baz']; | ||
| 127 | + $this->assertSame(['#foo', '#bar', '#baz'], Arr::flatten(['#foo', '#bar', '#baz'])); | ||
| 128 | + // Nested arrays are flattened with existing flat items | ||
| 129 | + $array = [['#foo', '#bar'], '#baz']; | ||
| 130 | + $this->assertSame(['#foo', '#bar', '#baz'], Arr::flatten($array)); | ||
| 131 | + // Flattened array includes "null" items | ||
| 132 | + $array = [['#foo', null], '#baz', null]; | ||
| 133 | + $this->assertSame(['#foo', null, '#baz', null], Arr::flatten($array)); | ||
| 134 | + // Sets of nested arrays are flattened | ||
| 135 | + $array = [['#foo', '#bar'], ['#baz']]; | ||
| 136 | + $this->assertSame(['#foo', '#bar', '#baz'], Arr::flatten($array)); | ||
| 137 | + // Deeply nested arrays are flattened | ||
| 138 | + $array = [['#foo', ['#bar']], ['#baz']]; | ||
| 139 | + $this->assertSame(['#foo', '#bar', '#baz'], Arr::flatten($array)); | ||
| 140 | + // Nested arrays are flattened alongside arrays | ||
| 141 | + $array = [new Collection(['#foo', '#bar']), ['#baz']]; | ||
| 142 | + $this->assertSame(['#foo', '#bar', '#baz'], Arr::flatten($array)); | ||
| 143 | + // Nested arrays containing plain arrays are flattened | ||
| 144 | + $array = [new Collection(['#foo', ['#bar']]), ['#baz']]; | ||
| 145 | + $this->assertSame(['#foo', '#bar', '#baz'], Arr::flatten($array)); | ||
| 146 | + // Nested arrays containing arrays are flattened | ||
| 147 | + $array = [['#foo', new Collection(['#bar'])], ['#baz']]; | ||
| 148 | + $this->assertSame(['#foo', '#bar', '#baz'], Arr::flatten($array)); | ||
| 149 | + // Nested arrays containing arrays containing arrays are flattened | ||
| 150 | + $array = [['#foo', new Collection(['#bar', ['#zap']])], ['#baz']]; | ||
| 151 | + $this->assertSame(['#foo', '#bar', '#zap', '#baz'], Arr::flatten($array)); | ||
| 152 | + } | ||
| 153 | + | ||
| 154 | + public function testFlattenWithDepth() | ||
| 155 | + { | ||
| 156 | + // No depth flattens recursively | ||
| 157 | + $array = [['#foo', ['#bar', ['#baz']]], '#zap']; | ||
| 158 | + $this->assertSame(['#foo', '#bar', '#baz', '#zap'], Arr::flatten($array)); | ||
| 159 | + // Specifying a depth only flattens to that depth | ||
| 160 | + $array = [['#foo', ['#bar', ['#baz']]], '#zap']; | ||
| 161 | + $this->assertSame(['#foo', ['#bar', ['#baz']], '#zap'], Arr::flatten($array, 1)); | ||
| 162 | + $array = [['#foo', ['#bar', ['#baz']]], '#zap']; | ||
| 163 | + $this->assertSame(['#foo', '#bar', ['#baz'], '#zap'], Arr::flatten($array, 2)); | ||
| 164 | + } | ||
| 165 | + | ||
| 166 | + public function testGet() | ||
| 167 | + { | ||
| 168 | + $array = ['products.item' => ['price' => 100]]; | ||
| 169 | + $this->assertSame(['price' => 100], Arr::get($array, 'products.item')); | ||
| 170 | + $array = ['products' => ['item' => ['price' => 100]]]; | ||
| 171 | + $value = Arr::get($array, 'products.item'); | ||
| 172 | + $this->assertSame(['price' => 100], $value); | ||
| 173 | + // Test null array values | ||
| 174 | + $array = ['foo' => null, 'bar' => ['baz' => null]]; | ||
| 175 | + $this->assertNull(Arr::get($array, 'foo', 'default')); | ||
| 176 | + $this->assertNull(Arr::get($array, 'bar.baz', 'default')); | ||
| 177 | + // Test null key returns the whole array | ||
| 178 | + $array = ['foo', 'bar']; | ||
| 179 | + $this->assertSame($array, Arr::get($array, null)); | ||
| 180 | + // Test $array is empty and key is null | ||
| 181 | + $this->assertSame([], Arr::get([], null)); | ||
| 182 | + $this->assertSame([], Arr::get([], null, 'default')); | ||
| 183 | + } | ||
| 184 | + | ||
| 185 | + public function testHas() | ||
| 186 | + { | ||
| 187 | + $array = ['products.item' => ['price' => 100]]; | ||
| 188 | + $this->assertTrue(Arr::has($array, 'products.item')); | ||
| 189 | + $array = ['products' => ['item' => ['price' => 100]]]; | ||
| 190 | + $this->assertTrue(Arr::has($array, 'products.item')); | ||
| 191 | + $this->assertTrue(Arr::has($array, 'products.item.price')); | ||
| 192 | + $this->assertFalse(Arr::has($array, 'products.foo')); | ||
| 193 | + $this->assertFalse(Arr::has($array, 'products.item.foo')); | ||
| 194 | + $array = ['foo' => null, 'bar' => ['baz' => null]]; | ||
| 195 | + $this->assertTrue(Arr::has($array, 'foo')); | ||
| 196 | + $this->assertTrue(Arr::has($array, 'bar.baz')); | ||
| 197 | + $array = ['foo', 'bar']; | ||
| 198 | + $this->assertFalse(Arr::has($array, null)); | ||
| 199 | + $this->assertFalse(Arr::has([], null)); | ||
| 200 | + $array = ['products' => ['item' => ['price' => 100]]]; | ||
| 201 | + $this->assertTrue(Arr::has($array, ['products.item'])); | ||
| 202 | + $this->assertTrue(Arr::has($array, ['products.item', 'products.item.price'])); | ||
| 203 | + $this->assertTrue(Arr::has($array, ['products', 'products'])); | ||
| 204 | + $this->assertFalse(Arr::has($array, ['foo'])); | ||
| 205 | + $this->assertFalse(Arr::has($array, [])); | ||
| 206 | + $this->assertFalse(Arr::has($array, ['products.item', 'products.price'])); | ||
| 207 | + $this->assertFalse(Arr::has([], [null])); | ||
| 208 | + } | ||
| 209 | + | ||
| 210 | + public function testIsAssoc() | ||
| 211 | + { | ||
| 212 | + $this->assertTrue(Arr::isAssoc(['a' => 'a', 0 => 'b'])); | ||
| 213 | + $this->assertTrue(Arr::isAssoc([1 => 'a', 0 => 'b'])); | ||
| 214 | + $this->assertTrue(Arr::isAssoc([1 => 'a', 2 => 'b'])); | ||
| 215 | + $this->assertFalse(Arr::isAssoc([0 => 'a', 1 => 'b'])); | ||
| 216 | + $this->assertFalse(Arr::isAssoc(['a', 'b'])); | ||
| 217 | + } | ||
| 218 | + | ||
| 219 | + public function testOnly() | ||
| 220 | + { | ||
| 221 | + $array = ['name' => 'ThinkPHP', 'price' => 100, 'orders' => 10]; | ||
| 222 | + $array = Arr::only($array, ['name', 'price']); | ||
| 223 | + $this->assertSame(['name' => 'ThinkPHP', 'price' => 100], $array); | ||
| 224 | + } | ||
| 225 | + | ||
| 226 | + public function testPrepend() | ||
| 227 | + { | ||
| 228 | + $array = Arr::prepend(['one', 'two', 'three', 'four'], 'zero'); | ||
| 229 | + $this->assertSame(['zero', 'one', 'two', 'three', 'four'], $array); | ||
| 230 | + $array = Arr::prepend(['one' => 1, 'two' => 2], 0, 'zero'); | ||
| 231 | + $this->assertSame(['zero' => 0, 'one' => 1, 'two' => 2], $array); | ||
| 232 | + } | ||
| 233 | + | ||
| 234 | + public function testPull() | ||
| 235 | + { | ||
| 236 | + $array = ['name' => 'ThinkPHP', 'price' => 100]; | ||
| 237 | + $name = Arr::pull($array, 'name'); | ||
| 238 | + $this->assertSame('ThinkPHP', $name); | ||
| 239 | + $this->assertSame(['price' => 100], $array); | ||
| 240 | + // Only works on first level keys | ||
| 241 | + $array = ['i@example.com' => 'Joe', 'jack@localhost' => 'Jane']; | ||
| 242 | + $name = Arr::pull($array, 'i@example.com'); | ||
| 243 | + $this->assertSame('Joe', $name); | ||
| 244 | + $this->assertSame(['jack@localhost' => 'Jane'], $array); | ||
| 245 | + // Does not work for nested keys | ||
| 246 | + $array = ['emails' => ['i@example.com' => 'Joe', 'jack@localhost' => 'Jane']]; | ||
| 247 | + $name = Arr::pull($array, 'emails.i@example.com'); | ||
| 248 | + $this->assertNull($name); | ||
| 249 | + $this->assertSame(['emails' => ['i@example.com' => 'Joe', 'jack@localhost' => 'Jane']], $array); | ||
| 250 | + } | ||
| 251 | + | ||
| 252 | + public function testRandom() | ||
| 253 | + { | ||
| 254 | + $randomValue = Arr::random(['foo', 'bar', 'baz']); | ||
| 255 | + $this->assertContains($randomValue, ['foo', 'bar', 'baz']); | ||
| 256 | + $randomValues = Arr::random(['foo', 'bar', 'baz'], 1); | ||
| 257 | + $this->assertIsArray($randomValues); | ||
| 258 | + $this->assertCount(1, $randomValues); | ||
| 259 | + $this->assertContains($randomValues[0], ['foo', 'bar', 'baz']); | ||
| 260 | + $randomValues = Arr::random(['foo', 'bar', 'baz'], 2); | ||
| 261 | + $this->assertIsArray($randomValues); | ||
| 262 | + $this->assertCount(2, $randomValues); | ||
| 263 | + $this->assertContains($randomValues[0], ['foo', 'bar', 'baz']); | ||
| 264 | + $this->assertContains($randomValues[1], ['foo', 'bar', 'baz']); | ||
| 265 | + } | ||
| 266 | + | ||
| 267 | + public function testSet() | ||
| 268 | + { | ||
| 269 | + $array = ['products' => ['item' => ['price' => 100]]]; | ||
| 270 | + Arr::set($array, 'products.item.price', 200); | ||
| 271 | + Arr::set($array, 'goods.item.price', 200); | ||
| 272 | + $this->assertSame(['products' => ['item' => ['price' => 200]], 'goods' => ['item' => ['price' => 200]]], $array); | ||
| 273 | + } | ||
| 274 | + | ||
| 275 | + public function testWhere() | ||
| 276 | + { | ||
| 277 | + $array = [100, '200', 300, '400', 500]; | ||
| 278 | + $array = Arr::where($array, function ($value, $key) { | ||
| 279 | + return is_string($value); | ||
| 280 | + }); | ||
| 281 | + $this->assertSame([1 => '200', 3 => '400'], $array); | ||
| 282 | + } | ||
| 283 | + | ||
| 284 | + public function testWhereKey() | ||
| 285 | + { | ||
| 286 | + $array = ['10' => 1, 'foo' => 3, 20 => 2]; | ||
| 287 | + $array = Arr::where($array, function ($value, $key) { | ||
| 288 | + return is_numeric($key); | ||
| 289 | + }); | ||
| 290 | + $this->assertSame(['10' => 1, 20 => 2], $array); | ||
| 291 | + } | ||
| 292 | + | ||
| 293 | + public function testForget() | ||
| 294 | + { | ||
| 295 | + $array = ['products' => ['item' => ['price' => 100]]]; | ||
| 296 | + Arr::forget($array, null); | ||
| 297 | + $this->assertSame(['products' => ['item' => ['price' => 100]]], $array); | ||
| 298 | + $array = ['products' => ['item' => ['price' => 100]]]; | ||
| 299 | + Arr::forget($array, []); | ||
| 300 | + $this->assertSame(['products' => ['item' => ['price' => 100]]], $array); | ||
| 301 | + $array = ['products' => ['item' => ['price' => 100]]]; | ||
| 302 | + Arr::forget($array, 'products.item'); | ||
| 303 | + $this->assertSame(['products' => []], $array); | ||
| 304 | + $array = ['products' => ['item' => ['price' => 100]]]; | ||
| 305 | + Arr::forget($array, 'products.item.price'); | ||
| 306 | + $this->assertSame(['products' => ['item' => []]], $array); | ||
| 307 | + $array = ['products' => ['item' => ['price' => 100]]]; | ||
| 308 | + Arr::forget($array, 'products.final.price'); | ||
| 309 | + $this->assertSame(['products' => ['item' => ['price' => 100]]], $array); | ||
| 310 | + $array = ['shop' => ['cart' => [150 => 0]]]; | ||
| 311 | + Arr::forget($array, 'shop.final.cart'); | ||
| 312 | + $this->assertSame(['shop' => ['cart' => [150 => 0]]], $array); | ||
| 313 | + $array = ['products' => ['item' => ['price' => ['original' => 50, 'taxes' => 60]]]]; | ||
| 314 | + Arr::forget($array, 'products.item.price.taxes'); | ||
| 315 | + $this->assertSame(['products' => ['item' => ['price' => ['original' => 50]]]], $array); | ||
| 316 | + $array = ['products' => ['item' => ['price' => ['original' => 50, 'taxes' => 60]]]]; | ||
| 317 | + Arr::forget($array, 'products.item.final.taxes'); | ||
| 318 | + $this->assertSame(['products' => ['item' => ['price' => ['original' => 50, 'taxes' => 60]]]], $array); | ||
| 319 | + $array = ['products' => ['item' => ['price' => 50], null => 'something']]; | ||
| 320 | + Arr::forget($array, ['products.amount.all', 'products.item.price']); | ||
| 321 | + $this->assertSame(['products' => ['item' => [], null => 'something']], $array); | ||
| 322 | + // Only works on first level keys | ||
| 323 | + $array = ['i@example.com' => 'Joe', 'i@thinkphp.com' => 'Jane']; | ||
| 324 | + Arr::forget($array, 'i@example.com'); | ||
| 325 | + $this->assertSame(['i@thinkphp.com' => 'Jane'], $array); | ||
| 326 | + // Does not work for nested keys | ||
| 327 | + $array = ['emails' => ['i@example.com' => ['name' => 'Joe'], 'jack@localhost' => ['name' => 'Jane']]]; | ||
| 328 | + Arr::forget($array, ['emails.i@example.com', 'emails.jack@localhost']); | ||
| 329 | + $this->assertSame(['emails' => ['i@example.com' => ['name' => 'Joe']]], $array); | ||
| 330 | + } | ||
| 331 | + | ||
| 332 | + public function testWrap() | ||
| 333 | + { | ||
| 334 | + $string = 'a'; | ||
| 335 | + $array = ['a']; | ||
| 336 | + $object = new stdClass(); | ||
| 337 | + $object->value = 'a'; | ||
| 338 | + $this->assertSame(['a'], Arr::wrap($string)); | ||
| 339 | + $this->assertSame($array, Arr::wrap($array)); | ||
| 340 | + $this->assertSame([$object], Arr::wrap($object)); | ||
| 341 | + } | ||
| 342 | +} |
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +namespace Tests; | ||
| 4 | + | ||
| 5 | +use think\Collection; | ||
| 6 | + | ||
| 7 | +class CollectionTest extends TestCase | ||
| 8 | +{ | ||
| 9 | + public function testMerge() | ||
| 10 | + { | ||
| 11 | + $c = new Collection(['name' => 'Hello']); | ||
| 12 | + $this->assertSame(['name' => 'Hello', 'id' => 1], $c->merge(['id' => 1])->all()); | ||
| 13 | + } | ||
| 14 | + | ||
| 15 | + public function testFirst() | ||
| 16 | + { | ||
| 17 | + $c = new Collection(['name' => 'Hello', 'age' => 25]); | ||
| 18 | + | ||
| 19 | + $this->assertSame('Hello', $c->first()); | ||
| 20 | + } | ||
| 21 | + | ||
| 22 | + public function testLast() | ||
| 23 | + { | ||
| 24 | + $c = new Collection(['name' => 'Hello', 'age' => 25]); | ||
| 25 | + | ||
| 26 | + $this->assertSame(25, $c->last()); | ||
| 27 | + } | ||
| 28 | + | ||
| 29 | + public function testToArray() | ||
| 30 | + { | ||
| 31 | + $c = new Collection(['name' => 'Hello', 'age' => 25]); | ||
| 32 | + | ||
| 33 | + $this->assertSame(['name' => 'Hello', 'age' => 25], $c->toArray()); | ||
| 34 | + } | ||
| 35 | + | ||
| 36 | + public function testToJson() | ||
| 37 | + { | ||
| 38 | + $c = new Collection(['name' => 'Hello', 'age' => 25]); | ||
| 39 | + | ||
| 40 | + $this->assertSame(json_encode(['name' => 'Hello', 'age' => 25]), $c->toJson()); | ||
| 41 | + $this->assertSame(json_encode(['name' => 'Hello', 'age' => 25]), (string) $c); | ||
| 42 | + $this->assertSame(json_encode(['name' => 'Hello', 'age' => 25]), json_encode($c)); | ||
| 43 | + } | ||
| 44 | + | ||
| 45 | + public function testSerialize() | ||
| 46 | + { | ||
| 47 | + $c = new Collection(['name' => 'Hello', 'age' => 25]); | ||
| 48 | + | ||
| 49 | + $sc = serialize($c); | ||
| 50 | + $c = unserialize($sc); | ||
| 51 | + | ||
| 52 | + $this->assertSame(['name' => 'Hello', 'age' => 25], $c->all()); | ||
| 53 | + } | ||
| 54 | + | ||
| 55 | + public function testGetIterator() | ||
| 56 | + { | ||
| 57 | + $c = new Collection(['name' => 'Hello', 'age' => 25]); | ||
| 58 | + | ||
| 59 | + $this->assertInstanceOf(\ArrayIterator::class, $c->getIterator()); | ||
| 60 | + | ||
| 61 | + $this->assertSame(['name' => 'Hello', 'age' => 25], $c->getIterator()->getArrayCopy()); | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | + public function testCount() | ||
| 65 | + { | ||
| 66 | + $c = new Collection(['name' => 'Hello', 'age' => 25]); | ||
| 67 | + | ||
| 68 | + $this->assertCount(2, $c); | ||
| 69 | + } | ||
| 70 | +} |
| 1 | +<?php | ||
| 2 | +namespace Tests; | ||
| 3 | + | ||
| 4 | +use think\helper\Str; | ||
| 5 | + | ||
| 6 | +class StrTest extends TestCase | ||
| 7 | +{ | ||
| 8 | + public function testCamel() | ||
| 9 | + { | ||
| 10 | + $this->assertSame('fooBar', Str::camel('FooBar')); | ||
| 11 | + $this->assertSame('fooBar', Str::camel('FooBar')); | ||
| 12 | + $this->assertSame('fooBar', Str::camel('foo_bar')); | ||
| 13 | + $this->assertSame('fooBar', Str::camel('_foo_bar')); | ||
| 14 | + $this->assertSame('fooBar', Str::camel('_foo_bar_')); | ||
| 15 | + } | ||
| 16 | + | ||
| 17 | + public function testStudly() | ||
| 18 | + { | ||
| 19 | + $this->assertSame('FooBar', Str::studly('fooBar')); | ||
| 20 | + $this->assertSame('FooBar', Str::studly('_foo_bar')); | ||
| 21 | + $this->assertSame('FooBar', Str::studly('_foo_bar_')); | ||
| 22 | + $this->assertSame('FooBar', Str::studly('_foo_bar_')); | ||
| 23 | + } | ||
| 24 | + | ||
| 25 | + public function testSnake() | ||
| 26 | + { | ||
| 27 | + $this->assertSame('think_p_h_p_framework', Str::snake('ThinkPHPFramework')); | ||
| 28 | + $this->assertSame('think_php_framework', Str::snake('ThinkPhpFramework')); | ||
| 29 | + $this->assertSame('think php framework', Str::snake('ThinkPhpFramework', ' ')); | ||
| 30 | + $this->assertSame('think_php_framework', Str::snake('Think Php Framework')); | ||
| 31 | + $this->assertSame('think_php_framework', Str::snake('Think Php Framework ')); | ||
| 32 | + // ensure cache keys don't overlap | ||
| 33 | + $this->assertSame('think__php__framework', Str::snake('ThinkPhpFramework', '__')); | ||
| 34 | + $this->assertSame('think_php_framework_', Str::snake('ThinkPhpFramework_', '_')); | ||
| 35 | + $this->assertSame('think_php_framework', Str::snake('think php Framework')); | ||
| 36 | + $this->assertSame('think_php_frame_work', Str::snake('think php FrameWork')); | ||
| 37 | + // prevent breaking changes | ||
| 38 | + $this->assertSame('foo-bar', Str::snake('foo-bar')); | ||
| 39 | + $this->assertSame('foo-_bar', Str::snake('Foo-Bar')); | ||
| 40 | + $this->assertSame('foo__bar', Str::snake('Foo_Bar')); | ||
| 41 | + $this->assertSame('żółtałódka', Str::snake('ŻółtaŁódka')); | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + public function testTitle() | ||
| 45 | + { | ||
| 46 | + $this->assertSame('Welcome Back', Str::title('welcome back')); | ||
| 47 | + } | ||
| 48 | + | ||
| 49 | + public function testRandom() | ||
| 50 | + { | ||
| 51 | + $this->assertIsString(Str::random(10)); | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + public function testUpper() | ||
| 55 | + { | ||
| 56 | + $this->assertSame('USERNAME', Str::upper('username')); | ||
| 57 | + $this->assertSame('USERNAME', Str::upper('userNaMe')); | ||
| 58 | + } | ||
| 59 | +} |
vendor/topthink/think-queue/.gitignore
0 → 100644
vendor/topthink/think-queue/LICENSE
0 → 100644
| 1 | + Apache License | ||
| 2 | + Version 2.0, January 2004 | ||
| 3 | + http://www.apache.org/licenses/ | ||
| 4 | + | ||
| 5 | + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION | ||
| 6 | + | ||
| 7 | + 1. Definitions. | ||
| 8 | + | ||
| 9 | + "License" shall mean the terms and conditions for use, reproduction, | ||
| 10 | + and distribution as defined by Sections 1 through 9 of this document. | ||
| 11 | + | ||
| 12 | + "Licensor" shall mean the copyright owner or entity authorized by | ||
| 13 | + the copyright owner that is granting the License. | ||
| 14 | + | ||
| 15 | + "Legal Entity" shall mean the union of the acting entity and all | ||
| 16 | + other entities that control, are controlled by, or are under common | ||
| 17 | + control with that entity. For the purposes of this definition, | ||
| 18 | + "control" means (i) the power, direct or indirect, to cause the | ||
| 19 | + direction or management of such entity, whether by contract or | ||
| 20 | + otherwise, or (ii) ownership of fifty percent (50%) or more of the | ||
| 21 | + outstanding shares, or (iii) beneficial ownership of such entity. | ||
| 22 | + | ||
| 23 | + "You" (or "Your") shall mean an individual or Legal Entity | ||
| 24 | + exercising permissions granted by this License. | ||
| 25 | + | ||
| 26 | + "Source" form shall mean the preferred form for making modifications, | ||
| 27 | + including but not limited to software source code, documentation | ||
| 28 | + source, and configuration files. | ||
| 29 | + | ||
| 30 | + "Object" form shall mean any form resulting from mechanical | ||
| 31 | + transformation or translation of a Source form, including but | ||
| 32 | + not limited to compiled object code, generated documentation, | ||
| 33 | + and conversions to other media types. | ||
| 34 | + | ||
| 35 | + "Work" shall mean the work of authorship, whether in Source or | ||
| 36 | + Object form, made available under the License, as indicated by a | ||
| 37 | + copyright notice that is included in or attached to the work | ||
| 38 | + (an example is provided in the Appendix below). | ||
| 39 | + | ||
| 40 | + "Derivative Works" shall mean any work, whether in Source or Object | ||
| 41 | + form, that is based on (or derived from) the Work and for which the | ||
| 42 | + editorial revisions, annotations, elaborations, or other modifications | ||
| 43 | + represent, as a whole, an original work of authorship. For the purposes | ||
| 44 | + of this License, Derivative Works shall not include works that remain | ||
| 45 | + separable from, or merely link (or bind by name) to the interfaces of, | ||
| 46 | + the Work and Derivative Works thereof. | ||
| 47 | + | ||
| 48 | + "Contribution" shall mean any work of authorship, including | ||
| 49 | + the original version of the Work and any modifications or additions | ||
| 50 | + to that Work or Derivative Works thereof, that is intentionally | ||
| 51 | + submitted to Licensor for inclusion in the Work by the copyright owner | ||
| 52 | + or by an individual or Legal Entity authorized to submit on behalf of | ||
| 53 | + the copyright owner. For the purposes of this definition, "submitted" | ||
| 54 | + means any form of electronic, verbal, or written communication sent | ||
| 55 | + to the Licensor or its representatives, including but not limited to | ||
| 56 | + communication on electronic mailing lists, source code control systems, | ||
| 57 | + and issue tracking systems that are managed by, or on behalf of, the | ||
| 58 | + Licensor for the purpose of discussing and improving the Work, but | ||
| 59 | + excluding communication that is conspicuously marked or otherwise | ||
| 60 | + designated in writing by the copyright owner as "Not a Contribution." | ||
| 61 | + | ||
| 62 | + "Contributor" shall mean Licensor and any individual or Legal Entity | ||
| 63 | + on behalf of whom a Contribution has been received by Licensor and | ||
| 64 | + subsequently incorporated within the Work. | ||
| 65 | + | ||
| 66 | + 2. Grant of Copyright License. Subject to the terms and conditions of | ||
| 67 | + this License, each Contributor hereby grants to You a perpetual, | ||
| 68 | + worldwide, non-exclusive, no-charge, royalty-free, irrevocable | ||
| 69 | + copyright license to reproduce, prepare Derivative Works of, | ||
| 70 | + publicly display, publicly perform, sublicense, and distribute the | ||
| 71 | + Work and such Derivative Works in Source or Object form. | ||
| 72 | + | ||
| 73 | + 3. Grant of Patent License. Subject to the terms and conditions of | ||
| 74 | + this License, each Contributor hereby grants to You a perpetual, | ||
| 75 | + worldwide, non-exclusive, no-charge, royalty-free, irrevocable | ||
| 76 | + (except as stated in this section) patent license to make, have made, | ||
| 77 | + use, offer to sell, sell, import, and otherwise transfer the Work, | ||
| 78 | + where such license applies only to those patent claims licensable | ||
| 79 | + by such Contributor that are necessarily infringed by their | ||
| 80 | + Contribution(s) alone or by combination of their Contribution(s) | ||
| 81 | + with the Work to which such Contribution(s) was submitted. If You | ||
| 82 | + institute patent litigation against any entity (including a | ||
| 83 | + cross-claim or counterclaim in a lawsuit) alleging that the Work | ||
| 84 | + or a Contribution incorporated within the Work constitutes direct | ||
| 85 | + or contributory patent infringement, then any patent licenses | ||
| 86 | + granted to You under this License for that Work shall terminate | ||
| 87 | + as of the date such litigation is filed. | ||
| 88 | + | ||
| 89 | + 4. Redistribution. You may reproduce and distribute copies of the | ||
| 90 | + Work or Derivative Works thereof in any medium, with or without | ||
| 91 | + modifications, and in Source or Object form, provided that You | ||
| 92 | + meet the following conditions: | ||
| 93 | + | ||
| 94 | + (a) You must give any other recipients of the Work or | ||
| 95 | + Derivative Works a copy of this License; and | ||
| 96 | + | ||
| 97 | + (b) You must cause any modified files to carry prominent notices | ||
| 98 | + stating that You changed the files; and | ||
| 99 | + | ||
| 100 | + (c) You must retain, in the Source form of any Derivative Works | ||
| 101 | + that You distribute, all copyright, patent, trademark, and | ||
| 102 | + attribution notices from the Source form of the Work, | ||
| 103 | + excluding those notices that do not pertain to any part of | ||
| 104 | + the Derivative Works; and | ||
| 105 | + | ||
| 106 | + (d) If the Work includes a "NOTICE" text file as part of its | ||
| 107 | + distribution, then any Derivative Works that You distribute must | ||
| 108 | + include a readable copy of the attribution notices contained | ||
| 109 | + within such NOTICE file, excluding those notices that do not | ||
| 110 | + pertain to any part of the Derivative Works, in at least one | ||
| 111 | + of the following places: within a NOTICE text file distributed | ||
| 112 | + as part of the Derivative Works; within the Source form or | ||
| 113 | + documentation, if provided along with the Derivative Works; or, | ||
| 114 | + within a display generated by the Derivative Works, if and | ||
| 115 | + wherever such third-party notices normally appear. The contents | ||
| 116 | + of the NOTICE file are for informational purposes only and | ||
| 117 | + do not modify the License. You may add Your own attribution | ||
| 118 | + notices within Derivative Works that You distribute, alongside | ||
| 119 | + or as an addendum to the NOTICE text from the Work, provided | ||
| 120 | + that such additional attribution notices cannot be construed | ||
| 121 | + as modifying the License. | ||
| 122 | + | ||
| 123 | + You may add Your own copyright statement to Your modifications and | ||
| 124 | + may provide additional or different license terms and conditions | ||
| 125 | + for use, reproduction, or distribution of Your modifications, or | ||
| 126 | + for any such Derivative Works as a whole, provided Your use, | ||
| 127 | + reproduction, and distribution of the Work otherwise complies with | ||
| 128 | + the conditions stated in this License. | ||
| 129 | + | ||
| 130 | + 5. Submission of Contributions. Unless You explicitly state otherwise, | ||
| 131 | + any Contribution intentionally submitted for inclusion in the Work | ||
| 132 | + by You to the Licensor shall be under the terms and conditions of | ||
| 133 | + this License, without any additional terms or conditions. | ||
| 134 | + Notwithstanding the above, nothing herein shall supersede or modify | ||
| 135 | + the terms of any separate license agreement you may have executed | ||
| 136 | + with Licensor regarding such Contributions. | ||
| 137 | + | ||
| 138 | + 6. Trademarks. This License does not grant permission to use the trade | ||
| 139 | + names, trademarks, service marks, or product names of the Licensor, | ||
| 140 | + except as required for reasonable and customary use in describing the | ||
| 141 | + origin of the Work and reproducing the content of the NOTICE file. | ||
| 142 | + | ||
| 143 | + 7. Disclaimer of Warranty. Unless required by applicable law or | ||
| 144 | + agreed to in writing, Licensor provides the Work (and each | ||
| 145 | + Contributor provides its Contributions) on an "AS IS" BASIS, | ||
| 146 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
| 147 | + implied, including, without limitation, any warranties or conditions | ||
| 148 | + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A | ||
| 149 | + PARTICULAR PURPOSE. You are solely responsible for determining the | ||
| 150 | + appropriateness of using or redistributing the Work and assume any | ||
| 151 | + risks associated with Your exercise of permissions under this License. | ||
| 152 | + | ||
| 153 | + 8. Limitation of Liability. In no event and under no legal theory, | ||
| 154 | + whether in tort (including negligence), contract, or otherwise, | ||
| 155 | + unless required by applicable law (such as deliberate and grossly | ||
| 156 | + negligent acts) or agreed to in writing, shall any Contributor be | ||
| 157 | + liable to You for damages, including any direct, indirect, special, | ||
| 158 | + incidental, or consequential damages of any character arising as a | ||
| 159 | + result of this License or out of the use or inability to use the | ||
| 160 | + Work (including but not limited to damages for loss of goodwill, | ||
| 161 | + work stoppage, computer failure or malfunction, or any and all | ||
| 162 | + other commercial damages or losses), even if such Contributor | ||
| 163 | + has been advised of the possibility of such damages. | ||
| 164 | + | ||
| 165 | + 9. Accepting Warranty or Additional Liability. While redistributing | ||
| 166 | + the Work or Derivative Works thereof, You may choose to offer, | ||
| 167 | + and charge a fee for, acceptance of support, warranty, indemnity, | ||
| 168 | + or other liability obligations and/or rights consistent with this | ||
| 169 | + License. However, in accepting such obligations, You may act only | ||
| 170 | + on Your own behalf and on Your sole responsibility, not on behalf | ||
| 171 | + of any other Contributor, and only if You agree to indemnify, | ||
| 172 | + defend, and hold each Contributor harmless for any liability | ||
| 173 | + incurred by, or claims asserted against, such Contributor by reason | ||
| 174 | + of your accepting any such warranty or additional liability. | ||
| 175 | + | ||
| 176 | + END OF TERMS AND CONDITIONS | ||
| 177 | + | ||
| 178 | + APPENDIX: How to apply the Apache License to your work. | ||
| 179 | + | ||
| 180 | + To apply the Apache License to your work, attach the following | ||
| 181 | + boilerplate notice, with the fields enclosed by brackets "{}" | ||
| 182 | + replaced with your own identifying information. (Don't include | ||
| 183 | + the brackets!) The text should be enclosed in the appropriate | ||
| 184 | + comment syntax for the file format. We also recommend that a | ||
| 185 | + file or class name and description of purpose be included on the | ||
| 186 | + same "printed page" as the copyright notice for easier | ||
| 187 | + identification within third-party archives. | ||
| 188 | + | ||
| 189 | + Copyright {yyyy} {name of copyright owner} | ||
| 190 | + | ||
| 191 | + Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 192 | + you may not use this file except in compliance with the License. | ||
| 193 | + You may obtain a copy of the License at | ||
| 194 | + | ||
| 195 | + http://www.apache.org/licenses/LICENSE-2.0 | ||
| 196 | + | ||
| 197 | + Unless required by applicable law or agreed to in writing, software | ||
| 198 | + distributed under the License is distributed on an "AS IS" BASIS, | ||
| 199 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 200 | + See the License for the specific language governing permissions and | ||
| 201 | + limitations under the License. |
vendor/topthink/think-queue/README.md
0 → 100644
| 1 | +# think-queue | ||
| 2 | + | ||
| 3 | +## 安装 | ||
| 4 | +> composer require topthink/think-queue | ||
| 5 | + | ||
| 6 | +## 配置 | ||
| 7 | +> 配置文件位于 `application/extra/queue.php` | ||
| 8 | +### 公共配置 | ||
| 9 | + | ||
| 10 | +``` | ||
| 11 | +[ | ||
| 12 | + 'connector'=>'sync' //驱动类型,可选择 sync(默认):同步执行,database:数据库驱动,redis:Redis驱动,topthink:Topthink驱动 | ||
| 13 | + //或其他自定义的完整的类名 | ||
| 14 | +] | ||
| 15 | +``` | ||
| 16 | + | ||
| 17 | +### 驱动配置 | ||
| 18 | +> 各个驱动的具体可用配置项在`think\queue\connector`目录下各个驱动类里的`options`属性中,写在上面的`queue`配置里即可覆盖 | ||
| 19 | + | ||
| 20 | + | ||
| 21 | +## 使用 Database | ||
| 22 | +> 创建如下数据表 | ||
| 23 | + | ||
| 24 | +``` | ||
| 25 | +CREATE TABLE `prefix_jobs` ( | ||
| 26 | + `id` int(11) NOT NULL AUTO_INCREMENT, | ||
| 27 | + `queue` varchar(255) NOT NULL, | ||
| 28 | + `payload` longtext NOT NULL, | ||
| 29 | + `attempts` tinyint(3) unsigned NOT NULL, | ||
| 30 | + `reserved` tinyint(3) unsigned NOT NULL, | ||
| 31 | + `reserved_at` int(10) unsigned DEFAULT NULL, | ||
| 32 | + `available_at` int(10) unsigned NOT NULL, | ||
| 33 | + `created_at` int(10) unsigned NOT NULL, | ||
| 34 | + PRIMARY KEY (`id`) | ||
| 35 | +) ENGINE=InnoDB DEFAULT CHARSET=utf8; | ||
| 36 | +``` | ||
| 37 | + | ||
| 38 | +## 创建任务类 | ||
| 39 | +> 单模块项目推荐使用 `app\job` 作为任务类的命名空间 | ||
| 40 | +> 多模块项目可用使用 `app\module\job` 作为任务类的命名空间 | ||
| 41 | +> 也可以放在任意可以自动加载到的地方 | ||
| 42 | + | ||
| 43 | +任务类不需继承任何类,如果这个类只有一个任务,那么就只需要提供一个`fire`方法就可以了,如果有多个小任务,就写多个方法,下面发布任务的时候会有区别 | ||
| 44 | +每个方法会传入两个参数 `think\queue\Job $job`(当前的任务对象) 和 `$data`(发布任务时自定义的数据) | ||
| 45 | + | ||
| 46 | +还有个可选的任务失败执行的方法 `failed` 传入的参数为`$data`(发布任务时自定义的数据) | ||
| 47 | + | ||
| 48 | +### 下面写两个例子 | ||
| 49 | + | ||
| 50 | +``` | ||
| 51 | +namespace app\job; | ||
| 52 | + | ||
| 53 | +use think\queue\Job; | ||
| 54 | + | ||
| 55 | +class Job1{ | ||
| 56 | + | ||
| 57 | + public function fire(Job $job, $data){ | ||
| 58 | + | ||
| 59 | + //....这里执行具体的任务 | ||
| 60 | + | ||
| 61 | + if ($job->attempts() > 3) { | ||
| 62 | + //通过这个方法可以检查这个任务已经重试了几次了 | ||
| 63 | + } | ||
| 64 | + | ||
| 65 | + | ||
| 66 | + //如果任务执行成功后 记得删除任务,不然这个任务会重复执行,直到达到最大重试次数后失败后,执行failed方法 | ||
| 67 | + $job->delete(); | ||
| 68 | + | ||
| 69 | + // 也可以重新发布这个任务 | ||
| 70 | + $job->release($delay); //$delay为延迟时间 | ||
| 71 | + | ||
| 72 | + } | ||
| 73 | + | ||
| 74 | + public function failed($data){ | ||
| 75 | + | ||
| 76 | + // ...任务达到最大重试次数后,失败了 | ||
| 77 | + } | ||
| 78 | + | ||
| 79 | +} | ||
| 80 | + | ||
| 81 | +``` | ||
| 82 | + | ||
| 83 | +``` | ||
| 84 | + | ||
| 85 | +namespace app\lib\job; | ||
| 86 | + | ||
| 87 | +use think\queue\Job; | ||
| 88 | + | ||
| 89 | +class Job2{ | ||
| 90 | + | ||
| 91 | + public function task1(Job $job, $data){ | ||
| 92 | + | ||
| 93 | + | ||
| 94 | + } | ||
| 95 | + | ||
| 96 | + public function task2(Job $job, $data){ | ||
| 97 | + | ||
| 98 | + | ||
| 99 | + } | ||
| 100 | + | ||
| 101 | + public function failed($data){ | ||
| 102 | + | ||
| 103 | + | ||
| 104 | + } | ||
| 105 | + | ||
| 106 | +} | ||
| 107 | + | ||
| 108 | +``` | ||
| 109 | + | ||
| 110 | + | ||
| 111 | +## 发布任务 | ||
| 112 | +> `think\Queue::push($job, $data = '', $queue = null)` 和 `think\Queue::later($delay, $job, $data = '', $queue = null)` 两个方法,前者是立即执行,后者是在`$delay`秒后执行 | ||
| 113 | + | ||
| 114 | +`$job` 是任务名 | ||
| 115 | +单模块的,且命名空间是`app\job`的,比如上面的例子一,写`Job1`类名即可 | ||
| 116 | +多模块的,且命名空间是`app\module\job`的,写`model/Job1`即可 | ||
| 117 | +其他的需要些完整的类名,比如上面的例子二,需要写完整的类名`app\lib\job\Job2` | ||
| 118 | +如果一个任务类里有多个小任务的话,如上面的例子二,需要用@+方法名`app\lib\job\Job2@task1`、`app\lib\job\Job2@task2` | ||
| 119 | + | ||
| 120 | +`$data` 是你要传到任务里的参数 | ||
| 121 | + | ||
| 122 | +`$queue` 队列名,指定这个任务是在哪个队列上执行,同下面监控队列的时候指定的队列名,可不填 | ||
| 123 | + | ||
| 124 | +## 监听任务并执行 | ||
| 125 | + | ||
| 126 | +> php think queue:listen | ||
| 127 | + | ||
| 128 | +> php think queue:work --daemon(不加--daemon为执行单个任务) | ||
| 129 | + | ||
| 130 | +两种,具体的可选参数可以输入命令加 --help 查看 | ||
| 131 | + | ||
| 132 | +>可配合supervisor使用,保证进程常驻 |
vendor/topthink/think-queue/composer.json
0 → 100644
| 1 | +{ | ||
| 2 | + "name": "topthink/think-queue", | ||
| 3 | + "description": "The ThinkPHP5 Queue Package", | ||
| 4 | + "type": "think-extend", | ||
| 5 | + "authors": [ | ||
| 6 | + { | ||
| 7 | + "name": "yunwuxin", | ||
| 8 | + "email": "448901948@qq.com" | ||
| 9 | + } | ||
| 10 | + ], | ||
| 11 | + "license": "Apache-2.0", | ||
| 12 | + "autoload": { | ||
| 13 | + "psr-4": { | ||
| 14 | + "think\\": "src" | ||
| 15 | + }, | ||
| 16 | + "files": [ | ||
| 17 | + "src/common.php" | ||
| 18 | + ] | ||
| 19 | + }, | ||
| 20 | + "require": { | ||
| 21 | + "topthink/think-helper": ">=1.0.4", | ||
| 22 | + "topthink/think-installer": ">=1.0.10" | ||
| 23 | + }, | ||
| 24 | + "extra": { | ||
| 25 | + "think-config": { | ||
| 26 | + "queue": "src/config.php" | ||
| 27 | + } | ||
| 28 | + } | ||
| 29 | +} |
vendor/topthink/think-queue/src/Queue.php
0 → 100644
| 1 | +<?php | ||
| 2 | +// +---------------------------------------------------------------------- | ||
| 3 | +// | ThinkPHP [ WE CAN DO IT JUST THINK IT ] | ||
| 4 | +// +---------------------------------------------------------------------- | ||
| 5 | +// | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved. | ||
| 6 | +// +---------------------------------------------------------------------- | ||
| 7 | +// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) | ||
| 8 | +// +---------------------------------------------------------------------- | ||
| 9 | +// | Author: yunwuxin <448901948@qq.com> | ||
| 10 | +// +---------------------------------------------------------------------- | ||
| 11 | + | ||
| 12 | +namespace think; | ||
| 13 | + | ||
| 14 | +use think\helper\Str; | ||
| 15 | +use think\queue\Connector; | ||
| 16 | + | ||
| 17 | +/** | ||
| 18 | + * Class Queue | ||
| 19 | + * @package think\queue | ||
| 20 | + * | ||
| 21 | + * @method static push($job, $data = '', $queue = null) | ||
| 22 | + * @method static later($delay, $job, $data = '', $queue = null) | ||
| 23 | + * @method static pop($queue = null) | ||
| 24 | + * @method static marshal() | ||
| 25 | + */ | ||
| 26 | +class Queue | ||
| 27 | +{ | ||
| 28 | + /** @var Connector */ | ||
| 29 | + protected static $connector; | ||
| 30 | + | ||
| 31 | + private static function buildConnector() | ||
| 32 | + { | ||
| 33 | + $options = Config::get('queue'); | ||
| 34 | + $type = !empty($options['connector']) ? $options['connector'] : 'Sync'; | ||
| 35 | + | ||
| 36 | + if (!isset(self::$connector)) { | ||
| 37 | + | ||
| 38 | + $class = false !== strpos($type, '\\') ? $type : '\\think\\queue\\connector\\' . Str::studly($type); | ||
| 39 | + | ||
| 40 | + self::$connector = new $class($options); | ||
| 41 | + } | ||
| 42 | + return self::$connector; | ||
| 43 | + } | ||
| 44 | + | ||
| 45 | + public static function __callStatic($name, $arguments) | ||
| 46 | + { | ||
| 47 | + return call_user_func_array([self::buildConnector(), $name], $arguments); | ||
| 48 | + } | ||
| 49 | +} |
vendor/topthink/think-queue/src/common.php
0 → 100644
| 1 | +<?php | ||
| 2 | +// +---------------------------------------------------------------------- | ||
| 3 | +// | ThinkPHP [ WE CAN DO IT JUST THINK IT ] | ||
| 4 | +// +---------------------------------------------------------------------- | ||
| 5 | +// | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved. | ||
| 6 | +// +---------------------------------------------------------------------- | ||
| 7 | +// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) | ||
| 8 | +// +---------------------------------------------------------------------- | ||
| 9 | +// | Author: yunwuxin <448901948@qq.com> | ||
| 10 | +// +---------------------------------------------------------------------- | ||
| 11 | + | ||
| 12 | +\think\Console::addDefaultCommands([ | ||
| 13 | + "think\\queue\\command\\Work", | ||
| 14 | + "think\\queue\\command\\Restart", | ||
| 15 | + "think\\queue\\command\\Listen", | ||
| 16 | + "think\\queue\\command\\Subscribe" | ||
| 17 | +]); | ||
| 18 | + | ||
| 19 | +if (!function_exists('queue')) { | ||
| 20 | + | ||
| 21 | + /** | ||
| 22 | + * 添加到队列 | ||
| 23 | + * @param $job | ||
| 24 | + * @param string $data | ||
| 25 | + * @param int $delay | ||
| 26 | + * @param null $queue | ||
| 27 | + */ | ||
| 28 | + function queue($job, $data = '', $delay = 0, $queue = null) | ||
| 29 | + { | ||
| 30 | + if ($delay > 0) { | ||
| 31 | + \think\Queue::later($delay, $job, $data, $queue); | ||
| 32 | + } else { | ||
| 33 | + \think\Queue::push($job, $data, $queue); | ||
| 34 | + } | ||
| 35 | + } | ||
| 36 | +} |
vendor/topthink/think-queue/src/config.php
0 → 100644
| 1 | +<?php | ||
| 2 | +// +---------------------------------------------------------------------- | ||
| 3 | +// | ThinkPHP [ WE CAN DO IT JUST THINK IT ] | ||
| 4 | +// +---------------------------------------------------------------------- | ||
| 5 | +// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved. | ||
| 6 | +// +---------------------------------------------------------------------- | ||
| 7 | +// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) | ||
| 8 | +// +---------------------------------------------------------------------- | ||
| 9 | +// | Author: yunwuxin <448901948@qq.com> | ||
| 10 | +// +---------------------------------------------------------------------- | ||
| 11 | + | ||
| 12 | +return [ | ||
| 13 | + 'connector' => 'Sync' | ||
| 14 | +]; |
| 1 | +<?php | ||
| 2 | +// +---------------------------------------------------------------------- | ||
| 3 | +// | ThinkPHP [ WE CAN DO IT JUST THINK IT ] | ||
| 4 | +// +---------------------------------------------------------------------- | ||
| 5 | +// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved. | ||
| 6 | +// +---------------------------------------------------------------------- | ||
| 7 | +// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) | ||
| 8 | +// +---------------------------------------------------------------------- | ||
| 9 | +// | Author: yunwuxin <448901948@qq.com> | ||
| 10 | +// +---------------------------------------------------------------------- | ||
| 11 | + | ||
| 12 | +namespace think\queue; | ||
| 13 | + | ||
| 14 | +class CallQueuedHandler | ||
| 15 | +{ | ||
| 16 | + | ||
| 17 | + public function call(Job $job, array $data) | ||
| 18 | + { | ||
| 19 | + $command = unserialize($data['command']); | ||
| 20 | + | ||
| 21 | + call_user_func([$command, 'handle']); | ||
| 22 | + | ||
| 23 | + if (!$job->isDeletedOrReleased()) { | ||
| 24 | + $job->delete(); | ||
| 25 | + } | ||
| 26 | + } | ||
| 27 | + | ||
| 28 | + public function failed(array $data) | ||
| 29 | + { | ||
| 30 | + $command = unserialize($data['command']); | ||
| 31 | + | ||
| 32 | + if (method_exists($command, 'failed')) { | ||
| 33 | + $command->failed(); | ||
| 34 | + } | ||
| 35 | + } | ||
| 36 | +} |
| 1 | +<?php | ||
| 2 | +// +---------------------------------------------------------------------- | ||
| 3 | +// | ThinkPHP [ WE CAN DO IT JUST THINK IT ] | ||
| 4 | +// +---------------------------------------------------------------------- | ||
| 5 | +// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved. | ||
| 6 | +// +---------------------------------------------------------------------- | ||
| 7 | +// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) | ||
| 8 | +// +---------------------------------------------------------------------- | ||
| 9 | +// | Author: yunwuxin <448901948@qq.com> | ||
| 10 | +// +---------------------------------------------------------------------- | ||
| 11 | + | ||
| 12 | +namespace think\queue; | ||
| 13 | + | ||
| 14 | +use InvalidArgumentException; | ||
| 15 | + | ||
| 16 | +abstract class Connector | ||
| 17 | +{ | ||
| 18 | + protected $options = []; | ||
| 19 | + | ||
| 20 | + abstract public function push($job, $data = '', $queue = null); | ||
| 21 | + | ||
| 22 | + abstract public function later($delay, $job, $data = '', $queue = null); | ||
| 23 | + | ||
| 24 | + abstract public function pop($queue = null); | ||
| 25 | + | ||
| 26 | + public function marshal() | ||
| 27 | + { | ||
| 28 | + throw new \RuntimeException('pop queues not support for this type'); | ||
| 29 | + } | ||
| 30 | + | ||
| 31 | + protected function createPayload($job, $data = '', $queue = null) | ||
| 32 | + { | ||
| 33 | + if (is_object($job)) { | ||
| 34 | + $payload = json_encode([ | ||
| 35 | + 'job' => 'think\queue\CallQueuedHandler@call', | ||
| 36 | + 'data' => [ | ||
| 37 | + 'commandName' => get_class($job), | ||
| 38 | + 'command' => serialize(clone $job), | ||
| 39 | + ], | ||
| 40 | + ]); | ||
| 41 | + } else { | ||
| 42 | + $payload = json_encode($this->createPlainPayload($job, $data)); | ||
| 43 | + } | ||
| 44 | + | ||
| 45 | + if (JSON_ERROR_NONE !== json_last_error()) { | ||
| 46 | + throw new InvalidArgumentException('Unable to create payload: ' . json_last_error_msg()); | ||
| 47 | + } | ||
| 48 | + | ||
| 49 | + return $payload; | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + protected function createPlainPayload($job, $data) | ||
| 53 | + { | ||
| 54 | + return ['job' => $job, 'data' => $data]; | ||
| 55 | + } | ||
| 56 | + | ||
| 57 | + protected function setMeta($payload, $key, $value) | ||
| 58 | + { | ||
| 59 | + $payload = json_decode($payload, true); | ||
| 60 | + $payload[$key] = $value; | ||
| 61 | + $payload = json_encode($payload); | ||
| 62 | + | ||
| 63 | + if (JSON_ERROR_NONE !== json_last_error()) { | ||
| 64 | + throw new InvalidArgumentException('Unable to create payload: ' . json_last_error_msg()); | ||
| 65 | + } | ||
| 66 | + | ||
| 67 | + return $payload; | ||
| 68 | + } | ||
| 69 | +} |
| 1 | +<?php | ||
| 2 | +// +---------------------------------------------------------------------- | ||
| 3 | +// | ThinkPHP [ WE CAN DO IT JUST THINK IT ] | ||
| 4 | +// +---------------------------------------------------------------------- | ||
| 5 | +// | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved. | ||
| 6 | +// +---------------------------------------------------------------------- | ||
| 7 | +// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) | ||
| 8 | +// +---------------------------------------------------------------------- | ||
| 9 | +// | Author: yunwuxin <448901948@qq.com> | ||
| 10 | +// +---------------------------------------------------------------------- | ||
| 11 | + | ||
| 12 | +namespace think\queue; | ||
| 13 | + | ||
| 14 | +use DateTime; | ||
| 15 | +use think\Config; | ||
| 16 | + | ||
| 17 | +abstract class Job | ||
| 18 | +{ | ||
| 19 | + | ||
| 20 | + /** | ||
| 21 | + * The job handler instance. | ||
| 22 | + * @var mixed | ||
| 23 | + */ | ||
| 24 | + protected $instance; | ||
| 25 | + | ||
| 26 | + /** | ||
| 27 | + * The name of the queue the job belongs to. | ||
| 28 | + * @var string | ||
| 29 | + */ | ||
| 30 | + protected $queue; | ||
| 31 | + | ||
| 32 | + /** | ||
| 33 | + * Indicates if the job has been deleted. | ||
| 34 | + * @var bool | ||
| 35 | + */ | ||
| 36 | + protected $deleted = false; | ||
| 37 | + | ||
| 38 | + /** | ||
| 39 | + * Indicates if the job has been released. | ||
| 40 | + * @var bool | ||
| 41 | + */ | ||
| 42 | + protected $released = false; | ||
| 43 | + | ||
| 44 | + /** | ||
| 45 | + * Fire the job. | ||
| 46 | + * @return void | ||
| 47 | + */ | ||
| 48 | + abstract public function fire(); | ||
| 49 | + | ||
| 50 | + /** | ||
| 51 | + * Delete the job from the queue. | ||
| 52 | + * @return void | ||
| 53 | + */ | ||
| 54 | + public function delete() | ||
| 55 | + { | ||
| 56 | + $this->deleted = true; | ||
| 57 | + } | ||
| 58 | + | ||
| 59 | + /** | ||
| 60 | + * Determine if the job has been deleted. | ||
| 61 | + * @return bool | ||
| 62 | + */ | ||
| 63 | + public function isDeleted() | ||
| 64 | + { | ||
| 65 | + return $this->deleted; | ||
| 66 | + } | ||
| 67 | + | ||
| 68 | + /** | ||
| 69 | + * Release the job back into the queue. | ||
| 70 | + * @param int $delay | ||
| 71 | + * @return void | ||
| 72 | + */ | ||
| 73 | + public function release($delay = 0) | ||
| 74 | + { | ||
| 75 | + $this->released = true; | ||
| 76 | + } | ||
| 77 | + | ||
| 78 | + /** | ||
| 79 | + * Determine if the job was released back into the queue. | ||
| 80 | + * @return bool | ||
| 81 | + */ | ||
| 82 | + public function isReleased() | ||
| 83 | + { | ||
| 84 | + return $this->released; | ||
| 85 | + } | ||
| 86 | + | ||
| 87 | + /** | ||
| 88 | + * Determine if the job has been deleted or released. | ||
| 89 | + * @return bool | ||
| 90 | + */ | ||
| 91 | + public function isDeletedOrReleased() | ||
| 92 | + { | ||
| 93 | + return $this->isDeleted() || $this->isReleased(); | ||
| 94 | + } | ||
| 95 | + | ||
| 96 | + /** | ||
| 97 | + * Get the number of times the job has been attempted. | ||
| 98 | + * @return int | ||
| 99 | + */ | ||
| 100 | + abstract public function attempts(); | ||
| 101 | + | ||
| 102 | + /** | ||
| 103 | + * Get the raw body string for the job. | ||
| 104 | + * @return string | ||
| 105 | + */ | ||
| 106 | + abstract public function getRawBody(); | ||
| 107 | + | ||
| 108 | + /** | ||
| 109 | + * Resolve and fire the job handler method. | ||
| 110 | + * @param array $payload | ||
| 111 | + * @return void | ||
| 112 | + */ | ||
| 113 | + protected function resolveAndFire(array $payload) | ||
| 114 | + { | ||
| 115 | + list($class, $method) = $this->parseJob($payload['job']); | ||
| 116 | + | ||
| 117 | + $this->instance = $this->resolve($class); | ||
| 118 | + if ($this->instance) { | ||
| 119 | + $this->instance->{$method}($this, $payload['data']); | ||
| 120 | + } | ||
| 121 | + } | ||
| 122 | + | ||
| 123 | + /** | ||
| 124 | + * Parse the job declaration into class and method. | ||
| 125 | + * @param string $job | ||
| 126 | + * @return array | ||
| 127 | + */ | ||
| 128 | + protected function parseJob($job) | ||
| 129 | + { | ||
| 130 | + $segments = explode('@', $job); | ||
| 131 | + | ||
| 132 | + return count($segments) > 1 ? $segments : [$segments[0], 'fire']; | ||
| 133 | + } | ||
| 134 | + | ||
| 135 | + /** | ||
| 136 | + * Resolve the given job handler. | ||
| 137 | + * @param string $name | ||
| 138 | + * @return mixed | ||
| 139 | + */ | ||
| 140 | + protected function resolve($name) | ||
| 141 | + { | ||
| 142 | + if (strpos($name, '\\') === false) { | ||
| 143 | + | ||
| 144 | + if (strpos($name, '/') === false) { | ||
| 145 | + $module = ''; | ||
| 146 | + } else { | ||
| 147 | + list($module, $name) = explode('/', $name, 2); | ||
| 148 | + } | ||
| 149 | + | ||
| 150 | + $name = Config::get('app_namespace') . ($module ? '\\' . strtolower($module) : '') . '\\job\\' . $name; | ||
| 151 | + } | ||
| 152 | + if (class_exists($name)) { | ||
| 153 | + return new $name(); | ||
| 154 | + } | ||
| 155 | + } | ||
| 156 | + | ||
| 157 | + /** | ||
| 158 | + * Call the failed method on the job instance. | ||
| 159 | + * @return void | ||
| 160 | + */ | ||
| 161 | + public function failed() | ||
| 162 | + { | ||
| 163 | + $payload = json_decode($this->getRawBody(), true); | ||
| 164 | + | ||
| 165 | + list($class, $method) = $this->parseJob($payload['job']); | ||
| 166 | + | ||
| 167 | + $this->instance = $this->resolve($class); | ||
| 168 | + if ($this->instance && method_exists($this->instance, 'failed')) { | ||
| 169 | + $this->instance->failed($payload['data']); | ||
| 170 | + } | ||
| 171 | + } | ||
| 172 | + | ||
| 173 | + /** | ||
| 174 | + * Calculate the number of seconds with the given delay. | ||
| 175 | + * @param \DateTime|int $delay | ||
| 176 | + * @return int | ||
| 177 | + */ | ||
| 178 | + protected function getSeconds($delay) | ||
| 179 | + { | ||
| 180 | + if ($delay instanceof DateTime) { | ||
| 181 | + return max(0, $delay->getTimestamp() - $this->getTime()); | ||
| 182 | + } | ||
| 183 | + | ||
| 184 | + return (int) $delay; | ||
| 185 | + } | ||
| 186 | + | ||
| 187 | + /** | ||
| 188 | + * Get the current system time. | ||
| 189 | + * @return int | ||
| 190 | + */ | ||
| 191 | + protected function getTime() | ||
| 192 | + { | ||
| 193 | + return time(); | ||
| 194 | + } | ||
| 195 | + | ||
| 196 | + /** | ||
| 197 | + * Get the name of the queued job class. | ||
| 198 | + * @return string | ||
| 199 | + */ | ||
| 200 | + public function getName() | ||
| 201 | + { | ||
| 202 | + return json_decode($this->getRawBody(), true)['job']; | ||
| 203 | + } | ||
| 204 | + | ||
| 205 | + /** | ||
| 206 | + * Get the name of the queue the job belongs to. | ||
| 207 | + * @return string | ||
| 208 | + */ | ||
| 209 | + public function getQueue() | ||
| 210 | + { | ||
| 211 | + return $this->queue; | ||
| 212 | + } | ||
| 213 | +} |
| 1 | +<?php | ||
| 2 | +// +---------------------------------------------------------------------- | ||
| 3 | +// | ThinkPHP [ WE CAN DO IT JUST THINK IT ] | ||
| 4 | +// +---------------------------------------------------------------------- | ||
| 5 | +// | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved. | ||
| 6 | +// +---------------------------------------------------------------------- | ||
| 7 | +// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) | ||
| 8 | +// +---------------------------------------------------------------------- | ||
| 9 | +// | Author: yunwuxin <448901948@qq.com> | ||
| 10 | +// +---------------------------------------------------------------------- | ||
| 11 | + | ||
| 12 | +namespace think\queue; | ||
| 13 | + | ||
| 14 | +use Closure; | ||
| 15 | +use think\Process; | ||
| 16 | + | ||
| 17 | +class Listener | ||
| 18 | +{ | ||
| 19 | + | ||
| 20 | + /** | ||
| 21 | + * @var string | ||
| 22 | + */ | ||
| 23 | + protected $commandPath; | ||
| 24 | + | ||
| 25 | + /** | ||
| 26 | + * @var int | ||
| 27 | + */ | ||
| 28 | + protected $sleep = 3; | ||
| 29 | + | ||
| 30 | + /** | ||
| 31 | + * @var int | ||
| 32 | + */ | ||
| 33 | + protected $maxTries = 0; | ||
| 34 | + | ||
| 35 | + /** | ||
| 36 | + * @var string | ||
| 37 | + */ | ||
| 38 | + protected $workerCommand; | ||
| 39 | + | ||
| 40 | + /** | ||
| 41 | + * @var \Closure|null | ||
| 42 | + */ | ||
| 43 | + protected $outputHandler; | ||
| 44 | + | ||
| 45 | + /** | ||
| 46 | + * @param string $commandPath | ||
| 47 | + */ | ||
| 48 | + public function __construct($commandPath) | ||
| 49 | + { | ||
| 50 | + $this->commandPath = $commandPath; | ||
| 51 | + $this->workerCommand = | ||
| 52 | + '"' . PHP_BINARY . '" think queue:work --queue="%s" --delay=%s --memory=%s --sleep=%s --tries=%s'; | ||
| 53 | + } | ||
| 54 | + | ||
| 55 | + /** | ||
| 56 | + * @param string $queue | ||
| 57 | + * @param string $delay | ||
| 58 | + * @param string $memory | ||
| 59 | + * @param int $timeout | ||
| 60 | + * @return void | ||
| 61 | + */ | ||
| 62 | + public function listen($queue, $delay, $memory, $timeout = 60) | ||
| 63 | + { | ||
| 64 | + $process = $this->makeProcess($queue, $delay, $memory, $timeout); | ||
| 65 | + | ||
| 66 | + while (true) { | ||
| 67 | + $this->runProcess($process, $memory); | ||
| 68 | + } | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + /** | ||
| 72 | + * @param \Think\Process $process | ||
| 73 | + * @param int $memory | ||
| 74 | + */ | ||
| 75 | + public function runProcess(Process $process, $memory) | ||
| 76 | + { | ||
| 77 | + $process->run(function ($type, $line) { | ||
| 78 | + $this->handleWorkerOutput($type, $line); | ||
| 79 | + }); | ||
| 80 | + | ||
| 81 | + if ($this->memoryExceeded($memory)) { | ||
| 82 | + $this->stop(); | ||
| 83 | + } | ||
| 84 | + } | ||
| 85 | + | ||
| 86 | + /** | ||
| 87 | + * @param string $queue | ||
| 88 | + * @param int $delay | ||
| 89 | + * @param int $memory | ||
| 90 | + * @param int $timeout | ||
| 91 | + * @return \think\Process | ||
| 92 | + */ | ||
| 93 | + public function makeProcess($queue, $delay, $memory, $timeout) | ||
| 94 | + { | ||
| 95 | + $string = $this->workerCommand; | ||
| 96 | + $command = sprintf($string, $queue, $delay, $memory, $this->sleep, $this->maxTries); | ||
| 97 | + | ||
| 98 | + return new Process($command, $this->commandPath, null, null, $timeout); | ||
| 99 | + } | ||
| 100 | + | ||
| 101 | + /** | ||
| 102 | + * @param int $type | ||
| 103 | + * @param string $line | ||
| 104 | + * @return void | ||
| 105 | + */ | ||
| 106 | + protected function handleWorkerOutput($type, $line) | ||
| 107 | + { | ||
| 108 | + if (isset($this->outputHandler)) { | ||
| 109 | + call_user_func($this->outputHandler, $type, $line); | ||
| 110 | + } | ||
| 111 | + } | ||
| 112 | + | ||
| 113 | + /** | ||
| 114 | + * @param int $memoryLimit | ||
| 115 | + * @return bool | ||
| 116 | + */ | ||
| 117 | + public function memoryExceeded($memoryLimit) | ||
| 118 | + { | ||
| 119 | + return (memory_get_usage() / 1024 / 1024) >= $memoryLimit; | ||
| 120 | + } | ||
| 121 | + | ||
| 122 | + /** | ||
| 123 | + * @return void | ||
| 124 | + */ | ||
| 125 | + public function stop() | ||
| 126 | + { | ||
| 127 | + die; | ||
| 128 | + } | ||
| 129 | + | ||
| 130 | + /** | ||
| 131 | + * @param \Closure $outputHandler | ||
| 132 | + * @return void | ||
| 133 | + */ | ||
| 134 | + public function setOutputHandler(Closure $outputHandler) | ||
| 135 | + { | ||
| 136 | + $this->outputHandler = $outputHandler; | ||
| 137 | + } | ||
| 138 | + | ||
| 139 | + /** | ||
| 140 | + * @return int | ||
| 141 | + */ | ||
| 142 | + public function getSleep() | ||
| 143 | + { | ||
| 144 | + return $this->sleep; | ||
| 145 | + } | ||
| 146 | + | ||
| 147 | + /** | ||
| 148 | + * @param int $sleep | ||
| 149 | + * @return void | ||
| 150 | + */ | ||
| 151 | + public function setSleep($sleep) | ||
| 152 | + { | ||
| 153 | + $this->sleep = $sleep; | ||
| 154 | + } | ||
| 155 | + | ||
| 156 | + /** | ||
| 157 | + * @param int $tries | ||
| 158 | + * @return void | ||
| 159 | + */ | ||
| 160 | + public function setMaxTries($tries) | ||
| 161 | + { | ||
| 162 | + $this->maxTries = $tries; | ||
| 163 | + } | ||
| 164 | +} |
| 1 | +<?php | ||
| 2 | +// +---------------------------------------------------------------------- | ||
| 3 | +// | ThinkPHP [ WE CAN DO IT JUST THINK IT ] | ||
| 4 | +// +---------------------------------------------------------------------- | ||
| 5 | +// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved. | ||
| 6 | +// +---------------------------------------------------------------------- | ||
| 7 | +// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) | ||
| 8 | +// +---------------------------------------------------------------------- | ||
| 9 | +// | Author: yunwuxin <448901948@qq.com> | ||
| 10 | +// +---------------------------------------------------------------------- | ||
| 11 | + | ||
| 12 | +namespace think\queue; | ||
| 13 | + | ||
| 14 | +trait Queueable | ||
| 15 | +{ | ||
| 16 | + | ||
| 17 | + /** @var string 队列名称 */ | ||
| 18 | + public $queue; | ||
| 19 | + | ||
| 20 | + /** @var integer 延迟时间 */ | ||
| 21 | + public $delay; | ||
| 22 | + | ||
| 23 | + /** | ||
| 24 | + * 设置队列名 | ||
| 25 | + * @param $queue | ||
| 26 | + * @return $this | ||
| 27 | + */ | ||
| 28 | + public function queue($queue) | ||
| 29 | + { | ||
| 30 | + $this->queue = $queue; | ||
| 31 | + | ||
| 32 | + return $this; | ||
| 33 | + } | ||
| 34 | + | ||
| 35 | + /** | ||
| 36 | + * 设置延迟时间 | ||
| 37 | + * @param $delay | ||
| 38 | + * @return $this | ||
| 39 | + */ | ||
| 40 | + public function delay($delay) | ||
| 41 | + { | ||
| 42 | + $this->delay = $delay; | ||
| 43 | + | ||
| 44 | + return $this; | ||
| 45 | + } | ||
| 46 | +} |
| 1 | +<?php | ||
| 2 | +// +---------------------------------------------------------------------- | ||
| 3 | +// | ThinkPHP [ WE CAN DO IT JUST THINK IT ] | ||
| 4 | +// +---------------------------------------------------------------------- | ||
| 5 | +// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved. | ||
| 6 | +// +---------------------------------------------------------------------- | ||
| 7 | +// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) | ||
| 8 | +// +---------------------------------------------------------------------- | ||
| 9 | +// | Author: yunwuxin <448901948@qq.com> | ||
| 10 | +// +---------------------------------------------------------------------- | ||
| 11 | + | ||
| 12 | +namespace think\queue; | ||
| 13 | + | ||
| 14 | +interface ShouldQueue | ||
| 15 | +{ | ||
| 16 | + | ||
| 17 | +} |
| 1 | +<?php | ||
| 2 | +// +---------------------------------------------------------------------- | ||
| 3 | +// | ThinkPHP [ WE CAN DO IT JUST THINK IT ] | ||
| 4 | +// +---------------------------------------------------------------------- | ||
| 5 | +// | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved. | ||
| 6 | +// +---------------------------------------------------------------------- | ||
| 7 | +// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) | ||
| 8 | +// +---------------------------------------------------------------------- | ||
| 9 | +// | Author: yunwuxin <448901948@qq.com> | ||
| 10 | +// +---------------------------------------------------------------------- | ||
| 11 | + | ||
| 12 | +namespace think\queue; | ||
| 13 | + | ||
| 14 | +use Exception; | ||
| 15 | +use think\Hook; | ||
| 16 | +use think\Queue; | ||
| 17 | + | ||
| 18 | +class Worker | ||
| 19 | +{ | ||
| 20 | + | ||
| 21 | + /** | ||
| 22 | + * 执行下个任务 | ||
| 23 | + * @param string $queue | ||
| 24 | + * @param int $delay | ||
| 25 | + * @param int $sleep | ||
| 26 | + * @param int $maxTries | ||
| 27 | + * @return array | ||
| 28 | + */ | ||
| 29 | + public function pop($queue = null, $delay = 0, $sleep = 3, $maxTries = 0) | ||
| 30 | + { | ||
| 31 | + | ||
| 32 | + $job = $this->getNextJob($queue); | ||
| 33 | + | ||
| 34 | + if (!is_null($job)) { | ||
| 35 | + Hook::listen('worker_before_process', $queue); | ||
| 36 | + return $this->process($job, $maxTries, $delay); | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + Hook::listen('worker_before_sleep', $queue); | ||
| 40 | + $this->sleep($sleep); | ||
| 41 | + | ||
| 42 | + return ['job' => null, 'failed' => false]; | ||
| 43 | + } | ||
| 44 | + | ||
| 45 | + /** | ||
| 46 | + * 获取下个任务 | ||
| 47 | + * @param string $queue | ||
| 48 | + * @return Job | ||
| 49 | + */ | ||
| 50 | + protected function getNextJob($queue) | ||
| 51 | + { | ||
| 52 | + if (is_null($queue)) { | ||
| 53 | + return Queue::pop(); | ||
| 54 | + } | ||
| 55 | + | ||
| 56 | + foreach (explode(',', $queue) as $queue) { | ||
| 57 | + if (!is_null($job = Queue::pop($queue))) { | ||
| 58 | + return $job; | ||
| 59 | + } | ||
| 60 | + } | ||
| 61 | + } | ||
| 62 | + | ||
| 63 | + /** | ||
| 64 | + * Process a given job from the queue. | ||
| 65 | + * @param \think\queue\Job $job | ||
| 66 | + * @param int $maxTries | ||
| 67 | + * @param int $delay | ||
| 68 | + * @return array | ||
| 69 | + * @throws Exception | ||
| 70 | + */ | ||
| 71 | + public function process(Job $job, $maxTries = 0, $delay = 0) | ||
| 72 | + { | ||
| 73 | + if ($maxTries > 0 && $job->attempts() > $maxTries) { | ||
| 74 | + return $this->logFailedJob($job); | ||
| 75 | + } | ||
| 76 | + | ||
| 77 | + try { | ||
| 78 | + $job->fire(); | ||
| 79 | + | ||
| 80 | + return ['job' => $job, 'failed' => false]; | ||
| 81 | + } catch (Exception $e) { | ||
| 82 | + if (!$job->isDeleted()) { | ||
| 83 | + $job->release($delay); | ||
| 84 | + } | ||
| 85 | + | ||
| 86 | + throw $e; | ||
| 87 | + } | ||
| 88 | + } | ||
| 89 | + | ||
| 90 | + /** | ||
| 91 | + * Log a failed job into storage. | ||
| 92 | + * @param \Think\Queue\Job $job | ||
| 93 | + * @return array | ||
| 94 | + */ | ||
| 95 | + protected function logFailedJob(Job $job) | ||
| 96 | + { | ||
| 97 | + if (!$job->isDeleted()) { | ||
| 98 | + try { | ||
| 99 | + $job->delete(); | ||
| 100 | + $job->failed(); | ||
| 101 | + } finally { | ||
| 102 | + Hook::listen('queue_failed', $job); | ||
| 103 | + } | ||
| 104 | + } | ||
| 105 | + | ||
| 106 | + return ['job' => $job, 'failed' => true]; | ||
| 107 | + } | ||
| 108 | + | ||
| 109 | + /** | ||
| 110 | + * Sleep the script for a given number of seconds. | ||
| 111 | + * @param int $seconds | ||
| 112 | + * @return void | ||
| 113 | + */ | ||
| 114 | + public function sleep($seconds) | ||
| 115 | + { | ||
| 116 | + sleep($seconds); | ||
| 117 | + } | ||
| 118 | + | ||
| 119 | +} |
| 1 | +<?php | ||
| 2 | +// +---------------------------------------------------------------------- | ||
| 3 | +// | ThinkPHP [ WE CAN DO IT JUST THINK IT ] | ||
| 4 | +// +---------------------------------------------------------------------- | ||
| 5 | +// | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved. | ||
| 6 | +// +---------------------------------------------------------------------- | ||
| 7 | +// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) | ||
| 8 | +// +---------------------------------------------------------------------- | ||
| 9 | +// | Author: yunwuxin <448901948@qq.com> | ||
| 10 | +// +---------------------------------------------------------------------- | ||
| 11 | + | ||
| 12 | +namespace think\queue\command; | ||
| 13 | + | ||
| 14 | +use think\console\Command; | ||
| 15 | +use think\console\Input; | ||
| 16 | +use think\console\input\Option; | ||
| 17 | +use think\console\Output; | ||
| 18 | +use think\queue\Listener; | ||
| 19 | + | ||
| 20 | +class Listen extends Command | ||
| 21 | +{ | ||
| 22 | + /** @var Listener */ | ||
| 23 | + protected $listener; | ||
| 24 | + | ||
| 25 | + public function configure() | ||
| 26 | + { | ||
| 27 | + $this->setName('queue:listen') | ||
| 28 | + ->addOption('queue', null, Option::VALUE_OPTIONAL, 'The queue to listen on', null) | ||
| 29 | + ->addOption('delay', null, Option::VALUE_OPTIONAL, 'Amount of time to delay failed jobs', 0) | ||
| 30 | + ->addOption('memory', null, Option::VALUE_OPTIONAL, 'The memory limit in megabytes', 128) | ||
| 31 | + ->addOption('timeout', null, Option::VALUE_OPTIONAL, 'Seconds a job may run before timing out', 60) | ||
| 32 | + ->addOption('sleep', null, Option::VALUE_OPTIONAL, 'Seconds to wait before checking queue for jobs', 3) | ||
| 33 | + ->addOption('tries', null, Option::VALUE_OPTIONAL, 'Number of times to attempt a job before logging it failed', 0) | ||
| 34 | + ->setDescription('Listen to a given queue'); | ||
| 35 | + } | ||
| 36 | + | ||
| 37 | + public function initialize(Input $input, Output $output) | ||
| 38 | + { | ||
| 39 | + $this->listener = new Listener(getcwd()); | ||
| 40 | + $this->listener->setSleep($input->getOption('sleep')); | ||
| 41 | + $this->listener->setMaxTries($input->getOption('tries')); | ||
| 42 | + | ||
| 43 | + $this->listener->setOutputHandler(function ($type, $line) use ($output) { | ||
| 44 | + $output->write($line); | ||
| 45 | + }); | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + public function execute(Input $input, Output $output) | ||
| 49 | + { | ||
| 50 | + $delay = $input->getOption('delay'); | ||
| 51 | + | ||
| 52 | + $memory = $input->getOption('memory'); | ||
| 53 | + | ||
| 54 | + $timeout = $input->getOption('timeout'); | ||
| 55 | + | ||
| 56 | + $queue = $input->getOption('queue') ?: 'default'; | ||
| 57 | + | ||
| 58 | + $this->listener->listen($queue, $delay, $memory, $timeout); | ||
| 59 | + } | ||
| 60 | +} |
| 1 | +<?php | ||
| 2 | +// +---------------------------------------------------------------------- | ||
| 3 | +// | ThinkPHP [ WE CAN DO IT JUST THINK IT ] | ||
| 4 | +// +---------------------------------------------------------------------- | ||
| 5 | +// | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved. | ||
| 6 | +// +---------------------------------------------------------------------- | ||
| 7 | +// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) | ||
| 8 | +// +---------------------------------------------------------------------- | ||
| 9 | +// | Author: yunwuxin <448901948@qq.com> | ||
| 10 | +// +---------------------------------------------------------------------- | ||
| 11 | + | ||
| 12 | +namespace think\queue\command; | ||
| 13 | + | ||
| 14 | +use think\Cache; | ||
| 15 | +use think\console\Command; | ||
| 16 | +use think\console\Input; | ||
| 17 | +use think\console\Output; | ||
| 18 | + | ||
| 19 | +class Restart extends Command | ||
| 20 | +{ | ||
| 21 | + public function configure() | ||
| 22 | + { | ||
| 23 | + $this->setName('queue:restart')->setDescription('Restart queue worker daemons after their current job'); | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | + public function execute(Input $input, Output $output) | ||
| 27 | + { | ||
| 28 | + Cache::set('think:queue:restart', time()); | ||
| 29 | + $output->writeln("<info>Broadcasting queue restart signal.</info>"); | ||
| 30 | + } | ||
| 31 | +} |
| 1 | +<?php | ||
| 2 | +// +---------------------------------------------------------------------- | ||
| 3 | +// | ThinkPHP [ WE CAN DO IT JUST THINK IT ] | ||
| 4 | +// +---------------------------------------------------------------------- | ||
| 5 | +// | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved. | ||
| 6 | +// +---------------------------------------------------------------------- | ||
| 7 | +// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) | ||
| 8 | +// +---------------------------------------------------------------------- | ||
| 9 | +// | Author: yunwuxin <448901948@qq.com> | ||
| 10 | +// +---------------------------------------------------------------------- | ||
| 11 | + | ||
| 12 | +namespace think\queue\command; | ||
| 13 | + | ||
| 14 | +use think\console\Command; | ||
| 15 | +use think\console\Input; | ||
| 16 | +use think\console\input\Argument; | ||
| 17 | +use think\console\input\Option; | ||
| 18 | +use think\console\Output; | ||
| 19 | +use think\Queue; | ||
| 20 | +use think\Url; | ||
| 21 | + | ||
| 22 | +class Subscribe extends Command | ||
| 23 | +{ | ||
| 24 | + public function configure() | ||
| 25 | + { | ||
| 26 | + $this->setName('queue:subscribe') | ||
| 27 | + ->setDescription('Subscribe a URL to an push queue') | ||
| 28 | + ->addArgument('name', Argument::REQUIRED, 'name') | ||
| 29 | + ->addArgument('url', Argument::REQUIRED, 'The URL to be subscribed.') | ||
| 30 | + ->addArgument('queue', Argument::OPTIONAL, 'The URL to be subscribed.') | ||
| 31 | + ->addOption('option', null, Option::VALUE_IS_ARRAY | Option::VALUE_OPTIONAL, 'the options'); | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + public function execute(Input $input, Output $output) | ||
| 35 | + { | ||
| 36 | + | ||
| 37 | + $url = $input->getArgument('url'); | ||
| 38 | + if (!preg_match('/^https?:\/\//', $url)) { | ||
| 39 | + $url = Url::build($url); | ||
| 40 | + } | ||
| 41 | + | ||
| 42 | + Queue::subscribe($input->getArgument('name'), $url, $input->getArgument('queue'), $input->getOption('option')); | ||
| 43 | + | ||
| 44 | + $output->write('<info>Queue subscriber added:</info> <comment>' . $input->getArgument('url') . '</comment>'); | ||
| 45 | + } | ||
| 46 | +} |
| 1 | +<?php | ||
| 2 | +// +---------------------------------------------------------------------- | ||
| 3 | +// | ThinkPHP [ WE CAN DO IT JUST THINK IT ] | ||
| 4 | +// +---------------------------------------------------------------------- | ||
| 5 | +// | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved. | ||
| 6 | +// +---------------------------------------------------------------------- | ||
| 7 | +// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) | ||
| 8 | +// +---------------------------------------------------------------------- | ||
| 9 | +// | Author: yunwuxin <448901948@qq.com> | ||
| 10 | +// +---------------------------------------------------------------------- | ||
| 11 | +namespace think\queue\command; | ||
| 12 | + | ||
| 13 | +use think\Config; | ||
| 14 | +use think\console\Command; | ||
| 15 | +use think\console\Input; | ||
| 16 | +use think\console\input\Option; | ||
| 17 | +use think\console\Output; | ||
| 18 | +use think\Hook; | ||
| 19 | +use think\queue\Job; | ||
| 20 | +use think\queue\Worker; | ||
| 21 | +use Exception; | ||
| 22 | +use Throwable; | ||
| 23 | +use think\Cache; | ||
| 24 | +use think\exception\Handle; | ||
| 25 | +use think\exception\ThrowableError; | ||
| 26 | + | ||
| 27 | +class Work extends Command | ||
| 28 | +{ | ||
| 29 | + | ||
| 30 | + /** | ||
| 31 | + * The queue worker instance. | ||
| 32 | + * @var \think\queue\Worker | ||
| 33 | + */ | ||
| 34 | + protected $worker; | ||
| 35 | + | ||
| 36 | + protected function initialize(Input $input, Output $output) | ||
| 37 | + { | ||
| 38 | + $this->worker = new Worker(); | ||
| 39 | + } | ||
| 40 | + | ||
| 41 | + protected function configure() | ||
| 42 | + { | ||
| 43 | + $this->setName('queue:work') | ||
| 44 | + ->addOption('queue', null, Option::VALUE_OPTIONAL, 'The queue to listen on') | ||
| 45 | + ->addOption('daemon', null, Option::VALUE_NONE, 'Run the worker in daemon mode') | ||
| 46 | + ->addOption('delay', null, Option::VALUE_OPTIONAL, 'Amount of time to delay failed jobs', 0) | ||
| 47 | + ->addOption('force', null, Option::VALUE_NONE, 'Force the worker to run even in maintenance mode') | ||
| 48 | + ->addOption('memory', null, Option::VALUE_OPTIONAL, 'The memory limit in megabytes', 128) | ||
| 49 | + ->addOption('sleep', null, Option::VALUE_OPTIONAL, 'Number of seconds to sleep when no job is available', 3) | ||
| 50 | + ->addOption('tries', null, Option::VALUE_OPTIONAL, 'Number of times to attempt a job before logging it failed', 0) | ||
| 51 | + ->setDescription('Process the next job on a queue'); | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + /** | ||
| 55 | + * Execute the console command. | ||
| 56 | + * @param Input $input | ||
| 57 | + * @param Output $output | ||
| 58 | + * @return int|null|void | ||
| 59 | + */ | ||
| 60 | + public function execute(Input $input, Output $output) | ||
| 61 | + { | ||
| 62 | + $queue = $input->getOption('queue'); | ||
| 63 | + | ||
| 64 | + $delay = $input->getOption('delay'); | ||
| 65 | + | ||
| 66 | + $memory = $input->getOption('memory'); | ||
| 67 | + | ||
| 68 | + if ($input->getOption('daemon')) { | ||
| 69 | + Hook::listen('worker_daemon_start', $queue); | ||
| 70 | + $this->daemon( | ||
| 71 | + $queue, $delay, $memory, | ||
| 72 | + $input->getOption('sleep'), $input->getOption('tries') | ||
| 73 | + ); | ||
| 74 | + } else { | ||
| 75 | + $response = $this->worker->pop($queue, $delay, $input->getOption('sleep'), $input->getOption('tries')); | ||
| 76 | + $this->output($response); | ||
| 77 | + } | ||
| 78 | + } | ||
| 79 | + | ||
| 80 | + protected function output($response) | ||
| 81 | + { | ||
| 82 | + if (!is_null($response['job'])) { | ||
| 83 | + /** @var Job $job */ | ||
| 84 | + $job = $response['job']; | ||
| 85 | + if ($response['failed']) { | ||
| 86 | + $this->output->writeln('<error>Failed:</error> ' . $job->getName()); | ||
| 87 | + } else { | ||
| 88 | + $this->output->writeln('<info>Processed:</info> ' . $job->getName()); | ||
| 89 | + } | ||
| 90 | + } | ||
| 91 | + } | ||
| 92 | + | ||
| 93 | + /** | ||
| 94 | + * 启动一个守护进程执行任务. | ||
| 95 | + * | ||
| 96 | + * @param string $queue | ||
| 97 | + * @param int $delay | ||
| 98 | + * @param int $memory | ||
| 99 | + * @param int $sleep | ||
| 100 | + * @param int $maxTries | ||
| 101 | + * @return array | ||
| 102 | + */ | ||
| 103 | + protected function daemon($queue = null, $delay = 0, $memory = 128, $sleep = 3, $maxTries = 0) | ||
| 104 | + { | ||
| 105 | + $lastRestart = $this->getTimestampOfLastQueueRestart(); | ||
| 106 | + | ||
| 107 | + while (true) { | ||
| 108 | + $this->runNextJobForDaemon( | ||
| 109 | + $queue, $delay, $sleep, $maxTries | ||
| 110 | + ); | ||
| 111 | + | ||
| 112 | + if ( $this->memoryExceeded($memory) ) { | ||
| 113 | + Hook::listen('worker_memory_exceeded', $queue); | ||
| 114 | + $this->stop(); | ||
| 115 | + } | ||
| 116 | + | ||
| 117 | + if ( $this->queueShouldRestart($lastRestart) ) { | ||
| 118 | + Hook::listen('worker_queue_restart', $queue); | ||
| 119 | + $this->stop(); | ||
| 120 | + } | ||
| 121 | + } | ||
| 122 | + } | ||
| 123 | + | ||
| 124 | + /** | ||
| 125 | + * 以守护进程的方式执行下个任务. | ||
| 126 | + * | ||
| 127 | + * @param string $queue | ||
| 128 | + * @param int $delay | ||
| 129 | + * @param int $sleep | ||
| 130 | + * @param int $maxTries | ||
| 131 | + * @return void | ||
| 132 | + */ | ||
| 133 | + protected function runNextJobForDaemon($queue, $delay, $sleep, $maxTries) | ||
| 134 | + { | ||
| 135 | + try { | ||
| 136 | + $response = $this->worker->pop($queue, $delay, $sleep, $maxTries); | ||
| 137 | + | ||
| 138 | + $this->output($response); | ||
| 139 | + } catch (Exception $e) { | ||
| 140 | + $this->getExceptionHandler()->report($e); | ||
| 141 | + } catch (Throwable $e) { | ||
| 142 | + $this->getExceptionHandler()->report(new ThrowableError($e)); | ||
| 143 | + } | ||
| 144 | + } | ||
| 145 | + | ||
| 146 | + /** | ||
| 147 | + * 获取上次重启守护进程的时间 | ||
| 148 | + * | ||
| 149 | + * @return int|null | ||
| 150 | + */ | ||
| 151 | + protected function getTimestampOfLastQueueRestart() | ||
| 152 | + { | ||
| 153 | + return Cache::get('think:queue:restart'); | ||
| 154 | + } | ||
| 155 | + | ||
| 156 | + /** | ||
| 157 | + * 检查是否要重启守护进程 | ||
| 158 | + * | ||
| 159 | + * @param int|null $lastRestart | ||
| 160 | + * @return bool | ||
| 161 | + */ | ||
| 162 | + protected function queueShouldRestart($lastRestart) | ||
| 163 | + { | ||
| 164 | + return $this->getTimestampOfLastQueueRestart() != $lastRestart; | ||
| 165 | + } | ||
| 166 | + | ||
| 167 | + /** | ||
| 168 | + * 检查内存是否超出 | ||
| 169 | + * @param int $memoryLimit | ||
| 170 | + * @return bool | ||
| 171 | + */ | ||
| 172 | + protected function memoryExceeded($memoryLimit) | ||
| 173 | + { | ||
| 174 | + return (memory_get_usage() / 1024 / 1024) >= $memoryLimit; | ||
| 175 | + } | ||
| 176 | + | ||
| 177 | + /** | ||
| 178 | + * 获取异常处理实例 | ||
| 179 | + * | ||
| 180 | + * @return \think\exception\Handle | ||
| 181 | + */ | ||
| 182 | + protected function getExceptionHandler() | ||
| 183 | + { | ||
| 184 | + static $handle; | ||
| 185 | + | ||
| 186 | + if (!$handle) { | ||
| 187 | + | ||
| 188 | + if ($class = Config::get('exception_handle')) { | ||
| 189 | + if (class_exists($class) && is_subclass_of($class, "\\think\\exception\\Handle")) { | ||
| 190 | + $handle = new $class; | ||
| 191 | + } | ||
| 192 | + } | ||
| 193 | + if (!$handle) { | ||
| 194 | + $handle = new Handle(); | ||
| 195 | + } | ||
| 196 | + } | ||
| 197 | + | ||
| 198 | + return $handle; | ||
| 199 | + } | ||
| 200 | + | ||
| 201 | + /** | ||
| 202 | + * 停止执行任务的守护进程. | ||
| 203 | + * @return void | ||
| 204 | + */ | ||
| 205 | + public function stop() | ||
| 206 | + { | ||
| 207 | + die; | ||
| 208 | + } | ||
| 209 | + | ||
| 210 | +} |
| 1 | +<?php | ||
| 2 | +// +---------------------------------------------------------------------- | ||
| 3 | +// | ThinkPHP [ WE CAN DO IT JUST THINK IT ] | ||
| 4 | +// +---------------------------------------------------------------------- | ||
| 5 | +// | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved. | ||
| 6 | +// +---------------------------------------------------------------------- | ||
| 7 | +// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) | ||
| 8 | +// +---------------------------------------------------------------------- | ||
| 9 | +// | Author: yunwuxin <448901948@qq.com> | ||
| 10 | +// +---------------------------------------------------------------------- | ||
| 11 | + | ||
| 12 | +namespace think\queue\connector; | ||
| 13 | + | ||
| 14 | +use think\Db; | ||
| 15 | +use think\queue\Connector; | ||
| 16 | +use think\queue\job\Database as DatabaseJob; | ||
| 17 | + | ||
| 18 | +class Database extends Connector | ||
| 19 | +{ | ||
| 20 | + protected $db; | ||
| 21 | + | ||
| 22 | + protected $options = [ | ||
| 23 | + 'expire' => 60, | ||
| 24 | + 'default' => 'default', | ||
| 25 | + 'table' => 'jobs', | ||
| 26 | + 'dsn' => [] | ||
| 27 | + ]; | ||
| 28 | + | ||
| 29 | + public function __construct($options) | ||
| 30 | + { | ||
| 31 | + if (!empty($options)) { | ||
| 32 | + $this->options = array_merge($this->options, $options); | ||
| 33 | + } | ||
| 34 | + | ||
| 35 | + $this->db = Db::connect($this->options['dsn']); | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + public function push($job, $data = '', $queue = null) | ||
| 39 | + { | ||
| 40 | + return $this->pushToDatabase(0, $queue, $this->createPayload($job, $data)); | ||
| 41 | + } | ||
| 42 | + | ||
| 43 | + public function later($delay, $job, $data = '', $queue = null) | ||
| 44 | + { | ||
| 45 | + return $this->pushToDatabase($delay, $queue, $this->createPayload($job, $data)); | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + public function pop($queue = null) | ||
| 49 | + { | ||
| 50 | + $queue = $this->getQueue($queue); | ||
| 51 | + | ||
| 52 | + if (!is_null($this->options['expire'])) { | ||
| 53 | + $this->releaseJobsThatHaveBeenReservedTooLong($queue); | ||
| 54 | + } | ||
| 55 | + | ||
| 56 | + if ($job = $this->getNextAvailableJob($queue)) { | ||
| 57 | + $this->markJobAsReserved($job->id); | ||
| 58 | + | ||
| 59 | + $this->db->commit(); | ||
| 60 | + | ||
| 61 | + return new DatabaseJob($this, $job, $queue); | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | + $this->db->commit(); | ||
| 65 | + } | ||
| 66 | + | ||
| 67 | + /** | ||
| 68 | + * 重新发布任务 | ||
| 69 | + * @param string $queue | ||
| 70 | + * @param \StdClass $job | ||
| 71 | + * @param int $delay | ||
| 72 | + * @return mixed | ||
| 73 | + */ | ||
| 74 | + public function release($queue, $job, $delay) | ||
| 75 | + { | ||
| 76 | + return $this->pushToDatabase($delay, $queue, $job->payload, $job->attempts); | ||
| 77 | + } | ||
| 78 | + | ||
| 79 | + /** | ||
| 80 | + * Push a raw payload to the database with a given delay. | ||
| 81 | + * | ||
| 82 | + * @param \DateTime|int $delay | ||
| 83 | + * @param string|null $queue | ||
| 84 | + * @param string $payload | ||
| 85 | + * @param int $attempts | ||
| 86 | + * @return mixed | ||
| 87 | + */ | ||
| 88 | + protected function pushToDatabase($delay, $queue, $payload, $attempts = 0) | ||
| 89 | + { | ||
| 90 | + return $this->db->name($this->options['table'])->insert([ | ||
| 91 | + 'queue' => $this->getQueue($queue), | ||
| 92 | + 'payload' => $payload, | ||
| 93 | + 'attempts' => $attempts, | ||
| 94 | + 'reserved' => 0, | ||
| 95 | + 'reserved_at' => null, | ||
| 96 | + 'available_at' => time() + $delay, | ||
| 97 | + 'created_at' => time() | ||
| 98 | + ]); | ||
| 99 | + } | ||
| 100 | + | ||
| 101 | + /** | ||
| 102 | + * 获取下个有效任务 | ||
| 103 | + * | ||
| 104 | + * @param string|null $queue | ||
| 105 | + * @return \StdClass|null | ||
| 106 | + */ | ||
| 107 | + protected function getNextAvailableJob($queue) | ||
| 108 | + { | ||
| 109 | + $this->db->startTrans(); | ||
| 110 | + | ||
| 111 | + $job = $this->db->name($this->options['table']) | ||
| 112 | + ->lock(true) | ||
| 113 | + ->where('queue', $this->getQueue($queue)) | ||
| 114 | + ->where('reserved', 0) | ||
| 115 | + ->where('available_at', '<=', time()) | ||
| 116 | + ->order('id', 'asc') | ||
| 117 | + ->find(); | ||
| 118 | + | ||
| 119 | + return $job ? (object) $job : null; | ||
| 120 | + } | ||
| 121 | + | ||
| 122 | + /** | ||
| 123 | + * 标记任务正在执行. | ||
| 124 | + * | ||
| 125 | + * @param string $id | ||
| 126 | + * @return void | ||
| 127 | + */ | ||
| 128 | + protected function markJobAsReserved($id) | ||
| 129 | + { | ||
| 130 | + $this->db->name($this->options['table'])->where('id', $id)->update([ | ||
| 131 | + 'reserved' => 1, | ||
| 132 | + 'reserved_at' => time() | ||
| 133 | + ]); | ||
| 134 | + } | ||
| 135 | + | ||
| 136 | + /** | ||
| 137 | + * 重新发布超时的任务 | ||
| 138 | + * | ||
| 139 | + * @param string $queue | ||
| 140 | + * @return void | ||
| 141 | + */ | ||
| 142 | + protected function releaseJobsThatHaveBeenReservedTooLong($queue) | ||
| 143 | + { | ||
| 144 | + $expired = time() - $this->options['expire']; | ||
| 145 | + | ||
| 146 | + $this->db->name($this->options['table']) | ||
| 147 | + ->where('queue', $this->getQueue($queue)) | ||
| 148 | + ->where('reserved', 1) | ||
| 149 | + ->where('reserved_at', '<=', $expired) | ||
| 150 | + ->update([ | ||
| 151 | + 'reserved' => 0, | ||
| 152 | + 'reserved_at' => null, | ||
| 153 | + 'attempts' => ['exp', 'attempts + 1'] | ||
| 154 | + ]); | ||
| 155 | + } | ||
| 156 | + | ||
| 157 | + /** | ||
| 158 | + * 删除任务 | ||
| 159 | + * @param string $id | ||
| 160 | + * @return void | ||
| 161 | + */ | ||
| 162 | + public function deleteReserved($id) | ||
| 163 | + { | ||
| 164 | + $this->db->name($this->options['table'])->delete($id); | ||
| 165 | + } | ||
| 166 | + | ||
| 167 | + protected function getQueue($queue) | ||
| 168 | + { | ||
| 169 | + return $queue ?: $this->options['default']; | ||
| 170 | + } | ||
| 171 | +} |
| 1 | +<?php | ||
| 2 | +// +---------------------------------------------------------------------- | ||
| 3 | +// | ThinkPHP [ WE CAN DO IT JUST THINK IT ] | ||
| 4 | +// +---------------------------------------------------------------------- | ||
| 5 | +// | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved. | ||
| 6 | +// +---------------------------------------------------------------------- | ||
| 7 | +// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) | ||
| 8 | +// +---------------------------------------------------------------------- | ||
| 9 | +// | Author: yunwuxin <448901948@qq.com> | ||
| 10 | +// +---------------------------------------------------------------------- | ||
| 11 | + | ||
| 12 | +namespace think\queue\connector; | ||
| 13 | + | ||
| 14 | +use Exception; | ||
| 15 | +use think\helper\Str; | ||
| 16 | +use think\queue\Connector; | ||
| 17 | +use think\queue\job\Redis as RedisJob; | ||
| 18 | + | ||
| 19 | +class Redis extends Connector | ||
| 20 | +{ | ||
| 21 | + /** @var \Redis */ | ||
| 22 | + protected $redis; | ||
| 23 | + | ||
| 24 | + protected $options = [ | ||
| 25 | + 'expire' => 60, | ||
| 26 | + 'default' => 'default', | ||
| 27 | + 'host' => '127.0.0.1', | ||
| 28 | + 'port' => 6379, | ||
| 29 | + 'password' => '', | ||
| 30 | + 'select' => 0, | ||
| 31 | + 'timeout' => 0, | ||
| 32 | + 'persistent' => false | ||
| 33 | + ]; | ||
| 34 | + | ||
| 35 | + public function __construct($options) | ||
| 36 | + { | ||
| 37 | + if (!extension_loaded('redis')) { | ||
| 38 | + throw new Exception('redis扩展未安装'); | ||
| 39 | + } | ||
| 40 | + if (!empty($options)) { | ||
| 41 | + $this->options = array_merge($this->options, $options); | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + $func = $this->options['persistent'] ? 'pconnect' : 'connect'; | ||
| 45 | + $this->redis = new \Redis; | ||
| 46 | + $this->redis->$func($this->options['host'], $this->options['port'], $this->options['timeout']); | ||
| 47 | + | ||
| 48 | + if ('' != $this->options['password']) { | ||
| 49 | + $this->redis->auth($this->options['password']); | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + if (0 != $this->options['select']) { | ||
| 53 | + $this->redis->select($this->options['select']); | ||
| 54 | + } | ||
| 55 | + } | ||
| 56 | + | ||
| 57 | + public function push($job, $data = '', $queue = null) | ||
| 58 | + { | ||
| 59 | + return $this->pushRaw($this->createPayload($job, $data), $queue); | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + public function later($delay, $job, $data = '', $queue = null) | ||
| 63 | + { | ||
| 64 | + $payload = $this->createPayload($job, $data); | ||
| 65 | + | ||
| 66 | + $this->redis->zAdd($this->getQueue($queue) . ':delayed', time() + $delay, $payload); | ||
| 67 | + } | ||
| 68 | + | ||
| 69 | + public function pop($queue = null) | ||
| 70 | + { | ||
| 71 | + $original = $queue ?: $this->options['default']; | ||
| 72 | + | ||
| 73 | + $queue = $this->getQueue($queue); | ||
| 74 | + | ||
| 75 | + $this->migrateExpiredJobs($queue . ':delayed', $queue, false); | ||
| 76 | + | ||
| 77 | + if (!is_null($this->options['expire'])) { | ||
| 78 | + $this->migrateExpiredJobs($queue . ':reserved', $queue); | ||
| 79 | + } | ||
| 80 | + | ||
| 81 | + $job = $this->redis->lPop($queue); | ||
| 82 | + | ||
| 83 | + if ($job !== false) { | ||
| 84 | + $this->redis->zAdd($queue . ':reserved', time() + $this->options['expire'], $job); | ||
| 85 | + | ||
| 86 | + return new RedisJob($this, $job, $original); | ||
| 87 | + } | ||
| 88 | + } | ||
| 89 | + | ||
| 90 | + /** | ||
| 91 | + * 重新发布任务 | ||
| 92 | + * | ||
| 93 | + * @param string $queue | ||
| 94 | + * @param string $payload | ||
| 95 | + * @param int $delay | ||
| 96 | + * @param int $attempts | ||
| 97 | + * @return void | ||
| 98 | + */ | ||
| 99 | + public function release($queue, $payload, $delay, $attempts) | ||
| 100 | + { | ||
| 101 | + $payload = $this->setMeta($payload, 'attempts', $attempts); | ||
| 102 | + | ||
| 103 | + $this->redis->zAdd($this->getQueue($queue) . ':delayed', time() + $delay, $payload); | ||
| 104 | + } | ||
| 105 | + | ||
| 106 | + public function pushRaw($payload, $queue = null) | ||
| 107 | + { | ||
| 108 | + $this->redis->rPush($this->getQueue($queue), $payload); | ||
| 109 | + | ||
| 110 | + return json_decode($payload, true)['id']; | ||
| 111 | + } | ||
| 112 | + | ||
| 113 | + protected function createPayload($job, $data = '', $queue = null) | ||
| 114 | + { | ||
| 115 | + $payload = $this->setMeta( | ||
| 116 | + parent::createPayload($job, $data), 'id', $this->getRandomId() | ||
| 117 | + ); | ||
| 118 | + | ||
| 119 | + return $this->setMeta($payload, 'attempts', 1); | ||
| 120 | + } | ||
| 121 | + | ||
| 122 | + /** | ||
| 123 | + * 删除任务 | ||
| 124 | + * | ||
| 125 | + * @param string $queue | ||
| 126 | + * @param string $job | ||
| 127 | + * @return void | ||
| 128 | + */ | ||
| 129 | + public function deleteReserved($queue, $job) | ||
| 130 | + { | ||
| 131 | + $this->redis->zRem($this->getQueue($queue) . ':reserved', $job); | ||
| 132 | + } | ||
| 133 | + | ||
| 134 | + /** | ||
| 135 | + * 移动延迟任务 | ||
| 136 | + * | ||
| 137 | + * @param string $from | ||
| 138 | + * @param string $to | ||
| 139 | + * @param bool $attempt | ||
| 140 | + */ | ||
| 141 | + public function migrateExpiredJobs($from, $to, $attempt = true) | ||
| 142 | + { | ||
| 143 | + $this->redis->watch($from); | ||
| 144 | + | ||
| 145 | + $jobs = $this->getExpiredJobs( | ||
| 146 | + $from, $time = time() | ||
| 147 | + ); | ||
| 148 | + if (count($jobs) > 0) { | ||
| 149 | + $this->transaction(function () use ($from, $to, $time, $jobs, $attempt) { | ||
| 150 | + $this->removeExpiredJobs($from, $time); | ||
| 151 | + $this->pushExpiredJobsOntoNewQueue($to, $jobs, $attempt); | ||
| 152 | + }); | ||
| 153 | + } | ||
| 154 | + $this->redis->unwatch(); | ||
| 155 | + } | ||
| 156 | + | ||
| 157 | + /** | ||
| 158 | + * redis事务 | ||
| 159 | + * @param \Closure $closure | ||
| 160 | + */ | ||
| 161 | + protected function transaction(\Closure $closure) | ||
| 162 | + { | ||
| 163 | + $this->redis->multi(); | ||
| 164 | + try { | ||
| 165 | + call_user_func($closure); | ||
| 166 | + if (!$this->redis->exec()) { | ||
| 167 | + $this->redis->discard(); | ||
| 168 | + } | ||
| 169 | + } catch (Exception $e) { | ||
| 170 | + $this->redis->discard(); | ||
| 171 | + } | ||
| 172 | + } | ||
| 173 | + | ||
| 174 | + /** | ||
| 175 | + * 获取所有到期任务 | ||
| 176 | + * | ||
| 177 | + * @param string $from | ||
| 178 | + * @param int $time | ||
| 179 | + * @return array | ||
| 180 | + */ | ||
| 181 | + protected function getExpiredJobs($from, $time) | ||
| 182 | + { | ||
| 183 | + return $this->redis->zRangeByScore($from, '-inf', $time); | ||
| 184 | + } | ||
| 185 | + | ||
| 186 | + /** | ||
| 187 | + * 删除过期任务 | ||
| 188 | + * | ||
| 189 | + * @param string $from | ||
| 190 | + * @param int $time | ||
| 191 | + * @return void | ||
| 192 | + */ | ||
| 193 | + protected function removeExpiredJobs($from, $time) | ||
| 194 | + { | ||
| 195 | + $this->redis->zRemRangeByScore($from, '-inf', $time); | ||
| 196 | + } | ||
| 197 | + | ||
| 198 | + /** | ||
| 199 | + * 重新发布到期任务 | ||
| 200 | + * | ||
| 201 | + * @param string $to | ||
| 202 | + * @param array $jobs | ||
| 203 | + * @param boolean $attempt | ||
| 204 | + */ | ||
| 205 | + protected function pushExpiredJobsOntoNewQueue($to, $jobs, $attempt = true) | ||
| 206 | + { | ||
| 207 | + if ($attempt) { | ||
| 208 | + foreach ($jobs as &$job) { | ||
| 209 | + $attempts = json_decode($job, true)['attempts']; | ||
| 210 | + $job = $this->setMeta($job, 'attempts', $attempts + 1); | ||
| 211 | + } | ||
| 212 | + } | ||
| 213 | + call_user_func_array([$this->redis, 'rPush'], array_merge([$to], $jobs)); | ||
| 214 | + } | ||
| 215 | + | ||
| 216 | + /** | ||
| 217 | + * 随机id | ||
| 218 | + * | ||
| 219 | + * @return string | ||
| 220 | + */ | ||
| 221 | + protected function getRandomId() | ||
| 222 | + { | ||
| 223 | + return Str::random(32); | ||
| 224 | + } | ||
| 225 | + | ||
| 226 | + /** | ||
| 227 | + * 获取队列名 | ||
| 228 | + * | ||
| 229 | + * @param string|null $queue | ||
| 230 | + * @return string | ||
| 231 | + */ | ||
| 232 | + protected function getQueue($queue) | ||
| 233 | + { | ||
| 234 | + return 'queues:' . ($queue ?: $this->options['default']); | ||
| 235 | + } | ||
| 236 | +} |
| 1 | +<?php | ||
| 2 | +// +---------------------------------------------------------------------- | ||
| 3 | +// | ThinkPHP [ WE CAN DO IT JUST THINK IT ] | ||
| 4 | +// +---------------------------------------------------------------------- | ||
| 5 | +// | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved. | ||
| 6 | +// +---------------------------------------------------------------------- | ||
| 7 | +// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) | ||
| 8 | +// +---------------------------------------------------------------------- | ||
| 9 | +// | Author: yunwuxin <448901948@qq.com> | ||
| 10 | +// +---------------------------------------------------------------------- | ||
| 11 | + | ||
| 12 | +namespace think\queue\connector; | ||
| 13 | + | ||
| 14 | +use Exception; | ||
| 15 | +use think\queue\Connector; | ||
| 16 | +use think\queue\job\Sync as SyncJob; | ||
| 17 | +use Throwable; | ||
| 18 | + | ||
| 19 | +class Sync extends Connector | ||
| 20 | +{ | ||
| 21 | + | ||
| 22 | + public function push($job, $data = '', $queue = null) | ||
| 23 | + { | ||
| 24 | + $queueJob = $this->resolveJob($this->createPayload($job, $data, $queue)); | ||
| 25 | + | ||
| 26 | + try { | ||
| 27 | + set_time_limit(0); | ||
| 28 | + $queueJob->fire(); | ||
| 29 | + } catch (Exception $e) { | ||
| 30 | + $queueJob->failed(); | ||
| 31 | + | ||
| 32 | + throw $e; | ||
| 33 | + } catch (Throwable $e) { | ||
| 34 | + $queueJob->failed(); | ||
| 35 | + | ||
| 36 | + throw $e; | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + return 0; | ||
| 40 | + } | ||
| 41 | + | ||
| 42 | + public function later($delay, $job, $data = '', $queue = null) | ||
| 43 | + { | ||
| 44 | + return $this->push($job, $data, $queue); | ||
| 45 | + } | ||
| 46 | + | ||
| 47 | + public function pop($queue = null) | ||
| 48 | + { | ||
| 49 | + | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + protected function resolveJob($payload) | ||
| 53 | + { | ||
| 54 | + return new SyncJob($payload); | ||
| 55 | + } | ||
| 56 | + | ||
| 57 | +} |
| 1 | +<?php | ||
| 2 | +// +---------------------------------------------------------------------- | ||
| 3 | +// | ThinkPHP [ WE CAN DO IT JUST THINK IT ] | ||
| 4 | +// +---------------------------------------------------------------------- | ||
| 5 | +// | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved. | ||
| 6 | +// +---------------------------------------------------------------------- | ||
| 7 | +// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) | ||
| 8 | +// +---------------------------------------------------------------------- | ||
| 9 | +// | Author: yunwuxin <448901948@qq.com> | ||
| 10 | +// +---------------------------------------------------------------------- | ||
| 11 | + | ||
| 12 | +namespace think\queue\connector; | ||
| 13 | + | ||
| 14 | +use think\exception\HttpException; | ||
| 15 | +use think\queue\Connector; | ||
| 16 | +use think\Request; | ||
| 17 | +use think\queue\job\Topthink as TopthinkJob; | ||
| 18 | +use think\Response; | ||
| 19 | + | ||
| 20 | +class Topthink extends Connector | ||
| 21 | +{ | ||
| 22 | + protected $options = [ | ||
| 23 | + 'token' => '', | ||
| 24 | + 'project_id' => '', | ||
| 25 | + 'protocol' => 'https', | ||
| 26 | + 'host' => 'qns.topthink.com', | ||
| 27 | + 'port' => 443, | ||
| 28 | + 'api_version' => 1, | ||
| 29 | + 'max_retries' => 3, | ||
| 30 | + 'default' => 'default' | ||
| 31 | + ]; | ||
| 32 | + | ||
| 33 | + /** @var Request */ | ||
| 34 | + protected $request; | ||
| 35 | + | ||
| 36 | + protected $url; | ||
| 37 | + | ||
| 38 | + protected $curl = null; | ||
| 39 | + | ||
| 40 | + protected $last_status; | ||
| 41 | + | ||
| 42 | + protected $headers = []; | ||
| 43 | + | ||
| 44 | + public function __construct($options) | ||
| 45 | + { | ||
| 46 | + if (!empty($options)) { | ||
| 47 | + $this->options = array_merge($this->options, $options); | ||
| 48 | + } | ||
| 49 | + | ||
| 50 | + $this->url = "{$this->options['protocol']}://{$this->options['host']}:{$this->options['port']}/v{$this->options['api_version']}/"; | ||
| 51 | + | ||
| 52 | + $this->headers['Authorization'] = "Bearer {$this->options['token']}"; | ||
| 53 | + | ||
| 54 | + $this->request = Request::instance(); | ||
| 55 | + } | ||
| 56 | + | ||
| 57 | + public function push($job, $data = '', $queue = null) | ||
| 58 | + { | ||
| 59 | + return $this->pushRaw(0, $queue, $this->createPayload($job, $data)); | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + public function later($delay, $job, $data = '', $queue = null) | ||
| 63 | + { | ||
| 64 | + return $this->pushRaw($delay, $queue, $this->createPayload($job, $data)); | ||
| 65 | + } | ||
| 66 | + | ||
| 67 | + public function release($queue, $job, $delay) | ||
| 68 | + { | ||
| 69 | + return $this->pushRaw($delay, $queue, $job->payload, $job->attempts); | ||
| 70 | + } | ||
| 71 | + | ||
| 72 | + public function marshal() | ||
| 73 | + { | ||
| 74 | + $job = new TopthinkJob($this, $this->marshalPushedJob(), $this->request->header('topthink-message-queue')); | ||
| 75 | + if ($this->request->header('topthink-message-status') == 'success') { | ||
| 76 | + $job->fire(); | ||
| 77 | + } else { | ||
| 78 | + $job->failed(); | ||
| 79 | + } | ||
| 80 | + return new Response('OK'); | ||
| 81 | + } | ||
| 82 | + | ||
| 83 | + public function pushRaw($delay, $queue, $payload, $attempts = 0) | ||
| 84 | + { | ||
| 85 | + $queue_name = $this->getQueue($queue); | ||
| 86 | + $queue = rawurlencode($queue_name); | ||
| 87 | + $url = "project/{$this->options['project_id']}/queue/{$queue}/message"; | ||
| 88 | + $message = [ | ||
| 89 | + 'payload' => $payload, | ||
| 90 | + 'attempts' => $attempts, | ||
| 91 | + 'delay' => $delay | ||
| 92 | + ]; | ||
| 93 | + | ||
| 94 | + return $this->apiCall('POST', $url, $message)->id; | ||
| 95 | + } | ||
| 96 | + | ||
| 97 | + public function deleteMessage($queue, $id) | ||
| 98 | + { | ||
| 99 | + $queue = rawurlencode($queue); | ||
| 100 | + $url = "project/{$this->options['project_id']}/queue/{$queue}/message/{$id}"; | ||
| 101 | + return $this->apiCall('DELETE', $url); | ||
| 102 | + } | ||
| 103 | + | ||
| 104 | + protected function apiCall($type, $url, $params = []) | ||
| 105 | + { | ||
| 106 | + $url = "{$this->url}$url"; | ||
| 107 | + | ||
| 108 | + if ($this->curl == null) { | ||
| 109 | + $this->curl = curl_init(); | ||
| 110 | + } | ||
| 111 | + | ||
| 112 | + switch ($type = strtoupper($type)) { | ||
| 113 | + case 'DELETE': | ||
| 114 | + curl_setopt($this->curl, CURLOPT_URL, $url); | ||
| 115 | + curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, $type); | ||
| 116 | + curl_setopt($this->curl, CURLOPT_POSTFIELDS, json_encode($params)); | ||
| 117 | + break; | ||
| 118 | + case 'PUT': | ||
| 119 | + curl_setopt($this->curl, CURLOPT_URL, $url); | ||
| 120 | + curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, $type); | ||
| 121 | + curl_setopt($this->curl, CURLOPT_POSTFIELDS, json_encode($params)); | ||
| 122 | + break; | ||
| 123 | + case 'POST': | ||
| 124 | + curl_setopt($this->curl, CURLOPT_URL, $url); | ||
| 125 | + curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, $type); | ||
| 126 | + curl_setopt($this->curl, CURLOPT_POST, true); | ||
| 127 | + curl_setopt($this->curl, CURLOPT_POSTFIELDS, $params); | ||
| 128 | + break; | ||
| 129 | + case 'GET': | ||
| 130 | + curl_setopt($this->curl, CURLOPT_POSTFIELDS, null); | ||
| 131 | + curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, $type); | ||
| 132 | + curl_setopt($this->curl, CURLOPT_HTTPGET, true); | ||
| 133 | + $url .= '?' . http_build_query($params); | ||
| 134 | + curl_setopt($this->curl, CURLOPT_URL, $url); | ||
| 135 | + break; | ||
| 136 | + } | ||
| 137 | + | ||
| 138 | + curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, false); | ||
| 139 | + curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true); | ||
| 140 | + | ||
| 141 | + $headers = []; | ||
| 142 | + foreach ($this->headers as $k => $v) { | ||
| 143 | + if ($k == 'Connection') { | ||
| 144 | + $v = 'Close'; | ||
| 145 | + } | ||
| 146 | + $headers[] = "$k: $v"; | ||
| 147 | + } | ||
| 148 | + | ||
| 149 | + curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers); | ||
| 150 | + curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, 10); | ||
| 151 | + | ||
| 152 | + return $this->callWithRetries(); | ||
| 153 | + } | ||
| 154 | + | ||
| 155 | + protected function callWithRetries() | ||
| 156 | + { | ||
| 157 | + for ($retry = 0; $retry < $this->options['max_retries']; $retry++) { | ||
| 158 | + $out = curl_exec($this->curl); | ||
| 159 | + if ($out === false) { | ||
| 160 | + $this->reportHttpError(0, curl_error($this->curl)); | ||
| 161 | + } | ||
| 162 | + $this->last_status = curl_getinfo($this->curl, CURLINFO_HTTP_CODE); | ||
| 163 | + | ||
| 164 | + if ($this->last_status >= 200 && $this->last_status < 300) { | ||
| 165 | + return self::jsonDecode($out); | ||
| 166 | + } elseif ($this->last_status >= 500) { | ||
| 167 | + self::waitRandomInterval($retry); | ||
| 168 | + } else { | ||
| 169 | + $this->reportHttpError($this->last_status, $out); | ||
| 170 | + } | ||
| 171 | + } | ||
| 172 | + $this->reportHttpError($this->last_status, "Service unavailable"); | ||
| 173 | + return; | ||
| 174 | + } | ||
| 175 | + | ||
| 176 | + protected static function jsonDecode($response) | ||
| 177 | + { | ||
| 178 | + $data = json_decode($response); | ||
| 179 | + | ||
| 180 | + $json_error = json_last_error(); | ||
| 181 | + if ($json_error != JSON_ERROR_NONE) { | ||
| 182 | + throw new \RuntimeException($json_error); | ||
| 183 | + } | ||
| 184 | + | ||
| 185 | + return $data; | ||
| 186 | + } | ||
| 187 | + | ||
| 188 | + protected static function waitRandomInterval($retry) | ||
| 189 | + { | ||
| 190 | + $max_delay = pow(4, $retry) * 100 * 1000; | ||
| 191 | + usleep(rand(0, $max_delay)); | ||
| 192 | + } | ||
| 193 | + | ||
| 194 | + protected function reportHttpError($status, $text) | ||
| 195 | + { | ||
| 196 | + throw new HttpException($status, "http error: {$status} | {$text}"); | ||
| 197 | + } | ||
| 198 | + | ||
| 199 | + /** | ||
| 200 | + * Marshal out the pushed job and payload. | ||
| 201 | + * | ||
| 202 | + * @return object | ||
| 203 | + */ | ||
| 204 | + protected function marshalPushedJob() | ||
| 205 | + { | ||
| 206 | + return (object) [ | ||
| 207 | + 'id' => $this->request->header('topthink-message-id'), | ||
| 208 | + 'payload' => $this->request->getContent(), | ||
| 209 | + 'attempts' => $this->request->header('topthink-message-attempts') | ||
| 210 | + ]; | ||
| 211 | + } | ||
| 212 | + | ||
| 213 | + public function __destruct() | ||
| 214 | + { | ||
| 215 | + if ($this->curl != null) { | ||
| 216 | + curl_close($this->curl); | ||
| 217 | + $this->curl = null; | ||
| 218 | + } | ||
| 219 | + } | ||
| 220 | + | ||
| 221 | + public function pop($queue = null) | ||
| 222 | + { | ||
| 223 | + throw new \RuntimeException('pop queues not support for this type'); | ||
| 224 | + } | ||
| 225 | +} |
| 1 | +<?php | ||
| 2 | +// +---------------------------------------------------------------------- | ||
| 3 | +// | ThinkPHP [ WE CAN DO IT JUST THINK IT ] | ||
| 4 | +// +---------------------------------------------------------------------- | ||
| 5 | +// | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved. | ||
| 6 | +// +---------------------------------------------------------------------- | ||
| 7 | +// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) | ||
| 8 | +// +---------------------------------------------------------------------- | ||
| 9 | +// | Author: yunwuxin <448901948@qq.com> | ||
| 10 | +// +---------------------------------------------------------------------- | ||
| 11 | +namespace think\queue\job; | ||
| 12 | + | ||
| 13 | +use think\queue\Job; | ||
| 14 | +use think\queue\connector\Database as DatabaseQueue; | ||
| 15 | + | ||
| 16 | +class Database extends Job | ||
| 17 | +{ | ||
| 18 | + /** | ||
| 19 | + * The database queue instance. | ||
| 20 | + * @var DatabaseQueue | ||
| 21 | + */ | ||
| 22 | + protected $database; | ||
| 23 | + | ||
| 24 | + /** | ||
| 25 | + * The database job payload. | ||
| 26 | + * @var Object | ||
| 27 | + */ | ||
| 28 | + protected $job; | ||
| 29 | + | ||
| 30 | + public function __construct(DatabaseQueue $database, $job, $queue) | ||
| 31 | + { | ||
| 32 | + $this->job = $job; | ||
| 33 | + $this->queue = $queue; | ||
| 34 | + $this->database = $database; | ||
| 35 | + $this->job->attempts = $this->job->attempts + 1; | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + /** | ||
| 39 | + * 执行任务 | ||
| 40 | + * @return void | ||
| 41 | + */ | ||
| 42 | + public function fire() | ||
| 43 | + { | ||
| 44 | + $this->resolveAndFire(json_decode($this->job->payload, true)); | ||
| 45 | + } | ||
| 46 | + | ||
| 47 | + /** | ||
| 48 | + * 删除任务 | ||
| 49 | + * @return void | ||
| 50 | + */ | ||
| 51 | + public function delete() | ||
| 52 | + { | ||
| 53 | + parent::delete(); | ||
| 54 | + $this->database->deleteReserved($this->job->id); | ||
| 55 | + } | ||
| 56 | + | ||
| 57 | + /** | ||
| 58 | + * 重新发布任务 | ||
| 59 | + * @param int $delay | ||
| 60 | + * @return void | ||
| 61 | + */ | ||
| 62 | + public function release($delay = 0) | ||
| 63 | + { | ||
| 64 | + parent::release($delay); | ||
| 65 | + | ||
| 66 | + $this->delete(); | ||
| 67 | + | ||
| 68 | + $this->database->release($this->queue, $this->job, $delay); | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + /** | ||
| 72 | + * 获取当前任务尝试次数 | ||
| 73 | + * @return int | ||
| 74 | + */ | ||
| 75 | + public function attempts() | ||
| 76 | + { | ||
| 77 | + return (int) $this->job->attempts; | ||
| 78 | + } | ||
| 79 | + | ||
| 80 | + /** | ||
| 81 | + * Get the raw body string for the job. | ||
| 82 | + * @return string | ||
| 83 | + */ | ||
| 84 | + public function getRawBody() | ||
| 85 | + { | ||
| 86 | + return $this->job->payload; | ||
| 87 | + } | ||
| 88 | +} |
| 1 | +<?php | ||
| 2 | +// +---------------------------------------------------------------------- | ||
| 3 | +// | ThinkPHP [ WE CAN DO IT JUST THINK IT ] | ||
| 4 | +// +---------------------------------------------------------------------- | ||
| 5 | +// | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved. | ||
| 6 | +// +---------------------------------------------------------------------- | ||
| 7 | +// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) | ||
| 8 | +// +---------------------------------------------------------------------- | ||
| 9 | +// | Author: yunwuxin <448901948@qq.com> | ||
| 10 | +// +---------------------------------------------------------------------- | ||
| 11 | + | ||
| 12 | +namespace think\queue\job; | ||
| 13 | + | ||
| 14 | +use think\queue\Job; | ||
| 15 | +use think\queue\connector\Redis as RedisQueue; | ||
| 16 | + | ||
| 17 | +class Redis extends Job | ||
| 18 | +{ | ||
| 19 | + | ||
| 20 | + /** | ||
| 21 | + * The redis queue instance. | ||
| 22 | + * @var RedisQueue | ||
| 23 | + */ | ||
| 24 | + protected $redis; | ||
| 25 | + | ||
| 26 | + /** | ||
| 27 | + * The database job payload. | ||
| 28 | + * @var Object | ||
| 29 | + */ | ||
| 30 | + protected $job; | ||
| 31 | + | ||
| 32 | + public function __construct(RedisQueue $redis, $job, $queue) | ||
| 33 | + { | ||
| 34 | + $this->job = $job; | ||
| 35 | + $this->queue = $queue; | ||
| 36 | + $this->redis = $redis; | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + /** | ||
| 40 | + * Fire the job. | ||
| 41 | + * @return void | ||
| 42 | + */ | ||
| 43 | + public function fire() | ||
| 44 | + { | ||
| 45 | + $this->resolveAndFire(json_decode($this->getRawBody(), true)); | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + /** | ||
| 49 | + * Get the number of times the job has been attempted. | ||
| 50 | + * @return int | ||
| 51 | + */ | ||
| 52 | + public function attempts() | ||
| 53 | + { | ||
| 54 | + return json_decode($this->job, true)['attempts']; | ||
| 55 | + } | ||
| 56 | + | ||
| 57 | + /** | ||
| 58 | + * Get the raw body string for the job. | ||
| 59 | + * @return string | ||
| 60 | + */ | ||
| 61 | + public function getRawBody() | ||
| 62 | + { | ||
| 63 | + return $this->job; | ||
| 64 | + } | ||
| 65 | + | ||
| 66 | + /** | ||
| 67 | + * 删除任务 | ||
| 68 | + * | ||
| 69 | + * @return void | ||
| 70 | + */ | ||
| 71 | + public function delete() | ||
| 72 | + { | ||
| 73 | + parent::delete(); | ||
| 74 | + | ||
| 75 | + $this->redis->deleteReserved($this->queue, $this->job); | ||
| 76 | + } | ||
| 77 | + | ||
| 78 | + /** | ||
| 79 | + * 重新发布任务 | ||
| 80 | + * | ||
| 81 | + * @param int $delay | ||
| 82 | + * @return void | ||
| 83 | + */ | ||
| 84 | + public function release($delay = 0) | ||
| 85 | + { | ||
| 86 | + parent::release($delay); | ||
| 87 | + | ||
| 88 | + $this->delete(); | ||
| 89 | + | ||
| 90 | + $this->redis->release($this->queue, $this->job, $delay, $this->attempts() + 1); | ||
| 91 | + } | ||
| 92 | +} |
| 1 | +<?php | ||
| 2 | +// +---------------------------------------------------------------------- | ||
| 3 | +// | ThinkPHP [ WE CAN DO IT JUST THINK IT ] | ||
| 4 | +// +---------------------------------------------------------------------- | ||
| 5 | +// | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved. | ||
| 6 | +// +---------------------------------------------------------------------- | ||
| 7 | +// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) | ||
| 8 | +// +---------------------------------------------------------------------- | ||
| 9 | +// | Author: yunwuxin <448901948@qq.com> | ||
| 10 | +// +---------------------------------------------------------------------- | ||
| 11 | + | ||
| 12 | +namespace think\queue\job; | ||
| 13 | + | ||
| 14 | +use think\queue\Job; | ||
| 15 | + | ||
| 16 | +class Sync extends Job | ||
| 17 | +{ | ||
| 18 | + /** | ||
| 19 | + * The queue message data. | ||
| 20 | + * | ||
| 21 | + * @var string | ||
| 22 | + */ | ||
| 23 | + protected $payload; | ||
| 24 | + | ||
| 25 | + public function __construct($payload) | ||
| 26 | + { | ||
| 27 | + $this->payload = $payload; | ||
| 28 | + } | ||
| 29 | + | ||
| 30 | + /** | ||
| 31 | + * Fire the job. | ||
| 32 | + * @return void | ||
| 33 | + */ | ||
| 34 | + public function fire() | ||
| 35 | + { | ||
| 36 | + $this->resolveAndFire(json_decode($this->payload, true)); | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + /** | ||
| 40 | + * Get the number of times the job has been attempted. | ||
| 41 | + * @return int | ||
| 42 | + */ | ||
| 43 | + public function attempts() | ||
| 44 | + { | ||
| 45 | + return 1; | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + /** | ||
| 49 | + * Get the raw body string for the job. | ||
| 50 | + * @return string | ||
| 51 | + */ | ||
| 52 | + public function getRawBody() | ||
| 53 | + { | ||
| 54 | + return $this->payload; | ||
| 55 | + } | ||
| 56 | +} |
| 1 | +<?php | ||
| 2 | +// +---------------------------------------------------------------------- | ||
| 3 | +// | ThinkPHP [ WE CAN DO IT JUST THINK IT ] | ||
| 4 | +// +---------------------------------------------------------------------- | ||
| 5 | +// | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved. | ||
| 6 | +// +---------------------------------------------------------------------- | ||
| 7 | +// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) | ||
| 8 | +// +---------------------------------------------------------------------- | ||
| 9 | +// | Author: yunwuxin <448901948@qq.com> | ||
| 10 | +// +---------------------------------------------------------------------- | ||
| 11 | + | ||
| 12 | +namespace think\queue\job; | ||
| 13 | + | ||
| 14 | +use think\queue\Job; | ||
| 15 | +use think\queue\connector\Topthink as TopthinkQueue; | ||
| 16 | + | ||
| 17 | +class Topthink extends Job | ||
| 18 | +{ | ||
| 19 | + | ||
| 20 | + /** | ||
| 21 | + * The Iron queue instance. | ||
| 22 | + * | ||
| 23 | + * @var TopthinkQueue | ||
| 24 | + */ | ||
| 25 | + protected $topthink; | ||
| 26 | + | ||
| 27 | + /** | ||
| 28 | + * The IronMQ message instance. | ||
| 29 | + * | ||
| 30 | + * @var object | ||
| 31 | + */ | ||
| 32 | + protected $job; | ||
| 33 | + | ||
| 34 | + public function __construct(TopthinkQueue $topthink, $job, $queue) | ||
| 35 | + { | ||
| 36 | + $this->topthink = $topthink; | ||
| 37 | + $this->job = $job; | ||
| 38 | + $this->queue = $queue; | ||
| 39 | + $this->job->attempts = $this->job->attempts + 1; | ||
| 40 | + } | ||
| 41 | + | ||
| 42 | + /** | ||
| 43 | + * Fire the job. | ||
| 44 | + * @return void | ||
| 45 | + */ | ||
| 46 | + public function fire() | ||
| 47 | + { | ||
| 48 | + $this->resolveAndFire(json_decode($this->job->payload, true)); | ||
| 49 | + } | ||
| 50 | + | ||
| 51 | + /** | ||
| 52 | + * Get the number of times the job has been attempted. | ||
| 53 | + * @return int | ||
| 54 | + */ | ||
| 55 | + public function attempts() | ||
| 56 | + { | ||
| 57 | + return (int) $this->job->attempts; | ||
| 58 | + } | ||
| 59 | + | ||
| 60 | + public function delete() | ||
| 61 | + { | ||
| 62 | + parent::delete(); | ||
| 63 | + | ||
| 64 | + $this->topthink->deleteMessage($this->queue, $this->job->id); | ||
| 65 | + } | ||
| 66 | + | ||
| 67 | + public function release($delay = 0) | ||
| 68 | + { | ||
| 69 | + parent::release($delay); | ||
| 70 | + | ||
| 71 | + $this->delete(); | ||
| 72 | + | ||
| 73 | + $this->topthink->release($this->queue, $this->job, $delay); | ||
| 74 | + } | ||
| 75 | + | ||
| 76 | + /** | ||
| 77 | + * Get the raw body string for the job. | ||
| 78 | + * @return string | ||
| 79 | + */ | ||
| 80 | + public function getRawBody() | ||
| 81 | + { | ||
| 82 | + return $this->job->payload; | ||
| 83 | + } | ||
| 84 | + | ||
| 85 | +} |
-
请 注册 或 登录 后发表评论