Skip to content

Commit e251e17

Browse files
committed
Adding step to reorganize things
1 parent bc624c5 commit e251e17

File tree

2 files changed

+311
-1
lines changed

2 files changed

+311
-1
lines changed

_tuts/reorganization.diff

Lines changed: 305 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,305 @@
1+
diff --git a/bootstrap.php b/bootstrap.php
2+
index 52faebf..986f2f4 100644
3+
--- a/bootstrap.php
4+
+++ b/bootstrap.php
5+
@@ -2,3 +2,88 @@
6+
7+
require __DIR__.'/vendor/autoload.php';
8+
9+
+use Pimple\Container;
10+
+use Symfony\Component\HttpFoundation\Response;
11+
+use Symfony\Component\HttpFoundation\Request;
12+
+use Aura\Router\RouterFactory;
13+
+use Zend\Log\Writer\Stream;
14+
+use Zend\Log\Logger;
15+
+
16+
+/*
17+
+ * *************** Our one-method framework
18+
+ */
19+
+
20+
+function _run_app(Container $c) {
21+
+ // run the framework!
22+
+ $route = $c['router']->match(
23+
+ $c['request']->getPathInfo(),
24+
+ $c['request']->server->all()
25+
+ );
26+
+
27+
+ // merge the matched attributes back into Symfony's request
28+
+ if ($route) {
29+
+ $c['request']->attributes->add($route->params);
30+
+ }
31+
+
32+
+ // get the "controller" out, or default to error404_controller
33+
+ $controller = $c['request']->attributes->get('controller', 'error404_controller');
34+
+
35+
+ if ($controller == 'error404_controller') {
36+
+ $msg = sprintf('Controller not found for "%s"', $c['request']->getPathInfo());
37+
+ $c['logger']->err($msg);
38+
+ } else {
39+
+ $c['logger']->info(sprintf('Found controller "%s"', $controller));
40+
+ }
41+
+
42+
+ // execute the controller and get the response
43+
+ $response = call_user_func_array($controller, array($c['request'], $c));
44+
+ if (!$response instanceof Response) {
45+
+ throw new Exception(sprintf('Your controller "%s" did not return a response!!', $controller));
46+
+ }
47+
+
48+
+ return $response;
49+
+}
50+
+
51+
+/*
52+
+ * *************** Container Setup
53+
+ */
54+
+
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+
+};
65+
+
66+
+$c['request'] = function() {
67+
+ return Request::createFromGlobals();
68+
+};
69+
+
70+
+$c['router'] = function() {
71+
+ $routerFactory = new RouterFactory();
72+
+
73+
+ $router = $routerFactory->newInstance();
74+
+
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']);
80+
+
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']);
89+
+
90+
+ return $logger;
91+
+};
92+
+
93+
+return $c;
94+
diff --git a/controllers.php b/controllers.php
95+
new file mode 100644
96+
index 0000000..9adb562
97+
--- /dev/null
98+
+++ b/controllers.php
99+
@@ -0,0 +1,49 @@
100+
+<?php
101+
+
102+
+use Symfony\Component\HttpFoundation\Request;
103+
+use Symfony\Component\HttpFoundation\Response;
104+
+use Pimple\Container;
105+
+
106+
+/*
107+
+ * Define our controllers
108+
+ */
109+
+
110+
+function homepage_controller(Request $request) {
111+
+
112+
+ $content = '<h1>PHP Camp!</h1>';
113+
+ $content .= '<a href="/attendees">See the attendees</a>';
114+
+ if ($name = $request->attributes->get('name')) {
115+
+ $content .= sprintf('<p>Oh, and hello %s!</p>', $name);
116+
+ }
117+
+
118+
+ return new Response($content);
119+
+}
120+
+
121+
+function attendees_controller(Request $request, Container $c) {
122+
+ $dbh = $c['connection'];
123+
+
124+
+ $sql = 'SELECT * FROM php_camp';
125+
+ $content = '<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" />';
126+
+ $content .= '<h1>PHP Camp Attendees</h1>';
127+
+ $content .= '<table class="table" style="width: 300px;">';
128+
+ foreach ($dbh->query($sql) as $row) {
129+
+ $content .= sprintf(
130+
+ '<tr><td style="font-size: 24px;">%s</td><td><img src="%s" height="120" /></td></tr>',
131+
+ $row['attendee'],
132+
+ $row['avatar_url']
133+
+ );
134+
+ }
135+
+ $content .= '</table>';
136+
+
137+
+ return new Response($content);
138+
+}
139+
+
140+
+function error404_controller(Request $request) {
141+
+ $content = '<h1>404 Page not Found</h1>';
142+
+ $content .= '<p>Find a boy (or girl) scout - they can fix this!</p>';
143+
+
144+
+ $response = new Response($content);
145+
+ $response->setStatusCode(404);
146+
+
147+
+ return $response;
148+
+}
149+
diff --git a/index.php b/index.php
150+
index 5f42f7a..15a2099 100644
151+
--- a/index.php
152+
+++ b/index.php
153+
@@ -1,128 +1,7 @@
154+
<?php
155+
-require __DIR__.'/bootstrap.php';
156+
-
157+
-try {
158+
- $dbPath = __DIR__.'/data/database.sqlite';
159+
- $dbh = new PDO('sqlite:'.$dbPath);
160+
-} catch(PDOException $e) {
161+
- die('Panic! '.$e->getMessage());
162+
-}
163+
-
164+
-// create a request object to help us
165+
-use Symfony\Component\HttpFoundation\Request;
166+
-use Symfony\Component\HttpFoundation\Response;
167+
-use Aura\Router\RouterFactory;
168+
-use Pimple\Container;
169+
-use Zend\Log\Writer\Stream;
170+
-use Zend\Log\Logger;
171+
-
172+
-$c = new Container();
173+
-
174+
-// configuration
175+
-$c['connection_string'] = 'sqlite:'.__DIR__.'/data/database.sqlite';
176+
-$c['log_path'] = __DIR__.'/data/web.log';
177+
-
178+
-// Service setup
179+
-$c['connection'] = function(Container $c) {
180+
- return new PDO($c['connection_string']);
181+
-};
182+
-
183+
-$c['request'] = function() {
184+
- return Request::createFromGlobals();
185+
-};
186+
-
187+
-$c['router'] = function() {
188+
- $routerFactory = new RouterFactory();
189+
-
190+
- $router = $routerFactory->newInstance();
191+
-
192+
- // create a router, build the routes, and then execute it
193+
- $router->add('attendees_list', '/attendees')
194+
- ->addValues(['controller' => 'attendees_controller']);
195+
- $router->add('homepage', '{/name}')
196+
- ->addValues(['controller' => 'homepage_controller']);
197+
-
198+
- return $router;
199+
-};
200+
-$c['logger_writer'] = function(Container $c) {
201+
- return new Stream($c['log_path']);
202+
-};
203+
-$c['logger'] = function(Container $c) {
204+
- $logger = new Logger();
205+
- $logger->addWriter($c['logger_writer']);
206+
-
207+
- return $logger;
208+
-};
209+
-
210+
-// run the framework!
211+
-$route = $c['router']->match(
212+
- $c['request']->getPathInfo(),
213+
- $c['request']->server->all()
214+
-);
215+
-
216+
-// merge the matched attributes back into Symfony's request
217+
-if ($route) {
218+
- $c['request']->attributes->add($route->params);
219+
-}
220+
-
221+
-// get the "controller" out, or default to error404_controller
222+
-$controller = $c['request']->attributes->get('controller', 'error404_controller');
223+
-
224+
-if ($controller == 'error404_controller') {
225+
- $msg = sprintf('Controller not found for "%s"', $c['request']->getPathInfo());
226+
- $c['logger']->err($msg);
227+
-} else {
228+
- $c['logger']->info(sprintf('Found controller "%s"', $controller));
229+
-}
230+
-
231+
-// execute the controller and get the response
232+
-$response = call_user_func_array($controller, array($c['request'], $c));
233+
-if (!$response instanceof Response) {
234+
- throw new Exception(sprintf('Your controller "%s" did not return a response!!', $controller));
235+
-}
236+
-
237+
-$response->send();
238+
-
239+
-/*
240+
- * My Controllers!
241+
- */
242+
-function homepage_controller(Request $request) {
243+
-
244+
- $content = '<h1>PHP Camp!</h1>';
245+
- $content .= '<a href="/attendees">See the attendees</a>';
246+
- if ($name = $request->attributes->get('name')) {
247+
- $content .= sprintf('<p>Oh, and hello %s!</p>', $name);
248+
- }
249+
-
250+
- return new Response($content);
251+
-}
252+
-
253+
-function attendees_controller(Request $request, Container $c) {
254+
- $dbh = $c['connection'];
255+
-
256+
- $sql = 'SELECT * FROM php_camp';
257+
- $content = '<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" />';
258+
- $content .= '<h1>PHP Camp Attendees</h1>';
259+
- $content .= '<table class="table" style="width: 300px;">';
260+
- foreach ($dbh->query($sql) as $row) {
261+
- $content .= sprintf(
262+
- '<tr><td style="font-size: 24px;">%s</td><td><img src="%s" height="120" /></td></tr>',
263+
- $row['attendee'],
264+
- $row['avatar_url']
265+
- );
266+
- }
267+
- $content .= '</table>';
268+
-
269+
- return new Response($content);
270+
-}
271+
-
272+
-function error404_controller(Request $request) {
273+
- $content = '<h1>404 Page not Found</h1>';
274+
- $content .= '<p>Find a boy (or girl) scout - they can fix this!</p>';
275+
-
276+
- $response = new Response($content);
277+
- $response->setStatusCode(404);
278+
-
279+
- return $response;
280+
-}
281+
+$c = require __DIR__.'/bootstrap.php';
282+
+require 'routing.php';
283+
+require 'controllers.php';
284+
285+
+$response = _run_app($c);
286+
+$response->send();
287+
\ No newline at end of file
288+
diff --git a/routing.php b/routing.php
289+
new file mode 100644
290+
index 0000000..2b48429
291+
--- /dev/null
292+
+++ b/routing.php
293+
@@ -0,0 +1,12 @@
294+
+<?php
295+
+
296+
+/*
297+
+ * Define our Routes
298+
+ */
299+
+/** @var \Aura\Router\Router|\Aura\Router\RouteCollection $router */
300+
+$router = $c['router'];
301+
+
302+
+$router->add('attendees_list', '/attendees')
303+
+ ->addValues(['controller' => 'attendees_controller']);
304+
+$router->add('homepage', '{/name}')
305+
+ ->addValues(['controller' => 'homepage_controller']);

_tuts/steps.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@
2424
"id": "zf-logger",
2525
"name": "ZF: Logger",
2626
"description": null
27+
},
28+
{
29+
"id": "reorganization",
30+
"name": "Reorganization",
31+
"description": null
2732
}
2833
],
29-
"sha": "35aba962caa126b84dedfc277194e58643cdf9cd"
34+
"sha": "bc624c5e8ca0fb8126e120712a21560498b7a882"
3035
}

0 commit comments

Comments
 (0)