Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 256 lines (212 sloc) 9.055 kb
7365e37 @jeremydmiller all razor UT's passing
jeremydmiller authored
1 using System;
2 using System.Collections.Generic;
3 using FubuCore;
4 using FubuCore.Util;
5 using FubuMVC.Core.View.Model;
6 using FubuMVC.Razor.RazorModel;
7 using FubuMVC.Razor.Rendering;
8 using RazorEngine.Templating;
9 using RazorEngine.Text;
10 using ITemplate = RazorEngine.Templating.ITemplate;
11
12 namespace FubuMVC.Razor
13 {
14 public interface IFubuTemplateService : ITemplateService
15 {
16 IFubuRazorView GetView(ViewDescriptor<IRazorTemplate> descriptor);
17 }
18
19 public class FubuTemplateService : IFubuTemplateService
20 {
21 private readonly TemplateRegistry<IRazorTemplate> _templateRegistry;
22 private readonly ITemplateService _inner;
23 private readonly IFileSystem _fileSystem;
24 private readonly Cache<string, long> _lastModifiedCache;
25
26 public FubuTemplateService(TemplateRegistry<IRazorTemplate> templateRegistry, ITemplateService inner, IFileSystem fileSystem)
27 {
28 _templateRegistry = templateRegistry;
29 _inner = inner;
30 _fileSystem = fileSystem;
31 _lastModifiedCache = new Cache<string, long>(name => name.LastModified());
32 }
33
34 public void Dispose()
35 {
36 //Don't dispose
37 }
38
39 public void AddNamespace(string ns)
40 {
41 _inner.AddNamespace(ns);
42 }
43
44 public void Compile(string razorTemplate, string name)
45 {
46 _inner.Compile(razorTemplate, name);
47 }
48
49 public void Compile(string razorTemplate, Type modelType, string name)
50 {
51 _inner.Compile(razorTemplate, modelType, name);
52 }
53
54 public void Compile<T>(string razorTemplate, string name)
55 {
56 _inner.Compile<T>(razorTemplate, name);
57 }
58
59 public ITemplate CreateTemplate(string razorTemplate)
60 {
61 return _inner.CreateTemplate(razorTemplate);
62 }
63
64 public ITemplate CreateTemplate<T>(string razorTemplate, T model)
65 {
66 return _inner.CreateTemplate(razorTemplate, model);
67 }
68
69 public IEnumerable<ITemplate> CreateTemplates(IEnumerable<string> razorTemplates, bool parallel = false)
70 {
71 return _inner.CreateTemplates(razorTemplates, parallel);
72 }
73
74 public IEnumerable<ITemplate> CreateTemplates<T>(IEnumerable<string> razorTemplates, IEnumerable<T> models, bool parallel = false)
75 {
76 return _inner.CreateTemplates(razorTemplates, models, parallel);
77 }
78
79 public Type CreateTemplateType(string razorTemplate)
80 {
81 return _inner.CreateTemplateType(razorTemplate);
82 }
83
84 public Type CreateTemplateType(string razorTemplate, Type modelType)
85 {
86 return _inner.CreateTemplateType(razorTemplate, modelType);
87 }
88
89 public IEnumerable<Type> CreateTemplateTypes(IEnumerable<string> razorTemplates, bool parallel = false)
90 {
91 return _inner.CreateTemplateTypes(razorTemplates, parallel);
92 }
93
94 public IEnumerable<Type> CreateTemplateTypes(IEnumerable<string> razorTemplates, Type modelType, bool parallel = false)
95 {
96 return _inner.CreateTemplateTypes(razorTemplates, modelType, parallel);
97 }
98
99 public ITemplate GetTemplate(string razorTemplate, string name)
100 {
101 var template = _inner.GetTemplate(razorTemplate, name);
102 template.TemplateService = this;
103 return template;
104 }
105
106 public ITemplate GetTemplate<T>(string razorTemplate, T model, string name)
107 {
108 var template = _inner.GetTemplate(razorTemplate, model, name);
109 template.TemplateService = this;
110 return template;
111 }
112
113 public IEnumerable<ITemplate> GetTemplates(IEnumerable<string> razorTemplates, IEnumerable<string> names, bool parallel = false)
114 {
115 throw new NotImplementedException();
116 }
117
118 public IEnumerable<ITemplate> GetTemplates<T>(IEnumerable<string> razorTemplates, IEnumerable<T> models, IEnumerable<string> names, bool parallel = false)
119 {
120 throw new NotImplementedException();
121 }
122
123 public bool HasTemplate(string name)
124 {
125 return _inner.HasTemplate(name);
126 }
127
128 public string Parse(string razorTemplate)
129 {
130 throw new NotImplementedException();
131 }
132
133 public string Parse(string razorTemplate, string name)
134 {
135 throw new NotImplementedException();
136 }
137
138 public string Parse(string razorTemplate, object model)
139 {
140 throw new NotImplementedException();
141 }
142
143 public string Parse<T>(string razorTemplate, T model)
144 {
145 throw new NotImplementedException();
146 }
147
148 public string Parse<T>(string razorTemplate, T model, string name)
149 {
150 throw new NotImplementedException();
151 }
152
153 public string Parse(string razorTemplate, object model, string name)
154 {
155 throw new NotImplementedException();
156 }
157
158 public IEnumerable<string> ParseMany(IEnumerable<string> razorTemplates, bool parallel = false)
159 {
160 throw new NotImplementedException();
161 }
162
163 public IEnumerable<string> ParseMany(IEnumerable<string> razorTemplates, IEnumerable<string> names, bool parallel = false)
164 {
165 throw new NotImplementedException();
166 }
167
168 public IEnumerable<string> ParseMany<T>(string razorTemplate, IEnumerable<T> models, bool parallel = false)
169 {
170 throw new NotImplementedException();
171 }
172
173 public IEnumerable<string> ParseMany<T>(IEnumerable<string> razorTemplates, IEnumerable<T> models, bool parallel = false)
174 {
175 throw new NotImplementedException();
176 }
177
178 public IEnumerable<string> ParseMany<T>(IEnumerable<string> razorTemplates, IEnumerable<T> models, IEnumerable<string> names, bool parallel = false)
179 {
180 throw new NotImplementedException();
181 }
182
183 public ITemplate Resolve(string name)
184 {
185 var fubuTemplate = _templateRegistry.FirstByName(name);
186 return GetView(fubuTemplate.Descriptor.As<ViewDescriptor<IRazorTemplate>>());
187 }
188
189 public ITemplate Resolve(string name, object model)
190 {
191 throw new NotImplementedException();
192 }
193
194 public ITemplate Resolve<T>(string name, T model)
195 {
196 var fubuTemplate = _templateRegistry.FirstByName(name);
197 GetView(fubuTemplate.Descriptor.As<ViewDescriptor<IRazorTemplate>>());
198 var template = _inner.Resolve(fubuTemplate.GeneratedViewId.ToString(), model);
199 template.TemplateService = this;
200 return template;
201 }
202
203 public string Run(string name)
204 {
205 throw new NotImplementedException();
206 }
207
208 public string Run(string name, object model)
209 {
210 throw new NotImplementedException();
211 }
212
213 public string Run<T>(string name, T model)
214 {
215 throw new NotImplementedException();
216 }
217
218 public IEncodedStringFactory EncodedStringFactory
219 {
220 get { return _inner.EncodedStringFactory; }
221 }
222
223 public IFubuRazorView GetView(ViewDescriptor<IRazorTemplate> descriptor)
224 {
225 var viewId = descriptor.Template.As<IRazorTemplate>().GeneratedViewId.ToString();
226
227 if (_inner.HasTemplate(viewId) && _lastModifiedCache[descriptor.Template.FilePath] == descriptor.Template.FilePath.LastModified())
228 {
229 return GetView(x => (IFubuRazorView)x.Resolve(viewId));
230 }
231 return GetView(x => (IFubuRazorView)x.GetTemplate(_fileSystem.ReadStringFromFile(descriptor.Template.FilePath), viewId));
232 }
233
234 public IFubuRazorView GetView(ViewDescriptor<IRazorTemplate> descriptor, object model)
235 {
236 var viewId = descriptor.Template.GeneratedViewId.ToString();
237
238 if (_inner.HasTemplate(viewId) && _lastModifiedCache[descriptor.Template.FilePath] == descriptor.Template.FilePath.LastModified())
239 {
240 return GetView(x => (IFubuRazorView)x.Resolve(viewId, model));
241 }
242 return GetView(x =>
243 {
244 x.GetTemplate(_fileSystem.ReadStringFromFile(descriptor.Template.FilePath), viewId);
245 return (IFubuRazorView)x.Resolve(viewId, model);
246 });
247 }
248
249 private IFubuRazorView GetView(Func<ITemplateService, IFubuRazorView> templateAction)
250 {
251 var template = templateAction(_inner);
252 template.TemplateService = this;
253 return template;
254 }
255 }
5367ffc @CoreyKaylor Cleaning up some of the API
CoreyKaylor authored
256 }
Something went wrong with that request. Please try again.