<?php

/*
 * This file is part of Flarum.
 *
 * (c) Toby Zerner <toby.zerner@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Flarum\Console;

use Flarum\Console\Command\GenerateExtensionCommand;
use Flarum\Console\Command\GenerateMigrationCommand;
use Flarum\Debug\Console\InfoCommand;
use Flarum\Foundation\AbstractServer;
use Flarum\Install\Console\InstallCommand;
use Flarum\Update\Console\MigrateCommand;
use Symfony\Component\Console\Application;

class Server extends AbstractServer
{
    public function listen()
    {
        $console = $this->getConsoleApplication();

        exit($console->run());
    }

    /**
     * @return Application
     */
    protected function getConsoleApplication()
    {
        $app = $this->getApp();
        $console = new Application('Flarum', $app->version());

        $app->register('Flarum\Install\InstallServiceProvider');

        $commands = [
            InstallCommand::class,
            MigrateCommand::class,
            InfoCommand::class,
            GenerateExtensionCommand::class,
            GenerateMigrationCommand::class,
        ];

        foreach ($commands as $command) {
            $console->add($app->make(
                $command,
                ['config' => $app->make('flarum.config')]
            ));
        }

        return $console;
    }
}
