Merged in controllers-architecture (pull request #6)

Controllers architecture
This commit is contained in:
Ivan Diaz 2016-03-20 15:14:27 -03:00
commit fefd530b2a
6 changed files with 94 additions and 39 deletions

View File

@ -1,40 +1,11 @@
<?php <?php
$app->group('/user', function () use ($app) { include 'user/login.php';
include 'user/signup.php';
$app->get('/get/(:by)/(:value)', function () use ($app) { $userControllers = new ControllerGroup();
echo "Returns the user with $by = $value as a json"; $userControllers->setGroupPath('/user');
});
//TODO: THIS METHOD CAN BE ONLY USED IF IT IS LOGIN AS ADMIN $userControllers->addController(new LoginController);
$app->get('/add/:email/:pass', function ($email, $pass) use ($app) { $userControllers->addController(new SignUpController);
$userInstance = new User();
$userInstance->setProperties(array(
'email' => $email,
'password' => $pass,
'admin' => 0
));
$id = $userInstance->store();
Response::respondSuccess(array(
'id' => $id
));
});
$app->post('/login', function () use ($app) { $userControllers->finalize();
$email = Controller::request('email');
$password = Controller::request('password');
if ($userInstance = User::getUser($email, 'email')) {
$pass = $userInstance->password;
}
else {
Response::respondError(ERRORS::INVALID_CREDENTIALS);
}
if ($userInstance->password === $password) {
Response::respondSuccess();
}
else {
Response::respondError(ERRORS::INVALID_CREDENTIALS);
}
});
});

View File

@ -0,0 +1,20 @@
<?php
class LoginController extends Controller {
const PATH = '/login';
public function handler() {
$email = Controller::request('email');
$password = Controller::request('password');
$user = User::getUser($email, 'email');
if ($user === null || $user->password !== $password) {
Response::respondError(ERRORS::INVALID_CREDENTIALS);
return;
}
Response::respondSuccess();
return;
}
}

View File

@ -0,0 +1,22 @@
<?php
class SignUpController extends Controller {
const PATH = '/signup';
public function handler() {
$email = Controller::request('email');
$password = Controller::request('password');
$userInstance = new User();
$userInstance->setProperties(array(
'email' => $email,
'password' => $password,
'admin' => 0
));
$id = $userInstance->store();
Response::respondSuccess(array(
'id' => $id
));
}
}

View File

@ -20,7 +20,9 @@ spl_autoload_register(function ($class) {
}); });
// LOAD CONTROLLERS // LOAD CONTROLLERS
include 'libs/Controller.php'; include_once 'libs/Controller.php';
include_once 'libs/ControllerGroup.php';
foreach (glob('controllers/*.php') as $controller) { foreach (glob('controllers/*.php') as $controller) {
include $controller; include $controller;
} }

View File

@ -1,8 +1,20 @@
<?php <?php
class Controller { abstract class Controller {
/**
* Instance-related stuff
*/
abstract public function handler();
public function getHandler() {
return function () {
$this->handler();
};
}
public static function request($key) { public static function request($key) {
$app = \Slim\Slim::getInstance(); $app = self::getAppInstance();
return $app->request()->post($key); return $app->request()->post($key);
} }
@ -27,4 +39,8 @@ class Controller {
public static function checkAdminLogged() { public static function checkAdminLogged() {
return self::checkUserLogged() && (self::getLoggedUser()->admin === 2); return self::checkUserLogged() && (self::getLoggedUser()->admin === 2);
} }
public static function getAppInstance() {
return \Slim\Slim::getInstance();
}
} }

View File

@ -0,0 +1,24 @@
<?php
class ControllerGroup {
private $groupPath;
private $controllers = array();
public function setGroupPath($groupPath) {
$this->groupPath = $groupPath;
}
public function addController($controller) {
array_push($this->controllers, $controller);
}
public function finalize() {
$app = Controller::getAppInstance();
$controllers = $this->controllers;
$app->group($this->groupPath, function () use ($app, $controllers) {
foreach ($controllers as $controller) {
$app->post($controller::PATH, $controller->getHandler());
}
});
}
}