This commit is contained in:
全栈小学生 2023-05-31 11:25:43 +08:00
parent 13cb6799c5
commit d9c2abcc3c
45 changed files with 1891 additions and 753 deletions

View File

@ -1 +1 @@
APP_DEBUG = false [APP] DEFAULT_TIMEZONE = Asia/Shanghai AUTH_KEY = [REDIS] REDIS_HOSTNAME = 127.0.0.1 PORT = 6379 REDIS_PASSWORD = SELECT = 0 [LANG] default_lang = zh-cn [SYSTEM] ADMIN_TOKEN_NAME = token API_TOKEN_NAME = token ADMIN_SITE_ID_NAME = site-id API_SITE_ID_NAME = site-id ADMIN_TOKEN_EXPIRE_TIME = 604800 API_TOKEN_EXPIRE_TIME = 86400 LANG_NAME = lang CHANNEL_NAME = channel WAP_DOMAIN = WEB_DOMAIN = APP_DEBUG = false [APP] DEFAULT_TIMEZONE = Asia/Shanghai AUTH_KEY = [REDIS] REDIS_HOSTNAME = 127.0.0.1 PORT = 6379 REDIS_PASSWORD = SELECT = 0 [LANG] default_lang = zh-cn [SYSTEM] ADMIN_TOKEN_NAME = token API_TOKEN_NAME = token ADMIN_SITE_ID_NAME = site-id API_SITE_ID_NAME = site-id ADMIN_TOKEN_EXPIRE_TIME = 604800 API_TOKEN_EXPIRE_TIME = 86400 LANG_NAME = lang CHANNEL_NAME = channel ADMIN_DOMAIN = WAP_DOMAIN = WEB_DOMAIN =

View File

