Page MenuHomeDevCentral

D1443.id3693.diff
No OneTemporary

D1443.id3693.diff

diff --git a/.gitignore b/.gitignore
new file mode 100644
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+vendor/
+composer.lock
diff --git a/composer.json b/composer.json
new file mode 100644
--- /dev/null
+++ b/composer.json
@@ -0,0 +1,20 @@
+{
+ "name": "nasqueron/saas-service",
+ "description": "Generic library to serve an entry point for SaaS applications",
+ "type": "library",
+ "license": "BSD-2-Clause",
+ "authors": [
+ {
+ "name": "Sébastien Santoro",
+ "email": "dereckson@espace-win.org"
+ }
+ ],
+ "require": {
+ },
+ "autoload": {
+ "psr-4": {
+ "Nasqueron\\SAAS\\": "src/",
+ "Nasqueron\\SAAS\\Tests\\": "tests/"
+ }
+ }
+}
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;
+ }
+
+}
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';
+ }
+
+}
diff --git a/tests/Mocks/TestService.php b/tests/Mocks/TestService.php
new file mode 100644
--- /dev/null
+++ b/tests/Mocks/TestService.php
@@ -0,0 +1,28 @@
+<?php
+
+namespace Nasqueron\SAAS\Tests\Mocks;
+
+use Nasqueron\SAAS\InstanceNotFoundException;
+use Nasqueron\SAAS\SaaSException;
+use Nasqueron\SAAS\Service;
+
+class TestService extends Service {
+
+ /**
+ * @var string
+ */
+ private $host;
+
+ /**
+ * @var Service
+ */
+ private static $service = null;
+
+ public function __construct (string $host = '') {
+ $this->host = $host ?: self::getServerHost();
+ }
+
+
+ public function run () : void {
+ }
+}

File Metadata

Mime Type
text/plain
Expires
Thu, Nov 28, 04:48 (3 h, 24 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2268434
Default Alt Text
D1443.id3693.diff (5 KB)

Event Timeline