2017-04-04 23:05:16 +00:00
|
|
|
import 'dart:io';
|
|
|
|
import 'dart:collection';
|
2017-04-05 20:38:03 +00:00
|
|
|
import 'package:path/path.dart' as path;
|
2017-04-04 23:05:16 +00:00
|
|
|
|
|
|
|
import 'package:angel_framework/angel_framework.dart';
|
2017-04-05 20:38:03 +00:00
|
|
|
import 'package:angel_mustache/src/mustache_context.dart';
|
2017-04-04 23:05:16 +00:00
|
|
|
|
|
|
|
class MustacheCacheController {
|
|
|
|
|
|
|
|
/**
|
2017-04-05 20:38:03 +00:00
|
|
|
* The context for which views and partials are
|
|
|
|
* served from.
|
2017-04-04 23:05:16 +00:00
|
|
|
*/
|
2017-04-05 20:38:03 +00:00
|
|
|
MustacheContext context;
|
2017-04-04 23:05:16 +00:00
|
|
|
|
2017-04-05 20:38:03 +00:00
|
|
|
HashMap<String, String> viewCache = new HashMap();
|
|
|
|
HashMap<String, String> partialCache = new HashMap();
|
2017-04-04 23:05:16 +00:00
|
|
|
|
2017-04-05 20:38:03 +00:00
|
|
|
MustacheCacheController([this.context]);
|
2017-04-04 23:05:16 +00:00
|
|
|
|
|
|
|
get_view(String viewName, Angel app) async {
|
|
|
|
if (app.isProduction) {
|
|
|
|
if (viewCache.containsKey(viewName)) {
|
|
|
|
return viewCache[viewName];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-05 20:38:03 +00:00
|
|
|
File viewFile = context.resolveView(viewName);
|
2017-04-04 23:05:16 +00:00
|
|
|
|
|
|
|
if (await viewFile.exists()) {
|
|
|
|
String viewTemplate = await viewFile.readAsString();
|
|
|
|
if (app.isProduction) {
|
|
|
|
this.viewCache[viewName] = viewTemplate;
|
|
|
|
}
|
|
|
|
return viewTemplate;
|
|
|
|
} else
|
|
|
|
throw new FileSystemException(
|
2017-04-05 20:38:03 +00:00
|
|
|
'View "$viewName" was not found.', viewFile.path);
|
2017-04-04 23:05:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get_partial(String partialName, Angel app) {
|
|
|
|
if (app.isProduction) {
|
|
|
|
if (partialCache.containsKey(partialName)) {
|
|
|
|
return partialCache[partialName];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-05 20:38:03 +00:00
|
|
|
File partialFile = context.resolvePartial(partialName);
|
2017-04-04 23:05:16 +00:00
|
|
|
|
2017-04-05 20:38:03 +00:00
|
|
|
if (partialFile.existsSync()) {
|
|
|
|
String partialTemplate = partialFile.readAsStringSync();
|
2017-04-04 23:05:16 +00:00
|
|
|
if (app.isProduction) {
|
|
|
|
this.partialCache[partialName] = partialTemplate;
|
|
|
|
}
|
|
|
|
return partialTemplate;
|
|
|
|
} else
|
|
|
|
throw new FileSystemException(
|
2017-04-05 20:38:03 +00:00
|
|
|
'View "$partialName" was not found.', partialFile.path);
|
2017-04-04 23:05:16 +00:00
|
|
|
}
|
|
|
|
}
|