Skip to content

Commit

Permalink
Ensure the compiled view directory exists
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamCampbell committed Mar 26, 2021
1 parent 90fbaa7 commit 545875f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/Illuminate/View/Compilers/BladeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,11 @@ public function compile($path = null)
$contents = $this->appendFilePath($contents);
}

$this->files->put(
$this->getCompiledPath($this->getPath()), $contents
);
$compiledPath = $this->getCompiledPath($this->getPath());

$this->ensureCompiledDirectoryExists($compiledPath);

This comment has been minimized.

Copy link
@4m1n0s

4m1n0s Mar 30, 2021

Contributor

there is a method on Filesystem to ensure that a directory exists, i wonder why you didn't use it ?

$this->files->ensureDirectoryExists($compiledPath, 0777);

This comment has been minimized.

Copy link
@GrahamCampbell

GrahamCampbell Mar 30, 2021

Author Member

We just did the exact same code as in the cache filestore.


$this->files->put($compiledPath, $contents);
}
}

Expand Down
13 changes: 13 additions & 0 deletions src/Illuminate/View/Compilers/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,17 @@ public function isExpired($path)
return $this->files->lastModified($path) >=
$this->files->lastModified($compiled);
}

/**
* Create the file compiled directory if necessary.
*
* @param string $path
* @return void
*/
protected function ensureCompiledDirectoryExists($path)
{
if (! $this->files->exists(dirname($path))) {
$this->files->makeDirectory(dirname($path), 0777, true, true);
}
}
}
16 changes: 16 additions & 0 deletions tests/View/ViewBladeCompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ public function testCompileCompilesFileAndReturnsContents()
{
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
$files->shouldReceive('get')->once()->with('foo')->andReturn('Hello World');
$files->shouldReceive('exists')->once()->with(__DIR__)->andReturn(true);
$files->shouldReceive('put')->once()->with(__DIR__.'/'.sha1('foo').'.php', 'Hello World<?php /**PATH foo ENDPATH**/ ?>');
$compiler->compile('foo');
}

public function testCompileCompilesFileAndReturnsContentsCreatingDirectory()
{
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
$files->shouldReceive('get')->once()->with('foo')->andReturn('Hello World');
$files->shouldReceive('exists')->once()->with(__DIR__)->andReturn(false);
$files->shouldReceive('makeDirectory')->once()->with(__DIR__, 0777, true, true);
$files->shouldReceive('put')->once()->with(__DIR__.'/'.sha1('foo').'.php', 'Hello World<?php /**PATH foo ENDPATH**/ ?>');
$compiler->compile('foo');
}
Expand All @@ -57,6 +68,7 @@ public function testCompileCompilesAndGetThePath()
{
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
$files->shouldReceive('get')->once()->with('foo')->andReturn('Hello World');
$files->shouldReceive('exists')->once()->with(__DIR__)->andReturn(true);
$files->shouldReceive('put')->once()->with(__DIR__.'/'.sha1('foo').'.php', 'Hello World<?php /**PATH foo ENDPATH**/ ?>');
$compiler->compile('foo');
$this->assertSame('foo', $compiler->getPath());
Expand All @@ -73,6 +85,7 @@ public function testCompileWithPathSetBefore()
{
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
$files->shouldReceive('get')->once()->with('foo')->andReturn('Hello World');
$files->shouldReceive('exists')->once()->with(__DIR__)->andReturn(true);
$files->shouldReceive('put')->once()->with(__DIR__.'/'.sha1('foo').'.php', 'Hello World<?php /**PATH foo ENDPATH**/ ?>');
// set path before compilation
$compiler->setPath('foo');
Expand Down Expand Up @@ -103,6 +116,7 @@ public function testIncludePathToTemplate($content, $compiled)
{
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
$files->shouldReceive('get')->once()->with('foo')->andReturn($content);
$files->shouldReceive('exists')->once()->with(__DIR__)->andReturn(true);
$files->shouldReceive('put')->once()->with(__DIR__.'/'.sha1('foo').'.php', $compiled);

$compiler->compile('foo');
Expand Down Expand Up @@ -157,6 +171,7 @@ public function testDontIncludeEmptyPath()
{
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
$files->shouldReceive('get')->once()->with('')->andReturn('Hello World');
$files->shouldReceive('exists')->once()->with(__DIR__)->andReturn(true);
$files->shouldReceive('put')->once()->with(__DIR__.'/'.sha1('').'.php', 'Hello World');
$compiler->setPath('');
$compiler->compile();
Expand All @@ -166,6 +181,7 @@ public function testDontIncludeNullPath()
{
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
$files->shouldReceive('get')->once()->with(null)->andReturn('Hello World');
$files->shouldReceive('exists')->once()->with(__DIR__)->andReturn(true);
$files->shouldReceive('put')->once()->with(__DIR__.'/'.sha1(null).'.php', 'Hello World');
$compiler->setPath(null);
$compiler->compile();
Expand Down

0 comments on commit 545875f

Please sign in to comment.