summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Config/Config.php17
-rw-r--r--src/Core/Main.php29
-rw-r--r--src/Game.php35
-rw-r--r--src/Message/Base.php72
-rw-r--r--src/Message/Handler.php69
-rw-r--r--src/Message/Overview.php25
6 files changed, 247 insertions, 0 deletions
diff --git a/src/Config/Config.php b/src/Config/Config.php
new file mode 100644
index 0000000..10236f0
--- /dev/null
+++ b/src/Config/Config.php
@@ -0,0 +1,17 @@
+<?php
+
+namespace App\Config;
+
+class Config
+{
+ protected static $config;
+
+ public static function getconfig()
+ {
+ if (!static::$config) {
+ static::$config = include SITE_ROOT . '/config/messages.php';
+ }
+
+ return static::$config;
+ }
+} \ No newline at end of file
diff --git a/src/Core/Main.php b/src/Core/Main.php
new file mode 100644
index 0000000..594b15c
--- /dev/null
+++ b/src/Core/Main.php
@@ -0,0 +1,29 @@
+<?php
+
+namespace App\Core;
+
+use App\Game;
+
+/**
+* Core Main class, deals with all set-up
+*/
+class Main
+{
+
+ public function __construct()
+ {
+ $this->init();
+
+ $this->run();
+ }
+
+ protected function init()
+ {
+ return true;
+ }
+
+ protected function run()
+ {
+ $game = new Game();
+ }
+} \ No newline at end of file
diff --git a/src/Game.php b/src/Game.php
new file mode 100644
index 0000000..3a62f3e
--- /dev/null
+++ b/src/Game.php
@@ -0,0 +1,35 @@
+<?php
+
+namespace App;
+
+use App\Message\Handler;
+
+/**
+ * A Game class that handles game execution
+ */
+class Game
+{
+ protected $messages;
+
+ public function __construct()
+ {
+ $this->init();
+ }
+
+ protected function init()
+ {
+ // Do any pre set-up
+ $this->handler = new Handler(['inject']);
+ $this->sendOpeningMessage();
+ }
+
+ public function sendOpeningMessage()
+ {
+ $message = $this->handler->renderOpeningMessage();
+
+ $important = $this->handler->returnImportant('inject');
+
+ var_dump($message, $important);
+ }
+
+}
diff --git a/src/Message/Base.php b/src/Message/Base.php
new file mode 100644
index 0000000..a4430da
--- /dev/null
+++ b/src/Message/Base.php
@@ -0,0 +1,72 @@
+<?php
+
+namespace App\Message;
+
+use Mustache_Engine;
+use App\Config\Config;
+use App\Message\Handler;
+
+/**
+* Base class for a message
+*/
+class Base
+{
+ protected $message_name;
+ protected $config;
+ protected $message;
+ protected $injects = [];
+ protected $important;
+
+ protected function loadMessage()
+ {
+ if (!array_key_exists($this->message_name, $this->config)) {
+ throw new Exception('Could not load messge from config (' . $this->message_name . ')');
+ }
+
+ $this->message = $this->config[$this->message_name];
+ }
+
+ protected function loadConfig()
+ {
+ $config =Config::getConfig();
+
+ if (!$config) {
+ throw new \Exception('Could not load config');
+ }
+
+ $this->config = $config;
+ }
+
+ public function __construct()
+ {
+ $this->loadConfig();
+
+ $this->loadMessage();
+ }
+
+ public function renderMessage()
+ {
+ $m = new Mustache_Engine;
+ return $m->render($this->message, $this->getInjectValues());
+ }
+
+ public function getInjectValues()
+ {
+ $out = [];
+ foreach ($this->injects as $inject) {
+ $out[$inject] = $this->$inject;
+ }
+
+ return $out;
+ }
+
+ public function getImportantValues()
+ {
+ $out = [];
+ foreach ($this->important as $important) {
+ $out[$important] = $this->$important;
+ }
+
+ return $out;
+ }
+} \ No newline at end of file
diff --git a/src/Message/Handler.php b/src/Message/Handler.php
new file mode 100644
index 0000000..8729c1a
--- /dev/null
+++ b/src/Message/Handler.php
@@ -0,0 +1,69 @@
+<?php
+
+namespace App\Message;
+
+use App\Message\Overview;
+
+/**
+* Handles messages and context
+*/
+class Handler
+{
+ protected $important = [];
+
+ protected $importantValues = [];
+
+ public function __construct($important)
+ {
+ $this->setupImportant($important);
+ }
+ /**
+ * Set's up important attributes on this class
+ */
+ public function setupImportant($important = [])
+ {
+ $this->important = $important;
+ }
+
+ protected function setImportantValues($array)
+ {
+ foreach ($array as $name => $value) {
+ $this->addImportantValue($name, $value);
+ }
+ }
+
+ protected function addImportantValue($name, $value)
+ {
+ if (in_array($name, $this->important)) {
+ $this->importantValues[$name] = $value;
+ }
+
+ return $name;
+ }
+
+ public function returnImportant($name)
+ {
+ if (!array_key_exists($name, $this->importantValues)) {
+ throw new \Exception('The var ' . $name . ' is not set in this context');
+ }
+
+ return $this->importantValues[$name];
+ }
+
+ public function __call($name, $arguments)
+ {
+ if (strpos($name, 'get') === 0) {
+ $this->returnImporant(substr($name, 3));
+ }
+ }
+
+ public function renderOpeningMessage()
+ {
+ $overview = new Overview();
+ $message = $overview->renderMessage();
+
+ $this->setImportantValues($overview->getImportantValues());
+
+ return $message;
+ }
+} \ No newline at end of file
diff --git a/src/Message/Overview.php b/src/Message/Overview.php
new file mode 100644
index 0000000..3676176
--- /dev/null
+++ b/src/Message/Overview.php
@@ -0,0 +1,25 @@
+<?php
+
+namespace App\Message;
+
+use App\Message\Base;
+
+class Overview extends Base
+{
+ protected $message_name = 'opening';
+ protected $injects = ['inject', 'second'];
+ protected $important = ['inject'];
+ public $inject;
+ public $second = "second";
+
+ public function __construct()
+ {
+ $this->inject = $this->getInject();
+ parent::__construct();
+ }
+
+ public function getInject()
+ {
+ return 'foo';
+ }
+} \ No newline at end of file