|
| 1 | +#!/usr/bin/env php |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * @file |
| 6 | + * Playground script to test Task streaming mode with dimmed output. |
| 7 | + * |
| 8 | + * Run: php playground/task-streaming.php |
| 9 | + */ |
| 10 | + |
| 11 | +declare(strict_types=1); |
| 12 | + |
| 13 | +require_once __DIR__ . '/../vendor/autoload.php'; |
| 14 | + |
| 15 | +use DrevOps\VortexInstaller\Task\Task; |
| 16 | +use DrevOps\VortexInstaller\Utils\Tui; |
| 17 | +use Symfony\Component\Console\Output\ConsoleOutput; |
| 18 | + |
| 19 | +$output = new ConsoleOutput(); |
| 20 | +Tui::init($output); |
| 21 | + |
| 22 | +echo PHP_EOL; |
| 23 | +echo "=== Task Streaming Mode Demo ===" . PHP_EOL; |
| 24 | +echo PHP_EOL; |
| 25 | + |
| 26 | +// Streaming mode with echo - output should be dimmed. |
| 27 | +echo "--- Streaming mode: output via echo ---" . PHP_EOL; |
| 28 | +Task::action( |
| 29 | + label: 'Streaming task using echo', |
| 30 | + action: function () { |
| 31 | + echo "Line 1 via echo...\n"; |
| 32 | + usleep(500000); |
| 33 | + echo "Line 2 via echo...\n"; |
| 34 | + usleep(500000); |
| 35 | + echo "Line 3 via echo...\n"; |
| 36 | + usleep(500000); |
| 37 | + return true; |
| 38 | + }, |
| 39 | + success: 'Echo streaming completed', |
| 40 | + streaming: true, |
| 41 | +); |
| 42 | +echo PHP_EOL; |
| 43 | + |
| 44 | +// Streaming mode with Tui::output() - output should be dimmed. |
| 45 | +echo "--- Streaming mode: output via Tui::output() ---" . PHP_EOL; |
| 46 | +Task::action( |
| 47 | + label: 'Streaming task using Tui::output()', |
| 48 | + action: function () { |
| 49 | + Tui::output()->writeln("Line 1 via Tui::output()..."); |
| 50 | + usleep(500000); |
| 51 | + Tui::output()->writeln("Line 2 via Tui::output()..."); |
| 52 | + usleep(500000); |
| 53 | + Tui::output()->writeln("Line 3 via Tui::output()..."); |
| 54 | + usleep(500000); |
| 55 | + return true; |
| 56 | + }, |
| 57 | + success: 'Tui::output() streaming completed', |
| 58 | + streaming: true, |
| 59 | +); |
| 60 | +echo PHP_EOL; |
| 61 | + |
| 62 | +// Streaming mode with mixed output. |
| 63 | +echo "--- Streaming mode: mixed echo and Tui::output() ---" . PHP_EOL; |
| 64 | +Task::action( |
| 65 | + label: 'Streaming task with mixed output', |
| 66 | + action: function () { |
| 67 | + echo "Line 1 via echo...\n"; |
| 68 | + usleep(400000); |
| 69 | + Tui::output()->writeln("Line 2 via Tui::output()..."); |
| 70 | + usleep(400000); |
| 71 | + echo "Line 3 via echo...\n"; |
| 72 | + usleep(400000); |
| 73 | + Tui::output()->writeln("Line 4 via Tui::output()..."); |
| 74 | + usleep(400000); |
| 75 | + return true; |
| 76 | + }, |
| 77 | + success: 'Mixed streaming completed', |
| 78 | + streaming: true, |
| 79 | +); |
| 80 | +echo PHP_EOL; |
| 81 | + |
| 82 | +// Streaming mode with failure. |
| 83 | +echo "--- Streaming mode: failure case ---" . PHP_EOL; |
| 84 | +Task::action( |
| 85 | + label: 'Streaming task that fails', |
| 86 | + action: function () { |
| 87 | + echo "Starting process...\n"; |
| 88 | + usleep(500000); |
| 89 | + echo "Error encountered!\n"; |
| 90 | + usleep(500000); |
| 91 | + return false; |
| 92 | + }, |
| 93 | + failure: 'Streaming task failed', |
| 94 | + streaming: true, |
| 95 | +); |
| 96 | +echo PHP_EOL; |
| 97 | + |
| 98 | +// Task after failure - verify output is restored. |
| 99 | +echo "--- Task after failure: verify output restoration ---" . PHP_EOL; |
| 100 | +Task::action( |
| 101 | + label: 'Task after failed streaming', |
| 102 | + action: function () { |
| 103 | + echo "This echo should be dimmed\n"; |
| 104 | + usleep(500000); |
| 105 | + Tui::output()->writeln("This Tui::output() should also be dimmed"); |
| 106 | + usleep(500000); |
| 107 | + return true; |
| 108 | + }, |
| 109 | + success: 'Output restoration verified', |
| 110 | + streaming: true, |
| 111 | +); |
| 112 | +echo PHP_EOL; |
| 113 | + |
| 114 | +// Streaming mode without success message (default "OK"). |
| 115 | +echo "--- Streaming mode: no success message (default OK) ---" . PHP_EOL; |
| 116 | +Task::action( |
| 117 | + label: 'Streaming task without success message', |
| 118 | + action: function () { |
| 119 | + echo "Some output...\n"; |
| 120 | + usleep(500000); |
| 121 | + return true; |
| 122 | + }, |
| 123 | + streaming: true, |
| 124 | +); |
| 125 | +echo PHP_EOL; |
| 126 | + |
| 127 | +// Streaming mode with nested spinner (simulates build command with requirements check). |
| 128 | +echo "--- Streaming mode: nested spinner (cursor control) ---" . PHP_EOL; |
| 129 | +Task::action( |
| 130 | + label: 'Streaming task with nested spinner', |
| 131 | + action: function () { |
| 132 | + // The nested command uses spin() which outputs cursor control sequences. |
| 133 | + \Laravel\Prompts\spin( |
| 134 | + function () { |
| 135 | + usleep(300000); |
| 136 | + usleep(300000); |
| 137 | + usleep(300000); |
| 138 | + }, |
| 139 | + 'Nested spinner task...' |
| 140 | + ); |
| 141 | + |
| 142 | + echo "AFTER SPINNER 1\n"; |
| 143 | + \Laravel\Prompts\spin( |
| 144 | + function () { |
| 145 | + usleep(1000000); |
| 146 | + usleep(1000000); |
| 147 | + }, |
| 148 | + 'Another nested spinner task...' |
| 149 | + ); |
| 150 | + |
| 151 | + echo "AFTER SPINNER 2\n"; |
| 152 | + |
| 153 | + return true; |
| 154 | + }, |
| 155 | + streaming: true, |
| 156 | +); |
| 157 | +echo PHP_EOL; |
| 158 | + |
| 159 | +// Streaming mode with colors and styles. |
| 160 | +echo "--- Streaming mode: colors and styles ---" . PHP_EOL; |
| 161 | +Task::action( |
| 162 | + label: 'Streaming task with styled output', |
| 163 | + action: function () { |
| 164 | + Tui::output()->writeln(Tui::green("Green text")); |
| 165 | + usleep(300000); |
| 166 | + Tui::output()->writeln(Tui::blue("Blue text")); |
| 167 | + usleep(300000); |
| 168 | + Tui::output()->writeln(Tui::yellow("Yellow text")); |
| 169 | + usleep(300000); |
| 170 | + Tui::output()->writeln(Tui::underscore("Underscored text")); |
| 171 | + usleep(300000); |
| 172 | + Tui::output()->writeln(Tui::bold("Bold text")); |
| 173 | + usleep(300000); |
| 174 | + Tui::output()->writeln("Mixed: " . Tui::green("green") . " and " . Tui::blue("blue") . " and " . Tui::underscore("underscored")); |
| 175 | + usleep(300000); |
| 176 | + return true; |
| 177 | + }, |
| 178 | + success: 'Styled streaming completed', |
| 179 | + streaming: true, |
| 180 | +); |
| 181 | +echo PHP_EOL; |
| 182 | + |
| 183 | +// Non-streaming task after streaming tasks. |
| 184 | +echo "--- Non-streaming task (spinner) after streaming ---" . PHP_EOL; |
| 185 | +Task::action( |
| 186 | + label: 'Spinner task after streaming', |
| 187 | + action: function () { |
| 188 | + usleep(1000000); |
| 189 | + return true; |
| 190 | + }, |
| 191 | + success: 'Spinner works after streaming', |
| 192 | +); |
| 193 | +echo PHP_EOL; |
| 194 | + |
| 195 | +echo "=== Demo Complete ===" . PHP_EOL; |
| 196 | +echo PHP_EOL; |
0 commit comments