#!/usr/bin/env php
<?php
/**
 * OrangeHRM is a comprehensive Human Resource Management (HRM) System that captures
 * all the essential functionalities required for any enterprise.
 * Copyright (C) 2006 OrangeHRM Inc., http://www.orangehrm.com
 *
 * OrangeHRM is free software: you can redistribute it and/or modify it under the terms of
 * the GNU General Public License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 *
 * OrangeHRM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with OrangeHRM.
 * If not, see <https://www.gnu.org/licenses/>.
 */

if (php_sapi_name() !== 'cli') {
    echo 'Unauthorized';
    exit(1);
}

$pathToAutoload = realpath(__DIR__ . '/../src/vendor/autoload.php');

$errorMessage = "
Cannot find composer dependencies.
Run below command and try again;\n
$ cd %s
$ composer install -d src
";

if (!$pathToAutoload) {
    echo sprintf($errorMessage, realpath(__DIR__ . '/../'));
    exit(1);
}

use OrangeHRM\Config\Config;
use OrangeHRM\Core\Command\CacheClearCommand;
use OrangeHRM\Core\Command\GenerateDoctrineProxiesCommand;
use OrangeHRM\Framework\Console\Console;
use OrangeHRM\Framework\Console\ConsoleConfigurationInterface;
use OrangeHRM\Framework\Framework;
use OrangeHRM\Framework\Http\Request;
use OrangeHRM\Framework\PluginConfigurationInterface;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Style\SymfonyStyle;

set_time_limit(0);

require_once $pathToAutoload;

$env = 'prod';
$debug = 'prod' !== $env;

$console = new Console();
new Framework($env, $debug); // initializing kernel

$request = new Request();
$pluginConfigs = Config::get('ohrm_plugin_configs');
$input = new ArgvInput();
$output = new ConsoleOutput();
$io = new SymfonyStyle($input, $output);

try {
    foreach (array_values($pluginConfigs) as $pluginConfig) {
        require_once $pluginConfig['filepath'];
        /** @var PluginConfigurationInterface $configClass */
        $configClass = new $pluginConfig['classname']();
        $configClass->initialize($request);
        if ($configClass instanceof ConsoleConfigurationInterface) {
            $configClass->registerCommands($console);
        }
    }
} catch (Throwable $e) {
    $io->warning($e->getMessage());
}

$console->add(new CacheClearCommand());
$console->add(new GenerateDoctrineProxiesCommand());
$console->run($input, $output);
