From edfd095021ee5d89f53da4fd78d7dea7346d7617 Mon Sep 17 00:00:00 2001 From: Fbenas Date: Sun, 12 Mar 2017 20:05:19 +0000 Subject: more magic in rendering mesages --- src/Config/Config.php | 2 +- src/Game.php | 6 ++-- src/Message/Base.php | 46 +++++++++++++++--------- src/Message/Handler.php | 93 +++++++++++++++++++++++++++++------------------- src/Message/Overview.php | 11 ++---- 5 files changed, 91 insertions(+), 67 deletions(-) (limited to 'src') 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 @@ -13,13 +14,6 @@ use App\Message\Handler; */ class Base { - /** - * Message name to load - * - * @var string - */ - protected $message_name; - /** * Cache of the loaded config array * @@ -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 + * @return string + */ + public function getName() + { + $split = explode("\\", get_class($this)); + return strtolower($split[count($split) - 1]); + } + /** * Construct, load config and message * * @author Phil Burton */ - public function __construct() + public function __construct(Handler $handler) { $this->loadConfig(); $this->loadMessage(); + + $this->setFromHandler($handler); } /** @@ -135,11 +143,15 @@ class Base * * @author Phil Burton */ - 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 @@ -18,13 +18,6 @@ class Handler */ protected $important = []; - /** - * The important set of values of injects for this handler - * - * @var mixed[] - */ - protected $importantValues = []; - /** * Construct our handler * @@ -32,18 +25,9 @@ class Handler * @param string[] $important */ public function __construct($important) - { - $this->setupImportant($important); - } - /** - * Set the important list - * - * @author Phil Burton - * @param string[] $important - */ - public function setupImportant($important = []) { $this->important = $important; + $this->initImportantValues(); } /** @@ -52,10 +36,10 @@ class Handler * @author Phil Burton * @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 + * @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 + * @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 * @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 * @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 @@ -11,13 +11,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 * @@ -50,10 +43,10 @@ class Overview extends Base * * @author Phil Burton */ - public function __construct() + public function __construct(Handler $handler) { $this->inject = $this->getInject(); - parent::__construct(); + parent::__construct($handler); } /** -- cgit v1.2.3