@ -10,9 +10,9 @@ return array(
'7b11c4dc42b3b3023073cb14e519683c' => $vendorDir . '/ralouphie/getallheaders/src/getallheaders.php', '7b11c4dc42b3b3023073cb14e519683c' => $vendorDir . '/ralouphie/getallheaders/src/getallheaders.php',
'0e6d7bf4a5811bfa5cf40c5ccd6fae6a' => $vendorDir . '/symfony/polyfill-mbstring/bootstrap.php', '0e6d7bf4a5811bfa5cf40c5ccd6fae6a' => $vendorDir . '/symfony/polyfill-mbstring/bootstrap.php',
'c964ee0ededf28c96ebd9db5099ef910' => $vendorDir . '/guzzlehttp/promises/src/functions_include.php', 'c964ee0ededf28c96ebd9db5099ef910' => $vendorDir . '/guzzlehttp/promises/src/functions_include.php',
'9b552a3cc426e3287cc811caefa3cf53' => $vendorDir . '/topthink/think-helper/src/helper.php',
'a4a119a56e50fbb293281d9a48007e0e' => $vendorDir . '/symfony/polyfill-php80/bootstrap.php',
'37a3dc5111fe8f707ab4c132ef1dbc62' => $vendorDir . '/guzzlehttp/guzzle/src/functions_include.php', '37a3dc5111fe8f707ab4c132ef1dbc62' => $vendorDir . '/guzzlehttp/guzzle/src/functions_include.php',
'a4a119a56e50fbb293281d9a48007e0e' => $vendorDir . '/symfony/polyfill-php80/bootstrap.php',
'9b552a3cc426e3287cc811caefa3cf53' => $vendorDir . '/topthink/think-helper/src/helper.php',
'35fab96057f1bf5e7aba31a8a6d5fdde' => $vendorDir . '/topthink/think-orm/stubs/load_stubs.php', '35fab96057f1bf5e7aba31a8a6d5fdde' => $vendorDir . '/topthink/think-orm/stubs/load_stubs.php',
'a1105708a18b76903365ca1c4aa61b02' => $vendorDir . '/symfony/translation/Resources/functions.php', 'a1105708a18b76903365ca1c4aa61b02' => $vendorDir . '/symfony/translation/Resources/functions.php',
'0d59ee240a4cd96ddbb4ff164fccea4d' => $vendorDir . '/symfony/polyfill-php73/bootstrap.php', '0d59ee240a4cd96ddbb4ff164fccea4d' => $vendorDir . '/symfony/polyfill-php73/bootstrap.php',

View File

@ -6,12 +6,12 @@ $vendorDir = dirname(__DIR__);
$baseDir = dirname($vendorDir); $baseDir = dirname($vendorDir);
return array( return array(
'yunwuxin\\cron\\' => array($vendorDir . '/yunwuxin/think-cron/src/cron'),
'think\\view\\driver\\' => array($vendorDir . '/topthink/think-view/src'), 'think\\view\\driver\\' => array($vendorDir . '/topthink/think-view/src'),
'think\\trace\\' => array($vendorDir . '/topthink/think-trace/src'), 'think\\trace\\' => array($vendorDir . '/topthink/think-trace/src'),
'think\\captcha\\' => array($vendorDir . '/topthink/think-captcha/src'), 'think\\captcha\\' => array($vendorDir . '/topthink/think-captcha/src'),
'think\\app\\' => array($vendorDir . '/topthink/think-multi-app/src'), 'think\\app\\' => array($vendorDir . '/topthink/think-multi-app/src'),
'think\\' => array($vendorDir . '/topthink/framework/src/think', $vendorDir . '/topthink/think-helper/src', $vendorDir . '/topthink/think-image/src', $vendorDir . '/topthink/think-orm/src', $vendorDir . '/topthink/think-queue/src', $vendorDir . '/topthink/think-template/src'), 'think\\' => array($vendorDir . '/topthink/framework/src/think', $vendorDir . '/topthink/think-helper/src', $vendorDir . '/topthink/think-image/src', $vendorDir . '/topthink/think-orm/src', $vendorDir . '/topthink/think-queue/src', $vendorDir . '/topthink/think-template/src'),
'schedule\\' => array($vendorDir . '/yzh52521/schedule/src'),
'dh2y\\qrcode\\' => array($vendorDir . '/dh2y/think-qrcode/src'), 'dh2y\\qrcode\\' => array($vendorDir . '/dh2y/think-qrcode/src'),
'core\\' => array($baseDir . '/core'), 'core\\' => array($baseDir . '/core'),
'clagiordano\\weblibs\\configmanager\\' => array($vendorDir . '/clagiordano/weblibs-configmanager/src'), 'clagiordano\\weblibs\\configmanager\\' => array($vendorDir . '/clagiordano/weblibs-configmanager/src'),

View File

@ -11,9 +11,9 @@ class ComposerStaticInitf082efa3600aae2b847c3e8b4e641a4e
'7b11c4dc42b3b3023073cb14e519683c' => __DIR__ . '/..' . '/ralouphie/getallheaders/src/getallheaders.php', '7b11c4dc42b3b3023073cb14e519683c' => __DIR__ . '/..' . '/ralouphie/getallheaders/src/getallheaders.php',
'0e6d7bf4a5811bfa5cf40c5ccd6fae6a' => __DIR__ . '/..' . '/symfony/polyfill-mbstring/bootstrap.php', '0e6d7bf4a5811bfa5cf40c5ccd6fae6a' => __DIR__ . '/..' . '/symfony/polyfill-mbstring/bootstrap.php',
'c964ee0ededf28c96ebd9db5099ef910' => __DIR__ . '/..' . '/guzzlehttp/promises/src/functions_include.php', 'c964ee0ededf28c96ebd9db5099ef910' => __DIR__ . '/..' . '/guzzlehttp/promises/src/functions_include.php',
'9b552a3cc426e3287cc811caefa3cf53' => __DIR__ . '/..' . '/topthink/think-helper/src/helper.php',
'a4a119a56e50fbb293281d9a48007e0e' => __DIR__ . '/..' . '/symfony/polyfill-php80/bootstrap.php',
'37a3dc5111fe8f707ab4c132ef1dbc62' => __DIR__ . '/..' . '/guzzlehttp/guzzle/src/functions_include.php', '37a3dc5111fe8f707ab4c132ef1dbc62' => __DIR__ . '/..' . '/guzzlehttp/guzzle/src/functions_include.php',
'a4a119a56e50fbb293281d9a48007e0e' => __DIR__ . '/..' . '/symfony/polyfill-php80/bootstrap.php',
'9b552a3cc426e3287cc811caefa3cf53' => __DIR__ . '/..' . '/topthink/think-helper/src/helper.php',
'35fab96057f1bf5e7aba31a8a6d5fdde' => __DIR__ . '/..' . '/topthink/think-orm/stubs/load_stubs.php', '35fab96057f1bf5e7aba31a8a6d5fdde' => __DIR__ . '/..' . '/topthink/think-orm/stubs/load_stubs.php',
'a1105708a18b76903365ca1c4aa61b02' => __DIR__ . '/..' . '/symfony/translation/Resources/functions.php', 'a1105708a18b76903365ca1c4aa61b02' => __DIR__ . '/..' . '/symfony/translation/Resources/functions.php',
'0d59ee240a4cd96ddbb4ff164fccea4d' => __DIR__ . '/..' . '/symfony/polyfill-php73/bootstrap.php', '0d59ee240a4cd96ddbb4ff164fccea4d' => __DIR__ . '/..' . '/symfony/polyfill-php73/bootstrap.php',
@ -39,10 +39,6 @@ class ComposerStaticInitf082efa3600aae2b847c3e8b4e641a4e
); );
public static $prefixLengthsPsr4 = array ( public static $prefixLengthsPsr4 = array (
'y' =>
array (
'yunwuxin\\cron\\' => 14,
),
't' => 't' =>
array ( array (
'think\\view\\driver\\' => 18, 'think\\view\\driver\\' => 18,
@ -51,6 +47,10 @@ class ComposerStaticInitf082efa3600aae2b847c3e8b4e641a4e
'think\\app\\' => 10, 'think\\app\\' => 10,
'think\\' => 6, 'think\\' => 6,
), ),
's' =>
array (
'schedule\\' => 9,
),
'd' => 'd' =>
array ( array (
'dh2y\\qrcode\\' => 12, 'dh2y\\qrcode\\' => 12,
@ -180,10 +180,6 @@ class ComposerStaticInitf082efa3600aae2b847c3e8b4e641a4e
); );
public static $prefixDirsPsr4 = array ( public static $prefixDirsPsr4 = array (
'yunwuxin\\cron\\' =>
array (
0 => __DIR__ . '/..' . '/yunwuxin/think-cron/src/cron',
),
'think\\view\\driver\\' => 'think\\view\\driver\\' =>
array ( array (
0 => __DIR__ . '/..' . '/topthink/think-view/src', 0 => __DIR__ . '/..' . '/topthink/think-view/src',
@ -209,6 +205,10 @@ class ComposerStaticInitf082efa3600aae2b847c3e8b4e641a4e
4 => __DIR__ . '/..' . '/topthink/think-queue/src', 4 => __DIR__ . '/..' . '/topthink/think-queue/src',
5 => __DIR__ . '/..' . '/topthink/think-template/src', 5 => __DIR__ . '/..' . '/topthink/think-template/src',
), ),
'schedule\\' =>
array (
0 => __DIR__ . '/..' . '/yzh52521/schedule/src',
),
'dh2y\\qrcode\\' => 'dh2y\\qrcode\\' =>
array ( array (
0 => __DIR__ . '/..' . '/dh2y/think-qrcode/src', 0 => __DIR__ . '/..' . '/dh2y/think-qrcode/src',

View File

@ -944,17 +944,17 @@
}, },
{ {
"name": "guzzlehttp/promises", "name": "guzzlehttp/promises",
"version": "1.5.2", "version": "1.5.3",
"version_normalized": "1.5.2.0", "version_normalized": "1.5.3.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/guzzle/promises.git", "url": "https://github.com/guzzle/promises.git",
"reference": "b94b2807d85443f9719887892882d0329d1e2598" "reference": "67ab6e18aaa14d753cc148911d273f6e6cb6721e"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/guzzle/promises/zipball/b94b2807d85443f9719887892882d0329d1e2598", "url": "https://api.github.com/repos/guzzle/promises/zipball/67ab6e18aaa14d753cc148911d273f6e6cb6721e",
"reference": "b94b2807d85443f9719887892882d0329d1e2598", "reference": "67ab6e18aaa14d753cc148911d273f6e6cb6721e",
"shasum": "", "shasum": "",
"mirrors": [ "mirrors": [
{ {
@ -969,13 +969,8 @@
"require-dev": { "require-dev": {
"symfony/phpunit-bridge": "^4.4 || ^5.1" "symfony/phpunit-bridge": "^4.4 || ^5.1"
}, },
"time": "2022-08-28T14:55:35+00:00", "time": "2023-05-21T12:31:43+00:00",
"type": "library", "type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.5-dev"
}
},
"installation-source": "dist", "installation-source": "dist",
"autoload": { "autoload": {
"files": [ "files": [
@ -1017,7 +1012,7 @@
], ],
"support": { "support": {
"issues": "https://github.com/guzzle/promises/issues", "issues": "https://github.com/guzzle/promises/issues",
"source": "https://github.com/guzzle/promises/tree/1.5.2" "source": "https://github.com/guzzle/promises/tree/1.5.3"
}, },
"funding": [ "funding": [
{ {
@ -5716,18 +5711,18 @@
"install-path": "../yansongda/supports" "install-path": "../yansongda/supports"
}, },
{ {
"name": "yunwuxin/think-cron", "name": "yzh52521/schedule",
"version": "v3.0.5", "version": "v1.0.0",
"version_normalized": "3.0.5.0", "version_normalized": "1.0.0.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/yunwuxin/think-cron.git", "url": "https://github.com/yzh52521/schedule.git",
"reference": "a5e5c679b7f5daedab9fb4bb00b641b6c4a054ca" "reference": "4c8f537f0c08417e785f84b8b91bf16b083cb163"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/yunwuxin/think-cron/zipball/a5e5c679b7f5daedab9fb4bb00b641b6c4a054ca", "url": "https://api.github.com/repos/yzh52521/schedule/zipball/4c8f537f0c08417e785f84b8b91bf16b083cb163",
"reference": "a5e5c679b7f5daedab9fb4bb00b641b6c4a054ca", "reference": "4c8f537f0c08417e785f84b8b91bf16b083cb163",
"shasum": "", "shasum": "",
"mirrors": [ "mirrors": [
{ {
@ -5737,48 +5732,35 @@
] ]
}, },
"require": { "require": {
"dragonmantank/cron-expression": "^3.0", "nesbot/carbon": "^2.0",
"nesbot/carbon": "^2.28", "php": ">=7.1"
"symfony/process": "^4.4|^5.0",
"topthink/framework": "^6.0"
}, },
"require-dev": { "time": "2020-07-02T01:34:32+00:00",
"topthink/think-swoole": "^4.0"
},
"time": "2021-12-22T09:25:54+00:00",
"type": "library", "type": "library",
"extra": {
"think": {
"config": {
"cron": "src/config.php"
},
"services": [
"yunwuxin\\cron\\Service"
]
}
},
"installation-source": "dist", "installation-source": "dist",
"autoload": { "autoload": {
"psr-4": { "psr-4": {
"yunwuxin\\cron\\": "src/cron" "schedule\\": "src/"
} }
}, },
"notification-url": "https://packagist.org/downloads/", "notification-url": "https://packagist.org/downloads/",
"license": [ "license": [
"Apache-2.0" "MIT"
], ],
"authors": [ "description": "task schedule,schedule,thinkphp schedule,任务调度",
{ "keywords": [
"name": "yunwuxin", "schedule",
"email": "448901948@qq.com" "task schedule",
} "think-schedule",
"thinkphp",
"thinkphp5.1",
"thinkphp6"
], ],
"description": "计划任务",
"support": { "support": {
"issues": "https://github.com/yunwuxin/think-cron/issues", "issues": "https://github.com/yzh52521/schedule/issues",
"source": "https://github.com/yunwuxin/think-cron/tree/v3.0.5" "source": "https://github.com/yzh52521/schedule/tree/v1.0.0"
}, },
"install-path": "../yunwuxin/think-cron" "install-path": "../yzh52521/schedule"
} }
], ],
"dev": true, "dev": true,

View File

@ -3,7 +3,7 @@
'name' => 'topthink/think', 'name' => 'topthink/think',
'pretty_version' => 'dev-master', 'pretty_version' => 'dev-master',
'version' => 'dev-master', 'version' => 'dev-master',
'reference' => '7939a59b413ebc73af2f7f44540d15df3ad38d1a', 'reference' => 'dc3bba8859823f8836483423b8b7bfdda3a18e7d',
'type' => 'project', 'type' => 'project',
'install_path' => __DIR__ . '/../../', 'install_path' => __DIR__ . '/../../',
'aliases' => array(), 'aliases' => array(),
@ -128,9 +128,9 @@
'dev_requirement' => false, 'dev_requirement' => false,
), ),
'guzzlehttp/promises' => array( 'guzzlehttp/promises' => array(
'pretty_version' => '1.5.2', 'pretty_version' => '1.5.3',
'version' => '1.5.2.0', 'version' => '1.5.3.0',
'reference' => 'b94b2807d85443f9719887892882d0329d1e2598', 'reference' => '67ab6e18aaa14d753cc148911d273f6e6cb6721e',
'type' => 'library', 'type' => 'library',
'install_path' => __DIR__ . '/../guzzlehttp/promises', 'install_path' => __DIR__ . '/../guzzlehttp/promises',
'aliases' => array(), 'aliases' => array(),
@ -652,7 +652,7 @@
'topthink/think' => array( 'topthink/think' => array(
'pretty_version' => 'dev-master', 'pretty_version' => 'dev-master',
'version' => 'dev-master', 'version' => 'dev-master',
'reference' => '7939a59b413ebc73af2f7f44540d15df3ad38d1a', 'reference' => 'dc3bba8859823f8836483423b8b7bfdda3a18e7d',
'type' => 'project', 'type' => 'project',
'install_path' => __DIR__ . '/../../', 'install_path' => __DIR__ . '/../../',
'aliases' => array(), 'aliases' => array(),
@ -766,12 +766,12 @@
'aliases' => array(), 'aliases' => array(),
'dev_requirement' => false, 'dev_requirement' => false,
), ),
'yunwuxin/think-cron' => array( 'yzh52521/schedule' => array(
'pretty_version' => 'v3.0.5', 'pretty_version' => 'v1.0.0',
'version' => '3.0.5.0', 'version' => '1.0.0.0',
'reference' => 'a5e5c679b7f5daedab9fb4bb00b641b6c4a054ca', 'reference' => '4c8f537f0c08417e785f84b8b91bf16b083cb163',
'type' => 'library', 'type' => 'library',
'install_path' => __DIR__ . '/../yunwuxin/think-cron', 'install_path' => __DIR__ . '/../yzh52521/schedule',
'aliases' => array(), 'aliases' => array(),
'dev_requirement' => false, 'dev_requirement' => false,
), ),

View File

@ -1,5 +1,11 @@
# CHANGELOG # CHANGELOG
## 1.5.3 - 2023-05-21
### Changed
- Removed remaining usage of deprecated functions
## 1.5.2 - 2022-08-07 ## 1.5.2 - 2022-08-07
### Changed ### Changed

View File

@ -46,11 +46,6 @@
"test": "vendor/bin/simple-phpunit", "test": "vendor/bin/simple-phpunit",
"test-ci": "vendor/bin/simple-phpunit --coverage-text" "test-ci": "vendor/bin/simple-phpunit --coverage-text"
}, },
"extra": {
"branch-alias": {
"dev-master": "1.5-dev"
}
},
"config": { "config": {
"preferred-install": "dist", "preferred-install": "dist",
"sort-packages": true "sort-packages": true

View File

@ -78,7 +78,7 @@ final class Each
$concurrency, $concurrency,
callable $onFulfilled = null callable $onFulfilled = null
) { ) {
return each_limit( return self::ofLimit(
$iterable, $iterable,
$concurrency, $concurrency,
$onFulfilled, $onFulfilled,

View File

@ -107,7 +107,7 @@ final class Utils
{ {
$results = []; $results = [];
foreach ($promises as $key => $promise) { foreach ($promises as $key => $promise) {
$results[$key] = inspect($promise); $results[$key] = self::inspect($promise);
} }
return $results; return $results;

View File

@ -1,10 +1,9 @@
<?php <?php
// This file is automatically generated at:2023-05-16 17:43:28 // This file is automatically generated at:2023-05-24 15:05:09
declare (strict_types = 1); declare (strict_types = 1);
return array ( return array (
0 => 'think\\captcha\\CaptchaService', 0 => 'think\\captcha\\CaptchaService',
1 => 'think\\app\\Service', 1 => 'think\\app\\Service',
2 => 'think\\queue\\Service', 2 => 'think\\queue\\Service',
3 => 'think\\trace\\Service', 3 => 'think\\trace\\Service',
4 => 'yunwuxin\\cron\\Service',
); );

View File

@ -117,7 +117,7 @@ class Captcha
} }
for ($i = 0; $i < $this->length; $i++) { for ($i = 0; $i < $this->length; $i++) {
$bag .= $characters[random_int(0, count($characters) - 1)]; $bag .= $characters[rand(0, count($characters) - 1)];
} }
$key = mb_strtolower($bag, 'UTF-8'); $key = mb_strtolower($bag, 'UTF-8');
@ -167,7 +167,7 @@ class Captcha
* @param bool $api * @param bool $api
* @return Response * @return Response
*/ */
public function create(string $config = null, bool $api = false): Response public function create(string $config = null, bool $api = false)
{ {
$this->configure($config); $this->configure($config);
@ -178,11 +178,11 @@ class Captcha
// 图片高(px) // 图片高(px)
$this->imageH || $this->imageH = $this->fontSize * 2.5; $this->imageH || $this->imageH = $this->fontSize * 2.5;
$this->imageW = (int)$this->imageW; $this->imageW = intval($this->imageW);
$this->imageH = (int)$this->imageH; $this->imageH = intval($this->imageH);
// 建立一幅 $this->imageW x $this->imageH 的图像 // 建立一幅 $this->imageW x $this->imageH 的图像
$this->im = imagecreate($this->imageW, $this->imageH); $this->im = imagecreate((int) $this->imageW, (int) $this->imageH);
// 设置背景 // 设置背景
imagecolorallocate($this->im, $this->bg[0], $this->bg[1], $this->bg[2]); imagecolorallocate($this->im, $this->bg[0], $this->bg[1], $this->bg[2]);
@ -196,7 +196,7 @@ class Captcha
$dir = dir($ttfPath); $dir = dir($ttfPath);
$ttfs = []; $ttfs = [];
while (false !== ($file = $dir->read())) { while (false !== ($file = $dir->read())) {
if (substr($file, -4) === '.ttf' || substr($file, -4) === '.otf') { if (substr($file, -4) == '.ttf' || substr($file, -4) == '.otf') {
$ttfs[] = $file; $ttfs[] = $file;
} }
} }
@ -228,7 +228,7 @@ class Captcha
$y = $this->fontSize + mt_rand(10, 20); $y = $this->fontSize + mt_rand(10, 20);
$angle = $this->math ? 0 : mt_rand(-40, 40); $angle = $this->math ? 0 : mt_rand(-40, 40);
imagettftext($this->im, (int)$this->fontSize, $angle, (int)$x, (int)$y, $this->color, $fontttf, $char); imagettftext($this->im, intval($this->fontSize), intval($this->fontSize), intval($x), intval($y), $this->color, $fontttf, $char);
} }
ob_start(); ob_start();
@ -237,6 +237,13 @@ class Captcha
$content = ob_get_clean(); $content = ob_get_clean();
imagedestroy($this->im); imagedestroy($this->im);
// api调用
if ($api) {
return [
'code' => implode('', $text),
'img' => 'data:image/png;base64,' . chunk_split(base64_encode($content))
];
}
return response($content, 200, ['Content-Length' => strlen($content)])->contentType('image/png'); return response($content, 200, ['Content-Length' => strlen($content)])->contentType('image/png');
} }
@ -257,30 +264,30 @@ class Captcha
$px = $py = 0; $px = $py = 0;
// 曲线前部分 // 曲线前部分
$A = mt_rand(1, (int)($this->imageH / 2)); // 振幅 $A = mt_rand(1, $this->imageH / 2); // 振幅
$b = mt_rand((int)(-$this->imageH / 4), (int)($this->imageH / 4)); // Y轴方向偏移量 $b = mt_rand(intval(-$this->imageH / 4), intval($this->imageH / 4)); // Y轴方向偏移量
$f = mt_rand((int)(-$this->imageH / 4), (int)($this->imageH / 4)); // X轴方向偏移量 $f = mt_rand(intval(-$this->imageH / 4), intval($this->imageH / 4)); // X轴方向偏移量
$T = mt_rand((int)$this->imageH, (int)$this->imageW * 2); // 周期 $T = mt_rand($this->imageH, $this->imageW * 2); // 周期
$w = (2 * M_PI) / $T; $w = (2 * M_PI) / $T;
$px1 = 0; // 曲线横坐标起始位置 $px1 = 0; // 曲线横坐标起始位置
$px2 = mt_rand((int)($this->imageW / 2), (int)($this->imageW * 0.8)); // 曲线横坐标结束位置 $px2 = mt_rand($this->imageW / 2, $this->imageW * 0.8); // 曲线横坐标结束位置
for ($px = $px1; $px <= $px2; $px = $px + 1) { for ($px = $px1; $px <= $px2; $px = $px + 1) {
if (0 != $w) { if (0 != $w) {
$py = $A * sin($w * $px + $f) + $b + $this->imageH / 2; // y = Asin(ωx+φ) + b $py = $A * sin($w * $px + $f) + $b + $this->imageH / 2; // y = Asin(ωx+φ) + b
$i = (int) ($this->fontSize / 5); $i = (int) ($this->fontSize / 5);
while ($i > 0) { while ($i > 0) {
imagesetpixel($this->im, (int)($px + $i), (int)($py + $i), $this->color); // 这里(while)循环画像素点比imagettftext和imagestring用字体大小一次画出不用这while循环性能要好很多 imagesetpixel($this->im, intval($px + $i), intval($py + $i), $this->color); // 这里(while)循环画像素点比imagettftext和imagestring用字体大小一次画出不用这while循环性能要好很多
$i--; $i--;
} }
} }
} }
// 曲线后部分 // 曲线后部分
$A = mt_rand(1, (int)($this->imageH / 2)); // 振幅 $A = mt_rand(1, $this->imageH / 2); // 振幅
$f = mt_rand((int)(-$this->imageH / 4), (int)($this->imageH / 4)); // X轴方向偏移量 $f = mt_rand(intval(-$this->imageH / 4), intval($this->imageH / 4)); // X轴方向偏移量
$T = mt_rand((int)$this->imageH, (int)$this->imageW * 2); // 周期 $T = mt_rand($this->imageH, $this->imageW * 2); // 周期
$w = (2 * M_PI) / $T; $w = (2 * M_PI) / $T;
$b = $py - $A * sin($w * $px + $f) - $this->imageH / 2; $b = $py - $A * sin($w * $px + $f) - $this->imageH / 2;
$px1 = $px2; $px1 = $px2;
@ -291,7 +298,7 @@ class Captcha
$py = $A * sin($w * $px + $f) + $b + $this->imageH / 2; // y = Asin(ωx+φ) + b $py = $A * sin($w * $px + $f) + $b + $this->imageH / 2; // y = Asin(ωx+φ) + b
$i = (int) ($this->fontSize / 5); $i = (int) ($this->fontSize / 5);
while ($i > 0) { while ($i > 0) {
imagesetpixel($this->im, (int)($px + $i), (int)($py + $i), $this->color); imagesetpixel($this->im, intval($px + $i), intval($py + $i), $this->color);
$i--; $i--;
} }
} }
@ -310,7 +317,7 @@ class Captcha
$noiseColor = imagecolorallocate($this->im, mt_rand(150, 225), mt_rand(150, 225), mt_rand(150, 225)); $noiseColor = imagecolorallocate($this->im, mt_rand(150, 225), mt_rand(150, 225), mt_rand(150, 225));
for ($j = 0; $j < 5; $j++) { for ($j = 0; $j < 5; $j++) {
// 绘杂点 // 绘杂点
imagestring($this->im, 5, mt_rand(-10, (int)$this->imageW), mt_rand(-10, (int)$this->imageH), $codeSet[mt_rand(0, 29)], $noiseColor); imagestring($this->im, 5, mt_rand(-10, $this->imageW), mt_rand(-10, $this->imageH), $codeSet[mt_rand(0, 29)], $noiseColor);
} }
} }
} }
@ -337,7 +344,7 @@ class Captcha
list($width, $height) = @getimagesize($gb); list($width, $height) = @getimagesize($gb);
// Resample // Resample
$bgImage = @imagecreatefromjpeg($gb); $bgImage = @imagecreatefromjpeg($gb);
@imagecopyresampled($this->im, $bgImage, 0, 0, 0, 0, (int)$this->imageW, (int)$this->imageH, $width, $height); @imagecopyresampled($this->im, $bgImage, 0, 0, 0, 0, $this->imageW, $this->imageH, $width, $height);
@imagedestroy($bgImage); @imagedestroy($bgImage);
} }
} }

View File

@ -1,3 +0,0 @@
.idea/
composer.lock
vendor/

View File

@ -1,66 +0,0 @@
# think-cron 计划任务
## 安装方法
```
composer require yunwuxin/think-cron
```
## 使用方法
### 创建任务类
```
<?php
namespace app\task;
use yunwuxin\cron\Task;
class DemoTask extends Task
{
public function configure()
{
$this->daily(); //设置任务的周期,每天执行一次,更多的方法可以查看源代码,都有注释
}
/**
* 执行任务
* @return mixed
*/
protected function execute()
{
//...具体的任务执行
}
}
```
### 配置
> 配置文件位于 application/extra/cron.php
```
return [
'tasks' => [
\app\task\DemoTask::class, //任务的完整类名
]
];
```
### 任务监听
#### 两种方法:
> 方法一 (推荐)
起一个常驻进程可以配合supervisor使用
~~~
php think cron:schedule
~~~
> 方法二
在系统的计划任务里添加
~~~
* * * * * php /path/to/think cron:run >> /dev/null 2>&1
~~~

View File

@ -1,42 +0,0 @@
{
"name": "yunwuxin/think-cron",
"description": "计划任务",
"license": "Apache-2.0",
"authors": [
{
"name": "yunwuxin",
"email": "448901948@qq.com"
}
],
"require": {
"topthink/framework": "^6.0",
"symfony/process": "^4.4|^5.0",
"nesbot/carbon": "^2.28",
"dragonmantank/cron-expression": "^3.0"
},
"autoload": {
"psr-4": {
"yunwuxin\\cron\\": "src/cron"
}
},
"extra": {
"think": {
"config": {
"cron": "src/config.php"
},
"services": [
"yunwuxin\\cron\\Service"
]
}
},
"require-dev": {
"topthink/think-swoole": "^4.0"
},
"config": {
"preferred-install": "dist",
"platform-check": false,
"platform": {
"ext-swoole": "4.6.0"
}
}
}

View File

@ -1,5 +0,0 @@
<?php
return [
'tasks' => []
];

View File

@ -1,94 +0,0 @@
<?php
namespace yunwuxin\cron;
use Carbon\Carbon;
use Exception;
use think\App;
use think\cache\Driver;
use yunwuxin\cron\event\TaskFailed;
use yunwuxin\cron\event\TaskProcessed;
use yunwuxin\cron\event\TaskSkipped;
class Scheduler
{
/** @var App */
protected $app;
/** @var Carbon */
protected $startedAt;
protected $tasks = [];
/** @var Driver */
protected $cache;
public function __construct(App $app)
{
$this->app = $app;
$this->tasks = $app->config->get('cron.tasks', []);
$this->cache = $app->cache->store($app->config->get('cron.store', null));
}
public function run()
{
$this->startedAt = Carbon::now();
foreach ($this->tasks as $taskClass) {
if (is_subclass_of($taskClass, Task::class)) {
/** @var Task $task */
$task = $this->app->invokeClass($taskClass, [$this->cache]);
if ($task->isDue()) {
if (!$task->filtersPass()) {
continue;
}
if ($task->onOneServer) {
$this->runSingleServerTask($task);
} else {
$this->runTask($task);
}
$this->app->event->trigger(new TaskProcessed($task));
}
}
}
}
/**
* @param $task Task
* @return bool
*/
protected function serverShouldRun($task)
{
$key = $task->mutexName() . $this->startedAt->format('Hi');
if ($this->cache->has($key)) {
return false;
}
$this->cache->set($key, true, 60);
return true;
}
protected function runSingleServerTask($task)
{
if ($this->serverShouldRun($task)) {
$this->runTask($task);
} else {
$this->app->event->trigger(new TaskSkipped($task));
}
}
/**
* @param $task Task
*/
protected function runTask($task)
{
try {
$task->run();
} catch (Exception $e) {
$this->app->event->trigger(new TaskFailed($task, $e));
}
}
}

View File

@ -1,30 +0,0 @@
<?php
namespace yunwuxin\cron;
use Swoole\Timer;
use think\swoole\Manager;
use yunwuxin\cron\command\Run;
use yunwuxin\cron\command\Schedule;
class Service extends \think\Service
{
public function boot()
{
$this->commands([
Run::class,
Schedule::class,
]);
$this->app->event->listen('swoole.init', function (Manager $manager) {
$manager->addWorker(function () use ($manager) {
Timer::tick(60 * 1000, function () use ($manager) {
$manager->runWithBarrier([$manager, 'runInSandbox'], function (Scheduler $scheduler) {
$scheduler->run();
});
});
}, "cron");
});
}
}

View File

@ -1,171 +0,0 @@
<?php
namespace yunwuxin\cron;
use Closure;
use Cron\CronExpression;
use think\App;
use think\Cache;
abstract class Task
{
use ManagesFrequencies;
/** @var string|null 时区 */
public $timezone = null;
/** @var string 任务周期 */
public $expression = '* * * * *';
/** @var bool 任务是否可以重叠执行 */
public $withoutOverlapping = false;
/** @var int 最大执行时间(重叠执行检查用) */
public $expiresAt = 1440;
/** @var bool 分布式部署 是否仅在一台服务器上运行 */
public $onOneServer = false;
protected $filters = [];
protected $rejects = [];
/** @var Cache */
protected $cache;
/** @var App */
protected $app;
public function __construct(App $app, Cache $cache)
{
$this->app = $app;
$this->cache = $cache;
$this->configure();
}
/**
* 是否到期执行
* @return bool
*/
public function isDue()
{
$cronExpression = new CronExpression($this->expression);
return $cronExpression->isDue('now', $this->timezone);
}
/**
* 配置任务
*/
protected function configure()
{
}
/**
* 执行任务
*/
protected function execute()
{
$this->app->invoke([$this, 'handle'], [], true);
}
final public function run()
{
if ($this->withoutOverlapping &&
!$this->createMutex()) {
return;
}
register_shutdown_function(function () {
$this->removeMutex();
});
try {
$this->execute();
} finally {
$this->removeMutex();
}
}
/**
* 过滤
* @return bool
*/
public function filtersPass()
{
foreach ($this->filters as $callback) {
if (!call_user_func($callback)) {
return false;
}
}
foreach ($this->rejects as $callback) {
if (call_user_func($callback)) {
return false;
}
}
return true;
}
/**
* 任务标识
*/
public function mutexName()
{
return 'task-' . sha1(static::class);
}
protected function removeMutex()
{
return $this->cache->delete($this->mutexName());
}
protected function createMutex()
{
$name = $this->mutexName();
return $this->cache->set($name, time(), $this->expiresAt);
}
protected function existsMutex()
{
if ($this->cache->has($this->mutexName())) {
$mutex = $this->cache->get($this->mutexName());
return $mutex + $this->expiresAt > time();
}
return false;
}
public function when(Closure $callback)
{
$this->filters[] = $callback;
return $this;
}
public function skip(Closure $callback)
{
$this->rejects[] = $callback;
return $this;
}
public function withoutOverlapping($expiresAt = 1440)
{
$this->withoutOverlapping = true;
$this->expiresAt = $expiresAt;
return $this->skip(function () {
return $this->existsMutex();
});
}
public function onOneServer()
{
$this->onOneServer = true;
return $this;
}
}

View File

@ -1,56 +0,0 @@
<?php
namespace yunwuxin\cron\command;
use Carbon\Carbon;
use think\console\Command;
use think\exception\Handle;
use yunwuxin\cron\event\TaskFailed;
use yunwuxin\cron\event\TaskProcessed;
use yunwuxin\cron\event\TaskSkipped;
use yunwuxin\cron\Scheduler;
class Run extends Command
{
/** @var Carbon */
protected $startedAt;
protected function configure()
{
$this->startedAt = Carbon::now();
$this->setName('cron:run');
}
public function handle(Scheduler $scheduler)
{
$this->listenForEvents();
$scheduler->run();
}
/**
* 注册事件
*/
protected function listenForEvents()
{
$this->app->event->listen(TaskProcessed::class, function (TaskProcessed $event) {
$this->output->writeln("Task {$event->getName()} run at " . Carbon::now());
});
$this->app->event->listen(TaskSkipped::class, function (TaskSkipped $event) {
$this->output->writeln('<info>Skipping task (has already run on another server):</info> ' . $event->getName());
});
$this->app->event->listen(TaskFailed::class, function (TaskFailed $event) {
$this->output->writeln("Task {$event->getName()} failed at " . Carbon::now());
/** @var Handle $handle */
$handle = $this->app->make(Handle::class);
$handle->renderForConsole($this->output, $event->exception);
$handle->report($event->exception);
});
}
}

View File

@ -1,34 +0,0 @@
<?php
namespace yunwuxin\cron\command;
use Symfony\Component\Process\Process;
use think\console\Command;
use think\console\Input;
use think\console\Output;
class Schedule extends Command
{
protected function configure()
{
$this->setName('cron:schedule');
}
protected function execute(Input $input, Output $output)
{
if ('\\' == DIRECTORY_SEPARATOR) {
$command = 'start /B "' . PHP_BINARY . '" think cron:run';
} else {
$command = 'nohup "' . PHP_BINARY . '" think cron:run >> /dev/null 2>&1 &';
}
$process = Process::fromShellCommandline($command);
while (true) {
$process->run();
sleep(60);
}
}
}

View File

@ -1,20 +0,0 @@
<?php
namespace yunwuxin\cron\event;
use yunwuxin\cron\Task;
abstract class TaskEvent
{
public $task;
public function __construct(Task $task)
{
$this->task = $task;
}
public function getName()
{
return get_class($this->task);
}
}

View File

@ -1,14 +0,0 @@
<?php
namespace yunwuxin\cron\event;
class TaskFailed extends TaskEvent
{
public $exception;
public function __construct($task, $exception)
{
parent::__construct($task);
$this->exception = $exception;
}
}

View File

@ -1,8 +0,0 @@
<?php
namespace yunwuxin\cron\event;
class TaskProcessed extends TaskEvent
{
}

View File

@ -1,7 +0,0 @@
<?php
namespace yunwuxin\cron\event;
class TaskSkipped extends TaskEvent
{
}

View File

@ -0,0 +1 @@
.idea

View File

@ -0,0 +1,130 @@
# schedule
thinkphp 任务调度
代码实现主要参考 laravel 相关用法请参考 laravel
具体用法:
第一步
运行指令
```
php think make:command Schedule schedule:run
```
会生成一个app\console\Schedule命令行指令类我们修改内容如下
```
namespace app\command;
use schedule\console\Command;
use think\console\Input;
use think\console\Output;
class Schedule extends Command
{
protected function configure()
{
$this->setName('schedule:run');
}
protected function execute(Input $input, Output $output)
{
//每天的上午十点和晚上八点执行这个命令
$this->command('test')->twiceDaily(10, 20);
parent::execute($input, $output);
}
}
```
继续运行指令
```
php think make:command Test test
```
第二步配置config/console.php文件
```
<?php
return [
'commands' => [
'schedule:run'=>\app\command\Schedule::class,
'test' => 'app\command\Test',
]
];
```
第三步,您应该在crontab中添加以下命令
```
* * * * * php /path/to/think schedule:run >> /dev/null 2>&1
```
时间表范例
此扩展支持Laravel Schedule的所有功能环境和维护模式除外。
Scheduling Closures
```
$this->call(function()
{
// Do some task...
})->hourly();
```
Running command of your application
```
$this->command('migrate')->cron('* * * * *');
```
Frequent Jobs
```
$this->command('foo')->everyFiveMinutes();
$this->command('foo')->everyTenMinutes();
$this->command('foo')->everyThirtyMinutes();
```
Daily Jobs
```
$this->command('foo')->daily();
```
Daily Jobs At A Specific Time (24 Hour Time)
```
$this->command('foo')->dailyAt('15:00');
```
Twice Daily Jobs
```
$this->command('foo')->twiceDaily();
```
Job That Runs Every Weekday
```
$this->command('foo')->weekdays();
```
Weekly Jobs
```
$this->command('foo')->weekly();
// Schedule weekly job for specific day (0-6) and time...
$this->command('foo')->weeklyOn(1, '8:00');
```
Monthly Jobs
```
$this->command('foo')->monthly();
```
Job That Runs On Specific Days
```
$this->command('foo')->mondays();
$this->command('foo')->tuesdays();
$this->command('foo')->wednesdays();
$this->command('foo')->thursdays();
$this->command('foo')->fridays();
$this->command('foo')->saturdays();
$this->command('foo')->sundays();
```
Only Allow Job To Run When Callback Is True
```
$this->command('foo')->monthly()->when(function()
{
return true;
});
```

View File

@ -0,0 +1,24 @@
{
"name": "yzh52521/schedule",
"description": "task schedule,schedule,thinkphp schedule,任务调度",
"keywords": [
"schedule",
"task schedule",
"thinkphp",
"thinkphp6",
"thinkphp5.1",
"think-schedule"
],
"type": "library",
"license": "MIT",
"minimum-stability": "dev",
"require": {
"php": ">=7.1",
"nesbot/carbon": "^2.0"
},
"autoload": {
"psr-4": {
"schedule\\": "src/"
}
}
}

View File

@ -0,0 +1,26 @@
<?php
namespace schedule;
use schedule\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
class Run extends Command
{
protected function configure()
{
$this->setName('schedule:run');
}
protected function execute(Input $input, Output $output)
{
//每天的上午十点和晚上八点执行这个命令
$this->command('test')->twiceDaily(10, 20);
parent::execute($input, $output);
}
}

View File

@ -0,0 +1,148 @@
<?php
namespace schedule\console;
/**
* Abstract CRON expression field
*/
abstract class AbstractField implements FieldInterface
{
/**
* Check to see if a field is satisfied by a value
*
* @param string $dateValue Date value to check
* @param string $value Value to test
*
* @return bool
*/
public function isSatisfied($dateValue, $value)
{
if ($this->isIncrementsOfRanges($value)) {
return $this->isInIncrementsOfRanges($dateValue, $value);
} elseif ($this->isRange($value)) {
return $this->isInRange($dateValue, $value);
}
return $value == '*' || $dateValue == $value;
}
/**
* Check if a value is a range
*
* @param string $value Value to test
*
* @return bool
*/
public function isRange($value)
{
return strpos($value, '-') !== false;
}
/**
* Check if a value is an increments of ranges
*
* @param string $value Value to test
*
* @return bool
*/
public function isIncrementsOfRanges($value)
{
return strpos($value, '/') !== false;
}
/**
* Test if a value is within a range
*
* @param string $dateValue Set date value
* @param string $value Value to test
*
* @return bool
*/
public function isInRange($dateValue, $value)
{
$parts = array_map('trim', explode('-', $value, 2));
return $dateValue >= $parts[0] && $dateValue <= $parts[1];
}
/**
* Test if a value is within an increments of ranges (offset[-to]/step size)
*
* @param string $dateValue Set date value
* @param string $value Value to test
*
* @return bool
*/
public function isInIncrementsOfRanges($dateValue, $value)
{
$parts = array_map('trim', explode('/', $value, 2));
$stepSize = isset($parts[1]) ? (int) $parts[1] : 0;
if ($stepSize === 0) {
return false;
}
if (($parts[0] == '*' || $parts[0] === '0')) {
return (int) $dateValue % $stepSize == 0;
}
$range = explode('-', $parts[0], 2);
$offset = $range[0];
$to = isset($range[1]) ? $range[1] : $dateValue;
// Ensure that the date value is within the range
if ($dateValue < $offset || $dateValue > $to) {
return false;
}
if ($dateValue > $offset && 0 === $stepSize) {
return false;
}
for ($i = $offset; $i <= $to; $i+= $stepSize) {
if ($i == $dateValue) {
return true;
}
}
return false;
}
/**
* Returns a range of values for the given cron expression
*
* @param string $expression The expression to evaluate
* @param int $max Maximum offset for range
*
* @return array
*/
public function getRangeForExpression($expression, $max)
{
$values = array();
if ($this->isRange($expression) || $this->isIncrementsOfRanges($expression)) {
if (!$this->isIncrementsOfRanges($expression)) {
list ($offset, $to) = explode('-', $expression);
$stepSize = 1;
}
else {
$range = array_map('trim', explode('/', $expression, 2));
$stepSize = isset($range[1]) ? $range[1] : 0;
$range = $range[0];
$range = explode('-', $range, 2);
$offset = $range[0];
$to = isset($range[1]) ? $range[1] : $max;
}
$offset = $offset == '*' ? 0 : $offset;
for ($i = $offset; $i <= $to; $i += $stepSize) {
$values[] = $i;
}
sort($values);
}
else {
$values = array($expression);
}
return $values;
}
}

View File

@ -0,0 +1,104 @@
<?php
namespace schedule\console;
use InvalidArgumentException;
use think\Container;
class CallbackEvent extends Event
{
protected $callback;
protected $parameters;
public function __construct($callback, array $parameters = [])
{
if (! is_string($callback) && ! is_callable($callback) && ! is_array($callback)) {
throw new InvalidArgumentException(
'Invalid scheduled callback event. Must be a string or callable.'
);
}
$this->callback = $callback;
$this->parameters = $parameters;
}
/**
* Run the given event.
*
* @param \think\Container $container
* @return mixed
*
* @throws \Exception
*/
public function run(Container $container)
{
$this->callBeforeCallbacks($container);
try {
if(is_object($this->callback)){
$response = $container->invokeFunction($this->callback, $this->parameters);
// $response = $container->invoke([$this->callback, 'doJob'], $this->parameters);
}else if(is_array($this->callback)){
$response = $container->invoke([$this->callback[0], $this->callback[1] ?? 'doJob'], $this->parameters);
}else{
$response = $container->invokeFunction($this->callback, $this->parameters);
}
// $response = is_object($this->callback)
// ? $container->invoke([$this->callback, 'doJob'], $this->parameters)
// : $container->invokeFunction($this->callback, $this->parameters);
// $container->make($this->callback)->invokeFunction('doJob', $this->parameters);
// $response = $container->invokeFunction($this->callback, $this->parameters);
} finally {
parent::callAfterCallbacks($container);
}
return $response;
}
public function isCallable($var, $syntaxOnly = false)
{
if(is_array($var)){
return true;
}
if (! is_array($var)) {
return is_callable($var, $syntaxOnly);
}
if ((! isset($var[0]) || ! isset($var[1])) ||
! is_string($var[1] ?? null)) {
return false;
}
if ($syntaxOnly &&
(is_string($var[0]) || is_object($var[0])) &&
is_string($var[1])) {
return true;
}
$class = is_object($var[0]) ? get_class($var[0]) : $var[0];
$method = $var[1];
if (! class_exists($class)) {
return false;
}
if (method_exists($class, $method)) {
return (new \ReflectionMethod($class, $method))->isPublic();
}
if (is_object($var[0]) && method_exists($class, '__call')) {
return (new \ReflectionMethod($class, '__call'))->isPublic();
}
if (! is_object($var[0]) && method_exists($class, '__callStatic')) {
return (new \ReflectionMethod($class, '__callStatic'))->isPublic();
}
return false;
}
}

View File

@ -0,0 +1,84 @@
<?php
namespace schedule\console;
use think\Queue;
use think\console\Command as ThinkCommand;
use think\Container;
use think\console\Input;
use think\console\Output;
class Command extends ThinkCommand
{
protected $app;
public $events = [];
public function __construct($name = null)
{
parent::__construct();
$this->app = Container::getInstance();
}
public function call($callback, array $parameters = [])
{
$this->events[] = $event = new CallbackEvent(
$callback, $parameters
);
return $event;
}
public function command($command, array $parameters = [])
{
$this->events[] = $event = new Event(
$command, $parameters
);
return $event;
}
public function job($job, $data, $queue = null)
{
return $this->call(function ($data) use ($job, $queue) {
Queue::push($job, $data, $queue);
}, [ $data ]);
}
/*
$this->command('article:pushed')->dailyAt("21:00");
/*$this->call(function () use ($input, $output){
echo '-------------';
echo 11;
})->twiceDaily(9, 20);
*/
protected function execute(Input $input, Output $output)
{
$eventsRan = false;
foreach ($this->dueEvents($this->events) as $event) {
//foreach ($this->events as $event) {
if (! $event->filtersPass($this->app)) {
continue;
}
$event->run($this->app);
$eventsRan = true;
}
if (! $eventsRan) {
$output->writeln('No scheduled commands are ready to run.');
}
}
public function dueEvents($app)
{
return collect($this->events)->filter(function($event) use ($app){
return $event->isDue($app);
});
}
}

View File

@ -0,0 +1,390 @@
<?php
namespace schedule\console;
use DateTime;
use DateTimeImmutable;
use DateTimeZone;
use Exception;
use InvalidArgumentException;
use RuntimeException;
/**
* CRON expression parser that can determine whether or not a CRON expression is
* due to run, the next run date and previous run date of a CRON expression.
* The determinations made by this class are accurate if checked run once per
* minute (seconds are dropped from date time comparisons).
*
* Schedule parts must map to:
* minute [0-59], hour [0-23], day of month, month [1-12|JAN-DEC], day of week
* [1-7|MON-SUN], and an optional year.
*
* @link http://en.wikipedia.org/wiki/Cron
*/
class CronExpression
{
const MINUTE = 0;
const HOUR = 1;
const DAY = 2;
const MONTH = 3;
const WEEKDAY = 4;
const YEAR = 5;
/**
* @var array CRON expression parts
*/
private $cronParts;
/**
* @var FieldFactory CRON field factory
*/
private $fieldFactory;
/**
* @var int Max iteration count when searching for next run date
*/
private $maxIterationCount = 1000;
/**
* @var array Order in which to test of cron parts
*/
private static $order = array(self::YEAR, self::MONTH, self::DAY, self::WEEKDAY, self::HOUR, self::MINUTE);
/**
* Factory method to create a new CronExpression.
*
* @param string $expression The CRON expression to create. There are
* several special predefined values which can be used to substitute the
* CRON expression:
*
* `@yearly`, `@annually` - Run once a year, midnight, Jan. 1 - 0 0 1 1 *
* `@monthly` - Run once a month, midnight, first of month - 0 0 1 * *
* `@weekly` - Run once a week, midnight on Sun - 0 0 * * 0
* `@daily` - Run once a day, midnight - 0 0 * * *
* `@hourly` - Run once an hour, first minute - 0 * * * *
* @param FieldFactory $fieldFactory Field factory to use
*
* @return CronExpression
*/
public static function factory($expression, FieldFactory $fieldFactory = null)
{
$mappings = array(
'@yearly' => '0 0 1 1 *',
'@annually' => '0 0 1 1 *',
'@monthly' => '0 0 1 * *',
'@weekly' => '0 0 * * 0',
'@daily' => '0 0 * * *',
'@hourly' => '0 * * * *'
);
if (isset($mappings[$expression])) {
$expression = $mappings[$expression];
}
return new static($expression, $fieldFactory ?: new FieldFactory());
}
/**
* Validate a CronExpression.
*
* @param string $expression The CRON expression to validate.
*
* @return bool True if a valid CRON expression was passed. False if not.
* @see \Cron\CronExpression::factory
*/
public static function isValidExpression($expression)
{
try {
self::factory($expression);
} catch (InvalidArgumentException $e) {
return false;
}
return true;
}
/**
* Parse a CRON expression
*
* @param string $expression CRON expression (e.g. '8 * * * *')
* @param FieldFactory $fieldFactory Factory to create cron fields
*/
public function __construct($expression, FieldFactory $fieldFactory)
{
$this->fieldFactory = $fieldFactory;
$this->setExpression($expression);
}
/**
* Set or change the CRON expression
*
* @param string $value CRON expression (e.g. 8 * * * *)
*
* @return CronExpression
* @throws \InvalidArgumentException if not a valid CRON expression
*/
public function setExpression($value)
{
$this->cronParts = preg_split('/\s/', $value, -1, PREG_SPLIT_NO_EMPTY);
if (count($this->cronParts) < 5) {
throw new InvalidArgumentException(
$value . ' is not a valid CRON expression'
);
}
foreach ($this->cronParts as $position => $part) {
$this->setPart($position, $part);
}
return $this;
}
/**
* Set part of the CRON expression
*
* @param int $position The position of the CRON expression to set
* @param string $value The value to set
*
* @return CronExpression
* @throws \InvalidArgumentException if the value is not valid for the part
*/
public function setPart($position, $value)
{
if (!$this->fieldFactory->getField($position)->validate($value)) {
throw new InvalidArgumentException(
'Invalid CRON field value ' . $value . ' at position ' . $position
);
}
$this->cronParts[$position] = $value;
return $this;
}
/**
* Set max iteration count for searching next run dates
*
* @param int $maxIterationCount Max iteration count when searching for next run date
*
* @return CronExpression
*/
public function setMaxIterationCount($maxIterationCount)
{
$this->maxIterationCount = $maxIterationCount;
return $this;
}
/**
* Get a next run date relative to the current date or a specific date
*
* @param string|\DateTime $currentTime Relative calculation date
* @param int $nth Number of matches to skip before returning a
* matching next run date. 0, the default, will return the current
* date and time if the next run date falls on the current date and
* time. Setting this value to 1 will skip the first match and go to
* the second match. Setting this value to 2 will skip the first 2
* matches and so on.
* @param bool $allowCurrentDate Set to TRUE to return the current date if
* it matches the cron expression.
*
* @return \DateTime
* @throws \RuntimeException on too many iterations
*/
public function getNextRunDate($currentTime = 'now', $nth = 0, $allowCurrentDate = false)
{
return $this->getRunDate($currentTime, $nth, false, $allowCurrentDate);
}
/**
* Get a previous run date relative to the current date or a specific date
*
* @param string|\DateTime $currentTime Relative calculation date
* @param int $nth Number of matches to skip before returning
* @param bool $allowCurrentDate Set to TRUE to return the
* current date if it matches the cron expression
*
* @return \DateTime
* @throws \RuntimeException on too many iterations
* @see \Cron\CronExpression::getNextRunDate
*/
public function getPreviousRunDate($currentTime = 'now', $nth = 0, $allowCurrentDate = false)
{
return $this->getRunDate($currentTime, $nth, true, $allowCurrentDate);
}
/**
* Get multiple run dates starting at the current date or a specific date
*
* @param int $total Set the total number of dates to calculate
* @param string|\DateTime $currentTime Relative calculation date
* @param bool $invert Set to TRUE to retrieve previous dates
* @param bool $allowCurrentDate Set to TRUE to return the
* current date if it matches the cron expression
*
* @return array Returns an array of run dates
*/
public function getMultipleRunDates($total, $currentTime = 'now', $invert = false, $allowCurrentDate = false)
{
$matches = array();
for ($i = 0; $i < max(0, $total); $i++) {
try {
$matches[] = $this->getRunDate($currentTime, $i, $invert, $allowCurrentDate);
} catch (RuntimeException $e) {
break;
}
}
return $matches;
}
/**
* Get all or part of the CRON expression
*
* @param string $part Specify the part to retrieve or NULL to get the full
* cron schedule string.
*
* @return string|null Returns the CRON expression, a part of the
* CRON expression, or NULL if the part was specified but not found
*/
public function getExpression($part = null)
{
if (null === $part) {
return implode(' ', $this->cronParts);
} elseif (array_key_exists($part, $this->cronParts)) {
return $this->cronParts[$part];
}
return null;
}
/**
* Helper method to output the full expression.
*
* @return string Full CRON expression
*/
public function __toString()
{
return $this->getExpression();
}
/**
* Determine if the cron is due to run based on the current date or a
* specific date. This method assumes that the current number of
* seconds are irrelevant, and should be called once per minute.
*
* @param string|\DateTime $currentTime Relative calculation date
*
* @return bool Returns TRUE if the cron is due to run or FALSE if not
*/
public function isDue($currentTime = 'now')
{
if ('now' === $currentTime) {
$currentDate = date('Y-m-d H:i');
$currentTime = strtotime($currentDate);
} elseif ($currentTime instanceof DateTime) {
$currentDate = clone $currentTime;
// Ensure time in 'current' timezone is used
$currentDate->setTimezone(new DateTimeZone(date_default_timezone_get()));
$currentDate = $currentDate->format('Y-m-d H:i');
$currentTime = strtotime($currentDate);
} elseif ($currentTime instanceof DateTimeImmutable) {
$currentDate = DateTime::createFromFormat('U', $currentTime->format('U'));
$currentDate->setTimezone(new DateTimeZone(date_default_timezone_get()));
$currentDate = $currentDate->format('Y-m-d H:i');
$currentTime = strtotime($currentDate);
} else {
$currentTime = new DateTime($currentTime);
$currentTime->setTime($currentTime->format('H'), $currentTime->format('i'), 0);
$currentDate = $currentTime->format('Y-m-d H:i');
$currentTime = $currentTime->getTimeStamp();
}
try {
return $this->getNextRunDate($currentDate, 0, true)->getTimestamp() == $currentTime;
} catch (Exception $e) {
return false;
}
}
/**
* Get the next or previous run date of the expression relative to a date
*
* @param string|\DateTime $currentTime Relative calculation date
* @param int $nth Number of matches to skip before returning
* @param bool $invert Set to TRUE to go backwards in time
* @param bool $allowCurrentDate Set to TRUE to return the
* current date if it matches the cron expression
*
* @return \DateTime
* @throws \RuntimeException on too many iterations
*/
protected function getRunDate($currentTime = null, $nth = 0, $invert = false, $allowCurrentDate = false)
{
if ($currentTime instanceof DateTime) {
$currentDate = clone $currentTime;
} elseif ($currentTime instanceof DateTimeImmutable) {
$currentDate = DateTime::createFromFormat('U', $currentTime->format('U'));
$currentDate->setTimezone($currentTime->getTimezone());
} else {
$currentDate = new DateTime($currentTime ?: 'now');
$currentDate->setTimezone(new DateTimeZone(date_default_timezone_get()));
}
$currentDate->setTime($currentDate->format('H'), $currentDate->format('i'), 0);
$nextRun = clone $currentDate;
$nth = (int) $nth;
// We don't have to satisfy * or null fields
$parts = array();
$fields = array();
foreach (self::$order as $position) {
$part = $this->getExpression($position);
if (null === $part || '*' === $part) {
continue;
}
$parts[$position] = $part;
$fields[$position] = $this->fieldFactory->getField($position);
}
// Set a hard limit to bail on an impossible date
for ($i = 0; $i < $this->maxIterationCount; $i++) {
foreach ($parts as $position => $part) {
$satisfied = false;
// Get the field object used to validate this part
$field = $fields[$position];
// Check if this is singular or a list
if (strpos($part, ',') === false) {
$satisfied = $field->isSatisfiedBy($nextRun, $part);
} else {
foreach (array_map('trim', explode(',', $part)) as $listPart) {
if ($field->isSatisfiedBy($nextRun, $listPart)) {
$satisfied = true;
break;
}
}
}
// If the field is not satisfied, then start over
if (!$satisfied) {
$field->increment($nextRun, $invert, $part);
continue 2;
}
}
// Skip this match if needed
if ((!$allowCurrentDate && $nextRun == $currentDate) || --$nth > -1) {
$this->fieldFactory->getField(0)->increment($nextRun, $invert, isset($parts[0]) ? $parts[0] : null);
continue;
}
return $nextRun;
}
// @codeCoverageIgnoreStart
throw new RuntimeException('Impossible CRON expression');
// @codeCoverageIgnoreEnd
}
}

View File

@ -0,0 +1,173 @@
<?php
namespace schedule\console;
use DateTime;
/**
* Day of month field. Allows: * , / - ? L W
*
* 'L' stands for "last" and specifies the last day of the month.
*
* The 'W' character is used to specify the weekday (Monday-Friday) nearest the
* given day. As an example, if you were to specify "15W" as the value for the
* day-of-month field, the meaning is: "the nearest weekday to the 15th of the
* month". So if the 15th is a Saturday, the trigger will fire on Friday the
* 14th. If the 15th is a Sunday, the trigger will fire on Monday the 16th. If
* the 15th is a Tuesday, then it will fire on Tuesday the 15th. However if you
* specify "1W" as the value for day-of-month, and the 1st is a Saturday, the
* trigger will fire on Monday the 3rd, as it will not 'jump' over the boundary
* of a month's days. The 'W' character can only be specified when the
* day-of-month is a single day, not a range or list of days.
*
* @author Michael Dowling <mtdowling@gmail.com>
*/
class DayOfMonthField extends AbstractField
{
/**
* Get the nearest day of the week for a given day in a month
*
* @param int $currentYear Current year
* @param int $currentMonth Current month
* @param int $targetDay Target day of the month
*
* @return \DateTime Returns the nearest date
*/
private static function getNearestWeekday($currentYear, $currentMonth, $targetDay)
{
$tday = str_pad($targetDay, 2, '0', STR_PAD_LEFT);
$target = DateTime::createFromFormat('Y-m-d', "$currentYear-$currentMonth-$tday");
$currentWeekday = (int) $target->format('N');
if ($currentWeekday < 6) {
return $target;
}
$lastDayOfMonth = $target->format('t');
foreach (array(-1, 1, -2, 2) as $i) {
$adjusted = $targetDay + $i;
if ($adjusted > 0 && $adjusted <= $lastDayOfMonth) {
$target->setDate($currentYear, $currentMonth, $adjusted);
if ($target->format('N') < 6 && $target->format('m') == $currentMonth) {
return $target;
}
}
}
}
public function isSatisfiedBy(DateTime $date, $value)
{
// ? states that the field value is to be skipped
if ($value == '?') {
return true;
}
$fieldValue = $date->format('d');
// Check to see if this is the last day of the month
if ($value == 'L') {
return $fieldValue == $date->format('t');
}
// Check to see if this is the nearest weekday to a particular value
if (strpos($value, 'W')) {
// Parse the target day
$targetDay = substr($value, 0, strpos($value, 'W'));
// Find out if the current day is the nearest day of the week
return $date->format('j') == self::getNearestWeekday(
$date->format('Y'),
$date->format('m'),
$targetDay
)->format('j');
}
return $this->isSatisfied($date->format('d'), $value);
}
public function increment(DateTime $date, $invert = false)
{
if ($invert) {
$date->modify('previous day');
$date->setTime(23, 59);
} else {
$date->modify('next day');
$date->setTime(0, 0);
}
return $this;
}
/**
* Validates that the value is valid for the Day of the Month field
* Days of the month can contain values of 1-31, *, L, or ? by default. This can be augmented with lists via a ',',
* ranges via a '-', or with a '[0-9]W' to specify the closest weekday.
*
* @param string $value
* @return bool
*/
public function validate($value)
{
// Allow wildcards and a single L
if ($value === '?' || $value === '*' || $value === 'L') {
return true;
}
// If you only contain numbers and are within 1-31
if ((bool) preg_match('/^\d{1,2}$/', $value) && ($value >= 1 && $value <= 31)) {
return true;
}
// If you have a -, we will deal with each of your chunks
if ((bool) preg_match('/-/', $value)) {
// We cannot have a range within a list or vice versa
if ((bool) preg_match('/,/', $value)) {
return false;
}
$chunks = explode('-', $value);
foreach ($chunks as $chunk) {
if (!$this->validate($chunk)) {
return false;
}
}
return true;
}
// If you have a comma, we will deal with each value
if ((bool) preg_match('/,/', $value)) {
// We cannot have a range within a list or vice versa
if ((bool) preg_match('/-/', $value)) {
return false;
}
$chunks = explode(',', $value);
foreach ($chunks as $chunk) {
if (!$this->validate($chunk)) {
return false;
}
}
return true;
}
// If you contain a /, we'll deal with it
if ((bool) preg_match('/\//', $value)) {
$chunks = explode('/', $value);
foreach ($chunks as $chunk) {
if (!$this->validate($chunk)) {
return false;
}
}
return true;
}
// If you end in W, make sure that it has a numeric in front of it
if ((bool) preg_match('/^\d{1,2}W$/', $value)) {
return true;
}
return false;
}
}

View File

@ -0,0 +1,141 @@
<?php
namespace schedule\console;
use DateTime;
use InvalidArgumentException;
/**
* Day of week field. Allows: * / , - ? L #
*
* Days of the week can be represented as a number 0-7 (0|7 = Sunday)
* or as a three letter string: SUN, MON, TUE, WED, THU, FRI, SAT.
*
* 'L' stands for "last". It allows you to specify constructs such as
* "the last Friday" of a given month.
*
* '#' is allowed for the day-of-week field, and must be followed by a
* number between one and five. It allows you to specify constructs such as
* "the second Friday" of a given month.
*/
class DayOfWeekField extends AbstractField
{
public function isSatisfiedBy(DateTime $date, $value)
{
if ($value == '?') {
return true;
}
// Convert text day of the week values to integers
$value = $this->convertLiterals($value);
$currentYear = $date->format('Y');
$currentMonth = $date->format('m');
$lastDayOfMonth = $date->format('t');
// Find out if this is the last specific weekday of the month
if (strpos($value, 'L')) {
$weekday = str_replace('7', '0', substr($value, 0, strpos($value, 'L')));
$tdate = clone $date;
$tdate->setDate($currentYear, $currentMonth, $lastDayOfMonth);
while ($tdate->format('w') != $weekday) {
$tdateClone = new DateTime();
$tdate = $tdateClone
->setTimezone($tdate->getTimezone())
->setDate($currentYear, $currentMonth, --$lastDayOfMonth);
}
return $date->format('j') == $lastDayOfMonth;
}
// Handle # hash tokens
if (strpos($value, '#')) {
list($weekday, $nth) = explode('#', $value);
// 0 and 7 are both Sunday, however 7 matches date('N') format ISO-8601
if ($weekday === '0') {
$weekday = 7;
}
// Validate the hash fields
if ($weekday < 0 || $weekday > 7) {
throw new InvalidArgumentException("Weekday must be a value between 0 and 7. {$weekday} given");
}
if ($nth > 5) {
throw new InvalidArgumentException('There are never more than 5 of a given weekday in a month');
}
// The current weekday must match the targeted weekday to proceed
if ($date->format('N') != $weekday) {
return false;
}
$tdate = clone $date;
$tdate->setDate($currentYear, $currentMonth, 1);
$dayCount = 0;
$currentDay = 1;
while ($currentDay < $lastDayOfMonth + 1) {
if ($tdate->format('N') == $weekday) {
if (++$dayCount >= $nth) {
break;
}
}
$tdate->setDate($currentYear, $currentMonth, ++$currentDay);
}
return $date->format('j') == $currentDay;
}
// Handle day of the week values
if (strpos($value, '-')) {
$parts = explode('-', $value);
if ($parts[0] == '7') {
$parts[0] = '0';
} elseif ($parts[1] == '0') {
$parts[1] = '7';
}
$value = implode('-', $parts);
}
// Test to see which Sunday to use -- 0 == 7 == Sunday
$format = in_array(7, str_split($value)) ? 'N' : 'w';
$fieldValue = $date->format($format);
return $this->isSatisfied($fieldValue, $value);
}
public function increment(DateTime $date, $invert = false)
{
if ($invert) {
$date->modify('-1 day');
$date->setTime(23, 59, 0);
} else {
$date->modify('+1 day');
$date->setTime(0, 0, 0);
}
return $this;
}
public function validate($value)
{
$value = $this->convertLiterals($value);
foreach (explode(',', $value) as $expr) {
if (!preg_match('/^(\*|[0-7](L?|#[1-5]))([\/\,\-][0-7]+)*$/', $expr)) {
return false;
}
}
return true;
}
private function convertLiterals($string)
{
return str_ireplace(
array('SUN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT'),
range(0, 6),
$string
);
}
}

View File

@ -0,0 +1,123 @@
<?php
namespace schedule\console;
use Carbon\Carbon;
use think\Container;
class Event
{
use ManagesFrequencies;
public $command;
protected $parameters;
public $timezone;
public $expression = '* * * * * *';
protected $filters = [];
protected $beforeCallbacks = [];
protected $afterCallbacks = [];
public function __construct($command, array $parameters = [])
{
$this->command = $command;
$this->parameters = $parameters;
}
public function run(Container $container)
{
$this->callBeforeCallbacks($container);
if (strpos(\think\App::VERSION, '6.0') !== false) {
\think\facade\Console::call($this->command, $this->parameters, 'console');
}else{
\think\Console::call($this->command, $this->parameters, 'console');
}
$this->callAfterCallbacks($container);
}
public function filtersPass($app)
{
foreach ($this->filters as $callback) {
if (! $app->call($callback)) {
return false;
}
}
return true;
}
public function isDue($app)
{
return $this->expressionPasses();
}
public function expressionPasses()
{
$date = Carbon::now();
if ($this->timezone) {
$date->setTimezone($this->timezone);
}
return CronExpression::factory($this->expression)->isDue($date->toDateTimeString());
}
public function when($callback)
{
$this->filters[] = is_callable($callback) ? $callback : function () use ($callback) {
return $callback;
};
return $this;
}
public function before(Closure $callback)
{
$this->beforeCallbacks[] = $callback;
return $this;
}
public function after(Closure $callback)
{
return $this->then($callback);
}
public function then(Closure $callback)
{
$this->afterCallbacks[] = $callback;
return $this;
}
public function callBeforeCallbacks(Container $container)
{
foreach ($this->beforeCallbacks as $callback) {
$container->invokeFunction($callback);
}
}
public function callAfterCallbacks(Container $container)
{
foreach ($this->afterCallbacks as $callback) {
$container->invokeFunction($callback);
}
}
public function timezone($timezone)
{
$this->timezone = $timezone;
return $this;
}
}

View File

@ -0,0 +1,58 @@
<?php
namespace schedule\console;
use InvalidArgumentException;
/**
* CRON field factory implementing a flyweight factory
* @link http://en.wikipedia.org/wiki/Cron
*/
class FieldFactory
{
/**
* @var array Cache of instantiated fields
*/
private $fields = array();
/**
* Get an instance of a field object for a cron expression position
*
* @param int $position CRON expression position value to retrieve
*
* @return FieldInterface
* @throws InvalidArgumentException if a position is not valid
*/
public function getField($position)
{
if (!isset($this->fields[$position])) {
switch ($position) {
case 0:
$this->fields[$position] = new MinutesField();
break;
case 1:
$this->fields[$position] = new HoursField();
break;
case 2:
$this->fields[$position] = new DayOfMonthField();
break;
case 3:
$this->fields[$position] = new MonthField();
break;
case 4:
$this->fields[$position] = new DayOfWeekField();
break;
case 5:
$this->fields[$position] = new YearField();
break;
default:
throw new InvalidArgumentException(
$position . ' is not a valid position'
);
}
}
return $this->fields[$position];
}
}

View File

@ -0,0 +1,41 @@
<?php
namespace schedule\console;
use DateTime;
/**
* CRON field interface
*/
interface FieldInterface
{
/**
* Check if the respective value of a DateTime field satisfies a CRON exp
*
* @param DateTime $date DateTime object to check
* @param string $value CRON expression to test against
*
* @return bool Returns TRUE if satisfied, FALSE otherwise
*/
public function isSatisfiedBy(DateTime $date, $value);
/**
* When a CRON expression is not satisfied, this method is used to increment
* or decrement a DateTime object by the unit of the cron field
*
* @param DateTime $date DateTime object to change
* @param bool $invert (optional) Set to TRUE to decrement
*
* @return FieldInterface
*/
public function increment(DateTime $date, $invert = false);
/**
* Validates a CRON expression for a given field
*
* @param string $value CRON expression value to validate
*
* @return bool Returns TRUE if valid, FALSE otherwise
*/
public function validate($value);
}

View File

@ -0,0 +1,71 @@
<?php
namespace schedule\console;
use DateTime;
use DateTimeZone;
/**
* Hours field. Allows: * , / -
*/
class HoursField extends AbstractField
{
public function isSatisfiedBy(DateTime $date, $value)
{
return $this->isSatisfied($date->format('H'), $value);
}
public function increment(DateTime $date, $invert = false, $parts = null)
{
// Change timezone to UTC temporarily. This will
// allow us to go back or forwards and hour even
// if DST will be changed between the hours.
if (is_null($parts) || $parts == '*') {
$timezone = $date->getTimezone();
$date->setTimezone(new DateTimeZone('UTC'));
if ($invert) {
$date->modify('-1 hour');
} else {
$date->modify('+1 hour');
}
$date->setTimezone($timezone);
$date->setTime($date->format('H'), $invert ? 59 : 0);
return $this;
}
$parts = strpos($parts, ',') !== false ? explode(',', $parts) : array($parts);
$hours = array();
foreach ($parts as $part) {
$hours = array_merge($hours, $this->getRangeForExpression($part, 23));
}
$current_hour = $date->format('H');
$position = $invert ? count($hours) - 1 : 0;
if (count($hours) > 1) {
for ($i = 0; $i < count($hours) - 1; $i++) {
if ((!$invert && $current_hour >= $hours[$i] && $current_hour < $hours[$i + 1]) ||
($invert && $current_hour > $hours[$i] && $current_hour <= $hours[$i + 1])) {
$position = $invert ? $i : $i + 1;
break;
}
}
}
$hour = $hours[$position];
if ((!$invert && $date->format('H') >= $hour) || ($invert && $date->format('H') <= $hour)) {
$date->modify(($invert ? '-' : '+') . '1 day');
$date->setTime($invert ? 23 : 0, $invert ? 59 : 0);
}
else {
$date->setTime($hour, $invert ? 59 : 0);
}
return $this;
}
public function validate($value)
{
return (bool) preg_match('/^[\*,\/\-0-9]+$/', $value);
}
}

View File

@ -1,18 +1,18 @@
<?php <?php
namespace yunwuxin\cron; namespace schedule\console;
use Carbon\Carbon; use Carbon\Carbon;
trait ManagesFrequencies trait ManagesFrequencies
{ {
/** /**
* 设置任务执行周期 * The Cron expression representing the event's frequency.
* *
* @param string $expression * @param string $expression
* @return $this * @return $this
*/ */
public function expression($expression) public function cron($expression)
{ {
$this->expression = $expression; $this->expression = $expression;
@ -20,10 +20,10 @@ trait ManagesFrequencies
} }
/** /**
* 设置区间时间 * Schedule the event to run between start and end time.
* *
* @param string $startTime * @param string $startTime
* @param string $endTime * @param string $endTime
* @return $this * @return $this
*/ */
public function between($startTime, $endTime) public function between($startTime, $endTime)
@ -32,10 +32,10 @@ trait ManagesFrequencies
} }
/** /**
* 排除区间时间 * Schedule the event to not run between start and end time.
* *
* @param string $startTime * @param string $startTime
* @param string $endTime * @param string $endTime
* @return $this * @return $this
*/ */
public function unlessBetween($startTime, $endTime) public function unlessBetween($startTime, $endTime)
@ -43,6 +43,13 @@ trait ManagesFrequencies
return $this->skip($this->inTimeInterval($startTime, $endTime)); return $this->skip($this->inTimeInterval($startTime, $endTime));
} }
/**
* Schedule the event to run between start and end time.
*
* @param string $startTime
* @param string $endTime
* @return \Closure
*/
private function inTimeInterval($startTime, $endTime) private function inTimeInterval($startTime, $endTime)
{ {
return function () use ($startTime, $endTime) { return function () use ($startTime, $endTime) {
@ -55,7 +62,57 @@ trait ManagesFrequencies
} }
/** /**
* 按小时执行 * Schedule the event to run every minute.
*
* @return $this
*/
public function everyMinute()
{
return $this->spliceIntoPosition(1, '*');
}
/**
* Schedule the event to run every five minutes.
*
* @return $this
*/
public function everyFiveMinutes()
{
return $this->spliceIntoPosition(1, '*/5');
}
/**
* Schedule the event to run every ten minutes.
*
* @return $this
*/
public function everyTenMinutes()
{
return $this->spliceIntoPosition(1, '*/10');
}
/**
* Schedule the event to run every fifteen minutes.
*
* @return $this
*/
public function everyFifteenMinutes()
{
return $this->spliceIntoPosition(1, '*/15');
}
/**
* Schedule the event to run every thirty minutes.
*
* @return $this
*/
public function everyThirtyMinutes()
{
return $this->spliceIntoPosition(1, '0,30');
}
/**
* Schedule the event to run hourly.
* *
* @return $this * @return $this
*/ */
@ -65,9 +122,9 @@ trait ManagesFrequencies
} }
/** /**
* 按小时延期执行 * Schedule the event to run hourly at a given offset in the hour.
* *
* @param int $offset * @param int $offset
* @return $this * @return $this
*/ */
public function hourlyAt($offset) public function hourlyAt($offset)
@ -76,20 +133,20 @@ trait ManagesFrequencies
} }
/** /**
* 按天执行 * Schedule the event to run daily.
* *
* @return $this * @return $this
*/ */
public function daily() public function daily()
{ {
return $this->spliceIntoPosition(1, 0) return $this->spliceIntoPosition(1, 0)
->spliceIntoPosition(2, 0); ->spliceIntoPosition(2, 0);
} }
/** /**
* 指定时间执行 * Schedule the command at a given time.
* *
* @param string $time * @param string $time
* @return $this * @return $this
*/ */
public function at($time) public function at($time)
@ -98,9 +155,9 @@ trait ManagesFrequencies
} }
/** /**
* 指定时间执行 * Schedule the event to run daily at a given time (10:00, 19:30, etc).
* *
* @param string $time * @param string $time
* @return $this * @return $this
*/ */
public function dailyAt($time) public function dailyAt($time)
@ -108,26 +165,26 @@ trait ManagesFrequencies
$segments = explode(':', $time); $segments = explode(':', $time);
return $this->spliceIntoPosition(2, (int) $segments[0]) return $this->spliceIntoPosition(2, (int) $segments[0])
->spliceIntoPosition(1, count($segments) == 2 ? (int) $segments[1] : '0'); ->spliceIntoPosition(1, count($segments) == 2 ? (int) $segments[1] : '0');
} }
/** /**
* 每天执行两次 * Schedule the event to run twice daily.
* *
* @param int $first * @param int $first
* @param int $second * @param int $second
* @return $this * @return $this
*/ */
public function twiceDaily($first = 1, $second = 13) public function twiceDaily($first = 1, $second = 13)
{ {
$hours = $first . ',' . $second; $hours = $first.','.$second;
return $this->spliceIntoPosition(1, 0) return $this->spliceIntoPosition(1, 0)
->spliceIntoPosition(2, $hours); ->spliceIntoPosition(2, $hours);
} }
/** /**
* 工作日执行 * Schedule the event to run only on weekdays.
* *
* @return $this * @return $this
*/ */
@ -137,7 +194,7 @@ trait ManagesFrequencies
} }
/** /**
* 周末执行 * Schedule the event to run only on weekends.
* *
* @return $this * @return $this
*/ */
@ -147,7 +204,7 @@ trait ManagesFrequencies
} }
/** /**
* 星期一执行 * Schedule the event to run only on Mondays.
* *
* @return $this * @return $this
*/ */
@ -157,7 +214,7 @@ trait ManagesFrequencies
} }
/** /**
* 星期二执行 * Schedule the event to run only on Tuesdays.
* *
* @return $this * @return $this
*/ */
@ -167,7 +224,7 @@ trait ManagesFrequencies
} }
/** /**
* 星期三执行 * Schedule the event to run only on Wednesdays.
* *
* @return $this * @return $this
*/ */
@ -177,7 +234,7 @@ trait ManagesFrequencies
} }
/** /**
* 星期四执行 * Schedule the event to run only on Thursdays.
* *
* @return $this * @return $this
*/ */
@ -187,7 +244,7 @@ trait ManagesFrequencies
} }
/** /**
* 星期五执行 * Schedule the event to run only on Fridays.
* *
* @return $this * @return $this
*/ */
@ -197,7 +254,7 @@ trait ManagesFrequencies
} }
/** /**
* 星期六执行 * Schedule the event to run only on Saturdays.
* *
* @return $this * @return $this
*/ */
@ -207,7 +264,7 @@ trait ManagesFrequencies
} }
/** /**
* 星期天执行 * Schedule the event to run only on Sundays.
* *
* @return $this * @return $this
*/ */
@ -217,22 +274,22 @@ trait ManagesFrequencies
} }
/** /**
* 按周执行 * Schedule the event to run weekly.
* *
* @return $this * @return $this
*/ */
public function weekly() public function weekly()
{ {
return $this->spliceIntoPosition(1, 0) return $this->spliceIntoPosition(1, 0)
->spliceIntoPosition(2, 0) ->spliceIntoPosition(2, 0)
->spliceIntoPosition(5, 0); ->spliceIntoPosition(5, 0);
} }
/** /**
* 指定每周的时间执行 * Schedule the event to run weekly on a given day and time.
* *
* @param int $day * @param int $day
* @param string $time * @param string $time
* @return $this * @return $this
*/ */
public function weeklyOn($day, $time = '0:0') public function weeklyOn($day, $time = '0:0')
@ -243,22 +300,22 @@ trait ManagesFrequencies
} }
/** /**
* 按月执行 * Schedule the event to run monthly.
* *
* @return $this * @return $this
*/ */
public function monthly() public function monthly()
{ {
return $this->spliceIntoPosition(1, 0) return $this->spliceIntoPosition(1, 0)
->spliceIntoPosition(2, 0) ->spliceIntoPosition(2, 0)
->spliceIntoPosition(3, 1); ->spliceIntoPosition(3, 1);
} }
/** /**
* 指定每月的执行时间 * Schedule the event to run monthly on a given day and time.
* *
* @param int $day * @param int $day
* @param string $time * @param string $time
* @return $this * @return $this
*/ */
public function monthlyOn($day = 1, $time = '0:0') public function monthlyOn($day = 1, $time = '0:0')
@ -269,15 +326,15 @@ trait ManagesFrequencies
} }
/** /**
* 每月执行两次 * Schedule the event to run twice monthly.
* *
* @param int $first * @param int $first
* @param int $second * @param int $second
* @return $this * @return $this
*/ */
public function twiceMonthly($first = 1, $second = 16) public function twiceMonthly($first = 1, $second = 16)
{ {
$days = $first . ',' . $second; $days = $first.','.$second;
return $this->spliceIntoPosition(1, 0) return $this->spliceIntoPosition(1, 0)
->spliceIntoPosition(2, 0) ->spliceIntoPosition(2, 0)
@ -285,75 +342,35 @@ trait ManagesFrequencies
} }
/** /**
* 按季度执行 * Schedule the event to run quarterly.
* *
* @return $this * @return $this
*/ */
public function quarterly() public function quarterly()
{ {
return $this->spliceIntoPosition(1, 0) return $this->spliceIntoPosition(1, 0)
->spliceIntoPosition(2, 0) ->spliceIntoPosition(2, 0)
->spliceIntoPosition(3, 1) ->spliceIntoPosition(3, 1)
->spliceIntoPosition(4, '*/3'); ->spliceIntoPosition(4, '1-12/3');
} }
/** /**
* 按年执行 * Schedule the event to run yearly.
* *
* @return $this * @return $this
*/ */
public function yearly() public function yearly()
{ {
return $this->spliceIntoPosition(1, 0) return $this->spliceIntoPosition(1, 0)
->spliceIntoPosition(2, 0) ->spliceIntoPosition(2, 0)
->spliceIntoPosition(3, 1) ->spliceIntoPosition(3, 1)
->spliceIntoPosition(4, 1); ->spliceIntoPosition(4, 1);
} }
/** /**
* 每分钟执行 * Set the days of the week the command should run on.
* *
* @return $this * @param array|mixed $days
*/
public function everyMinute()
{
return $this->spliceIntoPosition(1, '*');
}
/**
* 每5分钟执行
*
* @return $this
*/
public function everyFiveMinutes()
{
return $this->spliceIntoPosition(1, '*/5');
}
/**
* 每10分钟执行
*
* @return $this
*/
public function everyTenMinutes()
{
return $this->spliceIntoPosition(1, '*/10');
}
/**
* 每30分钟执行
*
* @return $this
*/
public function everyThirtyMinutes()
{
return $this->spliceIntoPosition(1, '0,30');
}
/**
* 按周设置天执行
*
* @param array|mixed $days
* @return $this * @return $this
*/ */
public function days($days) public function days($days)
@ -364,9 +381,9 @@ trait ManagesFrequencies
} }
/** /**
* 设置时区 * Set the timezone the date should be evaluated on.
* *
* @param string $timezone * @param \DateTimeZone|string $timezone
* @return $this * @return $this
*/ */
public function timezone($timezone) public function timezone($timezone)
@ -376,12 +393,19 @@ trait ManagesFrequencies
return $this; return $this;
} }
/**
* Splice the given value into the given position of the expression.
*
* @param int $position
* @param string $value
* @return $this
*/
protected function spliceIntoPosition($position, $value) protected function spliceIntoPosition($position, $value)
{ {
$segments = explode(' ', $this->expression); $segments = explode(' ', $this->expression);
$segments[$position - 1] = $value; $segments[$position - 1] = $value;
return $this->expression(implode(' ', $segments)); return $this->cron(implode(' ', $segments));
} }
} }

