Skip to content

Commit

Permalink
Made it possible to add your own FileViewFinder
Browse files Browse the repository at this point in the history
  • Loading branch information
nsrosenqvist committed Mar 20, 2017
1 parent 4690543 commit 3694738
Showing 1 changed file with 31 additions and 8 deletions.
39 changes: 31 additions & 8 deletions src/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,38 @@
use Illuminate\View\Factory;
use Illuminate\View\Engines\EngineResolver;
use Illuminate\View\View;
use Illuminate\View\Engines\FileEngine;
use Illuminate\View\Engines\PhpEngine;
use Illuminate\View\ViewFinderInterface;

class Compiler
{
function __construct($cacheDir = null, array $paths = [])
function __construct($cacheDir = null, $paths = [])
{
$this->paths = $paths;
// Make sure cache directory exists
$this->cacheDir = $cacheDir ?? sys_get_temp_dir().'/blade/views';

if ( ! file_exists($this->cacheDir)) {
mkdir($this->cacheDir, 0755, true);
}

// Register system that Blade depends on
// Register system that Blade depends on, starting with the view finder
$this->filesystem = new Filesystem();
$this->viewFinder = new FileViewFinder($this->filesystem, $this->paths);
$this->resolver = new EngineResolver;

if ($paths instanceof ViewFinderInterface) {
$this->paths = [];
$this->viewFinder = $paths;
}
else {
$this->paths = $paths;
$this->viewFinder = new FileViewFinder($this->filesystem, $this->paths);
}

// Next, we will register the various view engines with the resolver so that the
// environment will resolve the engines needed for various views based on the
// extension of view file.
$this->resolver = new EngineResolver;

$this->resolver->register('file', function () {
return new FileEngine;
});
Expand Down Expand Up @@ -69,9 +81,7 @@ function compile($path, array $data = [])
{
// If the file can't be found it's probably supplied as a template within
// one of the base directories
if ( ! file_exists($path)) {
$path = $this->viewFinder->find($path);
}
$path = $this->find($path);

// Make sure that we use the right resolver for the initial file
$engine = $this->factory->getEngineFromPath($path);
Expand All @@ -86,6 +96,19 @@ function compile($path, array $data = [])
);
}

function find($path) {
if ( ! file_exists($path)) {
$path = $this->viewFinder->find($path);
}

return $path;
}

function compiledPath($path)
{
return $this->compiler->getCompiledPath($this->find($path));
}

function render($path, array $data = [])
{
return $this->compile($path, $data)->render();
Expand Down

0 comments on commit 3694738

Please sign in to comment.