I'm using the Symfonys Console component and I'm creating a command that displays what files in a directory have been executed. To do this I want to display the data in a Table using their Table helper.
In their documentation it only uses examples with hard coded output, is it possible to have this data added dynamically. Here is my code:
// Create a new Table object
$table = new Table($output);
// Set the table headers
$table->setHeaders(['Executed', 'File', 'Processed']);
// Create a new Iterator object
$iterator = new FilesystemIterator(dirname(__DIR__) . Config::SCRIPT_PATH);
// Cycle through iterated files
foreach ($iterator as $item) {
// Set the file
$file = (object) [
'executed' => 'No',
'name' => $item->getFilename(),
'processed' => ''
];
// Find the script in the database
$script = Script::where('file', $file->name)->first();
// Check Script exists
if ($script) {
// Update the file properties
$file->executed = 'Yes';
$file->processed = $script->created_at
}
// Set the table rows
$table->setRows([
[$file->executed, $file->name, $file->processed]
]);
}
// Render the table to the console
$table->render();
To me, every file it should find (currently 3) should be displayed in the table with it's correct data, but it only shows the last one, so each time setRows() is obviously being overwritten by the last cycle of the $iterator loop.
I tried creating a $files array and pushing each $file into it at the end of the $iterator loop, then moving $table->setRows() outside of the $iterator loop but of course doing a foreach ($files as $file) with setRows() inside it brings you back to the same situation of the last loop overriding the previous one.
As far as I can see from their documentation there isn't a setRow() method to set an individual row that I could use for each $iterator loop and you can't put a foreach loop within the setRows method either.
There must be a way to set the rows dynamically but I'm failing to see it, hopefully someone can help me out.