Refactor Blade implementation. Use Container

This commit is contained in:
QWp6t
2016-12-06 03:28:41 -08:00
parent c69431efa5
commit 0d83ed4b8b
14 changed files with 250 additions and 257 deletions

View File

@@ -1,39 +0,0 @@
<?php
namespace Roots\Sage;
use Roots\Sage\Assets\ManifestInterface;
/**
* Class Template
* @package Roots\Sage
* @author QWp6t
*/
class Asset
{
public static $dist = '/dist';
/** @var ManifestInterface Currently used manifest */
protected $manifest;
protected $asset;
protected $dir;
public function __construct($file, ManifestInterface $manifest = null)
{
$this->manifest = $manifest;
$this->asset = $file;
}
public function __toString()
{
return $this->getUri();
}
public function getUri()
{
$file = ($this->manifest ? $this->manifest->get($this->asset) : $this->asset);
return get_template_directory_uri() . self::$dist . "/$file";
}
}

View File

@@ -10,26 +10,32 @@ namespace Roots\Sage\Assets;
class JsonManifest implements ManifestInterface
{
/** @var array */
protected $manifest = [];
public $manifest;
/** @var string */
public $dist;
/**
* JsonManifest constructor
*
* @param string $manifestPath Local filesystem path to JSON-encoded manifest
* @param string $distUri Remote URI to assets root
*/
public function __construct($manifestPath)
public function __construct($manifestPath, $distUri)
{
$this->manifest = file_exists($manifestPath) ? json_decode(file_get_contents($manifestPath), true) : [];
$this->dist = $distUri;
}
/** @inheritdoc */
public function get($file)
public function get($asset)
{
return isset($this->manifest[$file]) ? $this->manifest[$file] : $file;
return isset($this->manifest[$asset]) ? $this->manifest[$asset] : $asset;
}
/** @inheritdoc */
public function getAll()
public function getUri($asset)
{
return $this->manifest;
return "{$this->dist}/{$this->get($asset)}";
}
}

View File

@@ -12,17 +12,20 @@ interface ManifestInterface
/**
* Get the cache-busted filename
*
* If the manifest does not have an entry for $file, then return $file
* If the manifest does not have an entry for $asset, then return $asset
*
* @param string $file The original name of the file before cache-busting
* @param string $asset The original name of the file before cache-busting
* @return string
*/
public function get($file);
public function get($asset);
/**
* Get the asset manifest
* Get the cache-busted URI
*
* @return array
* If the manifest does not have an entry for $asset, then return URI for $asset
*
* @param string $asset The original name of the file before cache-busting
* @return string
*/
public function getAll();
public function getUri($asset);
}

View File

@@ -0,0 +1,10 @@
<?php
namespace Roots\Sage;
use Illuminate\Container\Container as BaseContainer;
class Container extends BaseContainer
{
}

View File

@@ -0,0 +1,80 @@
<?php
namespace Roots\Sage\Template;
use Jenssegers\Blade\Blade;
use Illuminate\View\Engines\CompilerEngine;
class BladeProvider extends Blade
{
/** @var Blade */
public $blade;
/** @var string */
protected $cachePath;
/**
* @param string $view
* @param array $data
* @param array $mergeData
* @return \Illuminate\View\View
*/
public function make($view, $data = [], $mergeData = [])
{
return $this->container['view']->make($this->normalizeViewPath($view), $data, $mergeData);
}
/**
* @param string $view
* @param array $data
* @param array $mergeData
* @return string
*/
public function render($view, $data = [], $mergeData = [])
{
return $this->make($view, $data, $mergeData)->render();
}
/**
* @param string $file
* @param array $data
* @param array $mergeData
* @return string
*/
public function compiledPath($file, $data = [], $mergeData = [])
{
$rendered = $this->make($file, $data, $mergeData);
$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);
// Remove unnecessary parts of the path
$view = str_replace(array_merge((array) $this->viewPaths, ['.blade.php', '.php']), '', $view);
// Remove leading slashes
$view = ltrim($view, '/');
// Convert `/` to `.`
return str_replace('/', '.', $view);
}
}

View File

@@ -1,132 +0,0 @@
<?php
namespace Roots\Sage\Template;
use Illuminate\View\Factory;
use Illuminate\Events\Dispatcher;
use Illuminate\View\FileViewFinder;
use Illuminate\View\Engines\PhpEngine;
use Illuminate\Support\ServiceProvider;
use Illuminate\View\Engines\CompilerEngine;
use Illuminate\View\Engines\EngineResolver;
use Illuminate\View\Compilers\BladeCompiler;
class ViewServiceProvider extends ServiceProvider
{
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->registerEngineResolver();
$this->registerViewFinder();
$this->registerFactory();
}
/**
* Register the engine resolver instance.
*
* @return void
*/
public function registerEngineResolver()
{
$this->app->singleton('view.engine.resolver', function () {
$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 (['php', 'blade'] as $engine) {
$this->{'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)
{
$app = $this->app;
// 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.
$app->singleton('blade.compiler', function ($app) {
$cache = apply_filters('roots/views/blade/compiled_path', wp_upload_dir()['basedir'] . '/cache/compiled');
if (!file_exists($cache)) {
wp_mkdir_p($cache);
}
return new BladeCompiler($app->make('Illuminate\\Filesystem\\Filesystem'), $cache);
});
$resolver->register('blade', function () use ($app) {
return new CompilerEngine($app['blade.compiler']);
});
}
/**
* Register the view finder implementation.
*
* @return void
*/
public function registerViewFinder()
{
$this->app->bind('view.finder', function ($app) {
$paths = apply_filters('roots/views/blade/template_directories', [TEMPLATEPATH]);
return new FileViewFinder($app['files'], $paths);
});
}
/**
* Register the view environment.
*
* @return void
*/
public function registerFactory()
{
$this->app->singleton('view', function ($app) {
// 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 = $app['view.engine.resolver'];
$finder = $app['view.finder'];
$env = new Factory($resolver, $finder, new Dispatcher($app));
// 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($app);
$env->share('app', $app);
return $env;
});
}
}