Gdn_ConfigurationSource::fromFile PHP Method

fromFile() public static method

Load config data from a file.
public static fromFile ( Gdn_Configuration $Parent, string $File, string $Name = 'Configuration' ) : Gdn_ConfigurationSource
$Parent Gdn_Configuration Parent config object
$File string Path to config file to load
$Name string Optional setting name
return Gdn_ConfigurationSource
    public static function fromFile($Parent, $File, $Name = 'Configuration')
    {
        $LoadedFromCache = false;
        $UseCache = false;
        if ($Parent && $Parent->caching()) {
            $FileKey = sprintf(Gdn_Configuration::CONFIG_FILE_CACHE_KEY, $File);
            if (Gdn::cache()->type() == Gdn_Cache::CACHE_TYPE_MEMORY && Gdn::cache()->activeEnabled()) {
                $UseCache = true;
                $CachedConfigData = Gdn::cache()->get($FileKey, array(Gdn_Cache::FEATURE_NOPREFIX => true));
                $LoadedFromCache = $CachedConfigData !== Gdn_Cache::CACHEOP_FAILURE;
            }
        }
        // Define the variable properly.
        ${$Name} = null;
        // If we're not loading config from cache, directly include the conf file
        if ($LoadedFromCache) {
            ${$Name} = $CachedConfigData;
        }
        if ((is_null(${$Name}) || !is_array(${$Name})) && file_exists($File)) {
            $LoadedFromCache = false;
            // Include the file.
            require $File;
        }
        // Make sure the config variable is here and is an array.
        if (!is_array(${$Name})) {
            ${$Name} = array();
        }
        // We're caching, using the cache, and this data was not loaded from cache.
        // Write it there now.
        if ($Parent && $Parent->caching() && $UseCache && !$LoadedFromCache) {
            Gdn::cache()->store($FileKey, ${$Name}, array(Gdn_Cache::FEATURE_NOPREFIX => true, Gdn_Cache::FEATURE_EXPIRY => 3600));
        }
        return new Gdn_ConfigurationSource($Parent, 'file', $File, $Name, ${$Name});
    }

Usage Example

Example #1
0
 /**
  * Loads an array of settings from a file into the object with the specified name;
  *
  * @param string $File A string containing the path to a file that contains the settings array.
  * @param string $Name The name of the variable and initial group settings.
  *   Note: When $Name is 'Configuration' then the data will be set to the root of the config.
  * @param boolean $Dynamic Optional, whether to treat this as the request's "dynamic" config, and
  *   to save config changes here. These settings will also be re-applied later when "OverlayDynamic"
  *   is called after all defaults are loaded.
  * @return boolean
  */
 public function load($File, $Name = 'Configuration', $Dynamic = false)
 {
     $ConfigurationSource = Gdn_ConfigurationSource::fromFile($this, $File, $Name);
     if (!$ConfigurationSource) {
         return false;
     }
     $UseSplitting = $this->splitting;
     $ConfigurationSource->splitting($UseSplitting);
     if (!$ConfigurationSource) {
         return false;
     }
     $SourceTag = "file:{$File}";
     $this->sources[$SourceTag] = $ConfigurationSource;
     if ($Dynamic) {
         $this->dynamic = $ConfigurationSource;
     }
     if (!$UseSplitting) {
         $this->massImport($ConfigurationSource->export());
     } else {
         $Loaded = $ConfigurationSource->export();
         self::mergeConfig($this->Data, $Loaded);
     }
 }