1+ <?php
2+ namespace Neuron \Mvc \Cache ;
3+
4+ use Neuron \Data \Setting \Source \ISettingSource ;
5+
6+ class CacheConfig
7+ {
8+ private array $ _Settings ;
9+
10+ /**
11+ * CacheConfig constructor
12+ *
13+ * @param array $Settings
14+ */
15+ public function __construct ( array $ Settings )
16+ {
17+ $ this ->_Settings = $ Settings ;
18+ }
19+
20+ /**
21+ * Check if cache is enabled
22+ *
23+ * @return bool
24+ */
25+ public function isEnabled (): bool
26+ {
27+ return $ this ->_Settings ['enabled ' ] ?? false ;
28+ }
29+
30+ /**
31+ * Get cache path
32+ *
33+ * @return string
34+ */
35+ public function getCachePath (): string
36+ {
37+ return $ this ->_Settings ['path ' ] ?? 'cache/views ' ;
38+ }
39+
40+ /**
41+ * Get default TTL
42+ *
43+ * @return int
44+ */
45+ public function getDefaultTtl (): int
46+ {
47+ return $ this ->_Settings ['ttl ' ] ?? 3600 ;
48+ }
49+
50+ /**
51+ * Get storage type
52+ *
53+ * @return string
54+ */
55+ public function getStorageType (): string
56+ {
57+ return $ this ->_Settings ['storage ' ] ?? 'file ' ;
58+ }
59+
60+ /**
61+ * Check if specific view type caching is enabled
62+ *
63+ * @param string $ViewType
64+ * @return bool
65+ */
66+ public function isViewTypeEnabled ( string $ ViewType ): bool
67+ {
68+ $ ViewSettings = $ this ->_Settings ['views ' ] ?? [];
69+
70+ return $ ViewSettings [$ ViewType ] ?? true ;
71+ }
72+
73+ /**
74+ * Create CacheConfig from settings source
75+ *
76+ * @param ISettingSource $Settings
77+ * @return self
78+ */
79+ public static function fromSettings ( ISettingSource $ Settings ): self
80+ {
81+ $ CacheSettings = [];
82+
83+ $ Enabled = $ Settings ->get ( 'cache ' , 'enabled ' );
84+ if ( $ Enabled !== null )
85+ {
86+ $ CacheSettings ['enabled ' ] = $ Enabled === 'true ' || $ Enabled === '1 ' ;
87+ }
88+
89+ $ Path = $ Settings ->get ( 'cache ' , 'path ' );
90+ if ( $ Path !== null )
91+ {
92+ $ CacheSettings ['path ' ] = $ Path ;
93+ }
94+
95+ $ Ttl = $ Settings ->get ( 'cache ' , 'ttl ' );
96+ if ( $ Ttl !== null )
97+ {
98+ $ CacheSettings ['ttl ' ] = (int ) $ Ttl ;
99+ }
100+
101+ $ Storage = $ Settings ->get ( 'cache ' , 'storage ' );
102+ if ( $ Storage !== null )
103+ {
104+ $ CacheSettings ['storage ' ] = $ Storage ;
105+ }
106+
107+ // For views settings, we need to check each view type
108+ $ ViewTypes = [ 'html ' , 'markdown ' , 'json ' , 'xml ' ];
109+ $ ViewSettings = [];
110+
111+ foreach ( $ ViewTypes as $ ViewType )
112+ {
113+ $ ViewEnabled = $ Settings ->get ( 'views ' , $ ViewType );
114+ if ( $ ViewEnabled !== null )
115+ {
116+ $ ViewSettings [$ ViewType ] = $ ViewEnabled === 'true ' || $ ViewEnabled === '1 ' ;
117+ }
118+ }
119+
120+ if ( !empty ( $ ViewSettings ) )
121+ {
122+ $ CacheSettings ['views ' ] = $ ViewSettings ;
123+ }
124+
125+ return new self ( $ CacheSettings );
126+ }
127+ }
0 commit comments