<commit_before><?php

namespace StackGuru;

use \StackGuru\CoreLogic\Utils;


/**
 * CommandRegistry is responsible for the initialization of commands, their storage
 * and accessibility.
 *
 * It maintains a map of every command and it's subcommands, and holds information
 * about the command and an instance of the class, which can be used to execute
 * commands.
 */
class CommandRegistry
{
    private $commands   = [
        // string "command_name"    => [
        //      string "command_name"       => \StackGuru\CommandInterface,
        //      string "sub_command_name"   => \StackGuru\CommandInterface,
        // ],
    ];


    /**
     * Initialize a CommandRegistry object.
     *
     * @param array $commandFolders = []
     */
    function __construct (array $commandFolders)
    {
        // Load all given folders into the registry.
        foreach ($commandFolders as $folder)
            $this->commands = array_merge($this->commands, $this->loadCommands($folder));
    }


    /**
     * Returns the command instance and trimmed query for the latest relative command in the string.
     *
     * @return array instance and new query string after matched command
     */
	public function parseQuery (string $query) : array
	{
        echo "Parsing query: ", $query, PHP_EOL;
        $prevCommand = null;
        $nextCommand = Utils\Commands::getFirstWordFromString($query);

        $response = [
            "query" => $query,
            "instance" => null
        ];

        $maxChain = 10;
        $depth = 0;

        $commands = $this->commands;

        while ($depth++ < $maxChain) {
            // Update command to the new first word..
            $nextCommand = Utils\Commands::getFirstWordFromString($query);

            /*
             * The first word, aka command, wasn't valid.
             * The first word is not a command.
             *
             * assumption1: $options is always an array
             *
             * assumption2: if there are no matches, it might be a default command..
             *                  Google.google = default..
             */
            if (!isset($this->commands[$nextCommand])) {
                if (null === $prevCommand || !isset($commands[$prevCommand])) {
                    return $response;
                }

                $command = $commands[$prevCommand];
                // var_dump(array(
                //     "prevCommand" => $prevCommand,<commit_msg>Fix command routing<commit_after><?php

namespace StackGuru;

use \StackGuru\CoreLogic\Utils;


/**
 * CommandRegistry is responsible for the initialization of commands, their storage
 * and accessibility.
 *
 * It maintains a map of every command and it's subcommands, and holds information
 * about the command and an instance of the class, which can be used to execute
 * commands.
 */
class CommandRegistry
{
    private $commands   = [
        // string "command_name"    => [
        //      string "command_name"       => \StackGuru\CommandInterface,
        //      string "sub_command_name"   => \StackGuru\CommandInterface,
        // ],
    ];


    /**
     * Initialize a CommandRegistry object.
     *
     * @param array $commandFolders = []
     */
    function __construct (array $commandFolders)
    {
        // Load all given folders into the registry.
        foreach ($commandFolders as $folder)
            $this->commands = array_merge($this->commands, $this->loadCommands($folder));
    }


    /**
     * Returns the command instance and trimmed query for the latest relative command in the string.
     *
     * @return array instance and new query string after matched command
     */
	public function parseQuery (string $query) : array
	{
        echo "Parsing query: ", $query, PHP_EOL;
        $prevCommand = null;
        $nextCommand = Utils\Commands::getFirstWordFromString($query);

        $response = [
            "query" => $query,
            "instance" => null
        ];

        $maxChain = 10;
        $depth = 0;

        $commands = $this->commands;

        while ($depth++ < $maxChain) {
            // Update command to the new first word..
            $nextCommand = Utils\Commands::getFirstWordFromString($query);

            /*
             * The first word, aka command, wasn't valid.
             * The first word is not a command.
             *
             * assumption1: $options is always an array
             *
             * assumption2: if there are no matches, it might be a default command..
             *                  Google.google = default..
             */
            if (!isset($commands[$nextCommand])) {
                if (null === $prevCommand || !isset($commands[$prevCommand])) {
                    return $response;
                }

                $command = $commands[$prevCommand];
                // var_dump(array(
                //     "prevCommand" => $prevCommand,
{'fraction_non_alphanumeric': 0.10650406504065041, 'fraction_numerical': 0.0020325203252032522, 'mean_word_length': 1.8577235772357723, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2451641', 'n_tokens_mistral': 1286, 'n_tokens_neox': 1232, 'n_words': 387}