#!/usr/bin/env php
<?php

/*
 * HiDev - integrate your development.
 *
 * @link      http://hidev.me/
 * @package   hidev
 * @license   BSD 3-clause
 * @copyright Copyright (c) 2015 HiQDev
 */

ini_set('error_reporting', E_ALL & ~E_DEPRECATED & ~E_STRICT & ~E_NOTICE);

defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');

// fcgi doesn't have STDIN and STDOUT defined by default
defined('STDIN') or define('STDIN', fopen('php://stdin', 'r'));
defined('STDOUT') or define('STDOUT', fopen('php://stdout', 'w'));

$autoload_path = __DIR__ . '/vendor/autoload.php';
if (!file_exists($autoload_path)) {
    system("composer install");
}

require($autoload_path);
require(__DIR__ . '/vendor/yiisoft/yii2/Yii.php');

Yii::setAlias('@hidev', __DIR__);
Yii::setLogger(Yii::createObject('hidev\base\Logger'));

$application = new hidev\base\Application([
    'id'                    => 'hidev',
    'name'                  => 'HiDev - integrate your development',
    'basePath'              => __DIR__,
    'enableCoreCommands'    => false,
    'controllerNamespace'   => 'hidev\\controllers',
    'defaultRoute'          => 'default',
    'bootstrap'             => ['log', 'pluginManager', 'config'],
    'components'            => [
        'log' => [
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning', 'info'],
                ],
            ],
        ],
        'robo' => [
            'class' => 'hidev\goals\RoboGoal',
        ],
        'config' => [
            'class' => 'hidev\goals\ConfigGoal',
        ],
        'pluginManager' => [
            'class' => 'hiqdev\pluginmanager\PluginManager',
            'goals' => [
                'README.md'             => 'readme',
                'README.txt'            => 'readme',
                'README.markdown'       => 'readme',
                'LICENSE'               => 'license',
                'LICENSE.md'            => 'license',
                'LICENSE.txt'           => 'license',
                'LICENSE.markdown'      => 'license',
                'CHANGELOG.md'          => 'changelog',
                'CHANGELOG.txt'         => 'changelog',
                'CHANGELOG.markdown'    => 'changelog',
            ],
        ],
        'request' => [
            'class' => 'hidev\base\Request',
        ],
        'view' => [
            'class' => 'hidev\base\View',
            'theme' => [
                'pathMap' => [
                    '@parent/templates' => ['@config/templates', '@app/views'],
                ],
            ],
            'renderers' => [
                'twig' => [
                    'class' => 'yii\twig\ViewRenderer',
                    'cachePath' => '@runtime/Twig/cache',
                    'options' => [
                        'auto_reload' => true,
                    ],
                    'extensions' => ['Twig_Extension_StringLoader'],
                ],
            ],
        ],
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
    ],
]);

$exitCode = $application->run();
exit($exitCode);

function d ()
{
    foreach (func_get_args() as $a) {
        var_dump($a);
    }
    debug_print_backtrace(0,1);
    die();
}
