<commit_before><?php
namespace Concrete\Core\Foundation\Command;

use Concrete\Core\Application\Application;
use Concrete\Core\Foundation\Command\Handler\MethodNameInflector\HandleClassNameWithFallbackInflector;
use League\Tactician\Bernard\QueueCommand;
use League\Tactician\Bernard\QueueMiddleware;
use League\Tactician\CommandBus;
use Concrete\Core\Foundation\Command\Middleware\BatchUpdatingMiddleware;
use League\Tactician\Handler\CommandHandlerMiddleware;
use League\Tactician\Handler\CommandNameExtractor\ClassNameExtractor;
use League\Tactician\Handler\Locator\InMemoryLocator;

class Dispatcher
{

    const BUS_TYPE_SYNC = 1; // Synchronous command bus
    const BUS_TYPE_ASYNC = 2; // Async/queue bus

    /**
     * @var mixed
     */
    protected $queuableCommands = [];

    /**
     * @var CommandBus[]
     */
    protected $buses;

    /**
     * @var InMemoryLocator
     */
    protected $locator;


    /**
     * @var Application
     */
    protected $app;

    /**
     * @var string
     */
    protected $defaultQueue;

    /**
     * @return string
     */
    public function getDefaultQueue()
    {
        return $this->defaultQueue;
    }

    /**
     * @param string $defaultQueue
     */
    public function setDefaultQueue($defaultQueue)
    {
        $this->defaultQueue = $defaultQueue;
    }

    /**
     * Dispatcher constructor.
     * @param Application $app
     */
    public function __construct(Application $app)
    {
        $this->app = $app;
        $this->locator = new InMemoryLocator();
        $this->addBus(self::BUS_TYPE_SYNC, new CommandBus([
            $this->app->make(BatchUpdatingMiddleware::class),
            new CommandHandlerMiddleware(
                new ClassNameExtractor(),
                $this->locator,
                new HandleClassNameWithFallbackInflector()
            )
        ]));
        $this->addBus(self::BUS_TYPE_ASYNC, new CommandBus([new QueueMiddleware($this->app->make('queue/producer'))]));
    }

    public function addBus(int $type, CommandBus $bus)
    {
        $this->buses[$type] = $bus;
    }

    public function getBus(int $type)
    {
        return $this->buses[$type];
    }

    public function getSynchronousBus()
    {
        return $this->getBus(self::BUS_TYPE_SYNC);
    }

    public function getQueueBus()
    {
        return $this->getBus(self::BUS_TYPE_ASYNC);
    }

    protected function registerQueuableCommand($command, $queue)
    {
        if ($queue === true) {
            $queue = $this->getDefaultQueue();
        }
        $this->queuableCommands[$command] = $queue;
    }

    public function registerCommand($handler, $command, $queue = null)
    {
        $this->locator->addHandler($handler, $command);
        if ($queue) {
            $this->registerQueuableCommand($command, $queue);
        }
    }

    /**
     * @param CommandInterface $command
     * @return string
     */
    public function getQueueForCommand(CommandInterface $command)
    {
        $queue = $this->getDefaultQueue();
        foreach($this->queuableCommands as $queueableCommand => $queue)
        {
            if ($command instanceof $queueableCommand) {
                $queue = $queue;
                break;
            }
        }
        return $queue;
    }<commit_msg>tweaks to command dispatcher<commit_after><?php
namespace Concrete\Core\Foundation\Command;

use Concrete\Core\Application\Application;
use Concrete\Core\Foundation\Command\Handler\MethodNameInflector\HandleClassNameWithFallbackInflector;
use League\Tactician\Bernard\QueueCommand;
use League\Tactician\Bernard\QueueMiddleware;
use League\Tactician\CommandBus;
use Concrete\Core\Foundation\Command\Middleware\BatchUpdatingMiddleware;
use League\Tactician\Handler\CommandHandlerMiddleware;
use League\Tactician\Handler\CommandNameExtractor\ClassNameExtractor;
use League\Tactician\Handler\Locator\InMemoryLocator;

class Dispatcher
{

    const BUS_TYPE_SYNC = 1; // Synchronous command bus
    const BUS_TYPE_ASYNC = 2; // Async/queue bus

    /**
     * @var mixed
     */
    protected $queuableCommands = [];

    /**
     * @var CommandBus[]
     */
    protected $buses;

    /**
     * @var InMemoryLocator
     */
    protected $locator;


    /**
     * @var Application
     */
    protected $app;

    /**
     * @var string
     */
    protected $defaultQueue;

    /**
     * @return string
     */
    public function getDefaultQueue()
    {
        return $this->defaultQueue;
    }

    /**
     * @param string $defaultQueue
     */
    public function setDefaultQueue($defaultQueue)
    {
        $this->defaultQueue = $defaultQueue;
    }

    /**
     * Dispatcher constructor.
     * @param Application $app
     */
    public function __construct(Application $app)
    {
        $this->app = $app;
        $this->locator = new InMemoryLocator();
        $this->addBus(self::BUS_TYPE_SYNC, new CommandBus([
            $this->app->make(BatchUpdatingMiddleware::class),
            new CommandHandlerMiddleware(
                new ClassNameExtractor(),
                $this->locator,
                new HandleClassNameWithFallbackInflector()
            )
        ]));
        $this->addBus(self::BUS_TYPE_ASYNC, new CommandBus([new QueueMiddleware($this->app->make('queue/producer'))]));
    }

    public function addBus(int $type, CommandBus $bus)
    {
        $this->buses[$type] = $bus;
    }

    public function getBus(int $type)
    {
        return $this->buses[$type];
    }

    public function getSynchronousBus()
    {
        return $this->getBus(self::BUS_TYPE_SYNC);
    }

    public function getQueueBus()
    {
        return $this->getBus(self::BUS_TYPE_ASYNC);
    }

    public function registerCommand($handler, $command, $queue = null)
    {
        $this->locator->addHandler($handler, $command);
        if ($queue) {
            if ($queue === true) {
                $queue = $this->getDefaultQueue();
            }
            $this->queuableCommands[$command] = $queue;
        }
    }

    /**
     * @param CommandInterface $command
     * @return string
     */
    public function getQueueForCommand(CommandInterface $command)
    {
        $queue = $this->getDefaultQueue();
        foreach($this->queuableCommands as $queueableCommand => $queue)
        {
            if ($command instanceof $queueableCommand) {
                $queue = $queue;
                break;
            }
        }
        return $queue;
    }
{'fraction_non_alphanumeric': 0.12057505023960427, 'fraction_numerical': 0.0006183335909723295, 'mean_word_length': 2.3558091286307055, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 48, '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': '5992361', 'n_tokens_mistral': 1886, 'n_tokens_neox': 1860, 'n_words': 335}