View File

@ -0,0 +1,62 @@
<?php
namespace schedule\console;
use DateTime;
/**
* Minutes field. Allows: * , / -
*/
class MinutesField extends AbstractField
{
public function isSatisfiedBy(DateTime $date, $value)
{
return $this->isSatisfied($date->format('i'), $value);
}
public function increment(DateTime $date, $invert = false, $parts = null)
{
if (is_null($parts)) {
if ($invert) {
$date->modify('-1 minute');
} else {
$date->modify('+1 minute');
}
return $this;
}
$parts = strpos($parts, ',') !== false ? explode(',', $parts) : array($parts);
$minutes = array();
foreach ($parts as $part) {
$minutes = array_merge($minutes, $this->getRangeForExpression($part, 59));
}
$current_minute = $date->format('i');
$position = $invert ? count($minutes) - 1 : 0;
if (count($minutes) > 1) {
for ($i = 0; $i < count($minutes) - 1; $i++) {
if ((!$invert && $current_minute >= $minutes[$i] && $current_minute < $minutes[$i + 1]) ||
($invert && $current_minute > $minutes[$i] && $current_minute <= $minutes[$i + 1])) {
$position = $invert ? $i : $i + 1;
break;
}
}
}
if ((!$invert && $current_minute >= $minutes[$position]) || ($invert && $current_minute <= $minutes[$position])) {
$date->modify(($invert ? '-' : '+') . '1 hour');
$date->setTime($date->format('H'), $invert ? 59 : 0);
}
else {
$date->setTime($date->format('H'), $minutes[$position]);
}
return $this;
}
public function validate($value)
{
return (bool) preg_match('/^[\*,\/\-0-9]+$/', $value);
}
}

