54 lines
1.5 KiB
PHP
54 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Providers;
|
|
|
|
use App\Game\GameBoardState;
|
|
use App\Game\GameService;
|
|
use App\Game\GameSession;
|
|
|
|
use Illuminate\Support\Facades\Session;
|
|
use Illuminate\Support\ServiceProvider;
|
|
use Illuminate\Contracts\Support\DeferrableProvider;
|
|
|
|
class GameServiceProvider extends ServiceProvider implements DeferrableProvider
|
|
{
|
|
/**
|
|
* Register any application services.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function register()
|
|
{
|
|
$this->app->bind(GameBoardState::class, function ($app) {
|
|
$width = (int) config('app.game.board.width', 4);
|
|
$height = (int) config('app.game.board.height', 4);
|
|
return new GameBoardState($width, $height);
|
|
});
|
|
|
|
$this->app->singleton(GameService::class, function ($app) {
|
|
// Try loading from session.
|
|
$session_key = config('app.game.session_name', 'game.service');
|
|
$service = Session::get($session_key);
|
|
|
|
// No service in session, create a new one and store it.
|
|
if (!$service) {
|
|
$game_session = $app->make(GameSession::class);
|
|
$service = new GameService($game_session);
|
|
Session::put($session_key, $service);
|
|
}
|
|
return $service;
|
|
});
|
|
|
|
$this->app->alias(GameService::class, 'game');
|
|
}
|
|
|
|
/**
|
|
* Get the services provided by the provider.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function provides()
|
|
{
|
|
return ['game', GameService::class, GameBoardState::class];
|
|
}
|
|
}
|