PageRenderTime 9ms CodeModel.GetById 1ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/stable-1.2.0/Powershell/RemovePHPSettingCmdlet.cs

#
C# | 79 lines | 64 code | 7 blank | 8 comment | 5 complexity | 9637b94b07c10af356475c565f51a76f 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.Management.Automation;
12using Microsoft.Web.Administration;
13using Web.Management.PHP.Config;
14
15namespace Web.Management.PHP.Powershell
16{
17
18    [Cmdlet(VerbsCommon.Remove, "PHPSetting",
19            SupportsShouldProcess = true,
20            ConfirmImpact = ConfirmImpact.Medium)]
21    public sealed class RemovePHPSettingCmdlet : BaseCmdlet
22    {
23        private string _name;
24        private bool _force;
25
26        [Parameter(Mandatory = false)]
27        public SwitchParameter Force
28        {
29            get {
30                 return _force;
31            }
32            set {
33                 _force = value;
34            }
35        }
36
37        [Parameter(Mandatory = true,
38           Position = 0)]
39        public string Name
40        {
41            get
42            {
43                return _name;
44            }
45            set
46            {
47                _name = value;
48            }
49        }
50
51        protected override void DoProcessing()
52        {
53            using (ServerManager serverManager = new ServerManager())
54            {
55                ServerManagerWrapper serverManagerWrapper = new ServerManagerWrapper(serverManager, this.SiteName, this.VirtualPath);
56                PHPConfigHelper configHelper = new PHPConfigHelper(serverManagerWrapper);
57                PHPIniFile phpIniFile = configHelper.GetPHPIniFile();
58
59                PHPIniSetting setting = Helper.FindSetting(phpIniFile.Settings, Name);
60                if (setting != null)
61                {
62                    if (ShouldProcess(Name))
63                    {
64                        string warningMessage = String.Format(Resources.DeleteSettingWarningMessage, setting.Name, setting.Value);
65                        if (Force || ShouldContinue(warningMessage, Resources.DeleteSettingWarningCaption))
66                        {
67                            configHelper.RemovePHPIniSetting(setting);
68                        }
69                    }
70                }
71                else
72                {
73                    ArgumentException ex = new ArgumentException(String.Format(Resources.SettingDoesNotExistError, Name));
74                    ReportNonTerminatingError(ex, "InvalidArgument", ErrorCategory.ObjectNotFound);
75                }
76            }
77        }
78    }
79}