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

Commit fd3764a

Browse files
authored
Merge pull request #12 from SebKay/separate-container
Separate container
2 parents 4df038a + 6e256cb commit fd3764a

File tree

8 files changed

+192
-68
lines changed

8 files changed

+192
-68
lines changed

.github/workflows/test-php.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ jobs:
99

1010
steps:
1111
- uses: actions/checkout@v2.3.4
12-
- uses: nanasess/setup-php@master
12+
- uses: shivammathur/setup-php@v2
1313
with:
14-
php-version: "7.4"
14+
php-version: '7.4'
1515

1616
- name: Install dependencies
1717
run: composer install --prefer-dist --no-progress --no-suggest
@@ -27,9 +27,9 @@ jobs:
2727

2828
steps:
2929
- uses: actions/checkout@v2.3.4
30-
- uses: nanasess/setup-php@master
30+
- uses: shivammathur/setup-php@v2
3131
with:
32-
php-version: "7.4"
32+
php-version: '7.4'
3333

3434
- name: Create test database
3535
run: |

src/App.php

+28-59
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,14 @@
22

33
namespace App;
44

5+
use App\Container\Container;
56
use App\Database\Database;
6-
use App\Database\DatabaseHelpers;
7-
use App\Dependencies\View;
87
use App\Middleware\ExampleMiddleware;
98
use App\Handlers\HttpErrorHandler;
109
use App\Handlers\ShutdownHandler;
11-
use DI\Container;
1210
use Psr\Http\Message\ResponseInterface;
1311
use Psr\Http\Message\ServerRequestInterface;
14-
use Psr\Http\Server\RequestHandlerInterface;
15-
use Slim\Csrf\Guard;
12+
use Slim\App as SlimApp;
1613
use Slim\Factory\AppFactory;
1714
use Slim\Factory\ServerRequestCreatorFactory;
1815

@@ -21,15 +18,15 @@ class App
2118
/**
2219
* @var boolean
2320
*/
24-
protected $dev_mode;
21+
public $dev_mode;
2522

2623
/**
2724
* @var Container
2825
*/
2926
protected $container;
3027

3128
/**
32-
* @var \Slim\App
29+
* @var SlimApp
3330
*/
3431
protected $slim;
3532

@@ -48,12 +45,12 @@ class App
4845
*/
4946
public function __construct()
5047
{
51-
$this->dev_mode = $this->isDevModeEnabled();
48+
$this->dev_mode = $this->isDevelopmentMode();
5249

53-
$this->container = new Container();
54-
$this->slim = AppFactory::createFromContainer($this->container());
50+
$this->container = new Container($this);
51+
$this->slim = AppFactory::createFromContainer($this->container()->get());
5552

56-
$this->setupContainer();
53+
$this->container()->setup();
5754
$this->setupSlim();
5855

5956
$this->database = new Database();
@@ -64,72 +61,44 @@ public function __construct()
6461
*
6562
* @return bool
6663
*/
67-
public function isDevModeEnabled(): bool
64+
public function isDevelopmentMode(): bool
6865
{
6966
return ($_ENV['APP_ENV'] == 'development' || $_ENV['APP_ENV'] == 'test' ? true : false);
7067
}
7168

7269
/**
73-
* Set up the container
70+
* Get the container
71+
*
72+
* @return Container
7473
*/
75-
protected function setupContainer(): void
74+
public function container(): Container
7675
{
77-
//---- CSRF protection
78-
$this->container->set('csrf', function () {
79-
$guard = new Guard($this->slim->getResponseFactory());
80-
81-
$guard->setFailureHandler(function (
82-
ServerRequestInterface $request,
83-
RequestHandlerInterface $handler
84-
): ResponseInterface {
85-
$status_code = 400;
86-
$response = $this->slim
87-
->getResponseFactory()
88-
->createResponse()
89-
->withStatus($status_code);
90-
91-
return $this->container
92-
->get('view')
93-
->respond($response, 'layouts/http-error.twig', [
94-
'code' => $status_code,
95-
'description' => 'There Was An Error',
96-
]);
97-
});
98-
99-
return $guard;
100-
});
101-
102-
//---- View
103-
$this->container->set('view', function () {
104-
return new View(
105-
__DIR__ . '/../resources/views',
106-
($this->dev_mode ? '' : '.cache/views')
107-
);
108-
});
76+
return $this->container;
10977
}
11078

11179
/**
112-
* Get the container
80+
* Get the slim app
11381
*
114-
* @return Container
82+
* @return SlimApp
11583
*/
116-
public function container(): Container
84+
public function slim(): SlimApp
11785
{
118-
return $this->container;
86+
return $this->slim;
11987
}
12088

12189
/**
12290
* Add middleware
12391
*/
12492
protected function addMiddleware(): void
12593
{
126-
$this->slim->addRoutingMiddleware();
94+
$this->slim()->addRoutingMiddleware();
12795

128-
$this->slim->addErrorMiddleware($this->dev_mode, false, false)
96+
$this->slim()
97+
->addErrorMiddleware($this->dev_mode, false, false)
12998
->setDefaultErrorHandler($this->error_handler);
13099

131-
$this->slim->add('csrf');
132-
$this->slim->add(ExampleMiddleware::class);
100+
$this->slim()->add('csrf');
101+
$this->slim()->add(ExampleMiddleware::class);
133102
}
134103

135104
/**
@@ -146,9 +115,9 @@ protected function addRoutes(): void
146115
protected function addErrorHandler(): void
147116
{
148117
$this->error_handler = new HttpErrorHandler(
149-
$this->container(),
150-
$this->slim->getCallableResolver(),
151-
$this->slim->getResponseFactory()
118+
$this->container()->get(),
119+
$this->slim()->getCallableResolver(),
120+
$this->slim()->getResponseFactory()
152121
);
153122
}
154123

@@ -183,7 +152,7 @@ protected function setupSlim(): void
183152
*/
184153
public function handle(ServerRequestInterface $request): ResponseInterface
185154
{
186-
return $this->slim->handle($request);
155+
return $this->slim()->handle($request);
187156
}
188157

189158
/**
@@ -193,6 +162,6 @@ public function handle(ServerRequestInterface $request): ResponseInterface
193162
*/
194163
public function run(): void
195164
{
196-
$this->slim->run();
165+
$this->slim()->run();
197166
}
198167
}

