Skip to content

Commit 0b4952f

Browse files
committed
Refactoring our refactoring to be more of an input request output response
1 parent 258f06b commit 0b4952f

File tree

2 files changed

+49
-51
lines changed

2 files changed

+49
-51
lines changed

_tuts/reorganization.diff

Lines changed: 48 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
diff --git a/bootstrap.php b/bootstrap.php
2-
index 52faebf..986f2f4 100644
2+
index 52faebf..fa4ccc1 100644
33
--- a/bootstrap.php
44
+++ b/bootstrap.php
5-
@@ -2,3 +2,88 @@
5+
@@ -2,3 +2,86 @@
66

77
require __DIR__.'/vendor/autoload.php';
88

@@ -17,30 +17,36 @@ index 52faebf..986f2f4 100644
1717
+ * *************** Our one-method framework
1818
+ */
1919
+
20-
+function _run_app(Container $c) {
20+
+function _run_app(Request $request) {
21+
+ $c = _create_container();
22+
+
23+
+ // include the routing and controllers
24+
+ require_once 'routing.php';
25+
+ require_once 'controllers.php';
26+
+
2127
+ // run the framework!
2228
+ $route = $c['router']->match(
23-
+ $c['request']->getPathInfo(),
24-
+ $c['request']->server->all()
29+
+ $request->getPathInfo(),
30+
+ $request->server->all()
2531
+ );
2632
+
2733
+ // merge the matched attributes back into Symfony's request
2834
+ if ($route) {
29-
+ $c['request']->attributes->add($route->params);
35+
+ $request->attributes->add($route->params);
3036
+ }
3137
+
3238
+ // get the "controller" out, or default to error404_controller
33-
+ $controller = $c['request']->attributes->get('controller', 'error404_controller');
39+
+ $controller = $request->attributes->get('controller', 'error404_controller');
3440
+
3541
+ if ($controller == 'error404_controller') {
36-
+ $msg = sprintf('Controller not found for "%s"', $c['request']->getPathInfo());
42+
+ $msg = sprintf('Controller not found for "%s"', $request->getPathInfo());
3743
+ $c['logger']->err($msg);
3844
+ } else {
3945
+ $c['logger']->info(sprintf('Found controller "%s"', $controller));
4046
+ }
4147
+
4248
+ // execute the controller and get the response
43-
+ $response = call_user_func_array($controller, array($c['request'], $c));
49+
+ $response = call_user_func_array($controller, array($request, $c));
4450
+ if (!$response instanceof Response) {
4551
+ throw new Exception(sprintf('Your controller "%s" did not return a response!!', $controller));
4652
+ }
@@ -52,45 +58,38 @@ index 52faebf..986f2f4 100644
5258
+ * *************** Container Setup
5359
+ */
5460
+
55-
+$c = new Container();
56-
+
57-
+// configuration
58-
+$c['connection_string'] = 'sqlite:'.__DIR__.'/data/database.sqlite';
59-
+$c['log_path'] = __DIR__.'/data/web.log';
60-
+
61-
+// Service setup
62-
+$c['connection'] = function(Container $c) {
63-
+ return new PDO($c['connection_string']);
64-
+};
61+
+function _create_container()
62+
+{
63+
+ $c = new Container();
6564
+
66-
+$c['request'] = function() {
67-
+ return Request::createFromGlobals();
68-
+};
65+
+ // configuration
66+
+ $c['connection_string'] = 'sqlite:' . __DIR__ . '/data/database.sqlite';
67+
+ $c['log_path'] = __DIR__ . '/data/web.log';
6968
+
70-
+$c['router'] = function() {
71-
+ $routerFactory = new RouterFactory();
69+
+ // Service setup
70+
+ $c['connection'] = function (Container $c) {
71+
+ return new PDO($c['connection_string']);
72+
+ };
7273
+
73-
+ $router = $routerFactory->newInstance();
74+
+ $c['router'] = function () {
75+
+ $routerFactory = new RouterFactory();
76+
+ $router = $routerFactory->newInstance();
7477
+
75-
+ // create a router, build the routes, and then execute it
76-
+ $router->add('attendees_list', '/attendees')
77-
+ ->addValues(['controller' => 'attendees_controller']);
78-
+ $router->add('homepage', '{/name}')
79-
+ ->addValues(['controller' => 'homepage_controller']);
78+
+ return $router;
79+
+ };
80+
+ $c['logger_writer'] = function (Container $c) {
81+
+ return new Stream($c['log_path']);
82+
+ };
83+
+ $c['logger'] = function (Container $c) {
84+
+ $logger = new Logger();
85+
+ $logger->addWriter($c['logger_writer']);
8086
+
81-
+ return $router;
82-
+};
83-
+$c['logger_writer'] = function(Container $c) {
84-
+ return new Stream($c['log_path']);
85-
+};
86-
+$c['logger'] = function(Container $c) {
87-
+ $logger = new Logger();
88-
+ $logger->addWriter($c['logger_writer']);
87+
+ return $logger;
88+
+ };
8989
+
90-
+ return $logger;
91-
+};
92-
+
93-
+return $c;
90+
+ return $c;
91+
+}
92+
\ No newline at end of file
9493
diff --git a/controllers.php b/controllers.php
9594
new file mode 100644
9695
index 0000000..9adb562
@@ -147,15 +146,15 @@ index 0000000..9adb562
147146
+ return $response;
148147
+}
149148
diff --git a/index.php b/index.php
150-
index 919eee1..dd22794 100644
149+
index 919eee1..2064c68 100644
151150
--- a/index.php
152151
+++ b/index.php
153-
@@ -1,121 +1,8 @@
152+
@@ -1,121 +1,9 @@
154153
<?php
155154
-require __DIR__.'/bootstrap.php';
156155

157156
-// create a request object to help us
158-
-use Symfony\Component\HttpFoundation\Request;
157+
use Symfony\Component\HttpFoundation\Request;
159158
-use Symfony\Component\HttpFoundation\Response;
160159
-use Aura\Router\RouterFactory;
161160
-use Pimple\Container;
@@ -213,7 +212,7 @@ index 919eee1..dd22794 100644
213212
-
214213
-// get the "controller" out, or default to error404_controller
215214
-$controller = $c['request']->attributes->get('controller', 'error404_controller');
216-
-
215+
217216
-if ($controller == 'error404_controller') {
218217
- $msg = sprintf('Controller not found for "%s"', $c['request']->getPathInfo());
219218
- $c['logger']->err($msg);
@@ -226,11 +225,10 @@ index 919eee1..dd22794 100644
226225
-if (!$response instanceof Response) {
227226
- throw new Exception(sprintf('Your controller "%s" did not return a response!!', $controller));
228227
-}
229-
+$c = require __DIR__.'/bootstrap.php';
230-
+require 'routing.php';
231-
+require 'controllers.php';
228+
+require __DIR__.'/bootstrap.php';
232229

233-
+$response = _run_app($c);
230+
+$request = Request::createFromGlobals();
231+
+$response = _run_app($request);
234232
$response->send();
235233
-
236234
-/*

_tuts/steps.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@
3131
"description": null
3232
}
3333
],
34-
"sha": "e251e17cf10ac544e14cfa93d6ec4bceb05dcef9"
34+
"sha": "258f06b2ef4dbf3f82b08ab4a5febaf8567bb299"
3535
}

0 commit comments

Comments
 (0)