2010-04-24 06:10:47 +08:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
2011-03-22 12:41:14 +08:00
using Orchard.Caching ;
2010-04-24 06:10:47 +08:00
using Orchard.Environment.Extensions ;
2010-04-28 02:24:06 +08:00
using Orchard.Environment.Extensions.Models ;
2010-06-12 08:28:46 +08:00
using Orchard.Environment.Descriptor ;
2011-03-15 02:14:35 +08:00
using Orchard.Environment.Features ;
2011-02-18 01:17:49 +08:00
using Orchard.FileSystems.VirtualPath ;
2010-05-19 01:04:14 +08:00
using Orchard.Localization ;
2011-02-22 04:03:06 +08:00
using Orchard.Modules.Models ;
2010-05-19 01:04:14 +08:00
using Orchard.UI.Notify ;
2010-04-24 06:10:47 +08:00
namespace Orchard.Modules.Services {
public class ModuleService : IModuleService {
2011-03-15 02:14:35 +08:00
private readonly IFeatureManager _featureManager ;
2011-02-18 01:17:49 +08:00
private readonly IVirtualPathProvider _virtualPathProvider ;
2010-04-24 06:10:47 +08:00
private readonly IExtensionManager _extensionManager ;
2010-04-29 02:47:29 +08:00
private readonly IShellDescriptorManager _shellDescriptorManager ;
2011-03-22 12:41:14 +08:00
private readonly ICacheManager _cacheManager ;
2010-04-24 06:10:47 +08:00
2010-10-12 06:24:11 +08:00
public ModuleService (
2011-03-15 02:14:35 +08:00
IFeatureManager featureManager ,
2011-02-18 01:17:49 +08:00
IOrchardServices orchardServices ,
IVirtualPathProvider virtualPathProvider ,
2010-10-12 06:24:11 +08:00
IExtensionManager extensionManager ,
2011-03-22 12:41:14 +08:00
IShellDescriptorManager shellDescriptorManager ,
ICacheManager cacheManager ) {
2010-05-19 01:04:14 +08:00
Services = orchardServices ;
2011-02-18 01:17:49 +08:00
2011-03-15 02:14:35 +08:00
_featureManager = featureManager ;
2011-02-18 01:17:49 +08:00
_virtualPathProvider = virtualPathProvider ;
2010-04-24 06:10:47 +08:00
_extensionManager = extensionManager ;
2010-04-29 02:47:29 +08:00
_shellDescriptorManager = shellDescriptorManager ;
2011-03-22 12:41:14 +08:00
_cacheManager = cacheManager ;
2011-02-18 01:17:49 +08:00
2011-03-15 02:14:35 +08:00
if ( _featureManager . FeatureDependencyNotification = = null ) {
_featureManager . FeatureDependencyNotification = GenerateWarning ;
}
2010-05-19 01:04:14 +08:00
T = NullLocalizer . Instance ;
2010-04-24 06:10:47 +08:00
}
2010-06-03 04:54:50 +08:00
public Localizer T { get ; set ; }
2010-05-19 01:04:14 +08:00
public IOrchardServices Services { get ; set ; }
2011-03-15 02:14:35 +08:00
/// <summary>
/// Retrieves an enumeration of the available features together with its state (enabled / disabled).
/// </summary>
/// <returns>An enumeration of the available features together with its state (enabled / disabled).</returns>
2010-11-07 11:27:45 +08:00
public IEnumerable < ModuleFeature > GetAvailableFeatures ( ) {
2010-05-29 04:03:57 +08:00
var enabledFeatures = _shellDescriptorManager . GetShellDescriptor ( ) . Features ;
2010-10-01 05:14:57 +08:00
return _extensionManager . AvailableExtensions ( )
2010-04-29 02:47:29 +08:00
. SelectMany ( m = > _extensionManager . LoadFeatures ( m . Features ) )
2010-07-19 04:10:10 +08:00
. Select ( f = > AssembleModuleFromDescriptor ( f , enabledFeatures
2010-11-19 07:37:47 +08:00
. FirstOrDefault ( sf = > string . Equals ( sf . Name , f . Descriptor . Id , StringComparison . OrdinalIgnoreCase ) ) ! = null ) ) ;
2010-04-29 02:47:29 +08:00
}
2010-04-28 02:24:06 +08:00
2011-03-15 02:14:35 +08:00
/// <summary>
/// Enables a list of features.
/// </summary>
/// <param name="featureIds">The IDs for the features to be enabled.</param>
public void EnableFeatures ( IEnumerable < string > featureIds ) {
EnableFeatures ( featureIds , false ) ;
2010-04-28 02:24:06 +08:00
}
2011-03-15 02:14:35 +08:00
/// <summary>
/// Enables a list of features.
/// </summary>
/// <param name="featureIds">The IDs for the features to be enabled.</param>
/// <param name="force">Boolean parameter indicating if the feature should enable it's dependencies if required or fail otherwise.</param>
public void EnableFeatures ( IEnumerable < string > featureIds , bool force ) {
foreach ( string featureId in _featureManager . EnableFeatures ( featureIds , force ) ) {
2012-01-05 08:32:37 +08:00
var featureName = _featureManager . GetAvailableFeatures ( ) . First ( f = > f . Id . Equals ( featureId , StringComparison . OrdinalIgnoreCase ) ) . Name ;
2011-03-31 09:19:14 +08:00
Services . Notifier . Information ( T ( "{0} was enabled" , featureName ) ) ;
2010-05-19 01:04:14 +08:00
}
2010-04-28 02:24:06 +08:00
}
2011-03-15 02:14:35 +08:00
/// <summary>
/// Disables a list of features.
/// </summary>
/// <param name="featureIds">The IDs for the features to be disabled.</param>
public void DisableFeatures ( IEnumerable < string > featureIds ) {
DisableFeatures ( featureIds , false ) ;
2010-05-19 15:00:56 +08:00
}
2011-03-15 02:14:35 +08:00
/// <summary>
/// Disables a list of features.
/// </summary>
/// <param name="featureIds">The IDs for the features to be disabled.</param>
/// <param name="force">Boolean parameter indicating if the feature should disable the features which depend on it if required or fail otherwise.</param>
public void DisableFeatures ( IEnumerable < string > featureIds , bool force ) {
foreach ( string featureId in _featureManager . DisableFeatures ( featureIds , force ) ) {
2014-08-09 03:12:48 +08:00
var featureName = _featureManager . GetAvailableFeatures ( ) . Single ( f = > f . Id . Equals ( featureId , StringComparison . OrdinalIgnoreCase ) ) . Name ;
2011-03-31 09:19:14 +08:00
Services . Notifier . Information ( T ( "{0} was disabled" , featureName ) ) ;
2010-05-19 15:00:56 +08:00
}
}
2011-02-18 01:17:49 +08:00
/// <summary>
2011-02-19 00:21:19 +08:00
/// Determines if a module was recently installed by using the project's last written time.
2011-02-18 01:17:49 +08:00
/// </summary>
2011-03-15 02:14:35 +08:00
/// <param name="extensionDescriptor">The extension descriptor.</param>
public bool IsRecentlyInstalled ( ExtensionDescriptor extensionDescriptor ) {
2011-03-22 12:41:14 +08:00
DateTime lastWrittenUtc = _cacheManager . Get ( extensionDescriptor , descriptor = > {
string projectFile = GetManifestPath ( extensionDescriptor ) ;
if ( ! string . IsNullOrEmpty ( projectFile ) ) {
// If project file was modified less than 24 hours ago, the module was recently deployed
return _virtualPathProvider . GetFileLastWriteTimeUtc ( projectFile ) ;
}
2011-02-18 01:17:49 +08:00
2011-03-22 12:41:14 +08:00
return DateTime . UtcNow ;
} ) ;
return DateTime . UtcNow . Subtract ( lastWrittenUtc ) < new TimeSpan ( 1 , 0 , 0 , 0 ) ;
2011-02-18 01:17:49 +08:00
}
2014-02-11 04:56:49 +08:00
public IEnumerable < FeatureDescriptor > GetDependentFeatures ( string featureId ) {
var dependants = _featureManager . GetDependentFeatures ( featureId ) ;
var availableFeatures = _featureManager . GetAvailableFeatures ( ) . ToLookup ( f = > f . Id , StringComparer . OrdinalIgnoreCase ) ;
return dependants
. SelectMany ( id = > availableFeatures [ id ] )
. ToList ( ) ;
}
2011-03-15 02:14:35 +08:00
/// <summary>
/// Retrieves the full path of the manifest file for a module's extension descriptor.
/// </summary>
/// <param name="extensionDescriptor">The module's extension descriptor.</param>
/// <returns>The full path to the module's manifest file.</returns>
private string GetManifestPath ( ExtensionDescriptor extensionDescriptor ) {
string projectPath = _virtualPathProvider . Combine ( extensionDescriptor . Location , extensionDescriptor . Id , "module.txt" ) ;
2011-02-18 01:17:49 +08:00
if ( ! _virtualPathProvider . FileExists ( projectPath ) ) {
return null ;
}
return projectPath ;
}
2010-11-07 11:27:45 +08:00
private static ModuleFeature AssembleModuleFromDescriptor ( Feature feature , bool isEnabled ) {
2010-04-29 02:47:29 +08:00
return new ModuleFeature {
Descriptor = feature . Descriptor ,
IsEnabled = isEnabled
} ;
}
2011-03-15 02:14:35 +08:00
2011-03-22 12:41:14 +08:00
private void GenerateWarning ( string messageFormat , string featureName , IEnumerable < string > featuresInQuestion ) {
if ( featuresInQuestion . Count ( ) < 1 )
return ;
Services . Notifier . Warning ( T (
messageFormat ,
featureName ,
featuresInQuestion . Count ( ) > 1
? string . Join ( "" ,
featuresInQuestion . Select (
( fn , i ) = >
T ( i = = featuresInQuestion . Count ( ) - 1
? "{0}"
: ( i = = featuresInQuestion . Count ( ) - 2
? "{0} and "
: "{0}, " ) , fn ) . ToString ( ) ) . ToArray ( ) )
: featuresInQuestion . First ( ) ) ) ;
}
2010-04-24 06:10:47 +08:00
}
}