Skip to content

Commit c7bf830

Browse files
committed
更新 README。
1 parent 679a606 commit c7bf830

File tree

3 files changed

+84
-106
lines changed

3 files changed

+84
-106
lines changed

README.md

Lines changed: 58 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# PDPHP\Async (PHP Library)
1+
# PD\Async
22

33
> PDPHP\Async is a PHP library using ReactPHP to perform asynchronous tasks, supporting task dependency handling and topological sorting.
44
@@ -7,87 +7,73 @@
77
- Handling of Task Dependencies
88
- Ensuring correct execution order via Topological Sorting
99

10-
## 創建者 / Creator
10+
## How to Use
1111

12-
<img src="https://pardn.io/image/head-s.jpg" align="left" style="float: left; margin-right: 0.5rem; width: 96px; height: 96px;" />
12+
### Install
1313

14-
<h4 style="padding-top: 0">邱敬幃 Pardn Chiu</h4>
14+
```SHELL
15+
composer require pardnchiu/async
16+
```
17+
18+
### Use
19+
20+
```PHP
21+
<?php
1522

16-
[![](https://pardn.io/image/mail.svg)](mailto:mail@pardn.ltd) [![](https://skillicons.dev/icons?i=linkedin)](https://linkedin.com/in/pardnchiu)
23+
use PDPHP\Async;
24+
25+
$tasks = [
26+
'task1' => [
27+
'method' => function () {
28+
return 'result1';
29+
},
30+
'tasks' => [],
31+
],
32+
'task2' => [
33+
'method' => function () {
34+
return 'result2';
35+
},
36+
'tasks' => ['task1'], // Run after task1
37+
],
38+
'task3' => [
39+
'method' => function () {
40+
return 'result3';
41+
},
42+
'tasks' => ['task1'], // Run after task1
43+
],
44+
'task4' => [
45+
'method' => function () {
46+
return 'result3';
47+
},
48+
'tasks' => ['task2'], // Run after task2
49+
],
50+
];
51+
52+
Async::run($tasks)
53+
->then(function ($results) {
54+
print_r($results);
55+
})
56+
->catch(function ($error) {
57+
echo 'Error: ' . $error->getMessage();
58+
});
59+
```
1760

1861
## License
1962

20-
This source code project is licensed under the [GPL-3.0](https://github.com/pardnchiu/PDMarkdownKit/blob/main/LICENSE) license.
63+
This source code project is licensed under the [MIT](https://github.com/pardnchiu/PHPAsync/blob/main/LICENSE) license.
2164

22-
## How to Use
65+
## Creator
2366

24-
- PHP version 7.4 or later
25-
- Download `Async.php` and place it in the directory `src/PDPHP`
26-
- Add dependencies in `composer.json`
27-
```JSON
28-
{
29-
"require": {
30-
"react/promise": "^2.8 || ^3.0",
31-
"react/event-loop": "^1.1 || ^2.0"
32-
},
33-
"autoload": {
34-
"psr-4": {
35-
"PD\\": "src/PDPHP/"
36-
}
37-
}
38-
}
39-
```
40-
- Install dependencies
41-
```SHELL
42-
composer install
43-
```
44-
- Example
45-
```PHP
46-
<?php
47-
48-
require 'vendor/autoload.php';
49-
50-
use PDPHP\Async;
51-
52-
$tasks = [
53-
'task1' => [
54-
'method' => function () {
55-
return 'result1';
56-
},
57-
'tasks' => [],
58-
],
59-
'task2' => [
60-
'method' => function () {
61-
return 'result2';
62-
},
63-
'tasks' => ['task1'], // Run after task1
64-
],
65-
'task3' => [
66-
'method' => function () {
67-
return 'result3';
68-
},
69-
'tasks' => ['task1'], // Run after task1
70-
],
71-
'task4' => [
72-
'method' => function () {
73-
return 'result3';
74-
},
75-
'tasks' => ['task2'], // Run after task2
76-
],
77-
];
78-
79-
Async::run($tasks)
80-
->then(function ($results) {
81-
print_r($results);
82-
})->catch(function ($error) {
83-
echo 'Error: ' . $error->getMessage();
84-
});
85-
```
67+
<img src="https://avatars.githubusercontent.com/u/25631760" align="left" width="96" height="96" style="margin-right: 0.5rem;">
8668

87-
***
69+
<h4 style="padding-top: 0">邱敬幃 Pardn Chiu</h4>
8870

89-
*All translations powered by ChatGPT*
71+
<a href="mailto:dev@pardn.io" target="_blank">
72+
<img src="https://pardn.io/image/email.svg" width="48" height="48">
73+
</a> <a href="https://linkedin.com/in/pardnchiu" target="_blank">
74+
<img src="https://pardn.io/image/linkedin.svg" width="48" height="48">
75+
</a>
9076

9177
***
9278

93-
©️ 2024 [邱敬幃 Pardn Chiu](https://www.linkedin.com/in/pardnchiu)
79+
©️ 2024 [邱敬幃 Pardn Chiu](https://pardn.io)

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
],
1212
"minimum-stability": "stable",
1313
"require": {
14-
"php": ">=8.0",
14+
"php": ">=7.4",
1515
"react/promise": "^2.8 || ^3.0",
1616
"react/event-loop": "^1.1 || ^2.0"
1717
},

src/Async.php

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,20 @@ public static function run($tasks)
1313
// 獲取事件循環實例
1414
$loop = Loop::get();
1515

16-
// 初始化任務流程和方法數組
1716
$flow = [];
1817
$methods = [];
1918

20-
// 遍歷所有的任務
2119
foreach ($tasks as $taskKey => $task) {
22-
// 提取每個任務的方法
2320
$methods[$taskKey] = $task['method'];
2421

25-
// 如果該任務沒有依賴的任務,設置為空數組;否則,保存依賴的任務
2622
if (empty($task['tasks'])) {
2723
$flow[$taskKey] = [];
2824
} else {
2925
$flow[$taskKey] = $task['tasks'];
3026
};
3127
};
3228

33-
// 對任務流程進行拓撲排序,確保任務按正確的順序執行
29+
// 對任務流程進行拓撲排序,確保正確的執行順序
3430
$sortedFlow = self::topologicalSort($flow);
3531

3632
// 創建所有的任務,並根據排序好的流程和查詢參數
@@ -50,68 +46,64 @@ public static function run($tasks)
5046
});
5147
}
5248

