Browse Source

Add Pico::setConfig() method

Thanks @dav-m85
Daniel Rudolf 9 years ago
parent
commit
1419cf1
1 changed files with 27 additions and 1 deletions
  1. 27 1
      lib/Pico.php

+ 27 - 1
lib/Pico.php

@@ -246,6 +246,9 @@ class Pico
      */
      */
     public function run()
     public function run()
     {
     {
+        // lock config
+        $this->config = is_array($this->config) ? $this->config : array();
+
         // load plugins
         // load plugins
         $this->loadPlugins();
         $this->loadPlugins();
         $this->triggerEvent('onPluginsLoaded', array(&$this->plugins));
         $this->triggerEvent('onPluginsLoaded', array(&$this->plugins));
@@ -418,7 +421,7 @@ class Pico
 
 
         $configFile = $this->getConfigDir() . 'config.php';
         $configFile = $this->getConfigDir() . 'config.php';
         $config = file_exists($configFile) ? require($configFile) : null;
         $config = file_exists($configFile) ? require($configFile) : null;
-        $this->config = is_array($config) ? $config + $defaultConfig : $defaultConfig;
+        $this->config += is_array($config) ? $config + $defaultConfig : $defaultConfig;
 
 
         if (empty($this->config['base_url'])) {
         if (empty($this->config['base_url'])) {
             $this->config['base_url'] = $this->getBaseUrl();
             $this->config['base_url'] = $this->getBaseUrl();
@@ -437,6 +440,29 @@ class Pico
         }
         }
     }
     }
 
 
+    /**
+     * Sets Picos config before calling Pico::run()
+     *
+     * This method allows you to modify Picos config without creating a
+     * {@path "config/config.php"} or changing some of its variables before
+     * Pico starts processing. It can only be called between the constructor
+     * call and Pico::run(). Options set with this method cannot be overwritten
+     * by {@path "config/config.php"}.
+     *
+     * @param array $config     array with configuration variables, like
+     *     $config in {@path "config/config.php"}
+     * @return void
+     * @throws RuntimeException thrown if Pico already started processing
+     */
+    public function setConfig(array $config)
+    {
+        if ($this->config !== null) {
+            throw new RuntimeException('You cannot modify Picos config after processing has started');
+        }
+
+        $this->config = $config;
+    }
+
     /**
     /**
      * Returns either the value of the specified config variable or
      * Returns either the value of the specified config variable or
      * the config array
      * the config array