Refactor FreemarkerConfiguration

pull/146/head
johnniang 2019-04-28 20:35:40 +08:00
parent 24a23bf7fb
commit 31be6e18a7
13 changed files with 57 additions and 108 deletions

View File

@ -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());
}
}
}

View File

@ -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);
}

View File

@ -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<ApplicationStartedEvent> {
@Autowired

View File

@ -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);
}
/**
*
*

View File

@ -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.

View File

@ -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.

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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;

View File

@ -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