partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

commit 6652b5be1b1e16d0ab724c002797f14f9a519dac
parent dec069b9edec98e5e541860dd5cf625e75e3140a
Author: Felicitus <felicitus@felicitus.org>
Date:   Wed,  4 Jul 2012 19:28:12 +0200

Refactored the ServiceManager to act better (proper OO methology)

Diffstat:
Msrc/backend/PartKeepr/Service/ServiceManager.php | 95++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------------
Msrc/frontend/rest.php | 5+++--
Msrc/frontend/service.php | 5+++--
3 files changed, 74 insertions(+), 31 deletions(-)

diff --git a/src/backend/PartKeepr/Service/ServiceManager.php b/src/backend/PartKeepr/Service/ServiceManager.php @@ -3,33 +3,70 @@ namespace PartKeepr\Service; use PartKeepr\Session\SessionManager, PartKeepr\Service\Exceptions\ServiceException, + PartKeepr\Util\Singleton, PartKeepr\PartKeepr, PartKeepr\User\User, PartKeepr\User\UserManager, PartKeepr\REST\Request; -class ServiceManager { +class ServiceManager extends Singleton { + private $request = null; + private $service = null; + private $mimetype = "application/json"; - public static function sendHeaders () { - header("Content-Type: text/html; charset=UTF-8"); + /** + * Initializes the request and service variables + */ + public function initialize () { + $this->request = new Request(array('restful' => true)); + $this->service = $this->request->getService(); + + // Special case because of file uploads + if ($this->service instanceof \PartKeepr\TempFile\TempFileService) { + $this->mimetype = "text/html"; + } + } + /** + * Sends the response headers. + * + * Sends cache control headers as well as the mime type. + * + * @param $mimeType string The mimetype to send, defaults to application/json + */ + public function sendHeaders () { + header("Content-Type: ".$this->mimetype."; charset=UTF-8"); header("Cache-Control: no-cache, must-revalidate"); header("Access-Control-Allow-Origin: *"); header("Access-Control-Allow-Headers: lang,call,service,X-Requested-With,X-PartKeepr-Locale,X-PartKeepr-Name,X-PartKeepr-Call"); } - public static function call () { - - $request = new Request(array('restful' => true)); - $service = $request->getService(); + /** + * Returns the effective call on a service which we need to process + * + * The following rules decide which call to make: + * - Use the "call" header, if set + * - Use the "call" parameter, if set + * - Use the action from a REST request (e.g. /controller/action/id) + * - Use the HTTP Method name, where: + * - "POST" calls "create" + * - "GET" calls "get" + * - "PUT" calls "update" + * - "DELETE" calls "destroy" + * - Any other HTTP verbs are mapped 1:1 + * + * @return string the method name to be called + */ + public function getCall () { + $call = null; - if ($service->hasHeader("call")) { - $call = $service->getHeader("call"); + if ($this->service->hasHeader("call")) { + $call = $this->service->getHeader("call"); } elseif (array_key_exists("call", $_REQUEST) && $_REQUEST["call"] != "") { $call = $_REQUEST["call"]; - } elseif ($request->action != "") { - $call = $request->action; + } elseif ($this->request->action != "") { + $call = $this->request->action; } else { - switch (strtoupper($request->getMethod())) { + switch (strtoupper($this->request->getMethod())) { case "POST": $call = "create"; break; @@ -43,18 +80,24 @@ class ServiceManager { $call = "destroy"; break; default: - $call = $request->getMethod(); + $call = $this->request->getMethod(); break; } } + + return $call; + } + + public function call () { + $call = $this->getCall(); $allowCall = true; - if (!is_subclass_of($service, "PartKeepr\\Service\\AnonService")) { + if (!is_subclass_of($this->service, "PartKeepr\\Service\\AnonService")) { $session = null; $sessionid = false; - $sessionid = self::getSession($service); + $sessionid = $this->getSession($this->service); if ($sessionid === null) @@ -65,7 +108,7 @@ class ServiceManager { $session = SessionManager::getInstance()->resumeSession($sessionid); } - if (!$service->mayCall($call)) { + if (!$this->service->mayCall($call)) { $allowCall = false; } } @@ -74,10 +117,10 @@ class ServiceManager { throw new ServiceException("Permission denied"); } - if (!method_exists($service, $call)) { - throw new \Exception(sprintf("The service %s doesn't implement %s", get_class($service), $call)); + if (!method_exists($this->service, $call)) { + throw new \Exception(sprintf("The service %s doesn't implement %s", get_class($this->service), $call)); } - $result = $service->$call(); + $result = $this->service->$call(); PartKeepr::getEM()->flush(); @@ -85,13 +128,13 @@ class ServiceManager { } - private static function getSession ($service) { + private function getSession ($service) { if ($service->hasHeader("username") && $service->hasHeader("password") && !$service->hasHeader("session")) { - return self::authenticateByUsername($service->getHeader("username"), $service->getHeader("password")); + return $this->authenticateByUsername($service->getHeader("username"), $service->getHeader("password")); } if (array_key_exists("username", $_REQUEST) && array_key_exists("password", $_REQUEST) && !array_key_exists("session", $_REQUEST)) { - return self::authenticateByUsername($_REQUEST["username"], $_REQUEST["password"]); + return $this->authenticateByUsername($_REQUEST["username"], $_REQUEST["password"]); } if ($service->hasHeader("session")) { @@ -103,7 +146,7 @@ class ServiceManager { } } - private static function authenticateByUsername ($username, $password) { + private function authenticateByUsername ($username, $password) { /* Build a temporary user */ $user = new User; $user->setRawUsername($username); @@ -121,6 +164,4 @@ class ServiceManager { } } -} - -?>- \ No newline at end of file +}+ \ No newline at end of file diff --git a/src/frontend/rest.php b/src/frontend/rest.php @@ -8,7 +8,8 @@ include("../src/backend/PartKeepr/PartKeepr.php"); PartKeepr::initialize(""); -ServiceManager::sendHeaders(); +ServiceManager::getInstance()->initialize(); +ServiceManager::getInstance()->sendHeaders(); $timingStart = microtime(true); @@ -52,7 +53,7 @@ try { $response = array(); $response["status"] = "ok"; $response["success"] = true; - $response["response"] = ServiceManager::call(); + $response["response"] = ServiceManager::getInstance()->call(); $response["timing"] = microtime(true) - $timingStart; echo json_encode($response); diff --git a/src/frontend/service.php b/src/frontend/service.php @@ -8,7 +8,8 @@ include("../src/backend/PartKeepr/PartKeepr.php"); PartKeepr::initialize(""); -ServiceManager::sendHeaders(); +ServiceManager::getInstance()->initialize(); +ServiceManager::getInstance()->sendHeaders(); $timingStart = microtime(true); @@ -24,7 +25,7 @@ try { $response = array(); $response["status"] = "ok"; - $response["response"] = ServiceManager::call($request); + $response["response"] = ServiceManager::getInstance()->call($request); $response["timing"] = microtime(true) - $timingStart; } catch (PartKeepr\Util\SerializableException $e) {