diff --git a/src/main/java/run/halo/app/config/FreeMarkerAutoConfiguration.java b/src/main/java/run/halo/app/config/FreeMarkerAutoConfiguration.java deleted file mode 100644 index 467be1a19..000000000 --- a/src/main/java/run/halo/app/config/FreeMarkerAutoConfiguration.java +++ /dev/null @@ -1,94 +0,0 @@ -package run.halo.app.config; - -import freemarker.template.TemplateModelException; -import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Configuration; -import run.halo.app.model.freemarker.method.RandomMethod; -import run.halo.app.model.freemarker.method.RecentCommentsMethod; -import run.halo.app.model.freemarker.method.RecentPostsMethod; -import run.halo.app.model.freemarker.tag.*; -import run.halo.app.service.OptionService; -import run.halo.app.service.ThemeService; -import run.halo.app.service.ThemeSettingService; -import run.halo.app.service.UserService; - -import javax.annotation.PostConstruct; - -/** - * FreeMarker configuration. - * - * @author ryanwang - * @date : 2018/4/26 - */ -@Slf4j -@Configuration -public class FreeMarkerAutoConfiguration { - - @Autowired - private freemarker.template.Configuration configuration; - - @Autowired - private OptionService optionsService; - - @Autowired - private UserService userService; - - @Autowired - private ThemeService themeService; - - @Autowired - private ThemeSettingService themeSettingService; - - @Autowired - private PostTagDirective postTagDirective; - - @Autowired - private CategoryTagDirective categoryTagDirective; - - @Autowired - private CommentTagDirective commentTagDirective; - - @Autowired - private LinkTagDirective linkTagDirective; - - @Autowired - private MenuTagDirective menuTagDirective; - - @Autowired - private TagTagDirective tagTagDirective; - - @Autowired - private PhotoTagDirective photoTagDirective; - - @Autowired - private RandomMethod randomMethod; - - @Autowired - private RecentPostsMethod recentPostsMethod; - - @Autowired - private RecentCommentsMethod recentCommentsMethod; - - @PostConstruct - public void setSharedVariable() { - try { - configuration.setSharedVariable("options", optionsService.listOptions()); - configuration.setSharedVariable("user", userService.getCurrentUser().orElse(null)); - configuration.setSharedVariable("settings", themeSettingService.listAsMapBy(themeService.getActivatedThemeId())); - //Freemarker custom tags - configuration.setSharedVariable("categoryTag", categoryTagDirective); - configuration.setSharedVariable("commentTag", commentTagDirective); - configuration.setSharedVariable("linkTag", linkTagDirective); - configuration.setSharedVariable("menuTag", menuTagDirective); - configuration.setSharedVariable("tagTag", tagTagDirective); - configuration.setSharedVariable("postTag", postTagDirective); - configuration.setSharedVariable("photoTag", photoTagDirective); - configuration.setSharedVariable("randomMethod", randomMethod); - configuration.setSharedVariable("recentPostsMethod", recentPostsMethod); - configuration.setSharedVariable("recentCommentsMethod", recentCommentsMethod); - } catch (TemplateModelException e) { - log.error("Custom tags failed to load:{}", e.getMessage()); - } - } -} diff --git a/src/main/java/run/halo/app/event/theme/FreemarkerConfigAwareListener.java b/src/main/java/run/halo/app/event/theme/FreemarkerConfigAwareListener.java index 5fc706806..c68634718 100644 --- a/src/main/java/run/halo/app/event/theme/FreemarkerConfigAwareListener.java +++ b/src/main/java/run/halo/app/event/theme/FreemarkerConfigAwareListener.java @@ -5,12 +5,15 @@ import freemarker.template.TemplateModelException; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.context.event.ApplicationStartedEvent; import org.springframework.context.event.EventListener; +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; import run.halo.app.handler.theme.config.support.ThemeProperty; import run.halo.app.service.OptionService; import run.halo.app.service.ThemeService; import run.halo.app.service.ThemeSettingService; +import run.halo.app.service.UserService; import java.util.Map; @@ -32,25 +35,35 @@ public class FreemarkerConfigAwareListener { private final ThemeSettingService themeSettingService; + private final OptionService optionsService; + + private final UserService userService; + public FreemarkerConfigAwareListener(OptionService optionService, Configuration configuration, ThemeService themeService, - ThemeSettingService themeSettingService) { + ThemeSettingService themeSettingService, + OptionService optionsService, + UserService userService) { this.optionService = optionService; this.configuration = configuration; this.themeService = themeService; this.themeSettingService = themeSettingService; + this.optionsService = optionsService; + this.userService = userService; } @Async @EventListener + @Order(Ordered.HIGHEST_PRECEDENCE + 1) public void onApplicationStartedEvent(ApplicationStartedEvent applicationStartedEvent) { try { - ThemeProperty activatedTheme = themeService.getActivatedTheme(); - log.debug("Set shared variable theme: [{}]", activatedTheme); - configuration.setSharedVariable("theme", activatedTheme); + configuration.setSharedVariable("options", optionsService.listOptions()); + configuration.setSharedVariable("user", userService.getCurrentUser().orElse(null)); + configuration.setSharedVariable("settings", themeSettingService.listAsMapBy(themeService.getActivatedThemeId())); } catch (TemplateModelException e) { log.warn("Failed to configure freemarker", e); + // Ignore this error } } @@ -65,7 +78,7 @@ public class FreemarkerConfigAwareListener { log.debug("Set shared variable options: [{}]", options); configuration.setSharedVariable("options", options); log.debug("Set shared variable theme settings: [{}]", options); - configuration.setSharedVariable("settings",themeSettingService.listAsMapBy(themeService.getActivatedThemeId())); + configuration.setSharedVariable("settings", themeSettingService.listAsMapBy(themeService.getActivatedThemeId())); } catch (TemplateModelException e) { log.warn("Failed to configure freemarker", e); } diff --git a/src/main/java/run/halo/app/listener/StartedListener.java b/src/main/java/run/halo/app/listener/StartedListener.java index 90e4978d8..a4930accf 100644 --- a/src/main/java/run/halo/app/listener/StartedListener.java +++ b/src/main/java/run/halo/app/listener/StartedListener.java @@ -5,6 +5,8 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.event.ApplicationStartedEvent; import org.springframework.context.ApplicationListener; import org.springframework.context.annotation.Configuration; +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; import org.springframework.util.ResourceUtils; import run.halo.app.config.properties.HaloProperties; import run.halo.app.model.entity.User; @@ -30,6 +32,7 @@ import java.util.List; */ @Slf4j @Configuration +@Order(Ordered.HIGHEST_PRECEDENCE) public class StartedListener implements ApplicationListener { @Autowired diff --git a/src/main/java/run/halo/app/model/freemarker/method/RandomMethod.java b/src/main/java/run/halo/app/model/freemarker/method/RandomMethod.java index fcb84788d..073047020 100644 --- a/src/main/java/run/halo/app/model/freemarker/method/RandomMethod.java +++ b/src/main/java/run/halo/app/model/freemarker/method/RandomMethod.java @@ -1,6 +1,7 @@ package run.halo.app.model.freemarker.method; import cn.hutool.core.util.RandomUtil; +import freemarker.template.Configuration; import freemarker.template.SimpleNumber; import freemarker.template.TemplateMethodModelEx; import freemarker.template.TemplateModelException; @@ -15,6 +16,10 @@ import java.util.List; @Component public class RandomMethod implements TemplateMethodModelEx { + public RandomMethod(Configuration configuration) { + configuration.setSharedVariable("randomMethod", this); + } + /** * 生成随机数 * diff --git a/src/main/java/run/halo/app/model/freemarker/method/RecentCommentsMethod.java b/src/main/java/run/halo/app/model/freemarker/method/RecentCommentsMethod.java index bccb758c3..4dd212871 100644 --- a/src/main/java/run/halo/app/model/freemarker/method/RecentCommentsMethod.java +++ b/src/main/java/run/halo/app/model/freemarker/method/RecentCommentsMethod.java @@ -1,5 +1,6 @@ package run.halo.app.model.freemarker.method; +import freemarker.template.Configuration; import freemarker.template.TemplateMethodModelEx; import freemarker.template.TemplateModelException; import org.springframework.stereotype.Component; @@ -12,6 +13,11 @@ import java.util.List; */ @Component public class RecentCommentsMethod implements TemplateMethodModelEx { + + public RecentCommentsMethod(Configuration configuration) { + configuration.setSharedVariable("recentCommentsMethod", this); + } + @Override public Object exec(List arguments) throws TemplateModelException { // TODO Complete recent comments method. diff --git a/src/main/java/run/halo/app/model/freemarker/method/RecentPostsMethod.java b/src/main/java/run/halo/app/model/freemarker/method/RecentPostsMethod.java index d1eceb6b9..0ad1aee78 100644 --- a/src/main/java/run/halo/app/model/freemarker/method/RecentPostsMethod.java +++ b/src/main/java/run/halo/app/model/freemarker/method/RecentPostsMethod.java @@ -1,5 +1,6 @@ package run.halo.app.model.freemarker.method; +import freemarker.template.Configuration; import freemarker.template.TemplateMethodModelEx; import freemarker.template.TemplateModelException; import org.springframework.stereotype.Component; @@ -12,6 +13,11 @@ import java.util.List; */ @Component public class RecentPostsMethod implements TemplateMethodModelEx { + + public RecentPostsMethod(Configuration configuration) { + configuration.setSharedVariable("recentPostsMethod", this); + } + @Override public Object exec(List arguments) throws TemplateModelException { // TODO Complete recent post method. diff --git a/src/main/java/run/halo/app/model/freemarker/tag/CategoryTagDirective.java b/src/main/java/run/halo/app/model/freemarker/tag/CategoryTagDirective.java index 502247793..af26e755b 100644 --- a/src/main/java/run/halo/app/model/freemarker/tag/CategoryTagDirective.java +++ b/src/main/java/run/halo/app/model/freemarker/tag/CategoryTagDirective.java @@ -20,8 +20,10 @@ public class CategoryTagDirective implements TemplateDirectiveModel { private final CategoryService categoryService; - public CategoryTagDirective(CategoryService categoryService) { + public CategoryTagDirective(Configuration configuration, CategoryService categoryService) { this.categoryService = categoryService; + + configuration.setSharedVariable("categoryTag", this); } @Override diff --git a/src/main/java/run/halo/app/model/freemarker/tag/CommentTagDirective.java b/src/main/java/run/halo/app/model/freemarker/tag/CommentTagDirective.java index 6b0bbfcf4..14ed98af8 100644 --- a/src/main/java/run/halo/app/model/freemarker/tag/CommentTagDirective.java +++ b/src/main/java/run/halo/app/model/freemarker/tag/CommentTagDirective.java @@ -20,8 +20,9 @@ public class CommentTagDirective implements TemplateDirectiveModel { private final PostCommentService postCommentService; - public CommentTagDirective(PostCommentService postCommentService) { + public CommentTagDirective(Configuration configuration, PostCommentService postCommentService) { this.postCommentService = postCommentService; + configuration.setSharedVariable("commentTag", this); } @Override diff --git a/src/main/java/run/halo/app/model/freemarker/tag/LinkTagDirective.java b/src/main/java/run/halo/app/model/freemarker/tag/LinkTagDirective.java index 07e74bfea..6cf6897a5 100644 --- a/src/main/java/run/halo/app/model/freemarker/tag/LinkTagDirective.java +++ b/src/main/java/run/halo/app/model/freemarker/tag/LinkTagDirective.java @@ -23,8 +23,9 @@ public class LinkTagDirective implements TemplateDirectiveModel { private final LinkService linkService; - public LinkTagDirective(LinkService linkService) { + public LinkTagDirective(Configuration configuration, LinkService linkService) { this.linkService = linkService; + configuration.setSharedVariable("linkTag", this); } @Override diff --git a/src/main/java/run/halo/app/model/freemarker/tag/MenuTagDirective.java b/src/main/java/run/halo/app/model/freemarker/tag/MenuTagDirective.java index 9e67984c7..2a9050a30 100644 --- a/src/main/java/run/halo/app/model/freemarker/tag/MenuTagDirective.java +++ b/src/main/java/run/halo/app/model/freemarker/tag/MenuTagDirective.java @@ -22,8 +22,9 @@ public class MenuTagDirective implements TemplateDirectiveModel { private final MenuService menuService; - public MenuTagDirective(MenuService menuService) { + public MenuTagDirective(Configuration configuration,MenuService menuService) { this.menuService = menuService; + configuration.setSharedVariable("menuTag", this); } @Override diff --git a/src/main/java/run/halo/app/model/freemarker/tag/PhotoTagDirective.java b/src/main/java/run/halo/app/model/freemarker/tag/PhotoTagDirective.java index c3dae5052..e7e8b0d61 100644 --- a/src/main/java/run/halo/app/model/freemarker/tag/PhotoTagDirective.java +++ b/src/main/java/run/halo/app/model/freemarker/tag/PhotoTagDirective.java @@ -23,8 +23,9 @@ public class PhotoTagDirective implements TemplateDirectiveModel { private final PhotoService photoService; - public PhotoTagDirective(PhotoService photoService) { + public PhotoTagDirective(Configuration configuration, PhotoService photoService) { this.photoService = photoService; + configuration.setSharedVariable("photoTag", this); } @Override diff --git a/src/main/java/run/halo/app/model/freemarker/tag/PostTagDirective.java b/src/main/java/run/halo/app/model/freemarker/tag/PostTagDirective.java index 6703f3566..ec68e2c54 100644 --- a/src/main/java/run/halo/app/model/freemarker/tag/PostTagDirective.java +++ b/src/main/java/run/halo/app/model/freemarker/tag/PostTagDirective.java @@ -20,8 +20,11 @@ public class PostTagDirective implements TemplateDirectiveModel { private final PostService postService; - public PostTagDirective(PostService postService) { + public PostTagDirective(Configuration configuration, + PostService postService) { this.postService = postService; + + configuration.setSharedVariable("postTag", this); } @Override @@ -34,10 +37,10 @@ public class PostTagDirective implements TemplateDirectiveModel { env.setVariable("count", builder.build().wrap(postService.count())); break; case "archiveYear": - env.setVariable("archives",builder.build().wrap(postService.listYearArchives())); + env.setVariable("archives", builder.build().wrap(postService.listYearArchives())); break; case "archiveMonth": - env.setVariable("archives",builder.build().wrap(postService.listMonthArchives())); + env.setVariable("archives", builder.build().wrap(postService.listMonthArchives())); break; default: break; diff --git a/src/main/java/run/halo/app/model/freemarker/tag/TagTagDirective.java b/src/main/java/run/halo/app/model/freemarker/tag/TagTagDirective.java index d63a0d5e8..ca2da570b 100644 --- a/src/main/java/run/halo/app/model/freemarker/tag/TagTagDirective.java +++ b/src/main/java/run/halo/app/model/freemarker/tag/TagTagDirective.java @@ -20,8 +20,9 @@ public class TagTagDirective implements TemplateDirectiveModel { private final TagService tagService; - public TagTagDirective(TagService tagService) { + public TagTagDirective(Configuration configuration, TagService tagService) { this.tagService = tagService; + configuration.setSharedVariable("tagTag", this); } @Override