Skip to content

Commit 99d07e4

Browse files
committed
fixed partial tests.
1 parent a56c80d commit 99d07e4

File tree

17 files changed

+1327
-25
lines changed

17 files changed

+1327
-25
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ build/
1010
.phpunit.result.cache
1111
/examples/test.log
1212
/examples/bad/test.log
13-
/CLAUDE.md
13+
CLAUDE.md
1414
/cache/
1515
/.claude/

examples/config/config.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ cache:
1212
storage: file
1313
path: cache/views
1414
ttl: 3600
15-
views:
16-
html: true
17-
markdown: true
18-
json: false
19-
xml: false
15+
html: true
16+
markdown: true
17+
json: false
18+
xml: false
19+
2020
# Garbage collection settings (optional, defaults shown)
2121
# gc_probability: 0.01 # 1% chance to run GC on cache write
2222
# gc_divisor: 100 # Used with probability for fine-tuning

examples/config/routes.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ routes:
1111
controller: Mvc\TestController@test
1212
request: test
1313

14+
partial:
15+
route: /partial-test
16+
method: GET
17+
controller: Mvc\TestController@partial
18+
1419
put:
1520
route: /test_put
1621
method: PUT

examples/views/Base/index.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22

33
Content of one:
44
<?php echo $var_one; ?>
5+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php Neuron\Mvc\Partial( 'test' ); ?>

examples/views/shared/_test.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php echo 'poop'; ?>

resources/views/shared/_test.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a test.

src/Bootstrap.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
<?php
22
namespace Neuron\Mvc;
33

4+
use Neuron\Core\Exceptions\NotFound;
45
use Neuron\Data\Filter\Get;
56
use Neuron\Data\Filter\Server;
67
use Neuron\Data\Object\Version;
78
use Neuron\Data\Setting\Source\Yaml;
9+
use Neuron\Patterns\Registry;
810

911
/**
1012
* Initialize the application.
@@ -40,7 +42,6 @@ function Boot( string $ConfigPath ) : Application
4042
*
4143
* @param Application $App
4244
*/
43-
4445
function Dispatch( Application $App ) : void
4546
{
4647
$Route = Get::filterScalar( 'route' ) ?? "";
@@ -72,3 +73,36 @@ function ClearExpiredCache( Application $App ) : int
7273
{
7374
return $App->clearExpiredCache();
7475
}
76+
77+
/**
78+
* Render a partial view from the shared directory.
79+
* This function looks for a file named _{name}.php in the shared views directory.
80+
* @param string $name
81+
* @return void
82+
* @throws NotFound
83+
*/
84+
function Partial( string $name ) : void
85+
{
86+
$Path = Registry::getInstance()
87+
->get( "Views.Path" );
88+
89+
if( !$Path )
90+
{
91+
$BasePath = Registry::getInstance()->get( "Base.Path" );
92+
$Path = "$BasePath/resources/views";
93+
}
94+
95+
$View = "$Path/shared/_$name.php";
96+
97+
if( !file_exists( $View ) )
98+
{
99+
throw new NotFound( "Partial not found: $View" );
100+
}
101+
102+
ob_start();
103+
require( $View );
104+
$Content = ob_get_contents();
105+
ob_end_clean();
106+
107+
echo $Content;
108+
}

src/Mvc/Cache/CacheConfig.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,7 @@ public function getStorageType(): string
6565
*/
6666
public function isViewTypeEnabled( string $ViewType ): bool
6767
{
68-
$ViewSettings = $this->_Settings['views'] ?? [];
69-
70-
return $ViewSettings[$ViewType] ?? true;
68+
return $this->_Settings[$ViewType] ?? true;
7169
}
7270

7371
/**
@@ -106,22 +104,16 @@ public static function fromSettings( ISettingSource $Settings ): self
106104

107105
// For views settings, we need to check each view type
108106
$ViewTypes = [ 'html', 'markdown', 'json', 'xml' ];
109-
$ViewSettings = [];
110107

111108
foreach( $ViewTypes as $ViewType )
112109
{
113-
$ViewEnabled = $Settings->get( 'views', $ViewType );
110+
$ViewEnabled = $Settings->get( 'cache', $ViewType );
114111
if( $ViewEnabled !== null )
115112
{
116-
$ViewSettings[$ViewType] = $ViewEnabled === 'true' || $ViewEnabled === '1';
113+
$CacheSettings[$ViewType] = $ViewEnabled === 'true' || $ViewEnabled === '1';
117114
}
118115
}
119116

120-
if( !empty( $ViewSettings ) )
121-
{
122-
$CacheSettings['views'] = $ViewSettings;
123-
}
124-
125117
// Get GC settings if present
126118
$GcProbability = $Settings->get( 'cache', 'gc_probability' );
127119
if( $GcProbability !== null )
@@ -159,4 +151,4 @@ public function getGcDivisor(): int
159151
// Default divisor for probability calculation
160152
return (int) ( $this->_Settings['gc_divisor'] ?? 100 );
161153
}
162-
}
154+
}

tests/Mvc/ApplicationTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,25 @@ public function testConfig()
5252
);
5353
}
5454

55+
public function testPartial()
56+
{
57+
$this->App->setCaptureOutput( true );
58+
59+
$this->App->run(
60+
[
61+
"type" => "GET",
62+
"route" => "/partial-test"
63+
]
64+
);
65+
66+
$Output = $this->App->getOutput();
67+
68+
$this->assertStringContainsString(
69+
"poop",
70+
$Output
71+
);
72+
}
73+
5574
/**
5675
* @throws \Exception
5776
*/

0 commit comments

Comments
 (0)