Skip to content

Commit

Permalink
♻️ Changed some Controllers
Browse files Browse the repository at this point in the history
- moved some controllers into a different namespace
- renamed some controllers -> removed the "Controller" suffix
  • Loading branch information
Heroyt committed Feb 14, 2024
1 parent 187f660 commit 97955d1
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 63 deletions.
2 changes: 1 addition & 1 deletion assets/js/api/endpoints/games.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export async function getLastGames(limit: number = 10, orderBy: string = 'start'
}

export async function getGameHighlights(code: string): Promise<Highlight[]> {
return fetchGet(`/laserliga/games/${code}/highlights`);
return fetchGet(`/api/laserliga/games/${code}/highlights`);
}

export async function reimportResults(code: string): Promise<FormSaveResponse> {
Expand Down
44 changes: 36 additions & 8 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
* @brief API route definitions
*/

use App\Controllers\Api\Cache;
use App\Controllers\Api\Debug;
use App\Controllers\Api\Events;
use App\Controllers\Api\GameHelpers;
use App\Controllers\Api\GameLoading;
use App\Controllers\Api\Games;
use App\Controllers\Api\LaserLiga;
use App\Controllers\Api\Logs;
use App\Controllers\Api\Mount;
use App\Controllers\Api\Results;
Expand Down Expand Up @@ -42,17 +44,43 @@
->put('incrementCache', [Debug::class, 'incrementCache'])
->get('glob', [Debug::class, 'glob']);

$gameGroup = $apiGroup->group('game')
->post('load/{system}', [GameLoading::class, 'loadGame'])
->get('loaded', [GameHelpers::class, 'getLoadedGameInfo'])
->get('gate', [GameHelpers::class, 'getGateGameInfo'])
->post('{code}/recalcSkill', [GameHelpers::class, 'recalcSkill'])
->post('{code}/recalcScores', [GameHelpers::class, 'recalcScores'])
->post('{code}/changeMode', [GameHelpers::class, 'changeGameMode']);
$gameGroup = $apiGroup->group('game')->post('load/{system}', [GameLoading::class, 'loadGame'])->get(
'loaded',
[
GameHelpers::class,
'getLoadedGameInfo',
]
)->get('gate', [GameHelpers::class, 'getGateGameInfo'])->post(
'{code}/recalcSkill',
[GameHelpers::class, 'recalcSkill']
)->post('{code}/recalcScores', [GameHelpers::class, 'recalcScores'])->post(
'{code}/changeMode',
[GameHelpers::class, 'changeGameMode']
);

$gamesGroup = $apiGroup->group('games')
->get('', [Games::class, 'listGames'])
->post('sync', [Games::class, 'syncGames'])
->post('sync/{limit}', [Games::class, 'syncGames'])
->get('{code}', [Games::class, 'getGame'])
->post('simulate', [Games::class, 'simulate']);
->post('simulate', [Games::class, 'simulate']);


$apiGroup->group('laserliga')->group('games')->group('{code}')->get('highlights', [LaserLiga::class, 'highlights']);

$apiGroup->group('cache')
->group('clear')
->post('', [Cache::class, 'clearAll'])
->name('cache-clear')
->post('system', [Cache::class, 'clearSystem'])
->name('cache-clear-system')
->post('di', [Cache::class, 'clearDi'])
->name('cache-clear-di')
->post('models', [Cache::class, 'clearModels'])
->name('cache-clear-models')
->post('config', [Cache::class, 'clearConfig'])
->name('cache-clear-config')
->post('results', [Cache::class, 'clearResults'])
->name('cache-clear-results')
->post('latte', [Cache::class, 'clearLatte'])
->name('cache-clear-latte');
22 changes: 7 additions & 15 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
*/

use App\Controllers\GamesList;
use App\Controllers\Gate;
use App\Controllers\Gate\Gate;
use App\Controllers\Lang;
use App\Controllers\LaserLigaController;
use App\Controllers\NewGame;
use App\Controllers\PlayersController;
use App\Controllers\Players;
use App\Controllers\Results;
use App\Controllers\Settings;
use App\Controllers\Settings\Settings;
use App\Core\App;
use App\Services\FeatureConfig;
use Lsr\Core\Routing\Route;
Expand Down Expand Up @@ -61,15 +60,8 @@
->post('/idle/{system}', [Gate::class, 'setGateIdle']);

Route::group('/players')
->get('/find', [PlayersController::class, 'find'])
->get('/find/{code}', [PlayersController::class, 'getPlayer'])
->get('/sync/{code}', [PlayersController::class, 'syncPlayer'])
->get('/find', [Players::class, 'find'])
->get('/find/{code}', [Players::class, 'getPlayer'])
->get('/sync/{code}', [Players::class, 'syncPlayer'])
->group('/public')
->get('/find', [PlayersController::class, 'findPublic']);

Route::group('laserliga')
->group('games')
->group('{code}')
->get('highlights', [LaserLigaController::class, 'highlights'])
->endGroup()
->endGroup();
->get('/find', [Players::class, 'findPublic']);
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
<?php

namespace App\Controllers;
namespace App\Controllers\Api;

use Lsr\Core\Caching\Cache;
use Lsr\Core\Caching\Cache as CacheService;
use Lsr\Core\Controllers\Controller;
use Lsr\Core\Routing\Attributes\Post;
use Lsr\Core\Templating\Latte;

class CacheController extends Controller
class Cache extends Controller
{

public function __construct(protected Latte $latte, protected Cache $cache) {
public function __construct(protected Latte $latte, protected CacheService $cache) {
parent::__construct($latte);
}

#[Post('/cache/clear', 'cache-clear')]
public function clearAll() : void {
public function clearAll(): void {
$this->cache->clean([\Nette\Caching\Cache::All => true]);
/** @var string[] $files */
$files = array_merge(
glob(TMP_DIR . '*.cache'),
glob(TMP_DIR . '*.php'),
glob(TMP_DIR . '*.php.lock'),
glob(TMP_DIR.'di/*'),
glob(TMP_DIR.'results/*'),
glob(TMP_DIR.'resultCaches/*'),
glob(TMP_DIR.'latte/*'),
glob(TMP_DIR . 'di/*'),
glob(TMP_DIR . 'results/*'),
glob(TMP_DIR . 'resultCaches/*'),
glob(TMP_DIR . 'latte/*'),
glob(TMP_DIR . 'models/*'),
);
$deleted = 0;
Expand All @@ -37,14 +35,12 @@ public function clearAll() : void {
$this->respond(['status' => 'ok', 'deleted' => $deleted, 'total' => count($files)]);
}

#[Post('/cache/clear/system', 'cache-clear-system')]
public function clearSystem() : void {
public function clearSystem(): void {
$this->cache->clean([\Nette\Caching\Cache::All => true]);
$this->respond(['status' => 'ok']);
}

#[Post('/cache/clear/di', 'cache-clear-di')]
public function clearDi() : void {
public function clearDi(): void {
/** @var string[] $files */
$files = array_merge(
glob(TMP_DIR . '*.php'),
Expand All @@ -60,7 +56,6 @@ public function clearDi() : void {
$this->respond(['status' => 'ok', 'deleted' => $deleted, 'total' => count($files)]);
}

#[Post('/cache/clear/models', 'cache-clear-models')]
public function clearModels(): void {
/** @var string[] $files */
$files = glob(TMP_DIR . 'models/*');
Expand All @@ -73,7 +68,6 @@ public function clearModels(): void {
$this->respond(['status' => 'ok', 'deleted' => $deleted, 'total' => count($files)]);
}

#[Post('/cache/clear/config', 'cache-clear-config')]
public function clearConfig(): void {
/** @var string[] $files */
$files = glob(TMP_DIR . '*.cache');
Expand All @@ -86,12 +80,11 @@ public function clearConfig(): void {
$this->respond(['status' => 'ok', 'deleted' => $deleted, 'total' => count($files)]);
}

#[Post('/cache/clear/results', 'cache-clear-results')]
public function clearResults() : void {
public function clearResults(): void {
/** @var string[] $files */
$files = array_merge(
glob(TMP_DIR.'results/*'),
glob(TMP_DIR.'resultCaches/*'),
glob(TMP_DIR . 'results/*'),
glob(TMP_DIR . 'resultCaches/*'),
);
$deleted = 0;
foreach ($files as $file) {
Expand All @@ -102,10 +95,9 @@ public function clearResults() : void {
$this->respond(['status' => 'ok', 'deleted' => $deleted, 'total' => count($files)]);
}

#[Post('/cache/clear/latte', 'cache-clear-latte')]
public function clearLatte() : void {
public function clearLatte(): void {
/** @var string[] $files */
$files = glob(TMP_DIR.'latte/*');
$files = glob(TMP_DIR . 'latte/*');
$deleted = 0;
foreach ($files as $file) {
if (unlink($file)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

namespace App\Controllers;
namespace App\Controllers\Api;

use App\Services\LigaApi;
use Lsr\Core\Controllers\Controller;
use Lsr\Core\Templating\Latte;

class LaserLigaController extends Controller
class LaserLiga extends Controller
{

public function __construct(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Controllers;
namespace App\Controllers\Gate;

use App\Core\App;
use App\Core\Info;
Expand Down
2 changes: 1 addition & 1 deletion src/Controllers/Gate.php → src/Controllers/Gate/Gate.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @author Tomáš Vojík <xvojik00@stud.fit.vutbr.cz>, <vojik@wboy.cz>
*/

namespace App\Controllers;
namespace App\Controllers\Gate;

use App\Core\Info;
use App\GameModels\Factory\GameFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Lsr\Core\Requests\Request;
use Lsr\Core\Templating\Latte;

class PlayersController extends Controller
class Players extends Controller
{

public function __construct(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Controllers;
namespace App\Controllers\Settings;

use App\Core\App;
use App\Exceptions\GameModeNotFoundException;
Expand Down Expand Up @@ -30,7 +30,7 @@
/**
*
*/
class ModesSettings extends Controller
class Modes extends Controller
{

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Controllers;
namespace App\Controllers\Settings;

use App\Core\App;
use App\Models\MusicMode;
Expand All @@ -19,7 +19,7 @@
/**
*
*/
class MusicSettings extends Controller
class Music extends Controller
{

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @author Tomáš Vojík <xvojik00@stud.fit.vutbr.cz>, <vojik@wboy.cz>
*/

namespace App\Controllers;
namespace App\Controllers\Settings;

use App\Core\App;
use App\Core\Info;
Expand Down
11 changes: 6 additions & 5 deletions templates/pages/settings/cache.latte
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,27 @@

<div class="my-2">
<button id="clear-all" class="btn btn-lg btn-danger btn-clear-cache w-100"
data-href="{link ['cache', 'clear']}">
data-href="{link ['api', 'cache', 'clear']}">
{lang 'Smazat vše', context: 'actions'}
</button>
</div>

<div class="text-center">
<div class="btn-group">
<button id="clear-system" class="btn btn-warning btn-clear-cache"
data-href="{link ['cache', 'clear', 'system']}">
data-href="{link ['api', 'cache', 'clear', 'system']}">
{lang 'Smazat systémovou cache', context: 'actions'}
</button>
<button id="clear-di" class="btn btn-info btn-clear-cache" data-href="{link ['cache', 'clear', 'di']}">
<button id="clear-di" class="btn btn-info btn-clear-cache"
data-href="{link ['api', 'cache', 'clear', 'di']}">
{lang 'Smazat DI cache', context: 'actions'}
</button>
<button id="clear-results" class="btn btn-primary btn-clear-cache"
data-href="{link ['cache', 'clear', 'results']}">
data-href="{link ['api', 'cache', 'clear', 'results']}">
{lang 'Smazat cache výsledků', context: 'actions'}
</button>
<button id="clear-latte" class="btn btn-success btn-clear-cache"
data-href="{link ['cache', 'clear', 'latte']}">
data-href="{link ['api', 'cache', 'clear', 'latte']}">
{lang 'Smazat cache šablon', context: 'actions'}
</button>
</div>
Expand Down

0 comments on commit 97955d1

Please sign in to comment.