id = $id; $this->name = $name; $this->computers = $computers; } } class Computer { public $id; public $type; public $users; public function __construct($id, $type, $users = array()) { $this->id = $id; $this->type = $type; $this->users = $users; } } } namespace Server\ServiceLayer { abstract class Manager { protected $dp; // DataProvider protected function output($result) { header('Content-Type: application/json'); echo json_encode($result); } public function __construct($dp) { $this->dp = $dp; } public abstract function get($pathid, $query); } class UserManager extends Manager { public function __construct($dp) { parent::__construct($dp); } public function get($pathid, $query) { if($pathid != null) { $id = intval($pathid); $this->output($this->dp->getOneUser($id)); } else { $this->output($this->dp->getAllUsers()); } } } class ComputerManager extends Manager { public function __construct($dp) { parent::__construct($dp); } public function get($pathid, $query) { if($pathid != null) { $id = intval($pathid); $this->output($this->dp->getOneComputer($id)); } else { $this->output($this->dp->getAllComputers()); } } } class Facade { private $usrmgr; private $compmgr; public function __construct($dp) { $this->usrmgr = new \Server\ServiceLayer\UserManager($dp); $this->compmgr = new \Server\ServiceLayer\ComputerManager($dp); } public function get() { if(isset($_SERVER['PATH_INFO'])) { $pathparts = explode('/', $_SERVER['PATH_INFO']); if(count($pathparts) >= 2) { $mgrnam = $pathparts[1]; $pathid = count($pathparts) >= 3 ? $pathparts[2] : null; $query = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : null; if($mgrnam == 'user') { return $this->usrmgr->get($pathid, $query); } else if($mgrnam == 'computer') { return $this->compmgr->get($pathid, $query); } else { throw new \Exception('Unknown resource'); } } } throw new \Exception('Bad path'); } public function process() { $method = $_SERVER['REQUEST_METHOD']; if($method == 'GET') { $this->get(); } else { throw new \Excpetion('Method not supported'); } } } } namespace Server\DataAccessLayer { interface DataProvider { public function getOneUser($id); public function getAllUSers(); public function getOneComputer($id); public function getAllComputers(); } class DummyDataProvider implements DataProvider { public function getOneUser($id) { return new \Server\DomainLayer\User(1,'Anders And', array(new \Server\DomainLayer\Computer(1, 'HP PC'))); } public function getAllUSers() { return array(new \Server\DomainLayer\User(1,'Anders And', array(new \Server\DomainLayer\Computer(1, 'HP PC'))), new \Server\DomainLayer\User(2,'Georg Gearløs', array(new \Server\DomainLayer\Computer(2, 'Dell PC')))); } public function getOneComputer($id) { return new \Server\DomainLayer\Computer(1, 'HP PC', array(new \Server\DomainLayer\User(1,'Anders And'))); } public function getAllComputers() { return array(new \Server\DomainLayer\Computer(1, 'HP PC', array(new \Server\DomainLayer\User(1,'Anders And'))), new \Server\DomainLayer\Computer(2, 'Dell PC', new \Server\DomainLayer\User(2,'Georg Gearløs'))); } } class DatabaseDataProvider implements DataProvider { private $db; // PDO connection public function __construct($db) { $this->db = $db; } public function getOneUser($id) { // SELECT name FROM users JOIN uc ON users.id=uc.userid JOIN computers ON uc.computerid=computers.id WHERE users.id=? throw new \Excpetion('Not implemented yet'); } public function getAllUSers() { // SELECT name FROM users JOIN uc ON users.id=uc.userid JOIN computers ON uc.computerid=computers.id throw new \Excpetion('Not implemented yet'); } public function getOneComputer($id) { // SELECT type FROM computers JOIN uc ON cmputers.id=uc.computerid JOIN users ON uc.userid=users.id WHERE computers.id=? throw new \Excpetion('Not implemented yet'); } public function getAllComputers() { // SELECT type FROM computers JOIN uc ON cmputers.id=uc.computerid JOIN users ON uc.userid=users.id throw new \Excpetion('Not implemented yet'); } } } namespace Server { $fac = new \Server\ServiceLayer\Facade(new \Server\DataAccessLayer\DummyDataProvider()); $fac->process(); } ?>