protected function writeConfig($clear = true)
    {
        $output = $this->dumpConfig();
        if ($output !== null
            && $output !== false
        ) {
            $localPath = $this->getLocalPath();

            if ($this->doNotWriteConfigInTests) {
                // simulate whether it would be successful
                $success = is_writable($localPath);
            } else {
                $success = @file_put_contents($localPath, $output, LOCK_EX);
            }

            if ($success === false) {
                throw $this->getConfigNotWritableException();
            }

            /**
             * Triggered when a INI config file is changed on disk.
             *
             * @param string $localPath Absolute path to the changed file on the server.
             */
            Piwik::postEvent('Core.configFileChanged', [$localPath]);
        }

        if ($clear) {
            $this->reload();
        }
    }