cc-codeconductor 0.4.2 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +75 -4
- package/dist/index.js +2698 -230
- package/package.json +1 -1
- package/presets/agy/skills/evaluation/SKILL.md +6 -0
- package/presets/agy/skills/openspec/SKILL.md +32 -0
- package/presets/agy/workflows/cc-openspec.md +62 -0
- package/presets/agy/workflows/cc-pipeline.md +69 -0
- package/presets/agy/workflows/cc-scorecard.md +17 -0
- package/presets/claude/commands/cc/openspec.md +144 -0
- package/presets/claude/commands/cc/review.md +13 -2
- package/presets/claude/commands/cc/scorecard.md +65 -0
- package/presets/claude/skills/evaluation/SKILL.md +42 -0
- package/presets/claude/skills/openspec/SKILL.md +54 -0
- package/presets/codex/AGENTS.md +57 -0
- package/presets/cursor/.cursorignore +15 -0
- package/presets/cursor/AGENTS.md +504 -0
- package/presets/cursor/agents/architect.md +211 -0
- package/presets/cursor/agents/complexity-auditor.md +76 -0
- package/presets/cursor/agents/contract-builder.md +75 -0
- package/presets/cursor/agents/docs.md +180 -0
- package/presets/cursor/agents/goal-planner.md +71 -0
- package/presets/cursor/agents/implementer.md +161 -0
- package/presets/cursor/agents/orchestrator.md +377 -0
- package/presets/cursor/agents/repo-explorer.md +100 -0
- package/presets/cursor/agents/reviewer.md +237 -0
- package/presets/cursor/agents/security-reviewer.md +113 -0
- package/presets/cursor/agents/task-coach.md +145 -0
- package/presets/cursor/agents/tester.md +241 -0
- package/presets/cursor/commands/cc/api-contract.md +58 -0
- package/presets/cursor/commands/cc/db-migration.md +58 -0
- package/presets/cursor/commands/cc/feature.md +115 -0
- package/presets/cursor/commands/cc/fix.md +121 -0
- package/presets/cursor/commands/cc/openspec.md +144 -0
- package/presets/cursor/commands/cc/pagespeed.md +103 -0
- package/presets/cursor/commands/cc/refactor.md +148 -0
- package/presets/cursor/commands/cc/review.md +137 -0
- package/presets/cursor/commands/cc/scorecard.md +65 -0
- package/presets/cursor/commands/cc/tdd-cycle.md +226 -0
- package/presets/cursor/commands/cc/test-plan.md +138 -0
- package/presets/cursor/rules/behavioral-discipline.mdc +14 -0
- package/presets/cursor/rules/context-budget.mdc +12 -0
- package/presets/cursor/rules/orchestration.mdc +12 -0
- package/presets/cursor/rules/yagni-stdlib.mdc +11 -0
- package/presets/cursor/skills/android/SKILL.md +122 -0
- package/presets/cursor/skills/api-versioning/SKILL.md +394 -0
- package/presets/cursor/skills/astro/SKILL.md +322 -0
- package/presets/cursor/skills/auth-token-inspector/SKILL.md +33 -0
- package/presets/cursor/skills/code-review/SKILL.md +208 -0
- package/presets/cursor/skills/conductor-setup/SKILL.md +127 -0
- package/presets/cursor/skills/django-orm/SKILL.md +463 -0
- package/presets/cursor/skills/django-testing/SKILL.md +417 -0
- package/presets/cursor/skills/django-uv/SKILL.md +409 -0
- package/presets/cursor/skills/drizzle-schema-architect/SKILL.md +54 -0
- package/presets/cursor/skills/evaluation/SKILL.md +8 -0
- package/presets/cursor/skills/fastapi-pydantic-strict/SKILL.md +46 -0
- package/presets/cursor/skills/find-skills/SKILL.md +144 -0
- package/presets/cursor/skills/jpa-nplusone-detector/SKILL.md +49 -0
- package/presets/cursor/skills/jpa-postgres/SKILL.md +626 -0
- package/presets/cursor/skills/laravel-specialist/SKILL.md +267 -0
- package/presets/cursor/skills/laravel-specialist/references/eloquent.md +351 -0
- package/presets/cursor/skills/laravel-specialist/references/livewire.md +512 -0
- package/presets/cursor/skills/laravel-specialist/references/queues.md +423 -0
- package/presets/cursor/skills/laravel-specialist/references/routing.md +362 -0
- package/presets/cursor/skills/laravel-specialist/references/testing.md +522 -0
- package/presets/cursor/skills/livewire-alpine-bridge/SKILL.md +39 -0
- package/presets/cursor/skills/multi-agent-orchestration/README.md +144 -0
- package/presets/cursor/skills/multi-agent-orchestration/SKILL.md +579 -0
- package/presets/cursor/skills/multi-agent-orchestration/examples/framework_implementations.py +362 -0
- package/presets/cursor/skills/multi-agent-orchestration/examples/orchestration_patterns.py +411 -0
- package/presets/cursor/skills/multi-agent-orchestration/scripts/agent_communication.py +334 -0
- package/presets/cursor/skills/multi-agent-orchestration/scripts/benchmarking.py +341 -0
- package/presets/cursor/skills/multi-agent-orchestration/scripts/workflow_management.py +334 -0
- package/presets/cursor/skills/nextjs-typescript/SKILL.md +394 -0
- package/presets/cursor/skills/openspec/SKILL.md +52 -0
- package/presets/cursor/skills/pagespeed-insights/SKILL.md +445 -0
- package/presets/cursor/skills/pagespeed-insights/reference.md +50 -0
- package/presets/cursor/skills/pagespeed-perf/SKILL.md +281 -0
- package/presets/cursor/skills/php-pro/SKILL.md +210 -0
- package/presets/cursor/skills/php-pro/references/async-patterns.md +412 -0
- package/presets/cursor/skills/php-pro/references/laravel-patterns.md +377 -0
- package/presets/cursor/skills/php-pro/references/modern-php-features.md +323 -0
- package/presets/cursor/skills/php-pro/references/symfony-patterns.md +466 -0
- package/presets/cursor/skills/php-pro/references/testing-quality.md +466 -0
- package/presets/cursor/skills/python/SKILL.md +613 -0
- package/presets/cursor/skills/python-django-stack/SKILL.md +500 -0
- package/presets/cursor/skills/python-fastapi-stack/SKILL.md +464 -0
- package/presets/cursor/skills/security/SKILL.md +384 -0
- package/presets/cursor/skills/seo-analytics-injector/SKILL.md +44 -0
- package/presets/cursor/skills/spring-auth-auditor/SKILL.md +33 -0
- package/presets/cursor/skills/spring-boot-feature/SKILL.md +566 -0
- package/presets/cursor/skills/spring-boot-kotlin/SKILL.md +408 -0
- package/presets/cursor/skills/spring-boot-testing-strategy/SKILL.md +479 -0
- package/presets/cursor/skills/sqlalchemy/SKILL.md +473 -0
- package/presets/cursor/skills/tailwind-responsive-auditor/SKILL.md +32 -0
- package/presets/cursor/skills/tdd-mutation-tester/SKILL.md +28 -0
- package/presets/cursor/skills/testing-tdd/SKILL.md +592 -0
- package/presets/cursor/skills/workflow-orchestration-patterns/SKILL.md +98 -0
- package/presets/cursor/skills/workflow-orchestration-patterns/references/details.md +223 -0
- package/presets/laravel-tall/agents/architect.md +8 -0
- package/presets/laravel-tall/agents/implementer.md +12 -0
- package/presets/laravel-tall/laravel-tall.yml +38 -0
- package/presets/opencode/agents/architect.md +153 -61
- package/presets/opencode/agents/complexity-auditor.md +1 -0
- package/presets/opencode/agents/contract-builder.md +93 -0
- package/presets/opencode/agents/docs.md +125 -40
- package/presets/opencode/agents/goal-planner.md +82 -0
- package/presets/opencode/agents/implementer.md +107 -38
- package/presets/opencode/agents/orchestrator.md +90 -71
- package/presets/opencode/agents/repo-explorer.md +1 -2
- package/presets/opencode/agents/reviewer.md +164 -75
- package/presets/opencode/agents/security-reviewer.md +129 -0
- package/presets/opencode/agents/task-coach.md +110 -59
- package/presets/opencode/agents/tester.md +1 -2
- package/presets/opencode/commands/cc-openspec.md +61 -0
- package/presets/opencode/commands/cc-scorecard.md +16 -0
- package/presets/opencode/prompts/v0.4.0/architect.md +221 -0
- package/presets/opencode/prompts/v0.4.0/complexity-auditor.md +89 -0
- package/presets/opencode/prompts/v0.4.0/docs.md +189 -0
- package/presets/opencode/prompts/v0.4.0/implementer.md +162 -0
- package/presets/opencode/prompts/v0.4.0/orchestrator.md +348 -0
- package/presets/opencode/prompts/v0.4.0/repo-explorer.md +110 -0
- package/presets/opencode/prompts/v0.4.0/reviewer.md +225 -0
- package/presets/opencode/prompts/v0.4.0/task-coach.md +155 -0
- package/presets/opencode/prompts/v0.4.0/tester.md +251 -0
- package/presets/opencode/prompts/v0.5.0/architect.md +222 -0
- package/presets/opencode/prompts/v0.5.0/complexity-auditor.md +91 -0
- package/presets/opencode/prompts/v0.5.0/contract-builder.md +84 -0
- package/presets/opencode/prompts/v0.5.0/docs.md +190 -0
- package/presets/opencode/prompts/v0.5.0/goal-planner.md +80 -0
- package/presets/opencode/prompts/v0.5.0/implementer.md +171 -0
- package/presets/opencode/prompts/v0.5.0/orchestrator.md +388 -0
- package/presets/opencode/prompts/v0.5.0/repo-explorer.md +111 -0
- package/presets/opencode/prompts/v0.5.0/reviewer.md +248 -0
- package/presets/opencode/prompts/v0.5.0/security-reviewer.md +123 -0
- package/presets/opencode/prompts/v0.5.0/task-coach.md +156 -0
- package/presets/opencode/prompts/v0.5.0/tester.md +252 -0
- package/presets/opencode/skills/auth-token-inspector/SKILL.md +31 -0
- package/presets/opencode/skills/drizzle-schema-architect/SKILL.md +51 -0
- package/presets/opencode/skills/evaluation/SKILL.md +6 -0
- package/presets/opencode/skills/fastapi-pydantic-strict/SKILL.md +44 -0
- package/presets/opencode/skills/jpa-nplusone-detector/SKILL.md +46 -0
- package/presets/opencode/skills/livewire-alpine-bridge/SKILL.md +36 -0
- package/presets/opencode/skills/openspec/SKILL.md +50 -0
- package/presets/opencode/skills/seo-analytics-injector/SKILL.md +44 -0
- package/presets/opencode/skills/spring-auth-auditor/SKILL.md +30 -0
- package/presets/opencode/skills/tailwind-responsive-auditor/SKILL.md +30 -0
- package/presets/opencode/skills/tdd-mutation-tester/SKILL.md +28 -0
- package/presets/python-data-api/agents/architect.md +8 -0
- package/presets/python-data-api/agents/implementer.md +9 -0
- package/presets/python-data-api/python-data-api.yml +37 -0
- package/presets/spring-kotlin-jpa/agents/architect.md +8 -0
- package/presets/spring-kotlin-jpa/agents/implementer.md +9 -0
- package/presets/spring-kotlin-jpa/spring-kotlin-jpa.yml +38 -0
- package/presets/templates/BACKLOG.md +33 -0
- package/presets/templates/execution-profile.yml +6 -0
- package/presets/templates/model-comparison.md +11 -0
- package/presets/templates/regression-checklist.yml +10 -0
- package/presets/ts-next-drizzle/agents/architect.md +8 -0
- package/presets/ts-next-drizzle/agents/implementer.md +10 -0
- package/presets/ts-next-drizzle/ts-next-drizzle.yml +41 -0
- package/src/presets/manifests/agy.yml +2 -2
- package/src/presets/manifests/claude.yml +2 -2
- package/src/presets/manifests/codex.yml +2 -2
- package/src/presets/manifests/cursor.yml +19 -3
- package/src/presets/manifests/gemini.yml +2 -2
- package/src/presets/manifests/opencode.yml +2 -2
- package/src/presets/models/agy.yml +21 -0
- package/src/presets/models/claude.yml +18 -0
- package/src/presets/models/codex.yml +18 -0
- package/src/presets/models/cursor.yml +39 -9
- package/src/presets/models/gemini.yml +18 -0
- package/src/presets/models/opencode.yml +18 -0
|
@@ -0,0 +1,412 @@
|
|
|
1
|
+
# Async PHP Patterns
|
|
2
|
+
|
|
3
|
+
## Swoole HTTP Server
|
|
4
|
+
|
|
5
|
+
```php
|
|
6
|
+
<?php
|
|
7
|
+
|
|
8
|
+
declare(strict_types=1);
|
|
9
|
+
|
|
10
|
+
use Swoole\HTTP\Server;
|
|
11
|
+
use Swoole\HTTP\Request;
|
|
12
|
+
use Swoole\HTTP\Response;
|
|
13
|
+
|
|
14
|
+
$server = new Server('0.0.0.0', 9501);
|
|
15
|
+
|
|
16
|
+
$server->set([
|
|
17
|
+
'worker_num' => 4,
|
|
18
|
+
'max_request' => 10000,
|
|
19
|
+
'task_worker_num' => 2,
|
|
20
|
+
'enable_coroutine' => true,
|
|
21
|
+
]);
|
|
22
|
+
|
|
23
|
+
$server->on('start', function (Server $server) {
|
|
24
|
+
echo "Swoole HTTP server started at http://0.0.0.0:9501\n";
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
$server->on('request', function (Request $request, Response $response) {
|
|
28
|
+
$response->header('Content-Type', 'application/json');
|
|
29
|
+
|
|
30
|
+
match ($request->server['request_uri']) {
|
|
31
|
+
'/api/users' => handleUsers($request, $response),
|
|
32
|
+
'/api/health' => $response->end(json_encode(['status' => 'healthy'])),
|
|
33
|
+
default => $response->status(404)->end(json_encode(['error' => 'Not found'])),
|
|
34
|
+
};
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
function handleUsers(Request $request, Response $response): void
|
|
38
|
+
{
|
|
39
|
+
// Coroutine for concurrent DB queries
|
|
40
|
+
go(function () use ($response) {
|
|
41
|
+
$users = queryDatabase('SELECT * FROM users LIMIT 10');
|
|
42
|
+
$response->end(json_encode(['data' => $users]));
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
$server->start();
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Swoole Coroutines
|
|
50
|
+
|
|
51
|
+
```php
|
|
52
|
+
<?php
|
|
53
|
+
|
|
54
|
+
declare(strict_types=1);
|
|
55
|
+
|
|
56
|
+
use Swoole\Coroutine;
|
|
57
|
+
use Swoole\Coroutine\Http\Client;
|
|
58
|
+
|
|
59
|
+
// Concurrent HTTP requests
|
|
60
|
+
Coroutine\run(function () {
|
|
61
|
+
$results = [];
|
|
62
|
+
|
|
63
|
+
// Create multiple coroutines
|
|
64
|
+
$wg = new Coroutine\WaitGroup();
|
|
65
|
+
|
|
66
|
+
$urls = [
|
|
67
|
+
'https://api.example.com/users',
|
|
68
|
+
'https://api.example.com/posts',
|
|
69
|
+
'https://api.example.com/comments',
|
|
70
|
+
];
|
|
71
|
+
|
|
72
|
+
foreach ($urls as $url) {
|
|
73
|
+
$wg->add();
|
|
74
|
+
go(function () use ($url, &$results, $wg) {
|
|
75
|
+
$client = new Client(parse_url($url, PHP_URL_HOST), 443, true);
|
|
76
|
+
$client->set(['timeout' => 5]);
|
|
77
|
+
$client->get(parse_url($url, PHP_URL_PATH));
|
|
78
|
+
|
|
79
|
+
$results[$url] = [
|
|
80
|
+
'status' => $client->statusCode,
|
|
81
|
+
'body' => $client->body,
|
|
82
|
+
];
|
|
83
|
+
|
|
84
|
+
$client->close();
|
|
85
|
+
$wg->done();
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
$wg->wait();
|
|
90
|
+
|
|
91
|
+
print_r($results);
|
|
92
|
+
});
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## Swoole Async MySQL
|
|
96
|
+
|
|
97
|
+
```php
|
|
98
|
+
<?php
|
|
99
|
+
|
|
100
|
+
declare(strict_types=1);
|
|
101
|
+
|
|
102
|
+
use Swoole\Coroutine;
|
|
103
|
+
use Swoole\Coroutine\MySQL;
|
|
104
|
+
|
|
105
|
+
Coroutine\run(function () {
|
|
106
|
+
$mysql = new MySQL();
|
|
107
|
+
|
|
108
|
+
$connected = $mysql->connect([
|
|
109
|
+
'host' => '127.0.0.1',
|
|
110
|
+
'port' => 3306,
|
|
111
|
+
'user' => 'root',
|
|
112
|
+
'password' => 'password',
|
|
113
|
+
'database' => 'test',
|
|
114
|
+
]);
|
|
115
|
+
|
|
116
|
+
if (!$connected) {
|
|
117
|
+
throw new \RuntimeException($mysql->connect_error);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Async query
|
|
121
|
+
$result = $mysql->query('SELECT * FROM users WHERE active = 1');
|
|
122
|
+
|
|
123
|
+
foreach ($result as $row) {
|
|
124
|
+
echo "User: {$row['name']}\n";
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Prepared statements
|
|
128
|
+
$stmt = $mysql->prepare('SELECT * FROM users WHERE id = ?');
|
|
129
|
+
$stmt->execute([42]);
|
|
130
|
+
$user = $stmt->fetchAll();
|
|
131
|
+
|
|
132
|
+
$mysql->close();
|
|
133
|
+
});
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## Swoole Channel (Communication)
|
|
137
|
+
|
|
138
|
+
```php
|
|
139
|
+
<?php
|
|
140
|
+
|
|
141
|
+
declare(strict_types=1);
|
|
142
|
+
|
|
143
|
+
use Swoole\Coroutine;
|
|
144
|
+
use Swoole\Coroutine\Channel;
|
|
145
|
+
|
|
146
|
+
Coroutine\run(function () {
|
|
147
|
+
$channel = new Channel(10); // Buffer size: 10
|
|
148
|
+
|
|
149
|
+
// Producer
|
|
150
|
+
go(function () use ($channel) {
|
|
151
|
+
for ($i = 1; $i <= 5; $i++) {
|
|
152
|
+
$channel->push("Task {$i}");
|
|
153
|
+
echo "Produced: Task {$i}\n";
|
|
154
|
+
Coroutine::sleep(0.5);
|
|
155
|
+
}
|
|
156
|
+
$channel->close();
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
// Consumer
|
|
160
|
+
go(function () use ($channel) {
|
|
161
|
+
while (true) {
|
|
162
|
+
$task = $channel->pop();
|
|
163
|
+
if ($task === false && $channel->errCode === SWOOLE_CHANNEL_CLOSED) {
|
|
164
|
+
break;
|
|
165
|
+
}
|
|
166
|
+
echo "Consumed: {$task}\n";
|
|
167
|
+
Coroutine::sleep(1);
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
## ReactPHP Event Loop
|
|
174
|
+
|
|
175
|
+
```php
|
|
176
|
+
<?php
|
|
177
|
+
|
|
178
|
+
declare(strict_types=1);
|
|
179
|
+
|
|
180
|
+
require 'vendor/autoload.php';
|
|
181
|
+
|
|
182
|
+
use React\EventLoop\Loop;
|
|
183
|
+
use React\Http\Message\Response;
|
|
184
|
+
use Psr\Http\Message\ServerRequestInterface;
|
|
185
|
+
|
|
186
|
+
// HTTP Server
|
|
187
|
+
$server = new React\Http\HttpServer(function (ServerRequestInterface $request) {
|
|
188
|
+
return new Response(
|
|
189
|
+
200,
|
|
190
|
+
['Content-Type' => 'application/json'],
|
|
191
|
+
json_encode([
|
|
192
|
+
'method' => $request->getMethod(),
|
|
193
|
+
'uri' => (string) $request->getUri(),
|
|
194
|
+
'timestamp' => time(),
|
|
195
|
+
])
|
|
196
|
+
);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
$socket = new React\Socket\SocketServer('0.0.0.0:8080');
|
|
200
|
+
$server->listen($socket);
|
|
201
|
+
|
|
202
|
+
echo "Server running at http://0.0.0.0:8080\n";
|
|
203
|
+
|
|
204
|
+
// Periodic timer
|
|
205
|
+
Loop::addPeriodicTimer(5.0, function () {
|
|
206
|
+
echo "Heartbeat: " . date('H:i:s') . "\n";
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
// One-time timer
|
|
210
|
+
Loop::addTimer(10.0, function () {
|
|
211
|
+
echo "This runs once after 10 seconds\n";
|
|
212
|
+
});
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
## ReactPHP Async MySQL
|
|
216
|
+
|
|
217
|
+
```php
|
|
218
|
+
<?php
|
|
219
|
+
|
|
220
|
+
declare(strict_types=1);
|
|
221
|
+
|
|
222
|
+
require 'vendor/autoload.php';
|
|
223
|
+
|
|
224
|
+
use React\MySQL\Factory;
|
|
225
|
+
use React\MySQL\QueryResult;
|
|
226
|
+
|
|
227
|
+
$factory = new Factory();
|
|
228
|
+
|
|
229
|
+
$connection = $factory->createLazyConnection('root:password@localhost/database');
|
|
230
|
+
|
|
231
|
+
$connection->query('SELECT * FROM users WHERE active = 1')
|
|
232
|
+
->then(
|
|
233
|
+
function (QueryResult $result) {
|
|
234
|
+
echo "Found " . count($result->resultRows) . " users\n";
|
|
235
|
+
foreach ($result->resultRows as $row) {
|
|
236
|
+
echo "User: {$row['name']}\n";
|
|
237
|
+
}
|
|
238
|
+
},
|
|
239
|
+
function (\Exception $error) {
|
|
240
|
+
echo "Error: " . $error->getMessage() . "\n";
|
|
241
|
+
}
|
|
242
|
+
);
|
|
243
|
+
|
|
244
|
+
// Prepared statements
|
|
245
|
+
$connection->query('SELECT * FROM users WHERE id = ?', [42])
|
|
246
|
+
->then(function (QueryResult $result) {
|
|
247
|
+
$user = $result->resultRows[0] ?? null;
|
|
248
|
+
var_dump($user);
|
|
249
|
+
});
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
## ReactPHP Promises
|
|
253
|
+
|
|
254
|
+
```php
|
|
255
|
+
<?php
|
|
256
|
+
|
|
257
|
+
declare(strict_types=1);
|
|
258
|
+
|
|
259
|
+
use React\Promise\Promise;
|
|
260
|
+
use React\Promise\Deferred;
|
|
261
|
+
use function React\Promise\all;
|
|
262
|
+
|
|
263
|
+
// Creating promises
|
|
264
|
+
function fetchUser(int $id): Promise
|
|
265
|
+
{
|
|
266
|
+
$deferred = new Deferred();
|
|
267
|
+
|
|
268
|
+
// Simulate async operation
|
|
269
|
+
Loop::addTimer(1.0, function () use ($deferred, $id) {
|
|
270
|
+
$deferred->resolve([
|
|
271
|
+
'id' => $id,
|
|
272
|
+
'name' => "User {$id}",
|
|
273
|
+
]);
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
return $deferred->promise();
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// Using promises
|
|
280
|
+
fetchUser(42)
|
|
281
|
+
->then(function ($user) {
|
|
282
|
+
echo "Got user: {$user['name']}\n";
|
|
283
|
+
return fetchUserPosts($user['id']);
|
|
284
|
+
})
|
|
285
|
+
->then(function ($posts) {
|
|
286
|
+
echo "Got " . count($posts) . " posts\n";
|
|
287
|
+
})
|
|
288
|
+
->catch(function (\Exception $error) {
|
|
289
|
+
echo "Error: " . $error->getMessage() . "\n";
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
// Parallel promises
|
|
293
|
+
all([
|
|
294
|
+
fetchUser(1),
|
|
295
|
+
fetchUser(2),
|
|
296
|
+
fetchUser(3),
|
|
297
|
+
])->then(function ($users) {
|
|
298
|
+
echo "Fetched " . count($users) . " users\n";
|
|
299
|
+
});
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
## PHP Fibers (Native PHP 8.1+)
|
|
303
|
+
|
|
304
|
+
```php
|
|
305
|
+
<?php
|
|
306
|
+
|
|
307
|
+
declare(strict_types=1);
|
|
308
|
+
|
|
309
|
+
// Simple async function using fibers
|
|
310
|
+
function async(callable $callback): Fiber
|
|
311
|
+
{
|
|
312
|
+
return new Fiber($callback);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function await(Fiber $fiber): mixed
|
|
316
|
+
{
|
|
317
|
+
if (!$fiber->isStarted()) {
|
|
318
|
+
return $fiber->start();
|
|
319
|
+
}
|
|
320
|
+
if ($fiber->isTerminated()) {
|
|
321
|
+
return $fiber->getReturn();
|
|
322
|
+
}
|
|
323
|
+
return $fiber->resume();
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// Simulate async I/O
|
|
327
|
+
function fetchData(string $url): Fiber
|
|
328
|
+
{
|
|
329
|
+
return async(function () use ($url) {
|
|
330
|
+
echo "Fetching: {$url}\n";
|
|
331
|
+
Fiber::suspend('pending');
|
|
332
|
+
|
|
333
|
+
// Simulate network delay
|
|
334
|
+
sleep(1);
|
|
335
|
+
|
|
336
|
+
return "Data from {$url}";
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
// Usage
|
|
341
|
+
$fiber1 = fetchData('https://api.example.com/users');
|
|
342
|
+
$fiber2 = fetchData('https://api.example.com/posts');
|
|
343
|
+
|
|
344
|
+
await($fiber1);
|
|
345
|
+
await($fiber2);
|
|
346
|
+
|
|
347
|
+
$result1 = await($fiber1);
|
|
348
|
+
$result2 = await($fiber2);
|
|
349
|
+
|
|
350
|
+
echo "{$result1}\n";
|
|
351
|
+
echo "{$result2}\n";
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
## Amphp Framework
|
|
355
|
+
|
|
356
|
+
```php
|
|
357
|
+
<?php
|
|
358
|
+
|
|
359
|
+
declare(strict_types=1);
|
|
360
|
+
|
|
361
|
+
require 'vendor/autoload.php';
|
|
362
|
+
|
|
363
|
+
use Amp\Http\Server\HttpServer;
|
|
364
|
+
use Amp\Http\Server\Request;
|
|
365
|
+
use Amp\Http\Server\Response;
|
|
366
|
+
use Amp\Http\Server\Router;
|
|
367
|
+
use Amp\Socket\Server as SocketServer;
|
|
368
|
+
use function Amp\async;
|
|
369
|
+
use function Amp\Future\await;
|
|
370
|
+
|
|
371
|
+
// HTTP Server with Amphp
|
|
372
|
+
$router = new Router();
|
|
373
|
+
|
|
374
|
+
$router->addRoute('GET', '/api/users', function (Request $request): Response {
|
|
375
|
+
// Concurrent database queries
|
|
376
|
+
$users = await([
|
|
377
|
+
async(fn() => queryUsers()),
|
|
378
|
+
async(fn() => queryUserStats()),
|
|
379
|
+
]);
|
|
380
|
+
|
|
381
|
+
return new Response(
|
|
382
|
+
status: 200,
|
|
383
|
+
headers: ['content-type' => 'application/json'],
|
|
384
|
+
body: json_encode(['users' => $users[0], 'stats' => $users[1]]),
|
|
385
|
+
);
|
|
386
|
+
});
|
|
387
|
+
|
|
388
|
+
$server = new HttpServer(
|
|
389
|
+
servers: [SocketServer::listen('0.0.0.0:8080')],
|
|
390
|
+
requestHandler: $router,
|
|
391
|
+
);
|
|
392
|
+
|
|
393
|
+
$server->start();
|
|
394
|
+
```
|
|
395
|
+
|
|
396
|
+
## Quick Reference
|
|
397
|
+
|
|
398
|
+
| Technology | Use Case | Performance |
|
|
399
|
+
|------------|----------|-------------|
|
|
400
|
+
| Swoole | High-performance servers, WebSockets | Very High |
|
|
401
|
+
| ReactPHP | Event-driven apps, real-time | High |
|
|
402
|
+
| Amphp | Modern async framework | High |
|
|
403
|
+
| Fibers | Native async (PHP 8.1+) | Medium |
|
|
404
|
+
| Generators | Simple async patterns | Medium |
|
|
405
|
+
|
|
406
|
+
| Feature | Swoole | ReactPHP | Amphp |
|
|
407
|
+
|---------|--------|----------|-------|
|
|
408
|
+
| Coroutines | Yes | No (Promises) | Yes (Fibers) |
|
|
409
|
+
| HTTP Server | Built-in | Via package | Via package |
|
|
410
|
+
| WebSockets | Built-in | Via package | Via package |
|
|
411
|
+
| Extension | Required | Not required | Not required |
|
|
412
|
+
| Learning Curve | Medium | Low | Medium |
|