Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F3805145
D1060.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
10 KB
Referenced Files
None
Subscribers
None
D1060.diff
View Options
diff --git a/.gitignore b/.gitignore
new file mode 100644
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+/vendor/
+composer.lock
diff --git a/README b/README
new file mode 100644
--- /dev/null
+++ b/README
@@ -0,0 +1,13 @@
+ ______ _______ _______ ___
+| _ \.---.-.-----.-----.--.--.-----.----.-----.-----.| _ | _ | |
+|. | | _ |__ --| _ | | | -__| _| _ | ||. 1 |. 1 |. |
+|. | |___._|_____|__ |_____|_____|__| |_____|__|__||. _ |. ____|. |
+|: | | |__| |: | |: | |: |
+|::.| | Servers log :: Add new entries |::.|:. |::.| |::.|
+`--- ---' https://api.nasqueron.org/serverslog `--- ---`---' `---'
+
+Allows to post a new entry to the servers log and saves it to all.json.
+
+To install:
+
+ composer install
diff --git a/composer.json b/composer.json
new file mode 100644
--- /dev/null
+++ b/composer.json
@@ -0,0 +1,27 @@
+{
+ "name": "nasqueron/api-serverslog",
+ "description": "Serve the servers log API",
+ "type": "project",
+ "license": "BSD-2-Clause",
+ "authors": [
+ {
+ "name": "Sébastien Santoro",
+ "email": "dereckson@espace-win.org"
+ }
+ ],
+ "require": {
+ "php": ">=7.1.0",
+ "netresearch/jsonmapper": "^1.1.1",
+ "vlucas/phpdotenv": "^2.4"
+ },
+ "require-dev": {
+ "psy/psysh": "^0.8.12",
+ "phpunit/phpunit": "^6.4"
+ },
+ "autoload": {
+ "psr-4": {
+ "Nasqueron\\Api\\ServersLog\\": "src/",
+ "Nasqueron\\Api\\ServersLog\\Tests\\": "tests/"
+ }
+ }
+}
diff --git a/src/BaseService.php b/src/BaseService.php
new file mode 100644
--- /dev/null
+++ b/src/BaseService.php
@@ -0,0 +1,34 @@
+<?php
+
+namespace Nasqueron\Api\ServersLog;
+
+use Exception;
+
+class BaseService {
+
+ ///
+ /// Helper methods
+ ///
+
+ protected function getBodyObject () {
+ $content= file_get_contents('php://input');
+
+ switch ($_SERVER["HTTP_CONTENT_TYPE"]) {
+ case "application/json":
+ return json_decode($content);
+
+ default:
+ throw new Exception("Unknown content type.");
+ }
+ }
+
+ protected function sendSuccessResponse () {
+ // HTTP 200 OK
+ }
+
+ protected function sendInvalidMethodResponse () {
+ header("HTTP/1.0 405 Method Not Allowed");
+ http_response_code(405);
+ }
+
+}
diff --git a/src/Log.php b/src/Log.php
new file mode 100644
--- /dev/null
+++ b/src/Log.php
@@ -0,0 +1,50 @@
+<?php
+
+namespace Nasqueron\Api\ServersLog;
+
+class Log {
+
+ /**
+ * @var LogEntry[]
+ */
+ private $entries = [];
+
+ public function add (LogEntry $entry) : void {
+ $this->entries[] = $entry;
+ }
+
+ public function getAll () : array {
+ return $this->entries;
+ }
+
+ public function toJSON () : string {
+ return json_encode($this->entries, JSON_PRETTY_PRINT);
+ }
+
+ public function fillFromJSON (string $json) : void {
+ $entries = json_decode($json);
+ foreach ($entries as $entry) {
+ $this->add(LogEntry::fromJSON($entry));
+ }
+ }
+
+ ///
+ /// Static helper methods
+ ///
+
+ public static function loadFromJSONFile (string $filename) : Log {
+ $log = new Log;
+
+ $json = file_get_contents($filename);
+ $log->fillFromJSON($json);
+
+ return $log;
+ }
+
+ public static function addEntryToJSONFile (string $filename, LogEntry $entry) : void {
+ $log = self::loadFromJSONFile($filename);
+ $log->add($entry);
+ file_put_contents($filename, $log->toJSON());
+ }
+
+}
diff --git a/src/LogEntry.php b/src/LogEntry.php
new file mode 100644
--- /dev/null
+++ b/src/LogEntry.php
@@ -0,0 +1,46 @@
+<?php
+
+namespace Nasqueron\Api\ServersLog;
+
+use JsonMapper;
+
+class LogEntry {
+
+ ///
+ /// Public properties
+ ///
+
+ public $date = "";
+
+ public $emitter = "";
+
+ public $source = "";
+
+ public $component = "";
+
+ public $entry = "";
+
+ ///
+ /// Constructor
+ ///
+
+ public function __construct () {
+ $this->date = self::get_current_timestamp();
+ }
+
+ ///
+ /// Helper methods
+ ///
+
+ // Helper methods
+ private static function get_current_timestamp () : string {
+ // Nasqueron log format: 2016-02-13T23:14:00Z (with a final Z for UTC)
+ return str_replace("+00:00", "Z", gmdate('c'));
+ }
+
+ public static function fromJSON ($json) : object {
+ $mapper = (new JsonMapper());
+ return $mapper->map($json, new LogEntry);
+ }
+
+}
diff --git a/src/Service.php b/src/Service.php
new file mode 100644
--- /dev/null
+++ b/src/Service.php
@@ -0,0 +1,57 @@
+<?php
+
+namespace Nasqueron\Api\ServersLog;
+
+use Dotenv\Dotenv;
+
+class Service extends BaseService {
+
+ ///
+ /// Initialisation
+ ///
+
+ public function __construct () {
+ $this->loadEnvironment();
+ }
+
+ ///
+ /// Controller
+ ///
+
+ public function handle () : void {
+ $body = $this->getBodyObject();
+
+ if ($_SERVER['REQUEST_METHOD'] === "PUT") {
+ $this->put($body);
+ return;
+ }
+
+ $this->sendInvalidMethodResponse();
+ }
+
+ public function put ($data) : void {
+ Log::addEntryToJSONFile(
+ self::getServersLogFile(),
+ LogEntry::fromJSON($data)
+ );
+
+ $this->sendSuccessResponse();
+ }
+
+ ///
+ /// Helper methods
+ ///
+
+ private function loadEnvironment () : void {
+ $env = new Dotenv(__DIR__);
+ if (file_exists(__DIR__ . '/.env')) {
+ $env->load();
+ }
+ $env->required('SERVERS_LOG_FILE');
+ }
+
+ private static function getServersLogFile () : string {
+ return getenv('SERVERS_LOG_FILE');
+ }
+
+}
diff --git a/src/public/index.php b/src/public/index.php
new file mode 100644
--- /dev/null
+++ b/src/public/index.php
@@ -0,0 +1,19 @@
+<?php
+
+/*
+ ______ _______ _______ ___
+| _ \.---.-.-----.-----.--.--.-----.----.-----.-----.| _ | _ | |
+|. | | _ |__ --| _ | | | -__| _| _ | ||. 1 |. 1 |. |
+|. | |___._|_____|__ |_____|_____|__| |_____|__|__||. _ |. ____|. |
+|: | | |__| |: | |: | |: |
+|::.| | Servers log :: Add new entries |::.|:. |::.| |::.|
+`--- ---' https://api.nasqueron.org/serverslog `--- ---`---' `---'
+
+ */
+
+use Nasqueron\Api\ServersLog\Service;
+
+require __DIR__ . '/../../vendor/autoload.php';
+
+$service = new Service();
+$service->handle();
diff --git a/tests/LogTest.php b/tests/LogTest.php
new file mode 100644
--- /dev/null
+++ b/tests/LogTest.php
@@ -0,0 +1,127 @@
+<?php
+
+use Nasqueron\Api\ServersLog\Log;
+use Nasqueron\Api\ServersLog\LogEntry;
+
+use PHPUnit\Framework\TestCase;
+
+class LogTest extends TestCase {
+
+ ///
+ /// Tests
+ ///
+
+ public function testAdd () {
+ $log = new Log;
+
+ for ($i = 0 ; $i < 5 ; $i++) {
+ $log->add(new LogEntry);
+ }
+
+ $entriesCount = count($log->getAll());
+
+ $this->assertEquals(5, $entriesCount);
+ }
+
+ public function testGetAllWhenLogIsEmpty () {
+ $log = new Log;
+
+ $this->assertEquals([], $log->getAll());
+ }
+
+ public function testGetAllWhenLogContainsEntries () {
+ $log = new Log;
+ $log->add(new LogEntry);
+
+ foreach ($log->getAll() as $entry) {
+ $this->assertInstanceOf(
+ "Nasqueron\\Api\\ServersLog\\LogEntry",
+ $entry
+ );
+ }
+ }
+
+ public function testToJson () {
+ $log = new Log;
+ $log->add($this->getSampleLogEntry());
+
+ $this->assertJsonStringEqualsJsonFile(
+ __DIR__ . "/log.json",
+ $log->toJSON()
+ );
+ }
+
+ public function testFillFromJSON () {
+ $json = file_get_contents(__DIR__ . "/log.json");
+ $log = new Log;
+ $log->fillFromJSON($json);
+
+ $entries = $log->getAll();
+ $entry = $entries[0];
+
+ $this->assertInstanceOf(
+ "Nasqueron\\Api\\ServersLog\\LogEntry",
+ $entry
+ );
+ }
+
+ public function testLoadFromJSONFile () {
+ $log = Log::loadFromJSONFile(__DIR__ . "/log.json");
+
+ $this->assertInstanceOf(
+ "Nasqueron\\Api\\ServersLog\\Log",
+ $log
+ );
+
+ $entries = $log->getAll();
+ $this->assertEquals(1, count($entries));
+
+ $entry = $entries[0];
+ $this->assertEquals("Acme", $entry->component);
+ $this->assertEquals("1974", $entry->date);
+ $this->assertInstanceOf(
+ "Nasqueron\\Api\\ServersLog\\LogEntry",
+ $entry
+ );
+ }
+
+ public function testAddEntryToJSONFile () {
+ $logFilename = tempnam(
+ sys_get_temp_dir(),
+ 'ServersLogTest'
+ );
+ file_put_contents(
+ $logFilename,
+ file_get_contents(__DIR__ . "/log.json")
+ );
+
+ Log::addEntryToJSONFile(
+ $logFilename,
+ $this->getSampleLogEntry()
+ );
+
+ $log = Log::loadFromJSONFile($logFilename);
+ $entriesCount = count($log->getAll());
+ $this->assertEquals(2, $entriesCount);
+
+ unlink($logFilename);
+
+ }
+
+ ///
+ /// Helper methods
+ ///
+
+ private function getSampleLogEntry () {
+ $entry = new Logentry;
+
+ $entry->component = "Acme";
+ $entry->date = "1974";
+ $entry->emitter = "Tests";
+ $entry->entry = "Something happens.";
+ $entry->source = "LogTest.php";
+
+ return $entry;
+ }
+
+}
diff --git a/tests/client/fireDummyLogEntry.tcl b/tests/client/fireDummyLogEntry.tcl
new file mode 100755
--- /dev/null
+++ b/tests/client/fireDummyLogEntry.tcl
@@ -0,0 +1,26 @@
+#!/usr/bin/env tclsh8.6
+
+package require json::write
+package require rest
+
+proc dict2json {dictToEncode} {
+ ::json::write object {*}[dict map {k v} $dictToEncode {
+ set v [::json::write string $v]
+ }]
+}
+
+proc get_source_script {} {
+ file tail [info script]
+}
+
+rest::simple http://localhost:8000 {} {
+ method PUT
+ content-type application/json
+ format json
+} [dict2json "
+ date [clock seconds]
+ emitter Tests
+ source [get_source_script]
+ component Acme
+ entry {Something happens.}
+"]
diff --git a/tests/log.json b/tests/log.json
new file mode 100644
--- /dev/null
+++ b/tests/log.json
@@ -0,0 +1,9 @@
+[
+ {
+ "date": "1974",
+ "emitter": "Tests",
+ "source": "LogTest.php",
+ "component": "Acme",
+ "entry": "Something happens."
+ }
+]
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sun, Dec 1, 01:05 (8 h, 21 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2273832
Default Alt Text
D1060.diff (10 KB)
Attached To
Mode
D1060: Initial revision
Attached
Detach File
Event Timeline
Log In to Comment