2
2
3
3
namespace App ;
4
4
5
+ use App \Container \Container ;
5
6
use App \Database \Database ;
6
- use App \Database \DatabaseHelpers ;
7
- use App \Dependencies \View ;
8
7
use App \Middleware \ExampleMiddleware ;
9
8
use App \Handlers \HttpErrorHandler ;
10
9
use App \Handlers \ShutdownHandler ;
11
- use DI \Container ;
12
10
use Psr \Http \Message \ResponseInterface ;
13
11
use Psr \Http \Message \ServerRequestInterface ;
14
- use Psr \Http \Server \RequestHandlerInterface ;
15
- use Slim \Csrf \Guard ;
12
+ use Slim \App as SlimApp ;
16
13
use Slim \Factory \AppFactory ;
17
14
use Slim \Factory \ServerRequestCreatorFactory ;
18
15
@@ -21,15 +18,15 @@ class App
21
18
/**
22
19
* @var boolean
23
20
*/
24
- protected $ dev_mode ;
21
+ public $ dev_mode ;
25
22
26
23
/**
27
24
* @var Container
28
25
*/
29
26
protected $ container ;
30
27
31
28
/**
32
- * @var \Slim\App
29
+ * @var SlimApp
33
30
*/
34
31
protected $ slim ;
35
32
@@ -48,12 +45,12 @@ class App
48
45
*/
49
46
public function __construct ()
50
47
{
51
- $ this ->dev_mode = $ this ->isDevModeEnabled ();
48
+ $ this ->dev_mode = $ this ->isDevelopmentMode ();
52
49
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 () );
55
52
56
- $ this ->setupContainer ();
53
+ $ this ->container ()-> setup ();
57
54
$ this ->setupSlim ();
58
55
59
56
$ this ->database = new Database ();
@@ -64,72 +61,44 @@ public function __construct()
64
61
*
65
62
* @return bool
66
63
*/
67
- public function isDevModeEnabled (): bool
64
+ public function isDevelopmentMode (): bool
68
65
{
69
66
return ($ _ENV ['APP_ENV ' ] == 'development ' || $ _ENV ['APP_ENV ' ] == 'test ' ? true : false );
70
67
}
71
68
72
69
/**
73
- * Set up the container
70
+ * Get the container
71
+ *
72
+ * @return Container
74
73
*/
75
- protected function setupContainer (): void
74
+ public function container (): Container
76
75
{
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 ;
109
77
}
110
78
111
79
/**
112
- * Get the container
80
+ * Get the slim app
113
81
*
114
- * @return Container
82
+ * @return SlimApp
115
83
*/
116
- public function container (): Container
84
+ public function slim (): SlimApp
117
85
{
118
- return $ this ->container ;
86
+ return $ this ->slim ;
119
87
}
120
88
121
89
/**
122
90
* Add middleware
123
91
*/
124
92
protected function addMiddleware (): void
125
93
{
126
- $ this ->slim ->addRoutingMiddleware ();
94
+ $ this ->slim () ->addRoutingMiddleware ();
127
95
128
- $ this ->slim ->addErrorMiddleware ($ this ->dev_mode , false , false )
96
+ $ this ->slim ()
97
+ ->addErrorMiddleware ($ this ->dev_mode , false , false )
129
98
->setDefaultErrorHandler ($ this ->error_handler );
130
99
131
- $ this ->slim ->add ('csrf ' );
132
- $ this ->slim ->add (ExampleMiddleware::class);
100
+ $ this ->slim () ->add ('csrf ' );
101
+ $ this ->slim () ->add (ExampleMiddleware::class);
133
102
}
134
103
135
104
/**
@@ -146,9 +115,9 @@ protected function addRoutes(): void
146
115
protected function addErrorHandler (): void
147
116
{
148
117
$ 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 ()
152
121
);
153
122
}
154
123
@@ -183,7 +152,7 @@ protected function setupSlim(): void
183
152
*/
184
153
public function handle (ServerRequestInterface $ request ): ResponseInterface
185
154
{
186
- return $ this ->slim ->handle ($ request );
155
+ return $ this ->slim () ->handle ($ request );
187
156
}
188
157
189
158
/**
@@ -193,6 +162,6 @@ public function handle(ServerRequestInterface $request): ResponseInterface
193
162
*/
194
163
public function run (): void
195
164
{
196
- $ this ->slim ->run ();
165
+ $ this ->slim () ->run ();
197
166
}
198
167
}
0 commit comments