src/Container/CSRFService.php

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace App\Container;
4+
5+
use Psr\Http\Message\ResponseInterface;
6+
use Psr\Http\Message\ServerRequestInterface;
7+
use Psr\Http\Server\RequestHandlerInterface;
8+
use Slim\Csrf\Guard;
9+
10+
class CSRFService extends Service
11+
{
12+
public function name(): string
13+
{
14+
return 'csrf';
15+
}
16+
17+
public function config(): Guard
18+
{
19+
$guard = new Guard($this->app->slim()->getResponseFactory());
20+
21+
$guard->setFailureHandler(function (
22+
ServerRequestInterface $request,
23+
RequestHandlerInterface $handler
24+
): ResponseInterface {
25+
$status_code = 400;
26+
$response = $this->app->slim()
27+
->getResponseFactory()
28+
->createResponse()
29+
->withStatus($status_code);
30+
31+
return $this->container
32+
->get('view')
33+
->respond($response, 'layouts/http-error.twig', [
34+
'code' => $status_code,
35+
'description' => 'There Was An Error',
36+
]);
37+
});
38+
39+
return $guard;
40+
}
41+
}

src/Container/Container.php

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace App\Container;
4+
5+
use App\App;
6+
7+
class Container
8+
{
9+
/**
10+
* @var App
11+
*/
12+
protected $app;
13+
14+
/**
15+
* @var \DI\Container
16+
*/
17+
protected $container;
18+
19+
public function __construct(App $app)
20+
{
21+
$this->app = $app;
22+
$this->container = new \DI\Container();
23+
}
24+
25+
protected function services(): array
26+
{
27+
return [
28+
new CSRFService($this->container, $this->app),
29+
new ViewService($this->container, $this->app),
30+
];
31+
}
32+
33+
public function addServices(): void
34+
{
35+
foreach ($this->services() as $service) {
36+
$this->container->set(
37+
$service->name(),
38+
$service->config()
39+
);
40+
}
41+
}
42+
43+
public function setup(): void
44+
{
45+
$this->addServices();
46+
}
47+
48+
public function get(): \DI\Container
49+
{
50+
return $this->container;
51+
}
52+
}

src/Container/Service.php

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace App\Container;
4+
5+
use App\App;
6+
7+
abstract class Service
8+
{
9+
/**
10+
* @var \DI\Container
11+
*/
12+
protected $container;
13+
14+
/**
15+
* @var App
16+
*/
17+
protected $app;
18+
19+
/**
20+
* @var bool
21+
*/
22+
protected $dev_mode;
23+
24+
public function __construct(\DI\Container $container, App $app)
25+
{
26+
$this->container = $container;
27+
$this->app = $app;
28+
$this->dev_mode = $this->app->dev_mode;
29+
}
30+
31+
abstract public function name(): string;
32+
33+
abstract public function config();
34+
}

src/Container/ViewService.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace App\Container;
4+
5+
use App\Dependencies\View;
6+
7+
class ViewService extends Service
8+
{
9+
public function name(): string
10+
{
11+
return 'view';
12+
}
13+
14+
public function config(): View
15+
{
16+
return new View(
17+
__DIR__ . '/../../resources/views',
18+
($this->dev_mode ? '' : '.cache/views')
19+
);
20+
}
21+
}

src/Database/DatabaseHelpers.php

+11-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,14 @@
88

99
class DatabaseHelpers
1010
{
11+
/**
12+
* @var Database
13+
*/
1114
public $db;
15+
16+
/**
17+
* @var \Faker\Generator
18+
*/
1219
public $faker;
1320

1421
public function __construct(Database $db)
@@ -17,17 +24,17 @@ public function __construct(Database $db)
1724
$this->faker = \Faker\Factory::create();
1825
}
1926

20-
public function migrateUsersTable()
27+
public function migrateUsersTable(): void
2128
{
2229
(new CreateUsersTable($this->db))->up();
2330
}
2431

25-
public function migrateTables()
32+
public function migrateTables(): void
2633
{
2734
$this->migrateUsersTable();
2835
}
2936

30-
public function createDummyUsers(int $amount = 3)
37+
public function createDummyUsers(int $amount = 3): void
3138
{
3239
for ($i = 0; $i < $amount; $i++) {
3340
User::create([
@@ -39,7 +46,7 @@ public function createDummyUsers(int $amount = 3)
3946
}
4047
}
4148

42-
public function createDummyData()
49+
public function createDummyData(): void
4350
{
4451
$this->createDummyUsers();
4552
}

0 commit comments

Comments
 (0)