Browse Source

Refactored server to controller classes for readability.

Frederic G. MARAND 6 years ago
parent
commit
7f71cf6c9a

+ 0 - 10
.idea/CodeSchool Backbone 1.iml

@@ -7,15 +7,5 @@
     </content>
     <orderEntry type="inheritedJdk" />
     <orderEntry type="sourceFolder" forTests="false" />
-    <orderEntry type="module-library">
-      <library name="PHARS">
-        <CLASSES>
-          <root url="phar://$MODULE_DIR$/vendor/twig/twig/test/Twig/Tests/Loader/Fixtures/phar/phar-sample.phar/" />
-        </CLASSES>
-        <SOURCES>
-          <root url="phar://$MODULE_DIR$/vendor/twig/twig/test/Twig/Tests/Loader/Fixtures/phar/phar-sample.phar/" />
-        </SOURCES>
-      </library>
-    </orderEntry>
   </component>
 </module>

+ 22 - 0
src/Controllers/ServerController.php

@@ -0,0 +1,22 @@
+<?php
+
+namespace Controllers;
+
+use Silex\Application;
+use Symfony\Component\HttpFoundation\Response;
+
+class ServerController {
+  const BACKBONE = '/client/index.html';
+
+  public function index(Application $app) {
+    return $app->redirect(self::BACKBONE);
+  }
+
+  public function server(Application $app) {
+    return $this->index($app);
+  }
+
+  public function catchAll($url) {
+    return new Response('Nothing to do', Response::HTTP_OK);
+  }
+}

+ 42 - 0
src/Controllers/TodosController.php

@@ -0,0 +1,42 @@
+<?php
+
+namespace Controllers;
+
+use Silex\Application;
+use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\HttpKernel\HttpKernelInterface;
+
+class TodosController {
+
+  public function get(Application $app, $id) {
+    /** @var \Model $model */
+    $model = $app['model'];
+    $todos = $model->load();
+    $todo = $todos[$id] ?? NULL;
+    return $app->json($todo);
+  }
+
+  public function put(Application $app, Request $request, int $id) {
+    $sub = Request::create('/todos', 'POST', [], [], [], [], $request->getContent());
+    return $app->handle($sub, HttpKernelInterface::SUB_REQUEST);
+  }
+
+  public function post(Application $app, Request $request) {
+    /** @var \Model $model */
+    $model = $app['model'];
+    $data = json_decode($request->getContent(), true);
+    $created = $model->save($data);
+    return new Response('Saved', $created ? Response::HTTP_CREATED : Response::HTTP_OK);
+  }
+
+  public function delete(Application $app, int $id) {
+    /** @var \Model $model */
+    $model = $app['model'];
+    $deleted = $model->delete($id);
+    return new Response(
+      $deleted ? 'Deleted' : 'It was not there anyway',
+      Response::HTTP_OK
+    );
+  }
+}

+ 3 - 1
src/boot.php

@@ -22,6 +22,8 @@ $app['twig'] = $app->extend('twig', function ($twig, $app) {
 // Support PATCH/PUT/DELETE.. emulation on POST for browsers. Beware XSS.
 Request::enableHttpMethodParameterOverride();
 
-$app['model'] = function (Application $app) { return new Model($app['data']); };
+$app['model'] = function (Application $app) {
+  return new Model($app['data']);
+};
 
 return $app;

+ 0 - 42
src/controllers.php

@@ -1,42 +0,0 @@
-<?php
-
-use Silex\Application;
-use Symfony\Component\HttpFoundation\Request;
-use Symfony\Component\HttpFoundation\Response;
-use Symfony\Component\HttpKernel\HttpKernelInterface;
-
-const BACKBONE = '/client/index.html';
-
-$app->get('/', function (Application $app) {
-  return $app->redirect(BACKBONE);
-});
-$app->get('/server', function (Application $app) {
-  return $app->redirect(BACKBONE);
-});
-$app->get('/todos/{id}', function (Application $app, int $id) {
-  /** @var \Model $model */
-  $model = $app['model'];
-  $todos = $model->load();
-  $todo = $todos[$id] ?? NULL;
-  return $app->json($todo);
-});
-$app->put('/todos/{id}', function (Application $app, Request $request, int $id) {
-  $sub = Request::create('/todos', 'POST', [], [], [], [], $request->getContent());
-  return $app->handle($sub, HttpKernelInterface::SUB_REQUEST);
-});
-$app->post('/todos', function (Application $app, Request $request) {
-  /** @var \Model $model */
-  $model = $app['model'];
-  $data = json_decode($request->getContent(), true);
-  $created = $model->save($data);
-  return new Response('Saved', $created ? Response::HTTP_CREATED : Response::HTTP_OK);
-})->value('id', NULL);
-$app->delete('/todos/{id}', function (Application $app, int $id) {
-  /** @var \Model $model */
-  $model = $app['model'];
-  $deleted = $model->delete($id);
-  return new Response($deleted ? 'Deleted' : 'It was not there anyway', Response::HTTP_OK);
-});
-$app->match('{url}', function ($url) {
-  return new Response('Nothing to do', Response::HTTP_OK);
-});

+ 14 - 0
src/routing.php

@@ -0,0 +1,14 @@
+<?php
+
+use Controllers\ServerController;
+use Controllers\TodosController;
+
+$app->get('/todos/{id}',    TodosController::class . '::get');
+$app->put('/todos/{id}',    TodosController::class . '::put');
+$app->post('/todos',        TodosController::class . '::post')
+  ->value('id', NULL);
+$app->delete('/todos/{id}', TodosController::class . '::delete');
+
+$app->get('/',              ServerController::class . '::index');
+$app->get('/server',        ServerController::class . '::server');
+$app->match('{url}',        ServerController::class . '::catchAll');

+ 1 - 1
web/data/items.json

@@ -2,7 +2,7 @@
     "1": {
         "id": 1,
         "description": "Remember the milk",
-        "status": "incomplete"
+        "status": "complete"
     },
     "2": {
         "status": "unfinished",

+ 1 - 1
web/server/index.php

@@ -9,5 +9,5 @@ Debug::enable();
 $app = require __DIR__ . '/../../src/boot.php';
 
 require __DIR__ . '/../../config/dev.php';
-require __DIR__ . '/../../src/controllers.php';
+require __DIR__ . '/../../src/routing.php';
 $app->run();