cc-codeconductor 0.4.3 → 1.0.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.
Files changed (262) hide show
  1. package/README.md +95 -95
  2. package/dist/cli/errors.d.ts +65 -0
  3. package/dist/core/compilation/compile-checker.d.ts +51 -0
  4. package/dist/core/config/codeconductor-config.d.ts +42 -0
  5. package/dist/core/config/config-loader.d.ts +11 -0
  6. package/dist/core/filesystem/credential-guard.d.ts +18 -0
  7. package/dist/core/goal/goal-planner.d.ts +8 -0
  8. package/dist/core/goal/goal-state.d.ts +15 -0
  9. package/dist/core/loop/git-stats.d.ts +13 -0
  10. package/dist/core/loop/loop-engine.d.ts +79 -0
  11. package/dist/core/memory/episodic-store.d.ts +6 -0
  12. package/dist/core/memory/operational-state.d.ts +6 -0
  13. package/dist/core/orchestrator/runtime-orchestrator.d.ts +25 -0
  14. package/dist/core/planner/product-planner.d.ts +18 -0
  15. package/dist/core/presets/package-paths.d.ts +4 -0
  16. package/dist/core/product-graph/graph-store.d.ts +11 -0
  17. package/dist/core/product-graph/paths.d.ts +18 -0
  18. package/dist/core/verification/verification-runner.d.ts +57 -0
  19. package/dist/domain/loop/loop-state.d.ts +74 -0
  20. package/dist/domain/product/entities.d.ts +11 -0
  21. package/dist/index.d.ts +15 -0
  22. package/dist/index.js +23721 -15064
  23. package/dist/library.js +2595 -0
  24. package/dist/utils/result.d.ts +36 -0
  25. package/dist/validation/schemas.d.ts +3660 -0
  26. package/package.json +15 -2
  27. package/policy.yml +12 -11
  28. package/presets/agy/AGENTS.md +11 -5
  29. package/presets/agy/gates/pre-commit/GATE.md +136 -0
  30. package/presets/agy/skills/cc-fix/SKILL.md +2 -2
  31. package/presets/agy/skills/cc-tdd-cycle/SKILL.md +11 -0
  32. package/presets/agy/skills/evaluation/SKILL.md +6 -0
  33. package/presets/agy/skills/openspec/SKILL.md +32 -0
  34. package/presets/agy/workflows/cc-api-contract.md +12 -0
  35. package/presets/agy/workflows/cc-ask.md +55 -0
  36. package/presets/agy/workflows/cc-clarify.md +33 -0
  37. package/presets/agy/workflows/cc-council.md +31 -7
  38. package/presets/agy/workflows/cc-db-migration.md +22 -9
  39. package/presets/agy/workflows/cc-explore.md +37 -0
  40. package/presets/agy/workflows/cc-feature.md +41 -19
  41. package/presets/agy/workflows/cc-fix.md +50 -23
  42. package/presets/agy/workflows/cc-handoff.md +42 -0
  43. package/presets/agy/workflows/cc-iterative.md +128 -0
  44. package/presets/agy/workflows/cc-openspec.md +75 -0
  45. package/presets/agy/workflows/cc-pagespeed.md +12 -0
  46. package/presets/agy/workflows/cc-prototype.md +39 -0
  47. package/presets/agy/workflows/cc-refactor.md +12 -0
  48. package/presets/agy/workflows/cc-review.md +12 -0
  49. package/presets/agy/workflows/cc-scorecard.md +29 -0
  50. package/presets/agy/workflows/cc-tdd-cycle.md +24 -0
  51. package/presets/agy/workflows/cc-test-plan.md +12 -0
  52. package/presets/agy/workflows/cc-triage.md +35 -0
  53. package/presets/claude/CLAUDE.md +64 -0
  54. package/presets/claude/commands/cc/api-contract.md +12 -0
  55. package/presets/claude/commands/cc/ask.md +55 -0
  56. package/presets/claude/commands/cc/clarify.md +32 -0
  57. package/presets/claude/commands/cc/council.md +87 -0
  58. package/presets/claude/commands/cc/db-migration.md +22 -9
  59. package/presets/claude/commands/cc/explore.md +36 -0
  60. package/presets/claude/commands/cc/feature.md +49 -22
  61. package/presets/claude/commands/cc/fix.md +74 -20
  62. package/presets/claude/commands/cc/handoff.md +44 -0
  63. package/presets/claude/commands/cc/iterative.md +132 -0
  64. package/presets/claude/commands/cc/openspec.md +169 -0
  65. package/presets/claude/commands/cc/pagespeed.md +12 -0
  66. package/presets/claude/commands/cc/prototype.md +38 -0
  67. package/presets/claude/commands/cc/refactor.md +152 -1
  68. package/presets/claude/commands/cc/review.md +90 -17
  69. package/presets/claude/commands/cc/scorecard.md +77 -0
  70. package/presets/claude/commands/cc/tdd-cycle.md +53 -3
  71. package/presets/claude/commands/cc/test-plan.md +12 -0
  72. package/presets/claude/commands/cc/triage.md +34 -0
  73. package/presets/claude/gates/pre-commit/GATE.md +136 -0
  74. package/presets/claude/settings.json +8 -46
  75. package/presets/claude/skills/evaluation/SKILL.md +42 -0
  76. package/presets/claude/skills/openspec/SKILL.md +54 -0
  77. package/presets/codex/AGENTS.md +62 -5
  78. package/presets/codex/commands/cc-ask.md +55 -0
  79. package/presets/codex/gates/pre-commit/GATE.md +136 -0
  80. package/presets/cursor/.cursorignore +15 -0
  81. package/presets/cursor/AGENTS.md +504 -0
  82. package/presets/cursor/agents/architect.md +211 -0
  83. package/presets/cursor/agents/business-agent.md +44 -0
  84. package/presets/cursor/agents/complexity-auditor.md +76 -0
  85. package/presets/cursor/agents/continuous-architect.md +37 -0
  86. package/presets/cursor/agents/contract-builder.md +75 -0
  87. package/presets/cursor/agents/docs.md +181 -0
  88. package/presets/cursor/agents/goal-planner.md +71 -0
  89. package/presets/cursor/agents/impact-analyst.md +43 -0
  90. package/presets/cursor/agents/implementer.md +161 -0
  91. package/presets/cursor/agents/orchestrator.md +377 -0
  92. package/presets/cursor/agents/repo-explorer.md +100 -0
  93. package/presets/cursor/agents/reviewer.md +237 -0
  94. package/presets/cursor/agents/security-reviewer.md +113 -0
  95. package/presets/cursor/agents/task-coach.md +145 -0
  96. package/presets/cursor/agents/tester.md +241 -0
  97. package/presets/cursor/commands/cc/api-contract.md +70 -0
  98. package/presets/cursor/commands/cc/ask.md +55 -0
  99. package/presets/cursor/commands/cc/clarify.md +32 -0
  100. package/presets/cursor/commands/cc/council.md +87 -0
  101. package/presets/cursor/commands/cc/db-migration.md +71 -0
  102. package/presets/cursor/commands/cc/explore.md +36 -0
  103. package/presets/cursor/commands/cc/feature.md +137 -0
  104. package/presets/cursor/commands/cc/fix.md +148 -0
  105. package/presets/cursor/commands/cc/handoff.md +41 -0
  106. package/presets/cursor/commands/cc/iterative.md +133 -0
  107. package/presets/cursor/commands/cc/openspec.md +159 -0
  108. package/presets/cursor/commands/cc/pagespeed.md +115 -0
  109. package/presets/cursor/commands/cc/prototype.md +38 -0
  110. package/presets/cursor/commands/cc/refactor.md +160 -0
  111. package/presets/cursor/commands/cc/review.md +149 -0
  112. package/presets/cursor/commands/cc/scorecard.md +77 -0
  113. package/presets/cursor/commands/cc/tdd-cycle.md +250 -0
  114. package/presets/cursor/commands/cc/test-plan.md +150 -0
  115. package/presets/cursor/commands/cc/triage.md +34 -0
  116. package/presets/cursor/gates/pre-commit/GATE.md +136 -0
  117. package/presets/cursor/rules/behavioral-discipline.mdc +14 -0
  118. package/presets/cursor/rules/context-budget.mdc +12 -0
  119. package/presets/cursor/rules/orchestration.mdc +12 -0
  120. package/presets/cursor/rules/yagni-stdlib.mdc +11 -0
  121. package/presets/cursor/skills/android/SKILL.md +122 -0
  122. package/presets/cursor/skills/api-versioning/SKILL.md +394 -0
  123. package/presets/cursor/skills/astro/SKILL.md +322 -0
  124. package/presets/cursor/skills/auth-token-inspector/SKILL.md +33 -0
  125. package/presets/cursor/skills/code-review/SKILL.md +208 -0
  126. package/presets/cursor/skills/conductor-setup/SKILL.md +127 -0
  127. package/presets/cursor/skills/django-orm/SKILL.md +463 -0
  128. package/presets/cursor/skills/django-testing/SKILL.md +417 -0
  129. package/presets/cursor/skills/django-uv/SKILL.md +409 -0
  130. package/presets/cursor/skills/drizzle-schema-architect/SKILL.md +54 -0
  131. package/presets/cursor/skills/evaluation/SKILL.md +8 -0
  132. package/presets/cursor/skills/fastapi-pydantic-strict/SKILL.md +46 -0
  133. package/presets/cursor/skills/find-skills/SKILL.md +144 -0
  134. package/presets/cursor/skills/jpa-nplusone-detector/SKILL.md +49 -0
  135. package/presets/cursor/skills/jpa-postgres/SKILL.md +626 -0
  136. package/presets/cursor/skills/laravel-specialist/SKILL.md +267 -0
  137. package/presets/cursor/skills/laravel-specialist/references/eloquent.md +351 -0
  138. package/presets/cursor/skills/laravel-specialist/references/livewire.md +512 -0
  139. package/presets/cursor/skills/laravel-specialist/references/queues.md +423 -0
  140. package/presets/cursor/skills/laravel-specialist/references/routing.md +362 -0
  141. package/presets/cursor/skills/laravel-specialist/references/testing.md +522 -0
  142. package/presets/cursor/skills/livewire-alpine-bridge/SKILL.md +39 -0
  143. package/presets/cursor/skills/multi-agent-orchestration/README.md +144 -0
  144. package/presets/cursor/skills/multi-agent-orchestration/SKILL.md +579 -0
  145. package/presets/cursor/skills/multi-agent-orchestration/examples/framework_implementations.py +362 -0
  146. package/presets/cursor/skills/multi-agent-orchestration/examples/orchestration_patterns.py +411 -0
  147. package/presets/cursor/skills/multi-agent-orchestration/scripts/agent_communication.py +334 -0
  148. package/presets/cursor/skills/multi-agent-orchestration/scripts/benchmarking.py +341 -0
  149. package/presets/cursor/skills/multi-agent-orchestration/scripts/workflow_management.py +334 -0
  150. package/presets/cursor/skills/nextjs-typescript/SKILL.md +394 -0
  151. package/presets/cursor/skills/openspec/SKILL.md +52 -0
  152. package/presets/cursor/skills/pagespeed-insights/SKILL.md +445 -0
  153. package/presets/cursor/skills/pagespeed-insights/reference.md +50 -0
  154. package/presets/cursor/skills/pagespeed-perf/SKILL.md +281 -0
  155. package/presets/cursor/skills/php-pro/SKILL.md +210 -0
  156. package/presets/cursor/skills/php-pro/references/async-patterns.md +412 -0
  157. package/presets/cursor/skills/php-pro/references/laravel-patterns.md +377 -0
  158. package/presets/cursor/skills/php-pro/references/modern-php-features.md +323 -0
  159. package/presets/cursor/skills/php-pro/references/symfony-patterns.md +466 -0
  160. package/presets/cursor/skills/php-pro/references/testing-quality.md +466 -0
  161. package/presets/cursor/skills/python/SKILL.md +613 -0
  162. package/presets/cursor/skills/python-django-stack/SKILL.md +500 -0
  163. package/presets/cursor/skills/python-fastapi-stack/SKILL.md +464 -0
  164. package/presets/cursor/skills/security/SKILL.md +384 -0
  165. package/presets/cursor/skills/seo-analytics-injector/SKILL.md +44 -0
  166. package/presets/cursor/skills/spring-auth-auditor/SKILL.md +33 -0
  167. package/presets/cursor/skills/spring-boot-feature/SKILL.md +566 -0
  168. package/presets/cursor/skills/spring-boot-kotlin/SKILL.md +408 -0
  169. package/presets/cursor/skills/spring-boot-testing-strategy/SKILL.md +479 -0
  170. package/presets/cursor/skills/sqlalchemy/SKILL.md +473 -0
  171. package/presets/cursor/skills/tailwind-responsive-auditor/SKILL.md +32 -0
  172. package/presets/cursor/skills/tdd-mutation-tester/SKILL.md +28 -0
  173. package/presets/cursor/skills/testing-tdd/SKILL.md +592 -0
  174. package/presets/cursor/skills/workflow-orchestration-patterns/SKILL.md +98 -0
  175. package/presets/cursor/skills/workflow-orchestration-patterns/references/details.md +223 -0
  176. package/presets/opencode/agents/architect.md +22 -2
  177. package/presets/opencode/agents/complexity-auditor.md +16 -0
  178. package/presets/opencode/agents/contract-builder.md +109 -0
  179. package/presets/opencode/agents/devil.md +158 -0
  180. package/presets/opencode/agents/docs.md +19 -2
  181. package/presets/opencode/agents/goal-planner.md +112 -0
  182. package/presets/opencode/agents/implementer.md +39 -2
  183. package/presets/opencode/agents/orchestrator.md +124 -19
  184. package/presets/opencode/agents/planner.md +61 -0
  185. package/presets/opencode/agents/repo-explorer.md +15 -0
  186. package/presets/opencode/agents/reviewer.md +53 -2
  187. package/presets/opencode/agents/security-reviewer.md +159 -0
  188. package/presets/opencode/agents/task-coach.md +69 -2
  189. package/presets/opencode/agents/tester.md +18 -2
  190. package/presets/opencode/commands/cc-api-contract.md +12 -0
  191. package/presets/opencode/commands/cc-ask.md +55 -0
  192. package/presets/opencode/commands/cc-clarify.md +31 -0
  193. package/presets/opencode/commands/cc-council.md +87 -0
  194. package/presets/opencode/commands/cc-db-migration.md +22 -9
  195. package/presets/opencode/commands/cc-explore.md +35 -0
  196. package/presets/opencode/commands/cc-feature.md +41 -19
  197. package/presets/opencode/commands/cc-fix.md +50 -23
  198. package/presets/opencode/commands/cc-handoff.md +40 -0
  199. package/presets/opencode/commands/cc-iterative.md +127 -0
  200. package/presets/opencode/commands/cc-openspec.md +74 -0
  201. package/presets/opencode/commands/cc-pagespeed.md +12 -0
  202. package/presets/opencode/commands/cc-prototype.md +37 -0
  203. package/presets/opencode/commands/cc-refactor.md +12 -0
  204. package/presets/opencode/commands/cc-review.md +12 -0
  205. package/presets/opencode/commands/cc-scorecard.md +28 -0
  206. package/presets/opencode/commands/cc-tdd-cycle.md +24 -0
  207. package/presets/opencode/commands/cc-test-plan.md +12 -0
  208. package/presets/opencode/commands/cc-triage.md +33 -0
  209. package/presets/opencode/gates/pre-commit/GATE.md +136 -0
  210. package/presets/opencode/prompts/v0.1.0/DEPRECATED.md +11 -0
  211. package/presets/opencode/prompts/v0.2.0/DEPRECATED.md +11 -0
  212. package/presets/opencode/prompts/v0.3.0/DEPRECATED.md +11 -0
  213. package/presets/opencode/prompts/v0.4.0/DEPRECATED.md +11 -0
  214. package/presets/opencode/prompts/v0.5.0/architect.md +222 -0
  215. package/presets/opencode/prompts/v0.5.0/complexity-auditor.md +91 -0
  216. package/presets/opencode/prompts/v0.5.0/contract-builder.md +84 -0
  217. package/presets/opencode/prompts/v0.5.0/docs.md +190 -0
  218. package/presets/opencode/prompts/v0.5.0/goal-planner.md +80 -0
  219. package/presets/opencode/prompts/v0.5.0/implementer.md +171 -0
  220. package/presets/opencode/prompts/v0.5.0/orchestrator.md +388 -0
  221. package/presets/opencode/prompts/v0.5.0/repo-explorer.md +111 -0
  222. package/presets/opencode/prompts/v0.5.0/reviewer.md +248 -0
  223. package/presets/opencode/prompts/v0.5.0/security-reviewer.md +123 -0
  224. package/presets/opencode/prompts/v0.5.0/task-coach.md +156 -0
  225. package/presets/opencode/prompts/v0.5.0/tester.md +252 -0
  226. package/presets/opencode/prompts/v0.6.0/implementer.md +35 -0
  227. package/presets/opencode/prompts/v0.6.0/planner.md +36 -0
  228. package/presets/opencode/prompts/v0.6.0/reviewer.md +40 -0
  229. package/presets/opencode/prompts/v1.0.0/README.md +47 -0
  230. package/presets/opencode/prompts/v1.0.0/architect.md +259 -0
  231. package/presets/opencode/prompts/v1.0.0/complexity-auditor.md +116 -0
  232. package/presets/opencode/prompts/v1.0.0/contract-builder.md +120 -0
  233. package/presets/opencode/prompts/v1.0.0/devil.md +169 -0
  234. package/presets/opencode/prompts/v1.0.0/docs.md +229 -0
  235. package/presets/opencode/prompts/v1.0.0/goal-planner.md +123 -0
  236. package/presets/opencode/prompts/v1.0.0/implementer.md +228 -0
  237. package/presets/opencode/prompts/v1.0.0/orchestrator.md +474 -0
  238. package/presets/opencode/prompts/v1.0.0/planner.md +72 -0
  239. package/presets/opencode/prompts/v1.0.0/repo-explorer.md +147 -0
  240. package/presets/opencode/prompts/v1.0.0/reviewer.md +295 -0
  241. package/presets/opencode/prompts/v1.0.0/security-reviewer.md +170 -0
  242. package/presets/opencode/prompts/v1.0.0/task-coach.md +235 -0
  243. package/presets/opencode/prompts/v1.0.0/tester.md +298 -0
  244. package/presets/opencode/skills/evaluation/SKILL.md +6 -0
  245. package/presets/opencode/skills/openspec/SKILL.md +50 -0
  246. package/presets/seo-hotel/settings.json +0 -17
  247. package/presets/templates/BACKLOG.md +33 -0
  248. package/presets/templates/execution-profile.yml +6 -0
  249. package/presets/templates/model-comparison.md +11 -0
  250. package/presets/templates/regression-checklist.yml +10 -0
  251. package/src/presets/manifests/agy.yml +2 -2
  252. package/src/presets/manifests/claude.yml +2 -2
  253. package/src/presets/manifests/codex.yml +2 -2
  254. package/src/presets/manifests/cursor.yml +19 -3
  255. package/src/presets/manifests/gemini.yml +2 -2
  256. package/src/presets/manifests/opencode.yml +2 -2
  257. package/src/presets/models/agy.yml +97 -49
  258. package/src/presets/models/claude.yml +83 -39
  259. package/src/presets/models/codex.yml +83 -40
  260. package/src/presets/models/cursor.yml +83 -39
  261. package/src/presets/models/gemini.yml +83 -39
  262. package/src/presets/models/opencode.yml +78 -34
@@ -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 |