|
| 1 | +#!/usr/bin/env php |
| 2 | +<?php |
| 3 | + |
| 4 | +use Symfony\Component\Console\Command\Command; |
| 5 | +use Symfony\Component\Console\Helper\Table; |
| 6 | +use Symfony\Component\Console\Input\InputArgument; |
| 7 | +use Symfony\Component\Console\Input\InputInterface; |
| 8 | +use Symfony\Component\Console\Output\OutputInterface; |
| 9 | +use Symfony\Component\Console\SingleCommandApplication; |
| 10 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 11 | +use Symfony\Component\Finder\Finder; |
| 12 | +use Symfony\Component\Finder\SplFileInfo; |
| 13 | +use Symfony\Component\Process\Process; |
| 14 | + |
| 15 | +require_once __DIR__.'/vendor/autoload.php'; |
| 16 | + |
| 17 | +$app = (new SingleCommandApplication('Symfony AI Example Runner')) |
| 18 | + ->setDescription('Runs all Symfony AI examples in folder examples/') |
| 19 | + ->addArgument('subdirectory', InputArgument::OPTIONAL, 'Subdirectory to run examples from, e.g. "anthropic" or "huggingface".') |
| 20 | + ->setCode(function (InputInterface $input, OutputInterface $output) { |
| 21 | + $io = new SymfonyStyle($input, $output); |
| 22 | + $io->title('Symfony AI Examples'); |
| 23 | + |
| 24 | + $directory = __DIR__.'/examples'; |
| 25 | + |
| 26 | + if ($subdirectory = $input->getArgument('subdirectory')) { |
| 27 | + $directory .= '/'.$subdirectory; |
| 28 | + if (!is_dir($directory)) { |
| 29 | + $io->error(sprintf('Subdirectory "%s" does not exist.', $subdirectory)); |
| 30 | + return Command::FAILURE; |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + $examples = (new Finder()) |
| 35 | + ->in($directory) |
| 36 | + ->name('*.php') |
| 37 | + ->sortByName() |
| 38 | + ->files(); |
| 39 | + |
| 40 | + /** @var array{example: SplFileInfo, process: Process} $exampleRuns */ |
| 41 | + $exampleRuns = []; |
| 42 | + foreach ($examples as $example) { |
| 43 | + $exampleRuns[] = [ |
| 44 | + 'example' => $example, |
| 45 | + 'process' => $process = new Process(['php', $example->getRealPath()]), |
| 46 | + ]; |
| 47 | + $process->start(); |
| 48 | + } |
| 49 | + |
| 50 | + $section = $output->section(); |
| 51 | + $renderTable = function () use ($exampleRuns, $section) { |
| 52 | + $section->clear(); |
| 53 | + $table = new Table($section); |
| 54 | + $table->setHeaders(['Example', 'State', 'Output']); |
| 55 | + foreach ($exampleRuns as $run) { |
| 56 | + /** @var SplFileInfo $example */ |
| 57 | + /** @var Process $process */ |
| 58 | + ['example' => $example, 'process' => $process] = $run; |
| 59 | + |
| 60 | + $output = str_replace(PHP_EOL, ' ', $process->getOutput()); |
| 61 | + $output = strlen($output) <= 100 ? $output : substr($output, 0, 100).'...'; |
| 62 | + $emptyOutput = 0 === strlen(trim($output)); |
| 63 | + |
| 64 | + $state = 'Running'; |
| 65 | + if ($process->isTerminated()) { |
| 66 | + $success = $process->isSuccessful() && !$emptyOutput; |
| 67 | + $state = $success ? '<info>Finished</info>' |
| 68 | + : (1 === $run['process']->getExitCode() || $emptyOutput ? '<error>Failed</error>' : '<comment>Skipped</comment>'); |
| 69 | + } |
| 70 | + |
| 71 | + $table->addRow([$example->getRelativePathname(), $state, $output]); |
| 72 | + } |
| 73 | + $table->render(); |
| 74 | + }; |
| 75 | + |
| 76 | + $examplesRunning = fn () => array_reduce($exampleRuns, fn ($running, $example) => $running || $example['process']->isRunning(), false); |
| 77 | + while ($examplesRunning()) { |
| 78 | + $renderTable(); |
| 79 | + sleep(1); |
| 80 | + } |
| 81 | + |
| 82 | + $renderTable(); |
| 83 | + $io->newLine(); |
| 84 | + |
| 85 | + $successCount = array_reduce($exampleRuns, function ($count, $example) { |
| 86 | + if ($example['process']->isSuccessful() && strlen(trim($example['process']->getOutput())) > 0) { |
| 87 | + return $count + 1; |
| 88 | + } |
| 89 | + return $count; |
| 90 | + }, 0); |
| 91 | + |
| 92 | + $totalCount = count($exampleRuns); |
| 93 | + |
| 94 | + if ($successCount < $totalCount) { |
| 95 | + $io->warning(sprintf('%d out of %d examples ran successfully.', $successCount, $totalCount)); |
| 96 | + } else { |
| 97 | + $io->success(sprintf('All %d examples ran successfully!', $totalCount)); |
| 98 | + } |
| 99 | + |
| 100 | + foreach ($exampleRuns as $run) { |
| 101 | + if (!$run['process']->isSuccessful()) { |
| 102 | + $io->section('Error in ' . $run['example']->getRelativePathname()); |
| 103 | + $io->text($run['process']->getOutput()); |
| 104 | + $io->text($run['process']->getErrorOutput()); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + return Command::SUCCESS; |
| 109 | + }) |
| 110 | + ->run(); |
0 commit comments