<?php

header("access-control-allow-origin: *");
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

date_default_timezone_set('asia/karachi');

if (PHP_SAPI == 'cli-server') {
    // To help the built-in PHP dev server, check if the request was actually for
    // something which should probably be served as a static file
    $url  = parse_url($_SERVER['REQUEST_URI']);
    $file = __DIR__ . $url['path'];
    if (is_file($file)) {
        return false;
    }
}

require __DIR__ . '/vendor/autoload.php';

//After making any changes in structure, run this command on command prompt
//composer dump-autoload -o

session_cache_limiter(false);
session_start();

// App Configurations
require __DIR__ . '/config.php';

// Instantiate the app
$settings = require __DIR__ . '/app/settings.php';

/***************************************************/
$console = PHP_SAPI == 'cli' ? true : false;

// Making app work from cli aswell
if (PHP_SAPI == 'cli') {
    $argv = $GLOBALS['argv'];
    array_shift($argv);

    $pathInfo       = implode('/', $argv);

    $env = \Slim\Http\Environment::mock(['REQUEST_URI' => '/' . $pathInfo]);
    $settings['environment'] = $env; 
}
/***************************************************/

$app = new \Slim\App($settings);
$container = $app->getContainer();

/***************************************************/

//boot eloquent connection 
$capsule = new \Illuminate\Database\Capsule\Manager; 
$capsule->addConnection($container['settings']['db']);
$capsule->setAsGlobal();
$capsule->bootEloquent();

//pass the connection to global container (created in previous article) 
$container['db'] = function ($container) use ($capsule){
   return $capsule;
};
 
$container['HomeController'] = function ($container) {
   return new \App\Controllers\HomeController($container);
};

/***************************************************/

// Set up dependencies
require __DIR__ . '/app/dependencies.php';

// Register middleware
require __DIR__ . '/app/middleware.php';

// Register routes
if($console) {
	set_time_limit(0);
	require __DIR__ . '/app/routes_console.php';
} else {
	require __DIR__ . '/app/routes.php';
}

// Run app
$app->run();
