app.php 7.15 KB
<?php

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

/*
|--------------------------------------------------------------------------
| Application Environment
|--------------------------------------------------------------------------
|
| Since our legacy code base use ENV instead of APP_ENV to describe environment,
| we need to map it here so that Laravel understands.
|
*/

putenv('APP_ENV='.(getenv('ENV') ?: 'production'));

/*
|--------------------------------------------------------------------------
| Application Debug Mode
|--------------------------------------------------------------------------
|
| When your application is in debug mode, detailed error messages with
| stack traces will be shown on every error that occurs within your
| application. If disabled, a simple generic error page is shown.
|
*/
getenv('ENV') === 'development' || getenv('ENV') === 'testing' || (isset($_REQUEST['dbug']) && $_REQUEST['dbug'] === 'frsecret') ?
    putenv('APP_DEBUG=true') : putenv('APP_DEBUG=false');

/*
|--------------------------------------------------------------------------
| Create The Application
|--------------------------------------------------------------------------
|
| Here we will load the environment and create the application instance
| that serves as the central piece of this framework. We'll use this
| application as an "IoC" container and router for this framework.
|
*/

$app = new FootyRoom\Support\Application(
    realpath(__DIR__.'/../')
);

$app->withFacades(false);

/*
|--------------------------------------------------------------------------
| Register Container Bindings
|--------------------------------------------------------------------------
|
| Now we will register a few bindings in the service container. We will
| register the exception handler and the console kernel. You may add
| your own bindings here if you like or you can make another file.
|
*/

// This is needed because some Lumen classes incorrectly depend on this concrete
// class rather than on contract which does have container binding.
$app->singleton(\Laravel\Lumen\Application::class, function ($app) {
    return $app;
});

$app->singleton(
    \Illuminate\Contracts\Debug\ExceptionHandler::class,
    \FootyRoom\App\Exceptions\Handler::class
);

$app->singleton(
    \Illuminate\Contracts\Console\Kernel::class,
    \FootyRoom\Console\Kernel::class
);

$app->singleton(\FootyRoom\Support\WebpackAssets::class);

$app->singleton(\FootyRoom\Config::class);

$app->singleton(\FootyRoom\Http\Middleware\StartSession::class);

$app->singleton(\FootyRoom\Repositories\IUserRepository::class, \FootyRoom\Repositories\UserRepository::class);

$app->singleton(\Illuminate\Database\Connection::class, function ($app) {
    return $app->make('db')->connection();
});

$app->singleton(\FootyRoom\Support\MongoClient::class);

$app->singleton(\ReCaptcha\ReCaptcha::class, function ($app) {
    $config = $app->make(\FootyRoom\Config::class);

    return new \ReCaptcha\ReCaptcha($config->get('recaptcha.secret'));
});

// Needed for Validation Factory to be injectable.
$app->bind(\Illuminate\Contracts\Translation\Translator::class, function ($app) {
    return $app['translator'];
});

$app->bind(\FootyRoom\Core\Notification\CommentNotifier::class, \FootyRoom\Support\SqlCommentNotifier::class);

// Sessions.
$app->singleton('session', function ($app) {
    return $app->loadComponent('session', \Illuminate\Session\SessionServiceProvider::class);
});

$app->singleton('session.store', function ($app) {
    return $app->loadComponent('session', \Illuminate\Session\SessionServiceProvider::class, 'session.store');
});

$app->alias('session', \Illuminate\Session\SessionManager::class);

// Cookies.
$app->singleton('cookie', function ($app) {
    return $app->loadComponent('session', \Illuminate\Cookie\CookieServiceProvider::class, 'cookie');
});

$app->alias('cookie', \Illuminate\Contracts\Cookie\Factory::class);
$app->alias('cookie', \Illuminate\Contracts\Cookie\QueueingFactory::class);

// Mail.
$app->availableBindings[\Illuminate\Contracts\Mail\Mailer::class] = 'registerMailBindings';

/*
|--------------------------------------------------------------------------
| Load Configuration
|--------------------------------------------------------------------------
|
| Here we will load configuration files needed for operation.
|
*/

/*
|--------------------------------------------------------------------------
| Register Middleware
|--------------------------------------------------------------------------
|
| Next, we will register the middleware with the application. These can
| be global middleware that run before and after each request into a
| route or middleware that'll be assigned to some specific routes.
|
*/
$app->middleware([
    \FootyRoom\Http\Middleware\StartSession::class,
    // Illuminate\Cookie\Middleware\EncryptCookies::class,
    \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
    // Illuminate\Session\Middleware\StartSession::class,
    // Illuminate\View\Middleware\ShareErrorsFromSession::class,
    \FootyRoom\Http\Middleware\VerifyCsrfToken::class,
    \FootyRoom\Http\Middleware\Tracker::class,
    \FootyRoom\Http\Middleware\NewRelic::class,
    // \FootyRoom\Http\Middleware\Prerender::class,
]);

$app->routeMiddleware([
    'auth' => \FootyRoom\Http\Middleware\Auth::class,
    'auth.lock.json' => \FootyRoom\Http\Middleware\AuthLockJson::class,
    'auth.lock.redirect' => \FootyRoom\Http\Middleware\AuthLockRedirect::class,
    'auth.lock.moderator' => \FootyRoom\Http\Middleware\AuthLockModerator::class,
    'auth.lock.admin' => \FootyRoom\Http\Middleware\AuthLockAdmin::class,
    'cors' => \FootyRoom\Http\Middleware\Cors::class,
    'honeypot' => \FootyRoom\Http\Middleware\Honeypot::class,
]);

/*
|--------------------------------------------------------------------------
| Register Service Providers
|--------------------------------------------------------------------------
|
| Here we will register all of the application's service providers which
| are used to bind services into the container. Service providers are
| totally optional, so you are not required to uncomment this line.
|
*/

$app->register(\FootyRoom\Providers\ViewComposerServiceProvider::class);
$app->register(\FootyRoom\Providers\Auth\AuthServiceProvider::class);
$app->register(\FootyRoom\Providers\ElasticEmailServiceProvider::class);
$app->register(\FootyRoom\Providers\EventServiceProvider::class);
$app->register(\FootyRoom\Providers\DoctrineServiceProvider::class);

if (getenv('APP_ENV') === 'development' &&
    $app->make(\FootyRoom\Config::class)->get('debug.barEnabled')
) {
    $app->configure('debugbar');
    $app->register(Barryvdh\Debugbar\LumenServiceProvider::class);
}

/*
|--------------------------------------------------------------------------
| Load The Application Routes
|--------------------------------------------------------------------------
|
| Next we will include the routes file so that they can all be added to
| the application. This will provide all of the URLs the application
| can respond to, as well as the controllers that may handle them.
|
*/

$app->router->group(['namespace' => 'FootyRoom\Http\Controllers'], function ($router) {
    require __DIR__.'/../app/Http/routes.php';
});

return $app;