Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F7121268
D1060.id2907.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.id2907.diff
View Options
Index: .gitignore
===================================================================
--- /dev/null
+++ .gitignore
@@ -0,0 +1,2 @@
+/vendor/
+composer.lock
Index: README
===================================================================
--- /dev/null
+++ 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
Index: composer.json
===================================================================
--- /dev/null
+++ 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/"
+ }
+ }
+}
Index: src/BaseService.php
===================================================================
--- /dev/null
+++ 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);
+ }
+
+}
Index: src/Log.php
===================================================================
--- /dev/null
+++ 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());
+ }
+
+}
Index: src/LogEntry.php
===================================================================
--- /dev/null
+++ 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);
+ }
+
+}
Index: src/Service.php
===================================================================
--- /dev/null
+++ 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');
+ }
+
+}
Index: src/public/index.php
===================================================================
--- /dev/null
+++ 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();
Index: tests/LogTest.php
===================================================================
--- /dev/null
+++ 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;
+ }
+
+}
Index: tests/log.json
===================================================================
--- /dev/null
+++ 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
Tue, Apr 22, 01:27 (15 h, 1 s)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2593829
Default Alt Text
D1060.id2907.diff (10 KB)
Attached To
Mode
D1060: Initial revision
Attached
Detach File
Event Timeline
Log In to Comment