53-
// 排序任務流程,使用拓撲排序算法
49+
// 排序任務流程
5450
private static function topologicalSort($flow)
5551
{
56-
$sorted = []; // 已排序的任務
57-
$visited = []; // 已訪問的任務
58-
$temporary = []; // 臨時訪問的任務,用於檢測循環依賴
52+
$sorted = [];
53+
$visited = [];
54+
$temporary = [];
5955

60-
// 遍歷所有任務
6156
foreach ($flow as $taskKey => $tasks) {
62-
// 對每個任務進行訪問
6357
if (is_int($taskKey)) {
6458
$taskKey = $tasks;
6559
$tasks = [];
66-
};
60+
}
6761

6862
if (!isset($visited[$taskKey])) {
6963
self::visit($taskKey, $flow, $visited, $sorted, $temporary);
70-
};
71-
};
64+
}
65+
}
7266

73-
// 返回反轉後的排序結果,確保正確的順序
7467
return array_reverse($sorted);
7568
}
7669

7770
private static function visit($taskKey, $flow, &$visited, &$sorted, &$temporary)
7871
{
79-
// 如果在臨時數組中找到該任務,說明存在循環依賴
8072
if (isset($temporary[$taskKey])) {
8173
throw new \Exception("Circular dependency detected: " . $taskKey);
82-
};
74+
}
8375

84-
// 如果該任務還沒有被訪問過
8576
if (!isset($visited[$taskKey])) {
86-
$temporary[$taskKey] = true; // 標記為臨時訪問
77+
$temporary[$taskKey] = true;
8778

88-
// 遍歷該任務的所有依賴任務
8979
if (isset($flow[$taskKey])) {
9080
foreach ($flow[$taskKey] as $task) {
9181
self::visit($task, $flow, $visited, $sorted, $temporary);
92-
};
93-
};
82+
}
83+
}
9484

95-
unset($temporary[$taskKey]); // 移除臨時標記
96-
$visited[$taskKey] = true; // 標記為已訪問
97-
$sorted[] = $taskKey; // 添加到已排序的數組中
98-
};
85+
unset($temporary[$taskKey]);
86+
$visited[$taskKey] = true;
87+
$sorted[] = $taskKey;
88+
}
9989
}
10090

10191

102-
// 創建所有任務
92+
// 創建任務
10393
private static function createTasks($methods, $flow, $sortedKeys, $loop)
10494
{
105-
$tasks = []; // 初始化任務數組
106-
$resolvedTasks = []; // 初始化已解決的任務數組
95+
// 初始化任務數組
96+
$tasks = [];
97+
// 初始化已解決的任務數組
98+
$resolvedTasks = [];
10799

108100
// 遍歷排序後的任務鍵
109101
foreach ($sortedKeys as $taskKey) {
110102
// 獲取該任務的前置任務
111103
$dependentTasks = isset($flow[$taskKey]) ? $flow[$taskKey] : [];
112104
// 創建並儲存該任務
113105
$tasks[$taskKey] = self::createTask($methods, $taskKey, $dependentTasks, $resolvedTasks, $loop);
114-
};
106+
}
115107

116108
// 返回創建的所有任務
117109
return $tasks;
@@ -123,7 +115,7 @@ private static function createTask($methods, $taskKey, $tasks, &$resolvedTasks,
123115
// 如果該任務已經被解決,直接返回已解決的任務
124116
if (isset($resolvedTasks[$taskKey])) {
125117
return $resolvedTasks[$taskKey];
126-
};
118+
}
127119

128120
// 創建一個新的 Promise 延遲對象
129121
$deferred = new Promise\Deferred();
@@ -134,10 +126,10 @@ private static function createTask($methods, $taskKey, $tasks, &$resolvedTasks,
134126
// 如果前置任務尚未解決,創建前置任務
135127
if (!isset($resolvedTasks[$task])) {
136128
$resolvedTasks[$task] = self::createTask($methods, $task, [], $resolvedTasks, $loop);
137-
};
129+
}
138130
// 將前置任務的 Promise 添加到數組中
139131
$taskPromises[] = $resolvedTasks[$task];
140-
};
132+
}
141133

142134
// 當所有前置任務解決後,執行當前任務
143135
Promise\all($taskPromises)->then(function () use ($methods, $taskKey, $deferred) {

0 commit comments

Comments
 (0)