-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 500c0c1
Showing
3 changed files
with
207 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/vendor | ||
composer.phar | ||
composer.lock | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "philo/laravel-blade-standalone", | ||
"description": "Use the simple and yet powerful Laravel Blade templating engine as a standalone component.", | ||
"keywords": ["laravel", "blade"], | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Philo Hermans", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"illuminate/view": "4.1.*" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Philo\\Blade\\": "src" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
<?php namespace Philo\Blade; | ||
|
||
use Illuminate\Container\Container; | ||
use Illuminate\Events\Dispatcher; | ||
use Illuminate\Filesystem\Filesystem; | ||
use Illuminate\Support\MessageBag; | ||
use Illuminate\Support\ServiceProvider; | ||
use Illuminate\View\Engines\PhpEngine; | ||
use Illuminate\View\Engines\CompilerEngine; | ||
use Illuminate\View\Engines\EngineResolver; | ||
use Illuminate\View\Compilers\BladeCompiler; | ||
use Illuminate\View\FileViewFinder; | ||
use Illuminate\View\Environment; | ||
|
||
class Blade { | ||
|
||
/** | ||
* Array containg paths where to look for blade files | ||
* @var array | ||
*/ | ||
protected $viewPaths; | ||
|
||
/** | ||
* Location where to store cached views | ||
* @var string | ||
*/ | ||
protected $cachePath; | ||
|
||
/** | ||
* @var Illuminate\Container\Container | ||
*/ | ||
protected $container; | ||
|
||
/** | ||
* @var Illuminate\View\Environment | ||
*/ | ||
protected $instance; | ||
|
||
/** | ||
* Initialize class | ||
* @param array $viewPaths | ||
* @param string $cachePath | ||
*/ | ||
function __construct($viewPaths = array(), $cachePath) { | ||
|
||
$this->container = new Container; | ||
|
||
$this->viewPaths = (array) $viewPaths; | ||
|
||
$this->cachePath = $cachePath; | ||
|
||
$this->registerFilesystem(); | ||
|
||
$this->registerEvents(); | ||
|
||
$this->registerEngineResolver(); | ||
|
||
$this->registerViewFinder(); | ||
|
||
$this->instance = $this->registerEnvironment(); | ||
} | ||
|
||
public function view() | ||
{ | ||
return $this->instance; | ||
} | ||
|
||
public function registerFilesystem() | ||
{ | ||
$this->container->bindShared('files', function(){ | ||
return new Filesystem; | ||
}); | ||
} | ||
public function registerEvents() | ||
{ | ||
$this->container->bindShared('events', function(){ | ||
return new Dispatcher; | ||
}); | ||
} | ||
/** | ||
* Register the engine resolver instance. | ||
* | ||
* @return void | ||
*/ | ||
public function registerEngineResolver() | ||
{ | ||
$me = $this; | ||
|
||
$this->container->bindShared('view.engine.resolver', function($app) use ($me) | ||
{ | ||
$resolver = new EngineResolver; | ||
|
||
// Next we will register the various engines with the resolver so that the | ||
// environment can resolve the engines it needs for various views based | ||
// on the extension of view files. We call a method for each engines. | ||
foreach (array('php', 'blade') as $engine) | ||
{ | ||
$me->{'register'.ucfirst($engine).'Engine'}($resolver); | ||
} | ||
|
||
return $resolver; | ||
}); | ||
} | ||
|
||
/** | ||
* Register the PHP engine implementation. | ||
* | ||
* @param \Illuminate\View\Engines\EngineResolver $resolver | ||
* @return void | ||
*/ | ||
public function registerPhpEngine($resolver) | ||
{ | ||
$resolver->register('php', function() { return new PhpEngine; }); | ||
} | ||
|
||
/** | ||
* Register the Blade engine implementation. | ||
* | ||
* @param \Illuminate\View\Engines\EngineResolver $resolver | ||
* @return void | ||
*/ | ||
public function registerBladeEngine($resolver) | ||
{ | ||
$me = $this; | ||
$app = $this->container; | ||
|
||
// The Compiler engine requires an instance of the CompilerInterface, which in | ||
// this case will be the Blade compiler, so we'll first create the compiler | ||
// instance to pass into the engine so it can compile the views properly. | ||
$this->container->bindShared('blade.compiler', function($app) use ($me) | ||
{ | ||
$cache = $me->cachePath; | ||
|
||
return new BladeCompiler($app['files'], $cache); | ||
}); | ||
|
||
$resolver->register('blade', function() use ($app) | ||
{ | ||
return new CompilerEngine($app['blade.compiler'], $app['files']); | ||
}); | ||
} | ||
|
||
/** | ||
* Register the view finder implementation. | ||
* | ||
* @return void | ||
*/ | ||
public function registerViewFinder() | ||
{ | ||
$me = $this; | ||
$this->container->bindShared('view.finder', function($app) use ($me) | ||
{ | ||
$paths = $me->viewPaths; | ||
|
||
return new FileViewFinder($app['files'], $paths); | ||
}); | ||
} | ||
|
||
/** | ||
* Register the view environment. | ||
* | ||
* @return void | ||
*/ | ||
public function registerEnvironment() | ||
{ | ||
// Next we need to grab the engine resolver instance that will be used by the | ||
// environment. The resolver will be used by an environment to get each of | ||
// the various engine implementations such as plain PHP or Blade engine. | ||
$resolver = $this->container['view.engine.resolver']; | ||
|
||
$finder = $this->container['view.finder']; | ||
|
||
$env = new Environment($resolver, $finder, $this->container['events']); | ||
|
||
// We will also set the container instance on this view environment since the | ||
// view composers may be classes registered in the container, which allows | ||
// for great testable, flexible composers for the application developer. | ||
$env->setContainer($this->container); | ||
|
||
return $env; | ||
} | ||
|
||
} |