View File

@ -0,0 +1,44 @@
<?php
namespace schedule\console;
use DateTime;
/**
* Month field. Allows: * , / -
*/
class MonthField extends AbstractField
{
public function isSatisfiedBy(DateTime $date, $value)
{
// Convert text month values to integers
$value = str_ireplace(
array(
'JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN',
'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC'
),
range(1, 12),
$value
);
return $this->isSatisfied($date->format('m'), $value);
}
public function increment(DateTime $date, $invert = false)
{
if ($invert) {
$date->modify('last day of previous month');
$date->setTime(23, 59);
} else {
$date->modify('first day of next month');
$date->setTime(0, 0);
}
return $this;
}
public function validate($value)
{
return (bool) preg_match('/^[\*,\/\-0-9A-Z]+$/', $value);
}
}

View File

@ -0,0 +1,37 @@
<?php
namespace schedule\console;
use DateTime;
/**
* Year field. Allows: * , / -
*/
class YearField extends AbstractField
{
public function isSatisfiedBy(DateTime $date, $value)
{
return $this->isSatisfied($date->format('Y'), $value);
}
public function increment(DateTime $date, $invert = false)
{
if ($invert) {
$date->modify('-1 year');
$date->setDate($date->format('Y'), 12, 31);
$date->setTime(23, 59, 0);
} else {
$date->modify('+1 year');
$date->setDate($date->format('Y'), 1, 1);
$date->setTime(0, 0, 0);
}
return $this;
}
public function validate($value)
{
return (bool) preg_match('/^[\*,\/\-0-9]+$/', $value);
}
}

View File

@ -0,0 +1,8 @@
<?php
namespace schedule\exceptions;
class Exception extends \think\Exception
{
}

View File

@ -0,0 +1,10 @@
<?php
namespace schedule\exceptions;
/**
* Class InvalidArgumentException.
*/
class InvalidArgumentException extends Exception
{
}