Skip to content

Commit 1f9a53c

Browse files
committed
Merge branch 'release/0.6.39'
2 parents 08c5eec + d1c4708 commit 1f9a53c

File tree

12 files changed

+821
-41
lines changed

12 files changed

+821
-41
lines changed

.version.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
"strategy": "semver",
33
"major": 0,
44
"minor": 6,
5-
"patch": 38,
5+
"patch": 39,
66
"build": 0
7-
}
7+
}

examples/cache-control-example.php

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<?php
2+
/**
3+
* Example: Dynamic Cache Control in MVC Views
4+
*
5+
* This example demonstrates how to dynamically enable or disable
6+
* caching for individual page renders in controller methods.
7+
*/
8+
9+
use Neuron\Mvc\Controllers\Base;
10+
use Neuron\Mvc\Responses\HttpResponseStatus;
11+
use Neuron\Routing\Router;
12+
13+
class BlogController extends Base
14+
{
15+
/**
16+
* Blog listing - can be cached
17+
*/
18+
public function index()
19+
{
20+
$Posts = $this->getRecentPosts();
21+
22+
// Enable caching for the blog listing page
23+
return $this->renderHtml(
24+
HttpResponseStatus::OK,
25+
[ 'posts' => $Posts ],
26+
'index',
27+
'blog',
28+
true // Cache enabled
29+
);
30+
}
31+
32+
/**
33+
* User dashboard - should not be cached
34+
*/
35+
public function dashboard()
36+
{
37+
$User = $this->getCurrentUser();
38+
$PersonalizedData = $this->getUserDashboardData( $User );
39+
40+
// Disable caching for user-specific content
41+
return $this->renderHtml(
42+
HttpResponseStatus::OK,
43+
[
44+
'user' => $User,
45+
'data' => $PersonalizedData
46+
],
47+
'dashboard',
48+
'default',
49+
false // Cache explicitly disabled
50+
);
51+
}
52+
53+
/**
54+
* Static about page - force cache even if globally disabled
55+
*/
56+
public function about()
57+
{
58+
$Content = $this->getStaticContent( 'about' );
59+
60+
// Force caching for static content
61+
return $this->renderHtml(
62+
HttpResponseStatus::OK,
63+
[ 'content' => $Content ],
64+
'about',
65+
'static',
66+
true // Force cache even if globally disabled
67+
);
68+
}
69+
70+
/**
71+
* Search results - let global config decide
72+
*/
73+
public function search()
74+
{
75+
$Query = $_GET['q'] ?? '';
76+
$Results = $this->searchPosts( $Query );
77+
78+
// Use default cache behavior (null = use global config)
79+
return $this->renderHtml(
80+
HttpResponseStatus::OK,
81+
[
82+
'query' => $Query,
83+
'results' => $Results
84+
],
85+
'search',
86+
'default'
87+
// No cache parameter = uses global configuration
88+
);
89+
}
90+
91+
/**
92+
* Markdown documentation - cached
93+
*/
94+
public function docs()
95+
{
96+
$Page = $_GET['page'] ?? 'index';
97+
98+
// Enable caching for documentation pages
99+
return $this->renderMarkdown(
100+
HttpResponseStatus::OK,
101+
[ 'page' => $Page ],
102+
$Page,
103+
'docs',
104+
true // Cache enabled for docs
105+
);
106+
}
107+
108+
// Mock methods for example
109+
private function getRecentPosts() { return []; }
110+
private function getCurrentUser() { return null; }
111+
private function getUserDashboardData( $User ) { return []; }
112+
private function getStaticContent( $Page ) { return ''; }
113+
private function searchPosts( $Query ) { return []; }
114+
}
115+
116+
/**
117+
* Usage Summary:
118+
*
119+
* 1. Pass `true` as the 5th parameter to force caching
120+
* 2. Pass `false` to disable caching for that specific render
121+
* 3. Pass `null` or omit the parameter to use global cache configuration
122+
*
123+
* This allows fine-grained control over caching on a per-page basis,
124+
* useful for mixing static and dynamic content in the same application.
125+
*/

src/Bootstrap.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
<?php
2+
namespace Neuron\Mvc;
3+
24
use Neuron\Data\Filter\Get;
35
use Neuron\Data\Filter\Server;
46
use Neuron\Data\Object\Version;
57
use Neuron\Data\Setting\Source\Yaml;
6-
use Neuron\Mvc\Application;
78

