This repository was archived by the owner on Mar 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathSetPHPExtensionCmdlet.cs
More file actions
126 lines (112 loc) · 4.2 KB
/
SetPHPExtensionCmdlet.cs
File metadata and controls
126 lines (112 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//-----------------------------------------------------------------------
// <copyright>
// Copyright (C) Ruslan Yakushev for the PHP Manager for IIS project.
//
// This file is subject to the terms and conditions of the Microsoft Public License (MS-PL).
// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL for more details.
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Diagnostics;
using System.IO;
using System.Management.Automation;
using Microsoft.Web.Administration;
using Web.Management.PHP.Config;
namespace Web.Management.PHP.Powershell
{
[Cmdlet(VerbsCommon.Set, "PHPExtension",
SupportsShouldProcess = true,
ConfirmImpact = ConfirmImpact.Medium)]
public sealed class SetPHPExtensionCmdlet : BaseCmdlet
{
private PHPExtensionStatus _status = PHPExtensionStatus.Any;
private ServerManager _serverManager;
private PHPConfigHelper _configHelper;
private PHPIniFile _phpIniFile;
private RemoteObjectCollection<PHPIniExtension> _extensions;
[Parameter( Mandatory = true,
ValueFromPipelineByPropertyName = true,
Position = 0)]
public string[] Name { get; set; }
[Parameter(Mandatory = true, Position = 1)]
public PHPExtensionStatus Status
{
get
{
return _status;
}
set
{
_status = value;
}
}
protected override void BeginProcessing()
{
EnsureAdminUser();
try
{
_serverManager = new ServerManager();
var serverManagerWrapper = new ServerManagerWrapper(_serverManager, SiteName, VirtualPath);
_configHelper = new PHPConfigHelper(serverManagerWrapper);
_phpIniFile = _configHelper.GetPHPIniFile();
_extensions = new RemoteObjectCollection<PHPIniExtension>();
}
catch (FileNotFoundException ex)
{
DisposeServerManager();
ReportTerminatingError(ex, "FileNotFound", ErrorCategory.ObjectNotFound);
}
catch (InvalidOperationException ex)
{
DisposeServerManager();
ReportTerminatingError(ex, "InvalidOperation", ErrorCategory.InvalidOperation);
}
}
private void DisposeServerManager()
{
if (_serverManager != null)
{
_serverManager.Dispose();
_serverManager = null;
}
}
protected override void DoProcessing()
{
Debug.Assert(_phpIniFile != null);
Debug.Assert(_extensions != null);
foreach (var extensionName in Name)
{
var extension = Helper.FindExtension(_phpIniFile.Extensions, extensionName);
if (extension != null)
{
if ((extension.Enabled && Status == PHPExtensionStatus.Disabled) ||
(!extension.Enabled && Status == PHPExtensionStatus.Enabled))
{
if (ShouldProcess(extensionName))
{
extension = new PHPIniExtension(extensionName, (Status == PHPExtensionStatus.Enabled));
_extensions.Add(extension);
}
}
}
else
{
var ex = new ArgumentException(String.Format(Resources.ExtensionDoesNotExistError, extensionName));
ReportNonTerminatingError(ex, "InvalidArgument", ErrorCategory.ObjectNotFound);
return;
}
}
}
protected override void EndProcessing()
{
Debug.Assert(_extensions != null);
Debug.Assert(_configHelper != null);
Debug.Assert(_serverManager != null);
if (_extensions.Count > 0)
{
_configHelper.UpdateExtensions(_extensions);
}
DisposeServerManager();
}
}
}