Skip to content

Commit 35b2296

Browse files
committed
refactor($app): removed $app
1 parent 3673258 commit 35b2296

File tree

3 files changed

+52
-58
lines changed

3 files changed

+52
-58
lines changed

app/config/bootstrap.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,16 @@
1111
Flight::halt(500, 'Config file not found. Please create a config.php file in the app/config directory to get started.');
1212
}
1313

14-
// It is better practice to not use static methods for everything. It makes your
15-
// app much more difficult to unit test easily.
16-
$app = Flight::app();
17-
1814
/*
1915
* Load the config file
2016
* P.S. When you require a php file and that file returns an array, the array
2117
* will be returned by the require statement where you can assign it to a var.
2218
*/
2319
$config = require __DIR__ . '/config.php';
24-
$app->set('config', $config);
20+
Flight::set('config', $config);
2521

2622
// Whip out the ol' router and we'll pass that to the routes file
27-
$router = $app->router();
23+
$router = Flight::router();
2824

2925
/*
3026
* Load the routes file. the $router variable above is passed into the routes.php
@@ -62,7 +58,8 @@
6258

6359
// At this point, your app should have all the instructions it needs and it'll
6460
// "start" processing everything. This is where the magic happens.
65-
$app->start();
61+
Flight::start();
62+
6663
/*
6764
.----..---. .--. .----. .---. .---. .-. .-. .--. .---. .----. .-. .-..----. .----..-. .-.
6865
{ {__ {_ _}/ {} \ | {} }{_ _} {_ _}| {_} | / {} \{_ _} | {} }| { } || {} }| {} }\ \/ /

app/config/routes.php

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,103 +4,106 @@
44
use app\middleware\HeaderSecurityMiddleware;
55
use app\utils\DocsLogic;
66
use app\utils\Translator;
7-
use app\utils\CustomEngine;
7+
use flight\Container;
88
use flight\net\Router;
99

10-
/** @var CustomEngine $app */
11-
/** @var Router $router */
12-
1310
Flight::route('GET /api/status', static fn() => Flight::json(['status' => 'ok']));
1411

