/**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     // Create a couple of websites
     $prod = Website::create(['name' => 'Main Production Website', 'http_host' => 'www.mysite.com', 'environment' => 'production']);
     $test = Website::create(['name' => 'Main Test Website', 'http_host' => 'test.mysite.com', 'environment' => 'test']);
     $dev = Website::create(['name' => 'Development Website', 'http_host' => 'localhost', 'environment' => 'test']);
     // Create some config entries
     ConfigModel::create(['website_id' => $prod->id, 'environment' => 'production', 'group' => 'config', 'key' => 'fruit', 'value' => 'apple-prod-www', 'type' => 'string']);
     ConfigModel::create(['website_id' => $test->id, 'environment' => 'test', 'group' => 'config', 'key' => 'fruit', 'value' => 'banana-test-test', 'type' => 'string']);
     ConfigModel::create(['group' => 'config', 'key' => 'fruit', 'value' => 'orange-default-default', 'type' => 'string']);
     ConfigModel::create(['environment' => 'test', 'group' => 'config', 'key' => 'fruit', 'value' => 'peach-test-default', 'type' => 'string']);
     ConfigModel::create(['environment' => 'production', 'group' => 'config', 'key' => 'fruit', 'value' => 'plum-production-default', 'type' => 'string']);
     ConfigModel::create(['group' => 'config', 'key' => 'animal', 'value' => 'elephant-default-default', 'type' => 'string']);
     ConfigModel::create(['group' => 'config', 'key' => 'bird', 'value' => 'canary-default-default', 'type' => 'string']);
     ConfigModel::create(['group' => 'config', 'key' => 'raptor', 'value' => 'falcon-default-default', 'type' => 'string']);
 }
 /**
  * Set a given configuration value and store it in the database.
  *
  * This stores a configuration value with a key => value pair into
  * the database against a specific environment and website_id (which
  * default to NULL).
  *
  * The key is represented as group.key for simple key=>value pairs within
  * group "group" or group.key1.key2.key3 ... for nested keys, stored as
  * arrays within the global configuration.
  *
  * @param  string      $key
  * @param  mixed       $value
  * @param  null|string $environment
  * @param  null|integer $website_id
  * @return void
  */
 public function set($key, $value, $environment = null, $website_id = null)
 {
     // Bootstrap the ConfigLoaderRepository class
     $siteConfigRepository = new ConfigLoaderRepository();
     // Parse the key here into group.key.part components.
     //
     // Any time a . is present in the key we are going to assume the first section
     // is the group.  If there is no group present then we assume that the group
     // is "config".
     $explodedOnGroup = explode('.', $key);
     if (count($explodedOnGroup) > 1) {
         $group = array_shift($explodedOnGroup);
         $item = implode('.', $explodedOnGroup);
     } else {
         $group = 'config';
         $item = $key;
     }
     // What type is the value we are setting?
     if (is_array($value)) {
         $type = 'array';
     } elseif (is_int($value)) {
         $type = 'integer';
     } else {
         $type = 'string';
     }
     // Now we have the group / item as separate values, we can store these
     ConfigModel::set($item, $value, $group, $environment, $website_id, $type);
     // Flush the cache
     $siteConfigRepository->forgetConfig();
     // Reload the config
     $siteConfigRepository->loadConfiguration();
     // Fetch the configuration that has already been loaded.
     $config = config();
     // Store it into the running config
     $siteConfigRepository->setRunningConfiguration($config);
 }
 /**
  * Load the database backed configuration.
  *
  * This function does the work of loading the entire site configuration
  * from the database and returns it as a group => [key=>value] set.  The
  * configuration is retrieved from cache if it already exists there, and
  * stored into the cache after it is loaded.
  *
  * @return array
  */
 public function loadConfiguration()
 {
     /** @var ConfigLoaderRepository $repository */
     $repository = $this->loadEnvironment();
     $cache = $repository->fetchConfig();
     if (!empty($cache)) {
         return $cache;
     }
     $config = [];
     // Fetch all groups, and fetch the config for each group based on the
     // environment and website ID of the current site.
     foreach ($repository->fetchAllGroups() as $group) {
         $groupConfig = ConfigModel::fetchSettings($repository->getEnvironment(), $repository->getWebsiteId(), $group);
         $config[$group] = $groupConfig;
     }
     $repository->storeConfig($config);
     return $config;
 }