PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Powershell/SetPHPExtensionCmdlet.cs

#
C# | 126 lines | 104 code | 14 blank | 8 comment | 17 complexity | f5fb5be1349f11dac1855a38c29e0068 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.Diagnostics;
  11. using System.IO;
  12. using System.Management.Automation;
  13. using Microsoft.Web.Administration;
  14. using Web.Management.PHP.Config;
  15. namespace Web.Management.PHP.Powershell
  16. {
  17. [Cmdlet(VerbsCommon.Set, "PHPExtension",
  18. SupportsShouldProcess = true,
  19. ConfirmImpact = ConfirmImpact.Medium)]
  20. public sealed class SetPHPExtensionCmdlet : BaseCmdlet
  21. {
  22. private PHPExtensionStatus _status = PHPExtensionStatus.Any;
  23. private ServerManager _serverManager;
  24. private PHPConfigHelper _configHelper;
  25. private PHPIniFile _phpIniFile;
  26. private RemoteObjectCollection<PHPIniExtension> _extensions;
  27. [Parameter( Mandatory = true,
  28. ValueFromPipelineByPropertyName = true,
  29. Position = 0)]
  30. public string[] Name { get; set; }
  31. [Parameter(Mandatory = true, Position = 1)]
  32. public PHPExtensionStatus Status
  33. {
  34. get
  35. {
  36. return _status;
  37. }
  38. set
  39. {
  40. _status = value;
  41. }
  42. }
  43. protected override void BeginProcessing()
  44. {
  45. EnsureAdminUser();
  46. try
  47. {
  48. _serverManager = new ServerManager();
  49. var serverManagerWrapper = new ServerManagerWrapper(_serverManager, SiteName, VirtualPath);
  50. _configHelper = new PHPConfigHelper(serverManagerWrapper);
  51. _phpIniFile = _configHelper.GetPHPIniFile();
  52. _extensions = new RemoteObjectCollection<PHPIniExtension>();
  53. }
  54. catch (FileNotFoundException ex)
  55. {
  56. DisposeServerManager();
  57. ReportTerminatingError(ex, "FileNotFound", ErrorCategory.ObjectNotFound);
  58. }
  59. catch (InvalidOperationException ex)
  60. {
  61. DisposeServerManager();
  62. ReportTerminatingError(ex, "InvalidOperation", ErrorCategory.InvalidOperation);
  63. }
  64. }
  65. private void DisposeServerManager()
  66. {
  67. if (_serverManager != null)
  68. {
  69. _serverManager.Dispose();
  70. _serverManager = null;
  71. }
  72. }
  73. protected override void DoProcessing()
  74. {
  75. Debug.Assert(_phpIniFile != null);
  76. Debug.Assert(_extensions != null);
  77. foreach (var extensionName in Name)
  78. {
  79. var extension = Helper.FindExtension(_phpIniFile.Extensions, extensionName);
  80. if (extension != null)
  81. {
  82. if ((extension.Enabled && Status == PHPExtensionStatus.Disabled) ||
  83. (!extension.Enabled && Status == PHPExtensionStatus.Enabled))
  84. {
  85. if (ShouldProcess(extensionName))
  86. {
  87. extension = new PHPIniExtension(extensionName, (Status == PHPExtensionStatus.Enabled));
  88. _extensions.Add(extension);
  89. }
  90. }
  91. }
  92. else
  93. {
  94. var ex = new ArgumentException(String.Format(Resources.ExtensionDoesNotExistError, extensionName));
  95. ReportNonTerminatingError(ex, "InvalidArgument", ErrorCategory.ObjectNotFound);
  96. return;
  97. }
  98. }
  99. }
  100. protected override void EndProcessing()
  101. {
  102. Debug.Assert(_extensions != null);
  103. Debug.Assert(_configHelper != null);
  104. Debug.Assert(_serverManager != null);
  105. if (_extensions.Count > 0)
  106. {
  107. _configHelper.UpdateExtensions(_extensions);
  108. }
  109. DisposeServerManager();
  110. }
  111. }
  112. }