Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Providers/ViewServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ public function registerBladeDirectives()

public function boot()
{
ViewFactory::addNamespace('compiled__views', storage_path('framework/views'));
ViewFactory::addNamespace('compiled__views', storage_path('statamic/tmp/nocache'));

Region::preserveContextKeys(IncludeTag::VIEW_DATA_KEYS);

Expand Down
2 changes: 1 addition & 1 deletion src/StaticCaching/NoCache/StringFragment.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct($region, $contents, $extension, $data)
$this->contents = $contents;
$this->extension = $extension;
$this->data = $data;
$this->directory = config('view.compiled').'/nocache';
$this->directory = storage_path('statamic/tmp/nocache');
}

public function render(): string
Expand Down
3 changes: 2 additions & 1 deletion src/View/Blade/AntlersBladePrecompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Statamic\View\Blade;

use Illuminate\Support\Str;
use Statamic\Facades\File;

class AntlersBladePrecompiler
{
Expand All @@ -27,7 +28,7 @@ public static function compile(string $content): string
$contentHash = sha1($innerContent);
$fileName = 'antlers_'.$contentHash;

file_put_contents(storage_path('framework/views/'.$fileName.'.antlers.html'), $innerContent);
File::put(storage_path('statamic/tmp/nocache/'.$fileName.'.antlers.html'), $innerContent);

$content = str_replace($original, '@include(\'compiled__views::'.$fileName.'\')', $content);
}
Expand Down
5 changes: 3 additions & 2 deletions src/View/Blade/Concerns/CompilesNocache.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Statamic\View\Blade\Concerns;

use Statamic\Facades\File;
use Statamic\View\Blade\StatamicTagCompiler;
use Stillat\BladeParser\Nodes\Components\ComponentNode;

Expand All @@ -11,8 +12,8 @@ protected function compileNocache(ComponentNode $component): string
{
$compiled = (new StatamicTagCompiler())->compile($component->innerDocumentContent);
$viewName = '_nocache'.sha1($compiled);
$path = storage_path('framework/views/'.$viewName.'.blade.php');
file_put_contents($path, $compiled);
$path = storage_path('statamic/tmp/nocache/'.$viewName.'.blade.php');
File::put($path, $compiled);

return '@nocache(\'compiled__views::'.$viewName.'\')';
}
Expand Down
4 changes: 3 additions & 1 deletion tests/StaticCaching/IncludeNocacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ private function setUpViews(string $layout, array $views)

private function setUpBladeViews(array $views)
{
app('files')->cleanDirectory(config('view.compiled'));

foreach ($views as $name => $contents) {
$this->viewShouldReturnRaw($name, $contents, 'blade.php');
}

view()->addNamespace('compiled__views', storage_path('framework/views'));
view()->addNamespace('compiled__views', storage_path('statamic/tmp/nocache'));
}

private function renderTwice(string $layout, array $views): array
Expand Down
28 changes: 28 additions & 0 deletions tests/StaticCaching/StringFragmentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Tests\StaticCaching;

use PHPUnit\Framework\Attributes\Test;
use Statamic\StaticCaching\NoCache\StringFragment;
use Tests\TestCase;

class StringFragmentTest extends TestCase
{
#[Test]
public function it_writes_temporary_views_to_statamic_storage()
{
$compiledViewDirectory = storage_path('framework/views/string-fragment-'.uniqid());

config()->set('view.compiled', $compiledViewDirectory);

try {
$fragment = new StringFragment('test-region', 'Hello {{ name }}', 'antlers.html', ['name' => 'World']);

$this->assertSame('Hello World', $fragment->render());
$this->assertDirectoryExists(storage_path('statamic/tmp/nocache'));
$this->assertDirectoryDoesNotExist($compiledViewDirectory.'/nocache');
} finally {
app('files')->deleteDirectory($compiledViewDirectory);
}
}
}
33 changes: 33 additions & 0 deletions tests/View/Blade/NocacheStorageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Tests\View\Blade;

use PHPUnit\Framework\Attributes\Test;
use Statamic\View\Blade\AntlersBladePrecompiler;
use Statamic\View\Blade\StatamicTagCompiler;
use Tests\TestCase;

class NocacheStorageTest extends TestCase
{
#[Test]
public function it_stores_compiled_nocache_components_in_statamic_storage()
{
$compiled = (new StatamicTagCompiler())->compile('<s:nocache>Hello</s:nocache>');
$viewName = '_nocache'.sha1('Hello');

$this->assertSame("@nocache('compiled__views::{$viewName}')", $compiled);
$this->assertSame('Hello', file_get_contents(storage_path("statamic/tmp/nocache/{$viewName}.blade.php")));
}

#[Test]
public function it_stores_precompiled_antlers_views_in_statamic_storage()
{
$antlers = 'Hello {{ name }}';
$viewName = 'antlers_'.sha1($antlers);

$compiled = AntlersBladePrecompiler::compile("@antlers{$antlers}@endantlers");

$this->assertSame("@include('compiled__views::{$viewName}')", $compiled);
$this->assertSame($antlers, file_get_contents(storage_path("statamic/tmp/nocache/{$viewName}.antlers.html")));
}
}
Loading