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