PageRenderTime 21ms CodeModel.GetById 16ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/stable-1.1.1/Setup/PHPManagerSetupHelper/InstallUtil.cs

#
C# | 97 lines | 66 code | 13 blank | 18 comment | 9 complexity | 814a847f8f9e3db4dc3be0fd8168dab7 MD5 | raw file
 1//-----------------------------------------------------------------------
 2// <copyright>
 3// Copyright (C) Ruslan Yakushev for the PHP Manager for IIS project.
 4//
 5// This file is subject to the terms and conditions of the Microsoft Public License (MS-PL).
 6// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL for more details.
 7// </copyright>
 8//----------------------------------------------------------------------- 
 9
10using System;
11using Microsoft.Web.Administration;
12
13namespace Web.Management.PHP.Setup
14{
15
16    public static class InstallUtil
17    {
18
19        public static void AddUIModuleProvider(string name, string type)
20        {
21            using (ServerManager mgr = new ServerManager())
22            {
23
24                // First register the Module Provider  
25                Configuration adminConfig = mgr.GetAdministrationConfiguration();
26
27                ConfigurationSection moduleProvidersSection = adminConfig.GetSection("moduleProviders");
28                ConfigurationElementCollection moduleProviders = moduleProvidersSection.GetCollection();
29                if (FindByAttribute(moduleProviders, "name", name) == null)
30                {
31                    ConfigurationElement moduleProvider = moduleProviders.CreateElement();
32                    moduleProvider.SetAttributeValue("name", name);
33                    moduleProvider.SetAttributeValue("type", type);
34                    moduleProviders.Add(moduleProvider);
35                }
36
37                // Now register it so that all Sites have access to this module 
38                ConfigurationSection modulesSection = adminConfig.GetSection("modules");
39                ConfigurationElementCollection modules = modulesSection.GetCollection();
40                if (FindByAttribute(modules, "name", name) == null)
41                {
42                    ConfigurationElement module = modules.CreateElement();
43                    module.SetAttributeValue("name", name);
44                    modules.Add(module);
45                }
46
47                mgr.CommitChanges();
48            }
49        }
50
51        /// <summary> 
52        /// Helper method to find an element based on an attribute 
53        /// </summary> 
54        private static ConfigurationElement FindByAttribute(ConfigurationElementCollection collection, string attributeName, string value)
55        {
56            foreach (ConfigurationElement element in collection)
57            {
58                if (String.Equals((string)element.GetAttribute(attributeName).Value, value, StringComparison.OrdinalIgnoreCase))
59                {
60                    return element;
61                }
62            }
63
64            return null;
65        }
66
67        /// <summary> 
68        /// Removes the specified UI Module by name 
69        /// </summary> 
70        public static void RemoveUIModuleProvider(string name)
71        {
72            using (ServerManager mgr = new ServerManager())
73            {
74                // First remove it from the sites 
75                Configuration adminConfig = mgr.GetAdministrationConfiguration();
76                ConfigurationSection modulesSection = adminConfig.GetSection("modules");
77                ConfigurationElementCollection modules = modulesSection.GetCollection();
78                ConfigurationElement module = FindByAttribute(modules, "name", name);
79                if (module != null)
80                {
81                    modules.Remove(module);
82                }
83
84                // now remove the ModuleProvider 
85                ConfigurationSection moduleProvidersSection = adminConfig.GetSection("moduleProviders");
86                ConfigurationElementCollection moduleProviders = moduleProvidersSection.GetCollection();
87                ConfigurationElement moduleProvider = FindByAttribute(moduleProviders, "name", name);
88                if (moduleProvider != null)
89                {
90                    moduleProviders.Remove(moduleProvider);
91                }
92
93                mgr.CommitChanges();
94            }
95        }
96    }
97}