Gdn_ConfigurationSource::save PHP Method

save() public method

public save ( ) : boolean | null
return boolean | null
    public function save()
    {
        if (!$this->Dirty) {
            return null;
        }
        $this->EventArguments['ConfigDirty'] =& $this->Dirty;
        $this->EventArguments['ConfigNoSave'] = false;
        $this->EventArguments['ConfigType'] = $this->Type;
        $this->EventArguments['ConfigSource'] = $this->Source;
        $this->EventArguments['ConfigData'] = $this->Settings;
        $this->fireEvent('BeforeSave');
        if ($this->EventArguments['ConfigNoSave']) {
            $this->Dirty = false;
            return true;
        }
        // Check for and fire callback if one exists
        if ($this->Callback && is_callable($this->Callback)) {
            $CallbackOptions = array();
            if (!is_array($this->CallbackOptions)) {
                $this->CallbackOptions = array();
            }
            $CallbackOptions = array_merge($CallbackOptions, $this->CallbackOptions, array('ConfigDirty' => $this->Dirty, 'ConfigType' => $this->Type, 'ConfigSource' => $this->Source, 'ConfigData' => $this->Settings, 'SourceObject' => $this));
            $ConfigSaved = call_user_func($this->Callback, $CallbackOptions);
            if ($ConfigSaved) {
                $this->Dirty = false;
                return true;
            }
        }
        switch ($this->Type) {
            case 'file':
                if (empty($this->Source)) {
                    trigger_error(errorMessage('You must specify a file path to be saved.', 'Configuration', 'Save'), E_USER_ERROR);
                }
                $CheckWrite = $this->Source;
                if (!file_exists($CheckWrite)) {
                    $CheckWrite = dirname($CheckWrite);
                }
                if (!is_writable($CheckWrite)) {
                    throw new Exception(sprintf(t("Unable to write to config file '%s' when saving."), $this->Source));
                }
                $Group = $this->Group;
                $Data =& $this->Settings;
                ksort($Data, $this->Configuration->getSortFlag());
                // Check for the case when the configuration is the group.
                if (is_array($Data) && count($Data) == 1 && array_key_exists($Group, $Data)) {
                    $Data = $Data[$Group];
                }
                // Do a sanity check on the config save.
                if ($this->Source == Gdn::config()->defaultPath()) {
                    // Log root config changes
                    try {
                        $LogData = $this->Initial;
                        $LogData['_New'] = $this->Settings;
                        LogModel::insert('Edit', 'Configuration', $LogData);
                    } catch (Exception $Ex) {
                    }
                    if (!isset($Data['Database'])) {
                        if ($Pm = Gdn::pluginManager()) {
                            $Pm->EventArguments['Data'] = $Data;
                            $Pm->EventArguments['Backtrace'] = debug_backtrace();
                            $Pm->fireEvent('ConfigError');
                        }
                        return false;
                    }
                }
                // Write config data to string format, ready for saving
                $FileContents = Gdn_Configuration::format($Data, array('VariableName' => $Group, 'WrapPHP' => true, 'ByLine' => true));
                if ($FileContents === false) {
                    trigger_error(errorMessage('Failed to define configuration file contents.', 'Configuration', 'Save'), E_USER_ERROR);
                }
                // Save to cache if we're into that sort of thing
                $FileKey = sprintf(Gdn_Configuration::CONFIG_FILE_CACHE_KEY, $this->Source);
                if ($this->Configuration && $this->Configuration->caching() && Gdn::cache()->type() == Gdn_Cache::CACHE_TYPE_MEMORY && Gdn::cache()->activeEnabled()) {
                    $CachedConfigData = Gdn::cache()->store($FileKey, $Data, array(Gdn_Cache::FEATURE_NOPREFIX => true, Gdn_Cache::FEATURE_EXPIRY => 3600));
                }
                $TmpFile = tempnam(PATH_CONF, 'config');
                $Result = false;
                if (file_put_contents($TmpFile, $FileContents) !== false) {
                    chmod($TmpFile, 0775);
                    $Result = rename($TmpFile, $this->Source);
                }
                if ($Result) {
                    if (function_exists('apc_delete_file')) {
                        // This fixes a bug with some configurations of apc.
                        @apc_delete_file($this->Source);
                    } elseif (function_exists('opcache_invalidate')) {
                        @opcache_invalidate($this->Source);
                    }
                }
                $this->Dirty = false;
                return $Result;
                break;
            case 'json':
            case 'array':
            case 'string':
                /**
                 * How would these even save? String config data must be handled by
                 * an event hook or callback, if at all.
                 */
                $this->Dirty = false;
                return false;
                break;
        }
    }