Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F3773703
D1443.id3686.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
4 KB
Referenced Files
None
Subscribers
None
D1443.id3686.diff
View Options
diff --git a/composer.json b/composer.json
new file mode 100644
--- /dev/null
+++ b/composer.json
@@ -0,0 +1,14 @@
+{
+ "name": "nasqueron/saas-service",
+ "description": "Entry point for a generic SaaS",
+ "type": "library",
+ "license": "BSD-2-Clause",
+ "authors": [
+ {
+ "name": "Sébastien Santoro",
+ "email": "dereckson@espace-win.org"
+ }
+ ],
+ "require": {
+ }
+}
diff --git a/src/ConfigurationException.php b/src/ConfigurationException.php
new file mode 100644
--- /dev/null
+++ b/src/ConfigurationException.php
@@ -0,0 +1,8 @@
+<?php
+
+namespace Nasqueron\SAAS;
+
+
+class ConfigurationException extends SaaSException {
+
+}
diff --git a/src/InstanceNotFoundException.php b/src/InstanceNotFoundException.php
new file mode 100644
--- /dev/null
+++ b/src/InstanceNotFoundException.php
@@ -0,0 +1,38 @@
+<?php
+
+namespace Nasqueron\SAAS;
+
+use Throwable;
+
+class InstanceNotFoundException extends SaaSException {
+
+ /**
+ * @var string
+ */
+ private $instance;
+
+ /**
+ * @var string
+ */
+ const DEFAULT_MESSAGE = "The specified instance can't been found.";
+
+ public function __construct (string $instance, string $message = "",
+ int $code = 0, Throwable $previous = null) {
+ $this->instance = $instance;
+
+ if ($message === "") {
+ $message = self::DEFAULT_MESSAGE;
+ }
+
+ parent::__construct($message, $code, $previous);
+ }
+
+ public function getInstance () : string {
+ return $this->instance;
+ }
+
+ public function setInstance (string $instance) : void {
+ $this->instance = $instance;
+ }
+
+}
\ No newline at end of file
diff --git a/src/SaaSException.php b/src/SaaSException.php
new file mode 100644
--- /dev/null
+++ b/src/SaaSException.php
@@ -0,0 +1,8 @@
+<?php
+
+namespace Nasqueron\SAAS;
+
+
+class SaaSException extends \Exception {
+
+}
diff --git a/src/Service.php b/src/Service.php
new file mode 100644
--- /dev/null
+++ b/src/Service.php
@@ -0,0 +1,100 @@
+<?php
+
+namespace Nasqueron\SAAS;
+
+abstract class Service {
+
+ ///
+ /// Request methods
+ ///
+
+ public function handleNotExistingSite() : Service {
+ if (!$this->isExisting()) {
+ $this->serveNotExistingResponse();
+ }
+
+ return $this;
+ }
+
+ /**
+ * @throws SaasException where the URI can't be found
+ */
+ public function handleAliveRequest() : Service {
+ // Handles /status requests
+ if ($this->isAliveRequest()) {
+ $this->serveAliveResponse();
+ }
+
+ return $this;
+ }
+
+ public abstract function run();
+
+
+ ///
+ /// Default implementation
+ ///
+
+ public function isExisting () : bool {
+ return true;
+ }
+
+ public function serveAliveResponse() : void {
+ die("ALIVE");
+ }
+
+ public function serveNotExistingResponse(): void {
+ header("HTTP/1.0 404 Not Found");
+ die("This site doesn't exist.");
+ }
+
+ ///
+ /// Helper methods
+ ///
+
+ public function getServerHost() : string {
+ return self::extractHost($_SERVER['HTTP_HOST']);
+ }
+
+ /**
+ * Extracts host from a host:port expression
+ */
+ private static function extractHost (string $expression) : string {
+ $position = strpos($expression, ':');
+
+ if ($position === false) {
+ return $expression;
+ }
+
+ return substr ($expression, 0, $position);
+ }
+
+ /**
+ * @throws SaasException when no URL server parameter have been found.
+ */
+ public function getUri() : string {
+ $sources = [
+ 'DOCUMENT_URI',
+ 'REQUEST_URI',
+ ];
+
+ foreach ($sources as $source) {
+ if (isset($_SERVER[$source])) {
+ return $_SERVER[$source];
+ }
+ }
+
+ throw new SaasException("Can't get URI.");
+ }
+
+ /**
+ * @throws SaasException where the URI can't be found
+ */
+ private function isAliveRequest() : bool {
+ return
+ $_SERVER['REQUEST_METHOD'] === 'GET'
+ &&
+ $this->getUri() === '/status';
+ }
+
+}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Mon, Nov 25, 08:11 (20 h, 6 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2262215
Default Alt Text
D1443.id3686.diff (4 KB)
Attached To
Mode
D1443: Bootstrap base SaaS library
Attached
Detach File
Event Timeline
Log In to Comment