PageRenderTime 36ms CodeModel.GetById 26ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 1ms

/trunk/Powershell/NewPHPSettingCmdlet.cs

#
C# | 72 lines | 57 code | 7 blank | 8 comment | 3 complexity | d8d0e13446332d0c6f28c5d9db26e415 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    [Cmdlet(VerbsCommon.New, "PHPSetting",
18            SupportsShouldProcess = true,
19            ConfirmImpact = ConfirmImpact.Medium)]
20    public sealed class NewPHPSettingCmdlet : BaseCmdlet
21    {
22        private string _section = "PHP";
23
24        [Parameter(Mandatory = true,
25            Position = 0)]
26        public string Name { get; set; }
27
28        [Parameter(Mandatory = true, Position = 1)]
29        public string Value { get; set; }
30
31        [Parameter(Mandatory = false, Position = 2)]
32        public string Section
33        {
34            get
35            {
36                return _section;
37            }
38            set
39            {
40                _section = value;
41            }
42        }
43
44        protected override void DoProcessing()
45        {
46            using (var serverManager = new ServerManager())
47            {
48                var serverManagerWrapper = new ServerManagerWrapper(serverManager, SiteName, VirtualPath);
49                var configHelper = new PHPConfigHelper(serverManagerWrapper);
50                var phpIniFile = configHelper.GetPHPIniFile();
51
52                var setting = Helper.FindSetting(phpIniFile.Settings, Name);
53                if (setting == null)
54                {
55                    if (ShouldProcess(Name))
56                    {
57                        var settings = new RemoteObjectCollection<PHPIniSetting>
58                            {
59                                new PHPIniSetting(Name, Value, Section)
60                            };
61                        configHelper.AddOrUpdatePHPIniSettings(settings);
62                    }
63                }
64                else
65                {
66                    var ex = new ArgumentException(String.Format(Resources.SettingAlreadyExistsError, Name));
67                    ReportNonTerminatingError(ex, "InvalidArgument", ErrorCategory.InvalidArgument);
68                }
69            }
70        }
71    }
72}