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

Commit e4c1a79

Browse files
authored
Merge pull request #14 from SebKay/container-improvements
Container improvements
2 parents 8d87851 + 812ee2c commit e4c1a79

File tree

11 files changed

+53
-29
lines changed

11 files changed

+53
-29
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"slim/slim": "^4.6",
2525
"slim/psr7": "^1.2",
2626
"vlucas/phpdotenv": "^5.2",
27-
"php-di/slim-bridge": "^3.0",
27+
"php-di/slim-bridge": "^3.1",
2828
"twig/twig": "^3.1",
2929
"slim/csrf": "^1.0",
3030
"illuminate/database": "^8.27",
@@ -38,7 +38,7 @@
3838
},
3939
"scripts": {
4040
"build": "git clean -xff -e .env && composer install && yarn install && yarn run dev",
41-
"build:prod": "git clean -xffd -e .env && composer install --no-dev && yarn install && yarn run prod",
41+
"build:prod": "git clean -xffd -e .env && composer install --no-dev && yarn install --prod && yarn run prod",
4242
"lint:php": "./vendor/bin/phpcs --standard=PSR12 src",
4343
"fix:php": "./vendor/bin/phpcbf --standard=PSR12 src",
4444
"analyse:php": "./vendor/bin/psalm --show-info=true",

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

inc/routes/web.php

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

3+
/**
4+
* @var App\App $this
5+
*/
6+
37
use App\Controllers\HomeController;
48
use Slim\Routing\RouteCollectorProxy;
59

610
/**
711
* Home
812
*/
913
$this->slim->group('', function (RouteCollectorProxy $group) {
10-
$group->get('/', HomeController::class . ':index')->setName('home');
11-
$group->post('/', HomeController::class . ':update');
14+
$group->get('/', [HomeController::class, 'index'])->setName('home');
15+
$group->post('/', [HomeController::class, 'update']);
1216
});

src/App.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
namespace App;
44

55
use App\Container\Container;
6+
use App\Container\ContainerCreator;
67
use App\Database\Database;
78
use App\Middleware\ExampleMiddleware;
89
use App\Handlers\HttpErrorHandler;
910
use App\Handlers\ShutdownHandler;
1011
use Psr\Http\Message\ResponseInterface;
1112
use Psr\Http\Message\ServerRequestInterface;
1213
use Slim\App as SlimApp;
13-
use Slim\Factory\AppFactory;
14+
use DI\Bridge\Slim\Bridge as AppFactory;
1415
use Slim\Factory\ServerRequestCreatorFactory;
1516

1617
class App
@@ -21,9 +22,9 @@ class App
2122
public $dev_mode;
2223

2324
/**
24-
* @var Container
25+
* @var ContainerCreator
2526
*/
26-
protected $container;
27+
protected $container_creator;
2728

2829
/**
2930
* @var SlimApp
@@ -47,10 +48,10 @@ public function __construct()
4748
{
4849
$this->dev_mode = $this->isDevelopmentMode();
4950

50-
$this->container = new Container($this);
51-
$this->slim = AppFactory::createFromContainer($this->container()->get());
51+
$this->container_creator = new ContainerCreator($this);
52+
$this->slim = AppFactory::create($this->container());
5253

53-
$this->container()->setup();
54+
$this->container_creator->setup();
5455
$this->setupSlim();
5556

5657
$this->database = new Database();
@@ -69,11 +70,11 @@ public function isDevelopmentMode(): bool
6970
/**
7071
* Get the container
7172
*
72-
* @return Container
73+
* @return \DI\Container
7374
*/
74-
public function container(): Container
75+
public function container(): \DI\Container
7576
{
76-
return $this->container;
77+
return $this->container_creator->container();
7778
}
7879

7980
/**
@@ -97,7 +98,7 @@ protected function addMiddleware(): void
9798
->addErrorMiddleware($this->dev_mode, false, false)
9899
->setDefaultErrorHandler($this->error_handler);
99100

100-
$this->slim()->add('csrf');
101+
$this->slim()->add($this->container()->get('csrf'));
101102
// $this->slim()->add(ExampleMiddleware::class);
102103
}
103104

@@ -115,7 +116,7 @@ protected function addRoutes(): void
115116
protected function addErrorHandler(): void
116117
{
117118
$this->error_handler = new HttpErrorHandler(
118-
$this->container()->get(),
119+
$this->container(),
119120
$this->slim()->getCallableResolver(),
120121
$this->slim()->getResponseFactory()
121122
);

src/Container/CSRFService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
class CSRFService extends Service
1111
{
12-
public function name(): string
12+
public static function name(): string
1313
{
1414
return 'csrf';
1515
}
Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
namespace App\Container;
44

55
use App\App;
6+
use App\Controllers\HomeController;
67

7-
class Container
8+
class ContainerCreator
89
{
910
/**
1011
* @var App
@@ -30,7 +31,7 @@ protected function services(): array
3031
];
3132
}
3233

33-
public function addServices(): void
34+
protected function addServices(): void
3435
{
3536
foreach ($this->services() as $service) {
3637
$this->container->set(
@@ -40,12 +41,30 @@ public function addServices(): void
4041
}
4142
}
4243

44+
protected function controllers(): array
45+
{
46+
return [
47+
HomeController::class
48+
];
49+
}
50+
51+
protected function addControllers(): void
52+
{
53+
foreach ($this->controllers() as $controller) {
54+
$this->container->set(
55+
$controller,
56+
new $controller($this->container())
57+
);
58+
}
59+
}
60+
4361
public function setup(): void
4462
{
4563
$this->addServices();
64+
$this->addControllers();
4665
}
4766

48-
public function get(): \DI\Container
67+
public function container(): \DI\Container
4968
{
5069
return $this->container;
5170
}

src/Container/Service.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function __construct(\DI\Container $container, App $app)
2828
$this->dev_mode = $this->app->dev_mode;
2929
}
3030

31-
abstract public function name(): string;
31+
abstract public static function name(): string;
3232

3333
abstract public function config();
3434
}

src/Container/ViewService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class ViewService extends Service
88
{
9-
public function name(): string
9+
public static function name(): string
1010
{
1111
return 'view';
1212
}

src/Controllers/HomeController.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,16 @@ class HomeController extends Controller
1313
*
1414
* @param ServerRequestInterface $request
1515
* @param ResponseInterface $response
16-
* @param array $args
1716
* @return ResponseInterface
1817
*/
19-
public function index(ServerRequestInterface $request, ResponseInterface $response, array $args): ResponseInterface
18+
public function index(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
2019
{
2120
return $this->view->respond(
2221
$response,
2322
'layouts/home.twig',
2423
[
25-
'name' => 'Jim',
26-
'csrf' => Helpers::generateCSRFData($this->csrf, $request)
24+
'name' => 'Jim',
25+
'csrf' => Helpers::generateCSRFData($this->csrf, $request)
2726
]
2827
);
2928
}
@@ -33,10 +32,9 @@ public function index(ServerRequestInterface $request, ResponseInterface $respon
3332
*
3433
* @param ServerRequestInterface $request
3534
* @param ResponseInterface $response
36-
* @param array $args
3735
* @return ResponseInterface
3836
*/
39-
public function update(ServerRequestInterface $request, ResponseInterface $response, array $args): ResponseInterface
37+
public function update(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
4038
{
4139
return $this->view->respond(
4240
$response,

src/Handlers/HttpErrorHandler.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ protected function respond(): ResponseInterface
6666
$type = self::SERVER_ERROR;
6767
$description = 'An internal error has occurred while processing your request.';
6868

69+
// ray($exception);
70+
6971
if ($exception instanceof HttpException) {
7072
$statusCode = (int) $exception->getCode();
7173
$description = $exception->getMessage();

0 commit comments

Comments
 (0)