mirror of https://github.com/Squidex/squidex.git
7 changed files with 156 additions and 85 deletions
@ -0,0 +1,27 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using Microsoft.AspNetCore.Mvc; |
|||
using Squidex.Infrastructure.Commands; |
|||
using Squidex.Web; |
|||
|
|||
namespace Squidex.Extensions.Samples.Controllers |
|||
{ |
|||
public sealed class PluginController : ApiController |
|||
{ |
|||
public PluginController(ICommandBus commandBus) |
|||
: base(commandBus) |
|||
{ |
|||
} |
|||
|
|||
[Route("/plugins/sample")] |
|||
public IActionResult Test() |
|||
{ |
|||
return Ok(new { text = "I am Plugin" }); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,50 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Reflection; |
|||
using Microsoft.AspNetCore.Mvc.ApplicationParts; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
|
|||
namespace Squidex.Pipeline.Plugins |
|||
{ |
|||
public static class MvcParts |
|||
{ |
|||
public static void AddParts(this Assembly assembly, IMvcBuilder mvcBuilder) |
|||
{ |
|||
mvcBuilder.ConfigureApplicationPartManager(manager => |
|||
{ |
|||
var parts = manager.ApplicationParts; |
|||
|
|||
AddParts(parts, assembly); |
|||
|
|||
foreach (var reference in RelatedAssemblyAttribute.GetRelatedAssemblies(assembly, false)) |
|||
{ |
|||
AddParts(parts, assembly); |
|||
} |
|||
}); |
|||
} |
|||
|
|||
private static void AddParts(IList<ApplicationPart> applicationParts, Assembly assembly) |
|||
{ |
|||
var partFactory = ApplicationPartFactory.GetApplicationPartFactory(assembly); |
|||
|
|||
foreach (var part in partFactory.GetApplicationParts(assembly)) |
|||
{ |
|||
var existings = applicationParts.Where(x => x.Name == part.Name).ToList(); |
|||
|
|||
foreach (var existing in existings) |
|||
{ |
|||
applicationParts.Remove(existing); |
|||
} |
|||
|
|||
applicationParts.Add(part); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,73 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.IO; |
|||
using System.Reflection; |
|||
using McMaster.NETCore.Plugins; |
|||
using Squidex.Domain.Apps.Core; |
|||
using Squidex.Domain.Apps.Entities; |
|||
using Squidex.Domain.Apps.Events; |
|||
using Squidex.Infrastructure; |
|||
using Squidex.Infrastructure.Plugins; |
|||
using Squidex.Web; |
|||
|
|||
namespace Squidex.Pipeline.Plugins |
|||
{ |
|||
public static class PluginLoaders |
|||
{ |
|||
private static readonly Type[] SharedTypes = |
|||
{ |
|||
typeof(IPlugin), |
|||
typeof(SquidexCoreModel), |
|||
typeof(SquidexCoreOperations), |
|||
typeof(SquidexEntities), |
|||
typeof(SquidexEvents), |
|||
typeof(SquidexInfrastructure), |
|||
typeof(SquidexWeb) |
|||
}; |
|||
|
|||
public static PluginLoader LoadPlugin(string pluginPath) |
|||
{ |
|||
foreach (var candidate in GetPaths(pluginPath)) |
|||
{ |
|||
if (candidate.Extension.Equals(".dll", StringComparison.OrdinalIgnoreCase)) |
|||
{ |
|||
return PluginLoader.CreateFromAssemblyFile(candidate.FullName, PluginLoaderOptions.PreferSharedTypes); |
|||
} |
|||
|
|||
if (candidate.Extension.Equals(".json", StringComparison.OrdinalIgnoreCase)) |
|||
{ |
|||
return PluginLoader.CreateFromConfigFile(candidate.FullName, SharedTypes); |
|||
} |
|||
} |
|||
|
|||
return null; |
|||
} |
|||
|
|||
private static IEnumerable<FileInfo> GetPaths(string pluginPath) |
|||
{ |
|||
var candidate = new FileInfo(Path.GetFullPath(pluginPath)); |
|||
|
|||
if (candidate.Exists) |
|||
{ |
|||
yield return candidate; |
|||
} |
|||
|
|||
if (!Path.IsPathRooted(pluginPath)) |
|||
{ |
|||
candidate = new FileInfo(Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), pluginPath)); |
|||
|
|||
if (candidate.Exists) |
|||
{ |
|||
yield return candidate; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue