1+ using Microsoft . Extensions . Configuration ;
2+ using System ;
3+ using System . Collections . Generic ;
4+ using System . Linq ;
5+
6+ namespace Simplify . Web . Settings
7+ {
8+ /// <summary>
9+ /// Simplify.Web settings
10+ /// </summary>
11+ /// <seealso cref="ISimplifyWebSettings" />
12+ public sealed class ConfigurationBasedSimplifyWebSettings : ISimplifyWebSettings
13+ {
14+ /// <summary>
15+ /// Initializes a new instance of the <see cref="ConfigurationBasedSimplifyWebSettings"/> class.
16+ /// </summary>
17+ public ConfigurationBasedSimplifyWebSettings ( IConfiguration configuration )
18+ {
19+ var config = configuration . GetSection ( "SimplifyWebSettings" ) ;
20+
21+ if ( ! config . GetChildren ( ) . Any ( ) )
22+ return ;
23+
24+ LoadLanguageManagerSettings ( config ) ;
25+ LoadTemplatesSettings ( config ) ;
26+ LoadDataCollectorSettings ( config ) ;
27+ LoadStyleSettings ( config ) ;
28+ LoadOtherSettings ( config ) ;
29+ LoadEngineBehaviorSettings ( config ) ;
30+ LoadCacheSettings ( config ) ;
31+ LoadDiagnosticSettings ( config ) ;
32+ }
33+
34+ /// <summary>
35+ /// Default language, for example: "en", "ru", "de" etc., default value is "en"
36+ /// </summary>
37+ public string DefaultLanguage { get ; private set ; } = "en" ;
38+
39+ /// <summary>
40+ /// Gets a value indicating whether browser language should be accepted
41+ /// </summary>
42+ /// <value>
43+ /// <c>true</c> if browser language should be accepted; otherwise, <c>false</c>.
44+ /// </value>
45+ public bool AcceptBrowserLanguage { get ; private set ; }
46+
47+ /// <summary>
48+ /// Default templates directory path, for example: Templates, default value is "Templates"
49+ /// </summary>
50+ public string DefaultTemplatesPath { get ; private set ; } = "Templates" ;
51+
52+ /// <summary>
53+ /// Gets a value indicating whether all templates should be loaded from assembly
54+ /// </summary>
55+ /// <value>
56+ /// <c>true</c> if all templates should be loaded from assembly; otherwise, <c>false</c>.
57+ /// </value>
58+ public bool LoadTemplatesFromAssembly { get ; private set ; }
59+
60+ /// <summary>
61+ /// Gets or sets the master page template file name
62+ /// </summary>
63+ /// <value>
64+ /// The name of the master page template file
65+ /// </value>
66+ public string DefaultMasterTemplateFileName { get ; private set ; } = "Master.tpl" ;
67+
68+ /// <summary>
69+ /// Gets or sets the master template main content variable name.
70+ /// </summary>
71+ /// <value>
72+ /// The master template main content variable name.
73+ /// </value>
74+ public string DefaultMainContentVariableName { get ; private set ; } = "MainContent" ;
75+
76+ /// <summary>
77+ /// Gets or sets the master template title variable name.
78+ /// </summary>
79+ /// <value>
80+ /// The title variable name.
81+ /// </value>
82+ public string DefaultTitleVariableName { get ; private set ; } = "Title" ;
83+
84+ /// <summary>
85+ /// Default site style
86+ /// </summary>
87+ public string DefaultStyle { get ; private set ; } = "Main" ;
88+
89+ /// <summary>
90+ /// Data directory path, default value is "App_Data"
91+ /// </summary>
92+ public string DataPath { get ; private set ; } = "App_Data" ;
93+
94+ /// <summary>
95+ /// Gets a value indicating whether Simplify.Web static files processing is enabled or controllers requests should be processed only
96+ /// </summary>
97+ public bool StaticFilesEnabled { get ; private set ; } = true ;
98+
99+ /// <summary>
100+ /// Gets the static files paths.
101+ /// </summary>
102+ /// <value>
103+ /// The static files paths.
104+ /// </value>
105+ public IList < string > StaticFilesPaths { get ; }
106+ = new List < string > { "styles" , "scripts" , "images" , "content" , "fonts" } ;
107+
108+ /// <summary>
109+ /// Gets the string table files.
110+ /// </summary>
111+ /// <value>
112+ /// The string table files.
113+ /// </value>
114+ public IList < string > StringTableFiles { get ; }
115+ = new List < string > { "StringTable.xml" } ;
116+
117+ /// <summary>
118+ /// Gets or sets a value indicating whether site title postfix should be set automatically
119+ /// </summary>
120+ public bool DisableAutomaticSiteTitleSet { get ; private set ; }
121+
122+ /// <summary>
123+ /// Gets a value indicating whether exception details should be hide when some unhandled exception occured.
124+ /// </summary>
125+ public bool HideExceptionDetails { get ; private set ; }
126+
127+ /// <summary>
128+ /// Gets a value indicating whether templates memory cache enabled or disabled.
129+ /// </summary>
130+ /// <value>
131+ /// <c>true</c> if templates memory cache enabled; otherwise, <c>false</c>.
132+ /// </value>
133+ public bool TemplatesMemoryCache { get ; private set ; }
134+
135+ /// <summary>
136+ /// Gets a value indicating whether string table memory cache enabled or disabled.
137+ /// </summary>
138+ /// <value>
139+ /// <c>true</c> if string table memory cache enabled; otherwise, <c>false</c>.
140+ /// </value>
141+ public bool StringTableMemoryCache { get ; private set ; }
142+
143+ /// <summary>
144+ /// Gets a value indicating whether file reader caching should be disable.
145+ /// </summary>
146+ public bool DisableFileReaderCache { get ; private set ; }
147+
148+ /// <summary>
149+ /// Gets a value indicating whether console tracing is enabled.
150+ /// </summary>
151+ /// <value>
152+ /// <c>true</c> if console tracing is enabled; otherwise, <c>false</c>.
153+ /// </value>
154+ public bool ConsoleTracing { get ; private set ; }
155+
156+ private void LoadLanguageManagerSettings ( IConfiguration config )
157+ {
158+ var defaultLanguage = config [ "DefaultLanguage" ] ;
159+
160+ if ( ! string . IsNullOrEmpty ( defaultLanguage ) )
161+ DefaultLanguage = defaultLanguage ;
162+
163+ var acceptBrowserLanguage = config [ "AcceptBrowserLanguage" ] ;
164+
165+ if ( string . IsNullOrEmpty ( acceptBrowserLanguage ) )
166+ return ;
167+
168+ if ( bool . TryParse ( acceptBrowserLanguage , out var buffer ) )
169+ AcceptBrowserLanguage = buffer ;
170+ }
171+
172+ private void LoadTemplatesSettings ( IConfiguration config )
173+ {
174+ var defaultTemplatesPath = config [ "DefaultTemplatesPath" ] ;
175+
176+ if ( ! string . IsNullOrEmpty ( defaultTemplatesPath ) )
177+ DefaultTemplatesPath = defaultTemplatesPath ;
178+
179+ var loadTemplatesFromAssembly = config [ "LoadTemplatesFromAssembly" ] ;
180+
181+ if ( ! string . IsNullOrEmpty ( loadTemplatesFromAssembly ) )
182+ {
183+ if ( bool . TryParse ( loadTemplatesFromAssembly , out var buffer ) )
184+ LoadTemplatesFromAssembly = buffer ;
185+ }
186+
187+ var defaultMasterTemplateFileName = config [ "DefaultMasterTemplateFileName" ] ;
188+
189+ if ( ! string . IsNullOrEmpty ( defaultMasterTemplateFileName ) )
190+ DefaultMasterTemplateFileName = defaultMasterTemplateFileName ;
191+ }
192+
193+ private void LoadDataCollectorSettings ( IConfiguration config )
194+ {
195+ var defaultMainContentVariableName = config [ "DefaultMainContentVariableName" ] ;
196+
197+ if ( ! string . IsNullOrEmpty ( defaultMainContentVariableName ) )
198+ DefaultMainContentVariableName = defaultMainContentVariableName ;
199+
200+ var defaultTitleVariableName = config [ "DefaultTitleVariableName" ] ;
201+
202+ if ( ! string . IsNullOrEmpty ( defaultTitleVariableName ) )
203+ DefaultTitleVariableName = defaultTitleVariableName ;
204+ }
205+
206+ private void LoadStyleSettings ( IConfiguration config )
207+ {
208+ var defaultStyle = config [ "DefaultStyle" ] ;
209+
210+ if ( ! string . IsNullOrEmpty ( defaultStyle ) )
211+ DefaultStyle = defaultStyle ;
212+ }
213+
214+ private void LoadOtherSettings ( IConfiguration config )
215+ {
216+ var dataPath = config [ "DataPath" ] ;
217+
218+ if ( ! string . IsNullOrEmpty ( dataPath ) )
219+ DataPath = dataPath ;
220+
221+ var staticFilesEnabled = config [ "StaticFilesEnabled" ] ;
222+
223+ if ( ! string . IsNullOrEmpty ( staticFilesEnabled ) )
224+ {
225+ if ( bool . TryParse ( staticFilesEnabled , out var buffer ) )
226+ StaticFilesEnabled = buffer ;
227+ }
228+
229+ var staticFilesPaths = config [ "StaticFilesPaths" ] ;
230+
231+ if ( ! string . IsNullOrEmpty ( staticFilesPaths ) )
232+ {
233+ StaticFilesPaths . Clear ( ) ;
234+ var items = staticFilesPaths . Replace ( " " , "" ) . Split ( new [ ] { ',' } , StringSplitOptions . RemoveEmptyEntries ) ;
235+
236+ foreach ( var item in items )
237+ StaticFilesPaths . Add ( item ) ;
238+ }
239+
240+ var stringTableFiles = config [ "StringTableFiles" ] ;
241+
242+ if ( string . IsNullOrEmpty ( stringTableFiles ) )
243+ return ;
244+
245+ {
246+ StringTableFiles . Clear ( ) ;
247+ var items = stringTableFiles . Replace ( " " , "" ) . Split ( new [ ] { ',' } , StringSplitOptions . RemoveEmptyEntries ) ;
248+
249+ foreach ( var item in items )
250+ StringTableFiles . Add ( item ) ;
251+ }
252+ }
253+
254+ private void LoadEngineBehaviorSettings ( IConfiguration config )
255+ {
256+ var disableAutomaticSiteTitleSet = config [ "DisableAutomaticSiteTitleSet" ] ;
257+
258+ if ( ! string . IsNullOrEmpty ( disableAutomaticSiteTitleSet ) )
259+ {
260+ if ( bool . TryParse ( disableAutomaticSiteTitleSet , out var buffer ) )
261+ DisableAutomaticSiteTitleSet = buffer ;
262+ }
263+
264+ var hideExceptionDetails = config [ "HideExceptionDetails" ] ;
265+
266+ if ( string . IsNullOrEmpty ( hideExceptionDetails ) )
267+ return ;
268+
269+ {
270+ if ( bool . TryParse ( hideExceptionDetails , out var buffer ) )
271+ HideExceptionDetails = buffer ;
272+ }
273+ }
274+
275+ private void LoadCacheSettings ( IConfiguration config )
276+ {
277+ var templatesMemoryCache = config [ "TemplatesMemoryCache" ] ;
278+
279+ if ( ! string . IsNullOrEmpty ( templatesMemoryCache ) )
280+ {
281+ if ( bool . TryParse ( templatesMemoryCache , out var buffer ) )
282+ TemplatesMemoryCache = buffer ;
283+ }
284+
285+ var stringTableMemoryCache = config [ "StringTableMemoryCache" ] ;
286+
287+ if ( ! string . IsNullOrEmpty ( stringTableMemoryCache ) )
288+ {
289+ if ( bool . TryParse ( stringTableMemoryCache , out var buffer ) )
290+ StringTableMemoryCache = buffer ;
291+ }
292+
293+ var disableFileReaderCache = config [ "DisableFileReaderCache" ] ;
294+
295+ if ( string . IsNullOrEmpty ( disableFileReaderCache ) )
296+ return ;
297+
298+ {
299+ if ( bool . TryParse ( disableFileReaderCache , out var buffer ) )
300+ DisableFileReaderCache = buffer ;
301+ }
302+ }
303+
304+ private void LoadDiagnosticSettings ( IConfiguration config )
305+ {
306+ var consoleTracing = config [ "ConsoleTracing" ] ;
307+
308+ if ( string . IsNullOrEmpty ( consoleTracing ) )
309+ return ;
310+
311+ if ( bool . TryParse ( consoleTracing , out var buffer ) )
312+ ConsoleTracing = buffer ;
313+ }
314+ }
315+ }
0 commit comments