summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Config/Config.php18
-rw-r--r--src/Core/Main.php25
-rw-r--r--src/Game.php19
-rw-r--r--src/Message/Base.php83
-rw-r--r--src/Message/Handler.php72
-rw-r--r--src/Message/Overview.php50
6 files changed, 238 insertions, 29 deletions
diff --git a/src/Config/Config.php b/src/Config/Config.php
index 10236f0..710c9e0 100644
--- a/src/Config/Config.php
+++ b/src/Config/Config.php
@@ -2,10 +2,26 @@
namespace App\Config;
+/**
+ * Config class to handle loading and returning config stuff
+ *
+ * @author Phil Burton <phil@pgburton.com>
+ */
class Config
{
+ /**
+ * Cached config
+ *
+ * @var mixed[]
+ */
protected static $config;
+ /**
+ * Return the cofnig
+ *
+ * @author Phil Burton <phil@pgburton.com>
+ * @return mixed[]
+ */
public static function getconfig()
{
if (!static::$config) {
@@ -14,4 +30,4 @@ class Config
return static::$config;
}
-} \ No newline at end of file
+}
diff --git a/src/Core/Main.php b/src/Core/Main.php
index 594b15c..7aa643e 100644
--- a/src/Core/Main.php
+++ b/src/Core/Main.php
@@ -5,11 +5,15 @@ namespace App\Core;
use App\Game;
/**
-* Core Main class, deals with all set-up
-*/
+ * Not really sure if we need this
+ */
class Main
{
-
+ /**
+ * Construct
+ *
+ * @author Phil Burton <phil@pgburton.com>
+ */
public function __construct()
{
$this->init();
@@ -17,13 +21,24 @@ class Main
$this->run();
}
+ /**
+ * Initalise
+ *
+ * @author Phil Burton <phil@pgburton.com>
+ * @return bool
+ */
protected function init()
{
return true;
}
+ /**
+ * Run the Game
+ *
+ * @author Phil Burton <phil@pgburton.com>
+ */
protected function run()
{
- $game = new Game();
+ $game = new Game();
}
-} \ No newline at end of file
+}
diff --git a/src/Game.php b/src/Game.php
index c2141ce..1bedba2 100644
--- a/src/Game.php
+++ b/src/Game.php
@@ -6,16 +6,26 @@ use App\Message\Handler;
/**
* A Game class that handles game execution
+ *
+ * @author Phil Burton <phil@pgburton.com>
*/
class Game
{
- protected $messages;
-
+ /**
+ * Constuct a new Game
+ *
+ * @author Phil Burton <phil@pgburton.com>
+ */
public function __construct()
{
$this->init();
}
+ /**
+ * Initalise the Game
+ *
+ * @author Phil Burton <phil@pgburton.com>
+ */
protected function init()
{
// Do any pre set-up
@@ -23,6 +33,10 @@ class Game
$this->sendOpeningMessage();
}
+ /**
+ * Send opening message
+ * @author Phil Burton <phil@pgburton.com>
+ */
public function sendOpeningMessage()
{
$message = $this->handler->renderOpeningMessage();
@@ -33,5 +47,4 @@ class Game
var_dump($message, $important, $message2);
}
-
}
diff --git a/src/Message/Base.php b/src/Message/Base.php
index 4565de9..2b7b0e4 100644
--- a/src/Message/Base.php
+++ b/src/Message/Base.php
@@ -8,15 +8,51 @@ use App\Message\Handler;
/**
* Base class for a message
+*
+* @author Phil Burton <phil@pgburton.com>
*/
class Base
{
+ /**
+ * Message name to load
+ *
+ * @var string
+ */
protected $message_name;
+
+ /**
+ * Cache of the loaded config array
+ *
+ * @var string
+ */
protected $config;
+
+ /**
+ * The raw message string loaded from config
+ *
+ * @var string
+ */
protected $message;
+
+ /**
+ * The list of vars we need to inject
+ *
+ * @var array
+ */
protected $injects = [];
+
+ /**
+ * The array of important vars
+ *
+ * @var string
+ */
protected $important;
+ /**
+ * Load the message from the config
+ *
+ * @author Phil Burton <phil@pgburton.com>
+ */
protected function loadMessage()
{
if (!array_key_exists($this->message_name, $this->config)) {
@@ -26,6 +62,9 @@ class Base
$this->message = $this->config[$this->message_name];
}
+ /**
+ * Load the config
+ */
protected function loadConfig()
{
$config =Config::getConfig();
@@ -37,39 +76,65 @@ class Base
$this->config = $config;
}
+ /**
+ * Construct, load config and message
+ *
+ * @author Phil Burton <phil@pgburton.com>
+ */
public function __construct()
{
$this->loadConfig();
-
+
$this->loadMessage();
}
+ /**
+ * Render the message, injecting any values into the message using mustache
+ *
+ * @author Phil Burton <phil@pgburton.com>
+ */
public function renderMessage()
{
$m = new Mustache_Engine;
return $m->render($this->message, $this->getInjectValues());
}
+
+ /**
+ * Return the injected values
+ *
+ * @author Phil Burton <phil@pgburton.com>
+ */
public function getInjectValues()
{
$out = [];
- foreach ($this->injects as $inject) {
- $out[$inject] = $this->$inject;
- }
+ foreach ($this->injects as $inject) {
+ $out[$inject] = $this->$inject;
+ }
return $out;
}
+ /**
+ * Get the important injected values
+ *
+ * @author Phil Burton <phil@pgburton.com>
+ */
public function getImportantValues()
{
- $out = [];
- foreach ($this->important as $important) {
- $out[$important] = $this->$important;
- }
+ $out = [];
+ foreach ($this->important as $important) {
+ $out[$important] = $this->$important;
+ }
return $out;
}
+ /**
+ * Set-up this message's inject values based on an input array
+ *
+ * @author Phil Burton <phil@pgburton.com>
+ */
public function setFromArray(array $array)
{
foreach ($array as $key => $value) {
@@ -78,4 +143,4 @@ class Base
}
}
}
-} \ No newline at end of file
+}
diff --git a/src/Message/Handler.php b/src/Message/Handler.php
index 89752c5..825ceda 100644
--- a/src/Message/Handler.php
+++ b/src/Message/Handler.php
@@ -6,25 +6,52 @@ use App\Message\Overview;
/**
* Handles messages and context
+*
+* @author Phil Burton <phil@pgburton.com>
*/
class Handler
{
+ /**
+ * The important set of injects for this handler
+ *
+ * @var string[]
+ */
protected $important = [];
+ /**
+ * The important set of values of injects for this handler
+ *
+ * @var mixed[]
+ */
protected $importantValues = [];
+ /**
+ * Construct our handler
+ *
+ * @author Phil Burton <phil@pgburton.com>
+ * @param string[] $important
+ */
public function __construct($important)
{
$this->setupImportant($important);
}
/**
- * Set's up important attributes on this class
+ * Set the important list
+ *
+ * @author Phil Burton <phil@pgburton.com>
+ * @param string[] $important
*/
public function setupImportant($important = [])
{
$this->important = $important;
}
+ /**
+ * Set important values
+ *
+ * @author Phil Burton <phil@pgburton.com>
+ * @param mixed[] $array
+ */
protected function setImportantValues($array)
{
foreach ($array as $name => $value) {
@@ -32,6 +59,13 @@ class Handler
}
}
+ /**
+ * Add an important value to our array
+ *
+ * @author Phil Burton <phil@pgburton.com>
+ * @param string $name
+ * @param mixed[] $value
+ */
protected function addImportantValue($name, $value)
{
if (in_array($name, $this->important)) {
@@ -41,15 +75,31 @@ class Handler
return $name;
}
+ /**
+ * Get a single important value
+ *
+ * @author Phil Burton <phil@pgburton.com>
+ * @param string $name
+ * @return mixed
+ */
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];
}
+ /**
+ * Magic __call override to be able to get improtant values using getMyImportant()
+ * which then runs $this->returnImportant('myImportant')
+ *
+ * @author Phil Burton <phil@pgburton.com>
+ * @param string $name
+ * @param mixed[] $arguments
+ * @return string
+ */
public function __call($name, $arguments)
{
if (strpos($name, 'get') === 0) {
@@ -57,16 +107,22 @@ class Handler
}
}
+ /**
+ * Render the opening message
+ *
+ * @author Phil Burton <phil@pgburton.com>
+ * @return string
+ */
public function renderOpeningMessage()
{
- $overview = new Overview();
+ $overview = new Overview();
- $overview->setFromArray($this->importantValues);
+ $overview->setFromArray($this->importantValues);
- $message = $overview->renderMessage();
+ $message = $overview->renderMessage();
- $this->setImportantValues($overview->getImportantValues());
+ $this->setImportantValues($overview->getImportantValues());
- return $message;
+ return $message;
}
-} \ No newline at end of file
+}
diff --git a/src/Message/Overview.php b/src/Message/Overview.php
index c328acc..0494c16 100644
--- a/src/Message/Overview.php
+++ b/src/Message/Overview.php
@@ -4,22 +4,66 @@ namespace App\Message;
use App\Message\Base;
+/**
+ * Overview message
+ *
+ * @author Phil Burton <phil@pgburton.com>
+ */
class Overview extends Base
{
- protected $message_name = 'opening';
+ /**
+ * Message name to load
+ *
+ * @var string
+ */
+ protected $message_name;
+
+ /**
+ * The list of vars we need to inject
+ *
+ * @var array
+ */
protected $injects = ['inject', 'second'];
+
+ /**
+ * The array of important vars
+ *
+ * @var string
+ */
protected $important = ['inject'];
+
+ /**
+ * Inject
+ * @var string
+ */
public $inject;
+
+ /**
+ * Second inject
+ *
+ * @var string
+ */
public $second = "second";
+ /**
+ * Construct to set any inject vars from functions if required
+ *
+ * @author Phil Burton <phil@pgburton.com>
+ */
public function __construct()
{
$this->inject = $this->getInject();
parent::__construct();
}
+ /**
+ * Get our inject value
+ *
+ * @author Phil Burton <phil@pgburton.com>
+ * @return string
+ */
public function getInject()
{
- return time();
+ return (string) time();
}
-} \ No newline at end of file
+}