Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

introducing route-specific configurations - woo! more to come...

  • Loading branch information...
commit d85d324d7f578137c347ca1a0cbd16a6438f10d9 1 parent 74e372c
Chris Cornutt authored
Showing with 62 additions and 3 deletions.
  1. +49 −2 Shield/Config.php
  2. +13 −1 Shield/Shield.php
51 Shield/Config.php
View
@@ -87,9 +87,13 @@ public function getConfigFile()
*
* @return array Config container (options)
*/
- public function getConfig()
+ public function getConfig($context=null)
{
- return $this->config;
+ if ($context !== null && isset($this->config[$context])) {
+ return $this->config[$context];
+ } else {
+ return $this->config;
+ }
}
/**
@@ -131,4 +135,47 @@ public function set($name,$value,$context='general')
$this->config[$context][$name] = $value;
return $this;
}
+
+ /**
+ * Use the given config and make updates to the context's settings
+ *
+ * @param array $config Array of configuration settings
+ * @param string $context Context for the config options
+ *
+ * @return null
+ */
+ public function update($config,$context='general')
+ {
+ foreach ($config as $option => $value) {
+ if (strstr($option, '.') !== false) {
+ $opt = explode('.',$option);
+
+ } else {
+ $opt = array($option);
+ }
+ $this->recurseConfig($opt,$value,$this->config[$context]);
+ }
+ }
+
+ /**
+ * Recurse through the configuration to apply the new values
+ *
+ * @param array $opt New config option path
+ * @param mixed $value New value to set
+ * @param array $config Current configuration options
+ *
+ * @return null
+ */
+ private function recurseConfig($opt,$value,&$config)
+ {
+ $first = array_shift($opt);
+ if (isset($config[$first])) {
+ if (count($opt) == 0) {
+ $config[$first] = $value;
+ } else {
+ $this->recurseConfig($opt,$value,$config[$first]);
+ }
+ }
+
+ }
}
14 Shield/Shield.php
View
@@ -131,6 +131,11 @@ public function __call($func, $args)
$func = strtolower($func);
$path = strtolower($args[0]);
+ if (isset($args[2])) {
+ // we've been given a route-specific config, set it up!
+ $this->di->get('Config')->setConfig($args[2],'route::'.$path);
+ }
+
if (isset($args[1])) {
$this->routes[$func][$path] = $args[1];
$this->di->Log->log('SETTING PATH ['.strtoupper($func).']: '.$path);
@@ -185,6 +190,7 @@ public function run()
$method = strtolower($requestMethod);
if (isset($this->routes[$method][$uri])) {
+
$this->routeMatch($method, $uri, $uri);
} else {
@@ -220,12 +226,18 @@ public function run()
*/
private function routeMatch($method,$uri,$matches=null)
{
+ // given our URI, see if we have a match in our Config & update!
+ $config = $this->di->get('Config')->getConfig('route::'.$uri);
+ if ($config !== null) {
+ $this->di->get('Config')->update($config);
+ }
// route match!
$this->di->get('Log')->log('ROUTE MATCH ['.strtoupper($method).']: '.$uri);
$routeClosure = $this->routes[$method][$uri]($matches);
- echo $this->view->render($routeClosure);
+ $content = $this->view->render($routeClosure);
+ echo $content;
}
/**
Please sign in to comment.
Something went wrong with that request. Please try again.