forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewPageRef.cs
More file actions
171 lines (147 loc) · 4.2 KB
/
Copy pathViewPageRef.cs
File metadata and controls
171 lines (147 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading;
using ServiceStack.Text;
using ServiceStack.Logging;
using ServiceStack.Razor.Templating;
using ServiceStack.WebHost.Endpoints.Support.Markdown;
namespace ServiceStack.Razor
{
public class ViewPageRef : IViewPage
{
private static readonly ILog Log = LogManager.GetLogger(typeof (ViewPageRef));
public TemplateService Service { get; set; }
public RazorFormat RazorFormat { get; set; }
public string FilePath { get; set; }
public string Name { get; set; }
public string Contents { get; set; }
public RazorPageType PageType { get; set; }
public string Template { get; set; }
public DateTime? LastModified { get; set; }
public List<IExpirable> Dependents { get; private set; }
public const string ModelName = "Model";
public ViewPageRef()
{
this.Dependents = new List<IExpirable>();
}
public ViewPageRef(RazorFormat razorFormat, string fullPath, string name, string contents)
: this(razorFormat, fullPath, name, contents, RazorPageType.ViewPage) {}
public ViewPageRef(RazorFormat razorFormat, string fullPath, string name, string contents, RazorPageType pageType)
: this()
{
RazorFormat = razorFormat;
FilePath = fullPath;
Name = name;
Contents = contents;
PageType = pageType;
}
public DateTime? GetLastModified()
{
//if (!hasCompletedFirstRun) return null;
var lastModified = this.LastModified;
foreach (var expirable in this.Dependents)
{
if (!expirable.LastModified.HasValue) continue;
if (!lastModified.HasValue || expirable.LastModified > lastModified)
{
lastModified = expirable.LastModified;
}
}
return lastModified;
}
public string PageName
{
get
{
return this.PageType == RazorPageType.Template
|| this.PageType == RazorPageType.ContentPage
? this.FilePath
: this.Name;
}
}
bool isCompiled;
public bool IsCompiled
{
get { return isCompiled; }
}
public void Compile(bool force=false)
{
if (IsCompiled && !force) return;
lock (this)
{
if (IsCompiled && !force) return;
var sw = Stopwatch.StartNew();
try
{
Service.Compile(this, this.Contents, PageName);
Log.InfoFormat("Compiled {0} in {1}ms", this.FilePath, sw.ElapsedMilliseconds);
}
catch (Exception ex)
{
Log.Error("Error compiling {0}".Fmt(this.FilePath), ex);
throw;
}
isCompiled = true;
}
}
public void EnsureCompiled()
{
Compile();
}
private int timesRun;
private Exception initException;
readonly object readWriteLock = new object();
private bool isBusy;
public void Reload(string contents, DateTime lastModified)
{
lock (readWriteLock)
{
try
{
isBusy = true;
this.Contents = contents;
this.LastModified = lastModified;
initException = null;
timesRun = 0;
Compile(force:true);
}
catch (Exception ex)
{
initException = ex;
}
isBusy = false;
Monitor.PulseAll(readWriteLock);
}
}
public IRazorTemplate GetRazorTemplate()
{
return Service.GetTemplate(this.PageName);
}
public string RenderToHtml()
{
return RenderToString((object)null);
}
public string RenderToHtml<T>(T model)
{
return RenderToString(model);
}
public string RenderToString<T>(T model)
{
var template = ExecuteTemplate(model);
return template.Result;
}
public IRazorTemplate ExecuteTemplate<T>(T model)
{
return RazorFormat.ExecuteTemplate(model, this.PageName, this.Template);
}
public IRazorTemplate InitTemplate<T>(T model)
{
var templateService = RazorFormat.GetTemplateService(PageName);
var template = templateService.GetTemplate(PageName);
templateService.InitTemplate(model, template);
return template;
}
}
}