summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--config/messages.php4
-rw-r--r--src/Config/Config.php2
-rw-r--r--src/Game.php6
-rw-r--r--src/Message/Base.php46
-rw-r--r--src/Message/Handler.php93
-rw-r--r--src/Message/Overview.php11
6 files changed, 93 insertions, 69 deletions
diff --git a/config/messages.php b/config/messages.php
index 0d53096..53f21db 100644
--- a/config/messages.php
+++ b/config/messages.php
@@ -1,5 +1,5 @@
<?php
return [
- 'opening' => 'This is a message, {{inject}} is injected. Also {{second}}'
-]; \ No newline at end of file
+ 'overview' => 'This is a message, {{inject}} is injected. Also {{second}}'
+];
diff --git a/src/Config/Config.php b/src/Config/Config.php
index 710c9e0..4423d30 100644
--- a/src/Config/Config.php
+++ b/src/Config/Config.php
@@ -27,7 +27,7 @@ class Config
if (!static::$config) {
static::$config = include SITE_ROOT . '/config/messages.php';
}
-
+
return static::$config;
}
}
diff --git a/src/Game.php b/src/Game.php
index 1bedba2..c50bd34 100644
--- a/src/Game.php
+++ b/src/Game.php
@@ -39,12 +39,10 @@ class Game
*/
public function sendOpeningMessage()
{
- $message = $this->handler->renderOpeningMessage();
- sleep(1);
- $message2 = $this->handler->renderOpeningMessage();
+ $message = $this->handler->renderOverview();
$important = $this->handler->returnImportant('inject');
- var_dump($message, $important, $message2);
+ var_dump($message, $important);
}
}
diff --git a/src/Message/Base.php b/src/Message/Base.php
index 2b7b0e4..b6c1c8e 100644
--- a/src/Message/Base.php
+++ b/src/Message/Base.php
@@ -5,6 +5,7 @@ namespace App\Message;
use Mustache_Engine;
use App\Config\Config;
use App\Message\Handler;
+use Exception;
/**
* Base class for a message
@@ -14,13 +15,6 @@ use App\Message\Handler;
class Base
{
/**
- * Message name to load
- *
- * @var string
- */
- protected $message_name;
-
- /**
* Cache of the loaded config array
*
* @var string
@@ -55,11 +49,11 @@ class Base
*/
protected function loadMessage()
{
- if (!array_key_exists($this->message_name, $this->config)) {
- throw new Exception('Could not load messge from config (' . $this->message_name . ')');
+ if (!array_key_exists($this->getName(), $this->config)) {
+ throw new Exception('Could not load messge from config (' . $this->getName() . ')');
}
- $this->message = $this->config[$this->message_name];
+ $this->message = $this->config[$this->getName()];
}
/**
@@ -67,25 +61,39 @@ class Base
*/
protected function loadConfig()
{
- $config =Config::getConfig();
+ $config = Config::getConfig();
if (!$config) {
- throw new \Exception('Could not load config');
+ throw new Exception('Could not load config');
}
$this->config = $config;
}
/**
+ * Get the name for this message, by default we use the class name
+ *
+ * @author Phil Burton <phil@pgburton.com>
+ * @return string
+ */
+ public function getName()
+ {
+ $split = explode("\\", get_class($this));
+ return strtolower($split[count($split) - 1]);
+ }
+
+ /**
* Construct, load config and message
*
* @author Phil Burton <phil@pgburton.com>
*/
- public function __construct()
+ public function __construct(Handler $handler)
{
$this->loadConfig();
$this->loadMessage();
+
+ $this->setFromHandler($handler);
}
/**
@@ -135,11 +143,15 @@ class Base
*
* @author Phil Burton <phil@pgburton.com>
*/
- public function setFromArray(array $array)
+ public function setFromHandler(Handler $handler)
{
- foreach ($array as $key => $value) {
- if (in_array($key, $this->injects)) {
- $this->$key = (string) $value;
+ foreach ($handler->returnImportants() as $name) {
+ if (in_array($name, $this->injects)) {
+ $value = $handler->$name;
+
+ if (isset($value) && $value) {
+ $this->$name = $value;
+ }
}
}
}
diff --git a/src/Message/Handler.php b/src/Message/Handler.php
index 825ceda..8f216b2 100644
--- a/src/Message/Handler.php
+++ b/src/Message/Handler.php
@@ -2,7 +2,7 @@
namespace App\Message;
-use App\Message\Overview;
+use Exception;
/**
* Handles messages and context
@@ -19,13 +19,6 @@ class Handler
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>
@@ -33,17 +26,8 @@ class Handler
*/
public function __construct($important)
{
- $this->setupImportant($important);
- }
- /**
- * Set the important list
- *
- * @author Phil Burton <phil@pgburton.com>
- * @param string[] $important
- */
- public function setupImportant($important = [])
- {
$this->important = $important;
+ $this->initImportantValues();
}
/**
@@ -52,10 +36,10 @@ class Handler
* @author Phil Burton <phil@pgburton.com>
* @param mixed[] $array
*/
- protected function setImportantValues($array)
+ protected function initImportantValues()
{
- foreach ($array as $name => $value) {
- $this->addImportantValue($name, $value);
+ foreach ($this->important as $name) {
+ $this->addImportantValue($name, false);
}
}
@@ -69,7 +53,7 @@ class Handler
protected function addImportantValue($name, $value)
{
if (in_array($name, $this->important)) {
- $this->importantValues[$name] = $value;
+ $this->$name = $value;
}
return $name;
@@ -84,16 +68,52 @@ class Handler
*/
public function returnImportant($name)
{
- if (!array_key_exists($name, $this->importantValues)) {
- throw new \Exception('The var ' . $name . ' is not set in this context');
+ if (!isset($this->$name)) {
+ throw new Exception('The var ' . $name . ' is not set in this context');
}
- return $this->importantValues[$name];
+ return $this->$name;
+ }
+
+ protected function addImportantFromArray($array)
+ {
+ foreach ($array as $key => $value) {
+ if (!isset($this->$key)) {
+ throw new Exception('The var ' . $value . ' is not set in this context');
+ }
+
+ $this->$key = $value;
+ }
+ }
+
+ /**
+ * Return all importnat vars and values in array
+ *
+ * @author Phil Burton <phil@pgburton.com>
+ * @return string[]
+ */
+ public function returnImportants()
+ {
+ return $this->important;
+ }
+
+ /**
+ * Magic __get override to be able to get important values using ->myValue
+ * which then runs $this->returnImportant('myValue')
+ *
+ * @author Phil Burton <phil@pgburton.com>
+ * @param string $name
+ * @param mixed[] $arguments
+ * @return string
+ */
+ public function __get($name)
+ {
+ return $this->returnImporant(substr($name, 3));
}
/**
- * Magic __call override to be able to get improtant values using getMyImportant()
- * which then runs $this->returnImportant('myImportant')
+ * Allow for magic rendering
+ * Call renderFooBar to render the FooBar message class
*
* @author Phil Burton <phil@pgburton.com>
* @param string $name
@@ -102,8 +122,8 @@ class Handler
*/
public function __call($name, $arguments)
{
- if (strpos($name, 'get') === 0) {
- $this->returnImporant(substr($name, 3));
+ if (strpos($name, 'render') === 0) {
+ return $this->doRender(substr($name, 6));
}
}
@@ -113,16 +133,17 @@ class Handler
* @author Phil Burton <phil@pgburton.com>
* @return string
*/
- public function renderOpeningMessage()
+ public function doRender($name)
{
- $overview = new Overview();
-
- $overview->setFromArray($this->importantValues);
-
- $message = $overview->renderMessage();
+ $name = "App\\Message\\" . $name;
+ if (!class_exists($name)) {
+ throw new Exception('Could not find message class `' . $name . '`');
+ }
- $this->setImportantValues($overview->getImportantValues());
+ $messageObj = new $name($this);
+ $message = $messageObj->renderMessage();
+ $this->addImportantFromArray($messageObj->getImportantValues());
return $message;
}
}
diff --git a/src/Message/Overview.php b/src/Message/Overview.php
index 0494c16..b26dfe4 100644
--- a/src/Message/Overview.php
+++ b/src/Message/Overview.php
@@ -12,13 +12,6 @@ use App\Message\Base;
class Overview extends Base
{
/**
- * Message name to load
- *
- * @var string
- */
- protected $message_name;
-
- /**
* The list of vars we need to inject
*
* @var array
@@ -50,10 +43,10 @@ class Overview extends Base
*
* @author Phil Burton <phil@pgburton.com>
*/
- public function __construct()
+ public function __construct(Handler $handler)
{
$this->inject = $this->getInject();
- parent::__construct();
+ parent::__construct($handler);
}
/**