Skip to content
This repository was archived by the owner on Jun 8, 2022. It is now read-only.

Commit 4466b82

Browse files
authored
Merge pull request #18 from SebKay/configurable
Configurable
2 parents a280953 + 305f0f6 commit 4466b82

13 files changed

+136
-33
lines changed

.github/workflows/test-webpack.yml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: Webpack
2+
3+
on: [pull_request]
4+
5+
jobs:
6+
test-webpack-prod-build:
7+
name: Test production build
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v2.3.4
11+
12+
- name: Install Yarn dependencies
13+
run: yarn install --prod --non-interactive
14+
15+
- name: Build for production
16+
run: yarn run prod

config/controllers.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
/**
3+
* Set container controllers
4+
*/
5+
6+
use App\Controllers\HomeController;
7+
8+
return [
9+
HomeController::class,
10+
];

config/services.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
/**
3+
* Set container services
4+
*/
5+
6+
use App\Services\CSRFService;
7+
use App\Services\ErrorRendererHTMLService;
8+
use App\Services\LoggerService;
9+
use App\Services\ViewService;
10+
11+
return [
12+
LoggerService::class,
13+
ErrorRendererHTMLService::class,
14+
CSRFService::class,
15+
ViewService::class
16+
];

src/App.php

-7
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@
1414

1515
class App
1616
{
17-
/**
18-
* @var boolean
19-
*/
20-
public $dev_mode;
21-
2217
/**
2318
* @var ContainerCreator
2419
*/
@@ -49,8 +44,6 @@ class App
4944
*/
5045
public function __construct()
5146
{
52-
$this->dev_mode = $this->isDevelopmentMode();
53-
5447
$this->container_creator = new ContainerCreator($this);
5548
$this->slim = AppFactory::create($this->container());
5649

src/Config.php

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace App;
4+
5+
class Config
6+
{
7+
/**
8+
* @var array
9+
*/
10+
protected $services;
11+
12+
/**
13+
* @var array
14+
*/
15+
protected $controllers;
16+
17+
public function __construct()
18+
{
19+
$this->services = self::set('services');
20+
$this->controllers = self::set('controllers');
21+
}
22+
23+
public static function set(string $filename): array
24+
{
25+
$file = __DIR__ . "/../config/$filename.php";
26+
27+
if (!file_exists($file)) {
28+
return [];
29+
}
30+
31+
return include $file;
32+
}
33+
34+
public static function getServices(): array
35+
{
36+
return (new self())->services;
37+
}
38+
39+
public static function getControllers(): array
40+
{
41+
return (new self())->controllers;
42+
}
43+
}

src/Container/ContainerCreator.php

+4-20
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
namespace App\Container;
44

55
use App\App;
6-
use App\Controllers\HomeController;
6+
use App\Config;
7+
use App\Services\Service;
78

89
class ContainerCreator
910
{
@@ -23,19 +24,9 @@ public function __construct(App $app)
2324
$this->container = new \DI\Container();
2425
}
2526

26-
protected function services(): array
27-
{
28-
return [
29-
LoggerService::class,
30-
ErrorRendererHTMLService::class,
31-
CSRFService::class,
32-
ViewService::class
33-
];
34-
}
35-
3627
protected function addServices(): void
3728
{
38-
foreach ($this->services() as $service_class) {
29+
foreach (Config::getServices() as $service_class) {
3930
$service = new $service_class($this->container(), $this->app);
4031

4132
if ($service instanceof Service) {
@@ -47,16 +38,9 @@ protected function addServices(): void
4738
}
4839
}
4940

50-
protected function controllers(): array
51-
{
52-
return [
53-
HomeController::class
54-
];
55-
}
56-
5741
protected function addControllers(): void
5842
{
59-
foreach ($this->controllers() as $controller) {
43+
foreach (Config::getControllers() as $controller) {
6044
$this->container->set(
6145
$controller,
6246
new $controller($this->container())

src/Container/CSRFService.php src/Services/CSRFService.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace App\Container;
3+
namespace App\Services;
44

55
use Psr\Http\Message\ResponseInterface;
66
use Psr\Http\Message\ServerRequestInterface;

src/Container/ErrorRendererHTMLService.php src/Services/ErrorRendererHTMLService.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace App\Container;
3+
namespace App\Services;
44

55
use App\ErrorRendererHTML;
66

src/Container/LoggerService.php src/Services/LoggerService.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace App\Container;
3+
namespace App\Services;
44

55
use App\Dependencies\Logger;
66

src/Container/Service.php src/Services/Service.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace App\Container;
3+
namespace App\Services;
44

55
use App\App;
66

@@ -25,7 +25,7 @@ public function __construct(\DI\Container $container, App $app)
2525
{
2626
$this->container = $container;
2727
$this->app = $app;
28-
$this->dev_mode = $this->app->dev_mode;
28+
$this->dev_mode = $this->app->isDevelopmentMode();
2929
}
3030

3131
abstract public static function name(): string;

src/Container/ViewService.php src/Services/ViewService.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace App\Container;
3+
namespace App\Services;
44

55
use App\Dependencies\View;
66

tests/Unit/ConfigTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Tests\Unit;
4+
5+
use App\Config;
6+
7+
class ConfigTest extends UnitTest
8+
{
9+
/**
10+
* @testdox set() returns and empty array when the file doesn't exist
11+
*/
12+
public function test_invalid_file_returns_empty_array()
13+
{
14+
$set = Config::set('test.php');
15+
16+
$this->assertIsArray($set);
17+
$this->assertEmpty($set);
18+
}
19+
}

tests/Unit/HelpersTest.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Tests\Unit;
4+
5+
use App\Helpers;
6+
7+
class HelpersTest extends UnitTest
8+
{
9+
public function test_it_gets_all_major_http_status_codes()
10+
{
11+
$this->assertNotEmpty(Helpers::getHttpStatusMessage(200));
12+
$this->assertNotEmpty(Helpers::getHttpStatusMessage(301));
13+
$this->assertNotEmpty(Helpers::getHttpStatusMessage(302));
14+
$this->assertNotEmpty(Helpers::getHttpStatusMessage(400));
15+
$this->assertNotEmpty(Helpers::getHttpStatusMessage(403));
16+
$this->assertNotEmpty(Helpers::getHttpStatusMessage(404));
17+
$this->assertNotEmpty(Helpers::getHttpStatusMessage(500));
18+
$this->assertNotEmpty(Helpers::getHttpStatusMessage(502));
19+
$this->assertNotEmpty(Helpers::getHttpStatusMessage(503));
20+
$this->assertNotEmpty(Helpers::getHttpStatusMessage(504));
21+
}
22+
}

0 commit comments

Comments
 (0)