PageRenderTime 12ms CodeModel.GetById 2ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Setup/PHPManagerSetupHelper/InstallUtil.cs

#
C# | 91 lines | 61 code | 12 blank | 18 comment | 8 complexity | 8470a15c6ecc99608de6c33486ea1241 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 System.Collections.Generic;
12using System.Linq;
13using Microsoft.Web.Administration;
14
15namespace Web.Management.PHP.Setup
16{
17
18    public static class InstallUtil
19    {
20
21        public static void AddUIModuleProvider(string name, string type)
22        {
23            using (var mgr = new ServerManager())
24            {
25
26                // First register the Module Provider  
27                var adminConfig = mgr.GetAdministrationConfiguration();
28
29                var moduleProvidersSection = adminConfig.GetSection("moduleProviders");
30                var moduleProviders = moduleProvidersSection.GetCollection();
31                if (FindByAttribute(moduleProviders, "name", name) == null)
32                {
33                    var moduleProvider = moduleProviders.CreateElement();
34                    moduleProvider.SetAttributeValue("name", name);
35                    moduleProvider.SetAttributeValue("type", type);
36                    moduleProviders.Add(moduleProvider);
37                }
38
39                // Now register it so that all Sites have access to this module 
40                var modulesSection = adminConfig.GetSection("modules");
41                var modules = modulesSection.GetCollection();
42                if (FindByAttribute(modules, "name", name) == null)
43                {
44                    var module = modules.CreateElement();
45                    module.SetAttributeValue("name", name);
46                    modules.Add(module);
47                }
48
49                mgr.CommitChanges();
50            }
51        }
52
53        /// <summary> 
54        /// Helper method to find an element based on an attribute 
55        /// </summary> 
56        private static ConfigurationElement FindByAttribute(IEnumerable<ConfigurationElement> collection, string attributeName, string value)
57        {
58            return collection.FirstOrDefault(element => String.Equals((string) element.GetAttribute(attributeName).Value, value, StringComparison.OrdinalIgnoreCase));
59        }
60
61        /// <summary> 
62        /// Removes the specified UI Module by name 
63        /// </summary> 
64        public static void RemoveUIModuleProvider(string name)
65        {
66            using (var mgr = new ServerManager())
67            {
68                // First remove it from the sites 
69                var adminConfig = mgr.GetAdministrationConfiguration();
70                var modulesSection = adminConfig.GetSection("modules");
71                var modules = modulesSection.GetCollection();
72                var module = FindByAttribute(modules, "name", name);
73                if (module != null)
74                {
75                    modules.Remove(module);
76                }
77
78                // now remove the ModuleProvider 
79                var moduleProvidersSection = adminConfig.GetSection("moduleProviders");
80                var moduleProviders = moduleProvidersSection.GetCollection();
81                var moduleProvider = FindByAttribute(moduleProviders, "name", name);
82                if (moduleProvider != null)
83                {
84                    moduleProviders.Remove(moduleProvider);
85                }
86
87                mgr.CommitChanges();
88            }
89        }
90    }
91}