Exemplo 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);
                    }
                }
            }
        }
Exemplo n.º 2
0
        internal void Parse()
        {
            if (String.IsNullOrEmpty(FileName))
            {
                throw new InvalidOperationException();
            }

            string extensionDir = String.Empty;

            using (StreamReader reader = new StreamReader(FileName))
            {
                foreach (object o in PHPIniFile.ParseIniFile(reader))
                {
                    PHPIniSetting directive = o as PHPIniSetting;
                    if (directive != null)
                    {
                        Settings.Add(directive);
                        RawEntries.Add(directive);

                        // Get the path to the extension directory - this will be used later
                        if (String.Equals(directive.Name, "extension_dir", StringComparison.OrdinalIgnoreCase))
                        {
                            extensionDir = directive.GetTrimmedValue();
                        }
                    }
                    else
                    {
                        PHPIniExtension extension = o as PHPIniExtension;
                        if (extension != null)
                        {
                            Extensions.Add(extension);
                            RawEntries.Add(extension);
                        }
                        else
                        {
                            PHPIniBase entry = o as PHPIniBase;
                            if (entry != null)
                            {
                                RawEntries.Add(entry);
                            }
                        }
                    }
                }
            }

            if (String.IsNullOrEmpty(extensionDir) ||
                !Path.IsPathRooted(extensionDir))
            {
                extensionDir = Path.Combine(Path.GetDirectoryName(FileName), "ext");
            }

            if (Directory.Exists(extensionDir))
            {
                AddAllAvailableExtensions(extensionDir);
            }
        }
Exemplo n.º 3
0
 internal bool Remove(PHPIniBase entry)
 {
     return(RawEntries.Remove(entry));
 }
Exemplo n.º 4
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);
                    }
                }
            }
        }
Exemplo n.º 5
0
 internal bool Remove(PHPIniBase entry)
 {
     return RawEntries.Remove(entry);
 }