PageRenderTime 25ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Setup/PHPManagerSetupHelper/InstallUtil.cs

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