89
/**
910
* Initialize the application.
1011
*
1112
* @param string $ConfigPath
1213
* @return Application
13-
* @throws Exception
14+
* @throws \Exception
1415
*/
1516

1617
function Boot( string $ConfigPath ) : Application
@@ -22,7 +23,7 @@ function Boot( string $ConfigPath ) : Application
2223
$Settings = new Yaml( "$ConfigPath/config.yaml" );
2324
$BasePath = $Settings->get( 'system', 'base_path' );
2425
}
25-
catch( Exception $e )
26+
catch( \Exception $e )
2627
{
2728
$Settings = null;
2829
$BasePath = getenv( 'SYSTEM_BASE_PATH' ) ? : '.';
@@ -55,7 +56,7 @@ function Dispatch( Application $App ) : void
5556
]
5657
);
5758
}
58-
catch( Exception $e )
59+
catch( \Exception $e )
5960
{
6061
echo 'Ouch.';
6162
}

src/Mvc/Controllers/Base.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,31 @@ public function __construct( Router $Router )
2525
* @throws \Neuron\Core\Exceptions\NotFound
2626
* @throws CommonMarkException
2727
*/
28-
public function renderMarkdown( HttpResponseStatus $ResponseCode, array $Data = [], string $Page = "index", string $Layout = "default" ) : string
28+
public function renderMarkdown( HttpResponseStatus $ResponseCode, array $Data = [], string $Page = "index", string $Layout = "default", ?bool $CacheEnabled = null ) : string
2929
{
3030
@http_response_code( $ResponseCode->value );
3131

3232
$View = ( new Markdown() )
3333
->setController( (new \ReflectionClass( static::class ))->getShortName() )
3434
->setLayout( $Layout )
35-
->setPage( $Page );
35+
->setPage( $Page )
36+
->setCacheEnabled( $CacheEnabled );
3637

3738
return $View->render( $Data );
3839
}
3940

4041
/**
4142
* @throws \Neuron\Core\Exceptions\NotFound
4243
*/
43-
public function renderHtml( HttpResponseStatus $ResponseCode, array $Data = [], string $Page = "index", string $Layout = "default" ) : string
44+
public function renderHtml( HttpResponseStatus $ResponseCode, array $Data = [], string $Page = "index", string $Layout = "default", ?bool $CacheEnabled = null ) : string
4445
{
4546
@http_response_code( $ResponseCode->value );
4647

4748
$View = ( new Html() )
4849
->setController( (new \ReflectionClass( static::class ))->getShortName() )
4950
->setLayout( $Layout )
50-
->setPage( $Page );
51+
->setPage( $Page )
52+
->setCacheEnabled( $CacheEnabled );
5153

5254
return $View->render( $Data );
5355
}

src/Mvc/Controllers/IController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ interface IController
99
{
1010
public function __construct( Router $Router );
1111

12-
public function renderHtml( HttpResponseStatus $ResponseCode, array $Data = [], string $Page = "index", string $Layout = "default" ) : string;
12+
public function renderHtml( HttpResponseStatus $ResponseCode, array $Data = [], string $Page = "index", string $Layout = "default", ?bool $CacheEnabled = null ) : string;
1313
public function renderJson( HttpResponseStatus $ResponseCode, array $Data = [] ) : string;
1414
public function renderXml( HttpResponseStatus $ResponseCode, array $Data = [] ) : string;
1515
}

src/Mvc/Views/Base.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class Base
77
private string $_Layout;
88
private string $_Controller;
99
private string $_Page;
10+
private ?bool $_CacheEnabled = null;
1011

1112
public function __construct()
1213
{}
@@ -65,4 +66,26 @@ public function setPage( string $Page ): Base
6566
return $this;
6667
}
6768

69+
/**
70+
* Get cache enabled setting for this view instance
71+
*
72+
* @return bool|null null means use global config, true/false overrides global
73+
*/
74+
public function getCacheEnabled(): ?bool
75+
{
76+
return $this->_CacheEnabled;
77+
}
78+
79+
/**
80+
* Set cache enabled setting for this view instance
81+
*
82+
* @param bool|null $CacheEnabled null uses global config, true/false overrides
83+
* @return Base
84+
*/
85+
public function setCacheEnabled( ?bool $CacheEnabled ): Base
86+
{
87+
$this->_CacheEnabled = $CacheEnabled;
88+
return $this;
89+
}
90+
6891
}

0 commit comments

Comments
 (0)