PageRenderTime 13ms CodeModel.GetById 1ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
C# | 92 lines | 77 code | 7 blank | 8 comment | 3 complexity | 9cd4ad69d9648afdbaa9fa4bffeb4917 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 _name;
23        private string _value;
24        private string _section = "PHP";
25
26        [Parameter(Mandatory = true,
27                   Position = 0)]
28        public string Name
29        {
30            get
31            {
32                return _name;
33            }
34            set
35            {
36                _name = value;
37            }
38        }
39
40        [Parameter(Mandatory = true, Position = 1)]
41        public string Value
42        {
43            get
44            {
45                return _value;
46            }
47            set
48            {
49                _value = value;
50            }
51        }
52
53        [Parameter(Mandatory = false, Position = 2)]
54        public string Section
55        {
56            get
57            {
58                return _section;
59            }
60            set
61            {
62                _section = value;
63            }
64        }
65
66        protected override void DoProcessing()
67        {
68            using (ServerManager serverManager = new ServerManager())
69            {
70                ServerManagerWrapper serverManagerWrapper = new ServerManagerWrapper(serverManager, this.SiteName, this.VirtualPath);
71                PHPConfigHelper configHelper = new PHPConfigHelper(serverManagerWrapper);
72                PHPIniFile phpIniFile = configHelper.GetPHPIniFile();
73
74                PHPIniSetting setting = Helper.FindSetting(phpIniFile.Settings, Name);
75                if (setting == null)
76                {
77                    if (ShouldProcess(Name))
78                    {
79                        RemoteObjectCollection<PHPIniSetting> settings = new RemoteObjectCollection<PHPIniSetting>();
80                        settings.Add(new PHPIniSetting(Name, Value, Section));
81                        configHelper.AddOrUpdatePHPIniSettings(settings);
82                    }
83                }
84                else
85                {
86                    ArgumentException ex = new ArgumentException(String.Format(Resources.SettingAlreadyExistsError, Name));
87                    ReportNonTerminatingError(ex, "InvalidArgument", ErrorCategory.InvalidArgument);
88                }
89            }
90        }
91    }
92}