PageRenderTime 23ms CodeModel.GetById 17ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 1ms

/trunk/Powershell/SetPHPExtensionCmdlet.cs

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