Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Blade implementation (again), closes #1769 #1777

Merged
merged 5 commits into from
Dec 18, 2016
Merged
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
6 changes: 3 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
### HEAD
* Add Blade ([#1765](https://github.com/roots/sage/pull/1765))
* Add Blade ([#1765](https://github.com/roots/sage/pull/1765) and [#1777](https://github.com/roots/sage/pull/1777))
* Remove sidebar defaults ([#1760](https://github.com/roots/sage/pull/1760))
* Remove post formats ([#1759](https://github.com/roots/sage/pull/1759))

### 9.0.0-alpha.4: November 16th, 2016
* Use new webpack api schema ([8ac5f15](https://github.com/roots/sage/commit/e6e60aa))
* Update dependencies ([70ebba7](https://github.com/roots/sage/commit/70ebba7))
* Variables organization ([8ac5f15](https://github.com/roots/sage/commit/8ac5f15))
* Use $.fn.ready() (reverts 724d550) ([e7fccbe](https://github.com/roots/sage/commit/e7fccbe))
* Use `$.fn.ready()` (reverts [724d550](https://github.com/roots/sage/commit/724d550)) ([e7fccbe](https://github.com/roots/sage/commit/e7fccbe))
* Theme activation updates 'stylesheet' option instead of 'template' ([fb19145](https://github.com/roots/sage/commit/fb19145))
* Reorganize and refactor build routine ([8c9ba05](https://github.com/roots/sage/commit/8c9ba05))
* Switch assets manifest plugin ([c1bb2b3](https://github.com/roots/sage/commit/c1bb2b3))
* Add images to assets manifest ([c49793c](https://github.com/roots/sage/commit/c49793c))
* Switch from babel to buble ([0d38ab8](https://github.com/roots/sage/commit/0d38ab8))
* Update dependencies & webpack compatibility (BREAKING CHANGES) ([eae52fd](https://github.com/roots/sage/commit/eae52fd))
* Update dependencies & webpack compatibility ([eae52fd](https://github.com/roots/sage/commit/eae52fd))
* Use http by default (not https) to be consistent with Trellis ([e6f2f49](https://github.com/roots/sage/commit/e6f2f49))

### 9.0.0-alpha.3: September 11th, 2016
Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
"require": {
"php": ">=5.6.4",
"composer/installers": "~1.0",
"illuminate/view": "~5.3.0",
"jenssegers/blade": "dev-master#59ba2cc"
"illuminate/view": "~5.3.0"
},
"require-dev": {
"squizlabs/php_codesniffer": "^2.5.1",
Expand Down
90 changes: 20 additions & 70 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions src/filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
array_map(function ($type) {
add_filter("{$type}_template_hierarchy", function ($templates) {
return call_user_func_array('array_merge', array_map(function ($template) {
$normalizedTemplate = str_replace('.', '/', sage('blade')->normalizeViewPath($template));
$normalizedTemplate = preg_replace('%(\.blade)?(\.php)?$%', '', $template);
return ["{$normalizedTemplate}.blade.php", "{$normalizedTemplate}.php"];
}, $templates));
});
Expand All @@ -47,11 +47,14 @@
* Render page using Blade
*/
add_filter('template_include', function ($template) {
echo template($template, apply_filters('sage/template_data', []));
$data = array_reduce(get_body_class(), function ($data, $class) {
return apply_filters("sage/template/{$class}/data", $data);
}, []);
echo template($template, $data);

// Return a blank file to make WordPress happy
return get_template_directory() . '/index.php';
}, 1000);
}, PHP_INT_MAX);

/**
* Tell WordPress how to find the compiled path of comments.blade.php
Expand Down
129 changes: 129 additions & 0 deletions src/lib/Sage/Template/Blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php

namespace Roots\Sage\Template;

use Illuminate\Contracts\Container\Container as ContainerContract;
use Illuminate\Contracts\View\Factory as FactoryContract;
use Illuminate\View\Engines\CompilerEngine;
use Illuminate\View\Engines\EngineInterface;
use Illuminate\View\ViewFinderInterface;

/**
* Class BladeProvider
* @method \Illuminate\View\View file($file, $data = [], $mergeData = [])
* @method \Illuminate\View\View make($file, $data = [], $mergeData = [])
*/
class Blade
{
/** @var ContainerContract */
protected $app;

public function __construct(FactoryContract $env, ContainerContract $app)
{
$this->env = $env;
$this->app = $app;
}

/**
* Get the compiler
*
* @return \Illuminate\View\Compilers\BladeCompiler
*/
public function compiler()
{
static $engineResolver;
if (!$engineResolver) {
$engineResolver = $this->app->make('view.engine.resolver');
}
return $engineResolver->resolve('blade')->getCompiler();
}

/**
* @param string $view
* @param array $data
* @param array $mergeData
* @return string
*/
public function render($view, $data = [], $mergeData = [])
{
/** @var \Illuminate\Contracts\Filesystem\Filesystem $filesystem */
$filesystem = $this->app['files'];
return $this->{$filesystem->exists($view) ? 'file' : 'make'}($view, $data, $mergeData)->render();
}

/**
* @param string $file
* @param array $data
* @param array $mergeData
* @return string
*/
public function compiledPath($file, $data = [], $mergeData = [])
{
$rendered = $this->file($file, $data, $mergeData);
/** @var EngineInterface $engine */
$engine = $rendered->getEngine();

if (!($engine instanceof CompilerEngine)) {
// Using PhpEngine, so just return the file
return $file;
}

$compiler = $engine->getCompiler();
$compiledPath = $compiler->getCompiledPath($rendered->getPath());
if ($compiler->isExpired($compiledPath)) {
$compiler->compile($file);
}
return $compiledPath;
}

/**
* @param string $file
* @return string
*/
public function normalizeViewPath($file)
{
// Convert `\` to `/`
$view = str_replace('\\', '/', $file);

// Add namespace to path if necessary
$view = $this->applyNamespaceToPath($view);

// Remove unnecessary parts of the path
$view = str_replace(array_merge($this->app['config']['view.paths'], ['.blade.php', '.php']), '', $view);

// Remove superfluous and leading slashes
return ltrim(preg_replace('%//+%', '/', $view), '/');
}

/**
* Convert path to view namespace
* @param string $path
* @return string
*/
public function applyNamespaceToPath($path)
{
/** @var ViewFinderInterface $finder */
$finder = $this->app['view.finder'];
if (!method_exists($finder, 'getHints')) {
return $path;
}
$delimiter = $finder::HINT_PATH_DELIMITER;
$hints = $finder->getHints();
$view = array_reduce(array_keys($hints), function ($view, $namespace) use ($delimiter, $hints) {
return str_replace($hints[$namespace], $namespace.$delimiter, $view);
}, $path);
return preg_replace("%{$delimiter}[\\/]*%", $delimiter, $view);
}

/**
* Pass any method to the view Factory instance.
*
* @param string $method
* @param array $params
* @return mixed
*/
public function __call($method, $params)
{
return call_user_func_array([$this->env, $method], $params);
}
}
Loading