<?php namespace Mcnd\CLI; class CommandStack { public array $commands = []; public function __construct() {} public function merge(CommandStack $stack) : void { $this->commands = array_merge($this->commands, $stack->commands); } public function append(Command $command) : void { $this->commands[] = $command; } /** * Matches the closest command found from the stack * * @param array $commands * @param array $options * @return Command|null */ public function matchCommand(array $commands) : null|Command { while ($commands) { $cmd = implode(' ', $commands); foreach ($this->commands as $command) { if ($command->name === $cmd) { return $command; } } array_pop($commands); } return null; } }