Esempio n. 1
0
        internal void UpdateExtensions(IEnumerable <PHPIniExtension> extensions)
        {
            foreach (PHPIniExtension extension in extensions)
            {
                int foundIndex = -1;

                for (int i = 0; i < RawEntries.Count; i++)
                {
                    PHPIniBase b = RawEntries[i];

                    PHPIniExtension existing = b as PHPIniExtension;
                    if (existing != null)
                    {
                        if (String.Equals(existing.Name, extension.Name, StringComparison.OrdinalIgnoreCase))
                        {
                            foundIndex = i;
                            break;
                        }
                    }
                }

                // If extension is found...
                if (foundIndex >= 0)
                {
                    // ... and is disabled then...
                    if (!extension.Enabled)
                    {
                        PHPIniBase extensionLine = RawEntries[foundIndex];
                        // ... remove the extension section name if it exists
                        if (foundIndex > 0 &&
                            String.Equals(RawEntries[foundIndex - 1].GetText(), GetExtensionSection(extension.Name), StringComparison.OrdinalIgnoreCase))
                        {
                            RawEntries.Remove(RawEntries[foundIndex - 1]);
                        }

                        // remove the exension
                        RawEntries.Remove(extensionLine);
                    }
                }
                else
                {
                    // Extension is not found
                    if (extension.Enabled)
                    {
                        extension.UpdateText();

                        // Add it at the end of the file along with the extension section name
                        int lastIndex = RawEntries.Count - 1;
                        lastIndex++;
                        RawEntries.Insert(lastIndex, new PHPIniString(GetExtensionSection(extension.Name)));
                        lastIndex++;
                        RawEntries.Insert(lastIndex, extension);
                    }
                }
            }
        }
Esempio n. 2
0
        internal void AddOrUpdateSettings(IEnumerable <PHPIniSetting> settings)
        {
            foreach (PHPIniSetting setting in settings)
            {
                bool settingFound = false;
                int  index        = -1;
                int  lastIndex    = -1;

                for (int i = 0; i < RawEntries.Count; i++)
                {
                    PHPIniBase b = RawEntries[i];

                    PHPIniSetting existing = b as PHPIniSetting;
                    if (existing != null)
                    {
                        lastIndex = i;

                        if (String.Equals(existing.Name, setting.Name, StringComparison.OrdinalIgnoreCase))
                        {
                            existing.Value = setting.Value;
                            existing.UpdateText();
                            settingFound = true;

                            break;
                        }

                        // This finds the index after the last setting for a given section
                        if (String.Equals(existing.Section, setting.Section, StringComparison.OrdinalIgnoreCase))
                        {
                            index = i;
                        }
                    }
                    else
                    {
                        // This finds the index after section declaration,
                        // in case there are no settings defined in that section
                        PHPIniSection section = b as PHPIniSection;
                        if ((section != null) && (String.Equals(section.Name, setting.Section, StringComparison.OrdinalIgnoreCase)))
                        {
                            index = i;
                        }
                    }
                }

                if (!settingFound)
                {
                    setting.UpdateText();

                    if (index == -1)
                    {
                        lastIndex++;
                        RawEntries.Insert(lastIndex, new PHPIniString(""));
                        lastIndex++;
                        RawEntries.Insert(lastIndex, new PHPIniString('[' + setting.Section + ']'));
                        lastIndex++;
                        RawEntries.Insert(lastIndex, setting);
                    }
                    else
                    {
                        RawEntries.Insert(index + 1, setting);
                    }
                }
            }
        }