Skip to content

Commit 3481baa

Browse files
committed
Merge branch 'release/0.6.34'
2 parents da52ad5 + 540b404 commit 3481baa

18 files changed

+1288
-2
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ build/
1010
.phpunit.result.cache
1111
/examples/test.log
1212
/examples/bad/test.log
13+
/CLAUDE.md
14+
/cache/

.version.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
"strategy": "semver",
33
"major": 0,
44
"minor": 6,
5-
"patch": 33,
5+
"patch": 34,
66
"build": 0
77
}

examples/config/config.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ logging:
77
views:
88
path: views
99

10+
cache:
11+
enabled: true
12+
storage: file
13+
path: cache/views
14+
ttl: 3600
15+
views:
16+
html: true
17+
markdown: true
18+
json: false
19+
xml: false
20+
1021
system:
1122
timezone: US/Eastern
1223
base_path: examples

src/Mvc/Cache/CacheConfig.php

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
namespace Neuron\Mvc\Cache\Exceptions;
3+
4+
class CacheException extends \Exception
5+
{
6+
/**
7+
* Create exception for unable to write
8+
*
9+
* @param string $Path
10+
* @return self
11+
*/
12+
public static function unableToWrite( string $Path ): self
13+
{
14+
return new self( "Unable to write cache file: $Path" );
15+
}
16+
17+
/**
18+
* Create exception for invalid key
19+
*
20+
* @param string $Key
21+
* @return self
22+
*/
23+
public static function invalidKey( string $Key ): self
24+
{
25+
return new self( "Invalid cache key: $Key" );
26+
}
27+
28+
/**
29+
* Create exception for storage not configured
30+
*
31+
* @return self
32+
*/
33+
public static function storageNotConfigured(): self
34+
{
35+
return new self( "Cache storage is not properly configured" );
36+
}
37+
38+
/**
39+
* Create exception for unable to create directory
40+
*
41+
* @param string $Path
42+
* @return self
43+
*/
44+
public static function unableToCreateDirectory( string $Path ): self
45+
{
46+
return new self( "Unable to create cache directory: $Path" );
47+
}
48+
}

0 commit comments

Comments
 (0)