1512
// This acts like a global middleware
16-
$router->group('', function (Router $router) use ($app) {
17-
13+
Flight::group('', static function (): void {
1814
/*
1915
* Specific routes
2016
*/
2117
// This processes github webhooks
22-
$router->post('/update-stuff', [DocsController::class, 'updateStuffPost'], false, 'update_stuff');
18+
Flight::route(
19+
'POST /update-stuff',
20+
[DocsController::class, 'updateStuffPost'],
21+
false,
22+
'update_stuff'
23+
);
2324

2425
/*
2526
* Redirects
2627
*/
2728
// if theres no language or version in the url, redirect and default to en and v3
28-
$app->route('/', function () use ($app) {
29+
Flight::route('/', static function (): void {
2930
// pull out the default language by the accept header
3031
$language = Translator::getLanguageFromRequest();
31-
$app->redirect('/'.$language.'/v3/');
32+
Flight::redirect('/'.$language.'/v3/');
3233
});
3334

3435
// If the route only defines a language (ex: /en) redirect with a version
35-
$app->route('/@language:[a-z0-9]{2}', function (string $language) use ($app): void {
36+
Flight::route('/@language:[a-z0-9]{2}', static function (string $language): void {
3637
// if there's a number in it, it's actually probably the version so we'll need to pull the language out and consider this a version
3738
if (preg_match('/\d/', $language) === 1) {
3839
$version = $language;
3940
$language = Translator::getLanguageFromRequest();
40-
$app->redirect("/en/$language/");
41+
Flight::redirect("/en/$language/");
4142
} else {
4243
$version = 'v3';
4344
}
44-
$app->redirect("/$language/$version/");
45+
Flight::redirect("/$language/$version/");
4546
});
4647

4748
// Pick up old routes that didn't use to have a language and version header
48-
$app->route('/@section:[\w\-]{3,}(/@sub_section:[\w\-]{3,})', function (string $section, ?string $sub_section = '') use ($app): void {
49+
Flight::route('/@section:[\w\-]{3,}(/@sub_section:[\w\-]{3,})', function (string $section, ?string $sub_section = ''): void {
4950
$language = Translator::getLanguageFromRequest();
50-
$app->redirect("/{$language}/v3/$section/$sub_section/");
51+
Flight::redirect("/{$language}/v3/$section/$sub_section/");
5152
});
5253

5354
/*
5455
* Core routes
5556
*/
56-
$app->group('/@language:[a-z]{2}/@version:[a-z0-9]{2}', function (Router $router) use ($app): void {
57-
$router->get('/', [DocsController::class, 'aboutGet'], false, 'about');
58-
$router->get('/single-page', [DocsController::class, 'singlePageGet'], false, 'single_page');
59-
$router->get('/about', [DocsController::class, 'aboutGet']);
60-
$router->get('/install', [DocsController::class, 'installGet'], false, 'install');
57+
Flight::group('/@language:[a-z]{2}/@version:[a-z0-9]{2}', static function (): void {
58+
Flight::route('GET /', [DocsController::class, 'aboutGet'], false, 'about');
59+
Flight::route('GET /single-page', [DocsController::class, 'singlePageGet'], false, 'single_page');
60+
Flight::route('GET /about', [DocsController::class, 'aboutGet']);
61+
Flight::route('GET /install', [DocsController::class, 'installGet'], false, 'install');
6162

6263
// Unique URL workaround because this is the only 'single page' with a scrollspy for the time being.
63-
$router->get('/install/install', function () use ($app): void {
64-
$app->redirect($app->getUrl('install'));
64+
Flight::route('GET /install/install', static function (): void {
65+
Flight::redirect(Flight::getUrl('install'));
6566
});
6667

67-
$router->get('/license', [DocsController::class, 'licenseGet'], false, 'license');
68-
$router->get('/examples', [DocsController::class, 'examplesGet'], false, 'examples');
69-
$router->get('/media', [DocsController::class, 'mediaGet'], false, 'media');
70-
$router->get('/search', [DocsController::class, 'searchGet'], false, 'search');
68+
Flight::route('GET /license', [DocsController::class, 'licenseGet'], false, 'license');
69+
Flight::route('GET /examples', [DocsController::class, 'examplesGet'], false, 'examples');
70+
Flight::route('GET /media', [DocsController::class, 'mediaGet'], false, 'media');
71+
Flight::route('GET /search', [DocsController::class, 'searchGet'], false, 'search');
7172

72-
$router->group('/learn', function (Router $router): void {
73-
$router->get('', [DocsController::class, 'learnGet'], false, 'learn');
74-
$router->get('/@section_name', [DocsController::class, 'learnSectionsGet']);
73+
Flight::group('/learn', static function (): void {
74+
Flight::route('GET ', [DocsController::class, 'learnGet'], false, 'learn');
75+
Flight::route('GET /@section_name', [DocsController::class, 'learnSectionsGet']);
7576
});
7677

77-
$router->group('/guides', function (Router $router): void {
78-
$router->get('', [DocsController::class, 'guidesGet'], false, 'guides');
79-
$router->get('/@section_name', [DocsController::class, 'guidesSectionsGet']);
78+
Flight::group('/guides', static function (): void {
79+
Flight::route('GET ', [DocsController::class, 'guidesGet'], false, 'guides');
80+
Flight::route('GET /@section_name', [DocsController::class, 'guidesSectionsGet']);
8081
});
8182

82-
$router->group('/awesome-plugins', function (Router $router): void {
83-
$router->get('', [DocsController::class, 'awesomePluginsGet'], false, 'awesome_plugins');
84-
$router->get('/@plugin_name', [DocsController::class, 'pluginGet'], false, 'plugin');
83+
Flight::group('/awesome-plugins', static function (): void {
84+
Flight::route('GET ', [DocsController::class, 'awesomePluginsGet'], false, 'awesome_plugins');
85+
Flight::route('GET /@plugin_name', [DocsController::class, 'pluginGet'], false, 'plugin');
8586
});
8687
});
8788
}, [ new HeaderSecurityMiddleware() ]);
8889

8990
/*
9091
* 404 Handler
9192
*/
92-
$app->map('notFound', function () use ($app): void {
93+
Flight::map('notFound', static function (): void {
9394
// Clear out anything that may have been generated
94-
$app->response()->clearBody()->status(404);
95+
Flight::response()->clearBody()->status(404);
9596

9697
// pull the version out of the URL
97-
$url = $app->request()->url;
98+
$url = Flight::request()->url;
9899
$version = preg_match('~/(v\d)/~', $url, $matches) === 1 ? $matches[1] : 'v3';
99100

100-
(new DocsLogic($app))->renderPage('not_found.latte', [
101+
(new Container)->get(DocsLogic::class)->renderPage('not_found.latte', [
101102
'title' => '404 Not Found',
102103
'version' => $version
103104
]);
104-
$app->response()->send();
105+
106+
Flight::response()->send();
107+
105108
exit;
106109
});

app/config/services.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,19 @@
11
<?php
22

3-
use app\utils\CustomEngine;
43
use app\utils\Translator;
54
use Latte\Engine as LatteEngine;
65
use Latte\Essential\TranslatorExtension;
76
use Latte\Loaders\FileLoader;
87
use flight\Cache;
98

10-
/**
11-
* @var array $config
12-
* @var CustomEngine $app
13-
*/
14-
15-
// This translates some common parts of the page, not the content
16-
$app->register('translator', Translator::class);
9+
// This translates some common parts of the page, not the content
10+
Flight::register('translator', Translator::class);
1711

1812
// Templating Engine used to render the views
19-
$app->register('latte', LatteEngine::class, [], function (LatteEngine $latte) use ($app): void {
13+
Flight::register('latte', LatteEngine::class, [], static function (LatteEngine $latte): void {
2014
$latte->setTempDirectory(__DIR__ . '/../cache/');
2115
$latte->setLoader(new FileLoader(__DIR__ . '/../views/'));
22-
$translator = $app->translator();
16+
$translator = Flight::translator();
2317

2418
$translatorExtension = new TranslatorExtension(
2519
[$translator, 'translate'],
@@ -29,9 +23,9 @@
2923
});
3024

3125
// Cache for storing parsedown and other things
32-
$app->register('cache', Cache::class, [__DIR__ . '/../cache/'], function (Cache $cache) {
26+
Flight::register('cache', Cache::class, [__DIR__ . '/../cache/'], static function (Cache $cache): void {
3327
$cache->setDevMode(ENVIRONMENT === 'development');
3428
});
3529

3630
// Parsedown is a markdown parser
37-
$app->register('parsedown', Parsedown::class);
31+
Flight::register('parsedown', Parsedown::class);

0 commit comments

Comments
 (0)