Rename app to src
This commit is contained in:
35
src/lib/Sage/Asset.php
Normal file
35
src/lib/Sage/Asset.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?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 = basename($file);
|
||||
$this->dir = dirname($file) != '.' ? dirname($file) : '';
|
||||
}
|
||||
|
||||
public function __toString() {
|
||||
return $this->getUri();
|
||||
}
|
||||
|
||||
public function getUri() {
|
||||
$file = self::$dist . '/' . $this->dir . '/' . ($this->manifest ? $this->manifest->get($this->asset) : $this->asset);
|
||||
return get_template_directory_uri() . $file;
|
||||
}
|
||||
}
|
||||
29
src/lib/Sage/Assets/JsonManifest.php
Normal file
29
src/lib/Sage/Assets/JsonManifest.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php namespace Roots\Sage\Assets;
|
||||
|
||||
/**
|
||||
* Class JsonManifest
|
||||
* @package Roots\Sage
|
||||
* @author QWp6t
|
||||
*/
|
||||
class JsonManifest implements ManifestInterface {
|
||||
/** @var array */
|
||||
protected $manifest = [];
|
||||
|
||||
/**
|
||||
* JsonManifest constructor
|
||||
* @param string $manifestPath Local filesystem path to JSON-encoded manifest
|
||||
*/
|
||||
public function __construct($manifestPath) {
|
||||
$this->manifest = file_exists($manifestPath) ? json_decode(file_get_contents($manifestPath), true) : [];
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
public function get($file) {
|
||||
return isset($this->manifest[$file]) ? $this->manifest[$file] : $file;
|
||||
}
|
||||
|
||||
/** @inheritdoc */
|
||||
public function getAll() {
|
||||
return $this->manifest;
|
||||
}
|
||||
}
|
||||
26
src/lib/Sage/Assets/ManifestInterface.php
Normal file
26
src/lib/Sage/Assets/ManifestInterface.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php namespace Roots\Sage\Assets;
|
||||
|
||||
/**
|
||||
* Interface ManifestInterface
|
||||
* @package Roots\Sage
|
||||
* @author QWp6t
|
||||
*/
|
||||
interface ManifestInterface {
|
||||
|
||||
/**
|
||||
* Get the cache-busted filename
|
||||
*
|
||||
* If the manifest does not have an entry for $file, then return $file
|
||||
*
|
||||
* @param string $file The original name of the file before cache-busting
|
||||
* @return string
|
||||
*/
|
||||
public function get($file);
|
||||
|
||||
/**
|
||||
* Get the asset manifest
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getAll();
|
||||
}
|
||||
153
src/lib/Sage/Template.php
Normal file
153
src/lib/Sage/Template.php
Normal file
@@ -0,0 +1,153 @@
|
||||
<?php namespace Roots\Sage;
|
||||
|
||||
use Roots\Sage\Template\WrapperInterface;
|
||||
|
||||
/**
|
||||
* Class Template
|
||||
* @package Roots\Sage
|
||||
* @author QWp6t
|
||||
*/
|
||||
class Template {
|
||||
protected static $root = 'templates/';
|
||||
|
||||
/** @var WrapperInterface[] */
|
||||
protected static $wrappers = [];
|
||||
|
||||
protected $templates = [];
|
||||
|
||||
protected $context = [];
|
||||
|
||||
protected $html = '';
|
||||
|
||||
/**
|
||||
* @param WrapperInterface $wrapper
|
||||
* @param array $context Variables to pass to wrapper
|
||||
* @return static Template instance of wrapper
|
||||
*/
|
||||
public static function wrap(WrapperInterface $wrapper, $context = []) {
|
||||
self::$wrappers[$wrapper->getSlug()] = $wrapper;
|
||||
return new static($wrapper->getWrappers(), $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $slug
|
||||
* @param array $context
|
||||
* @return static
|
||||
*/
|
||||
public static function unwrap($slug = '', $context = []) {
|
||||
if (!$slug) {
|
||||
// If no slug is specified, we grab the most recently wrapped item
|
||||
end(self::$wrappers);
|
||||
$slug = key(self::$wrappers);
|
||||
}
|
||||
$template = new static(self::$wrappers[$slug]->getTemplate(), $context);
|
||||
unset(self::$wrappers[$slug]);
|
||||
return $template;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a delimeted template file into an array of parts
|
||||
*
|
||||
* Example:
|
||||
* Template::getConvertedTemplateParts('content-single-audio.php');
|
||||
* => ['content-single-audio.php', 'content-single.php', 'content.php']
|
||||
*
|
||||
* The returned value can then be passed to WordPress's locate_template.
|
||||
*
|
||||
* @param string $template
|
||||
* @param string $delimeter
|
||||
* @return array
|
||||
*/
|
||||
public static function convertParts($template, $delimeter = '-') {
|
||||
$templateParts = explode($delimeter, str_replace('.php', '', (string) $template));
|
||||
$templates[] = array_shift($templateParts);
|
||||
foreach ($templateParts as $i => $templatePart) {
|
||||
$templates[] = $templates[$i] . $delimeter . $templatePart;
|
||||
}
|
||||
return array_reverse($templates);
|
||||
}
|
||||
|
||||
/**
|
||||
* Template constructor
|
||||
* @param string|string[] $template
|
||||
* @param array $context
|
||||
*/
|
||||
public function __construct($template, array $context = []) {
|
||||
$this->set($template);
|
||||
$this->context = $context;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string HTML
|
||||
* @see get
|
||||
*/
|
||||
public function __toString() {
|
||||
return $this->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Echoes the output HTML
|
||||
* @see get
|
||||
*/
|
||||
public function render() {
|
||||
echo $this->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string HTML
|
||||
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
|
||||
*/
|
||||
public function get() {
|
||||
/** @noinspection PhpUnusedLocalVariableInspection $context is passed to the included template */
|
||||
$context = $this->context;
|
||||
extract($this->context);
|
||||
ob_start();
|
||||
if ($template = $this->locate()) {
|
||||
/** @noinspection PhpIncludeInspection */
|
||||
include $template;
|
||||
}
|
||||
$this->html = ob_get_clean() ?: '';
|
||||
return $this->html;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[]|string $template
|
||||
*/
|
||||
public function set($template) {
|
||||
if (is_array($template)) {
|
||||
$this->templates = self::format($template);
|
||||
return;
|
||||
}
|
||||
if (!is_string($template) || !(string) $template) {
|
||||
return;
|
||||
}
|
||||
// At this point, we assume it's something like `content-single.php` or `content-single-audio.php`
|
||||
$this->templates = self::format(self::convertParts($template));
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures that each template in $this->templates is appended with `.php`
|
||||
* @param $templates
|
||||
* @return array
|
||||
*/
|
||||
protected static function format($templates) {
|
||||
return array_map(function ($template) {
|
||||
if (substr($template, -4, 4) === '.php') {
|
||||
return $template;
|
||||
}
|
||||
return $template . '.php';
|
||||
}, $templates);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $templateDir Specify a template directory relative to your theme directory; e.g., `templates/`, `templates/partials/`, `woocommerce/`
|
||||
* @return string Filename
|
||||
*/
|
||||
public function locate($templateDir = '') {
|
||||
$templates = array_map(function ($template) use ($templateDir) {
|
||||
return ($templateDir ?: self::$root) . $template;
|
||||
}, $this->templates);
|
||||
$template = locate_template($templates);
|
||||
return apply_filters('sage/locate_template', $template, $templates) ?: $template;
|
||||
}
|
||||
}
|
||||
48
src/lib/Sage/Template/Wrapper.php
Normal file
48
src/lib/Sage/Template/Wrapper.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php namespace Roots\Sage\Template;
|
||||
|
||||
/**
|
||||
* Class Wrapper
|
||||
* @package Roots\Sage
|
||||
* @author QWp6t
|
||||
*/
|
||||
class Wrapper implements WrapperInterface {
|
||||
/** @var string Wrapper slug */
|
||||
protected $slug;
|
||||
|
||||
/** @var string Template file that is being wrapped */
|
||||
protected $template = '';
|
||||
|
||||
/** @var string[] Array of template wrappers; e.g., `base-singular.php`, `base-page.php`, `base.php` */
|
||||
protected $wrappers = [];
|
||||
|
||||
/**
|
||||
* Wrapper constructor
|
||||
*
|
||||
* @param string $templateSlug Template slug, typically from Template Heirarchy; e.g., `page`, `single`, `singular`
|
||||
* @param string $base Wrapper's base template, this is what will wrap around $template
|
||||
*/
|
||||
public function __construct($templateSlug, $base = 'layouts/base.php') {
|
||||
$this->slug = sanitize_title(basename($base, '.php'));
|
||||
$this->wrappers = [$base];
|
||||
$this->template = $templateSlug;
|
||||
$str = substr($base, 0, -4);
|
||||
array_unshift($this->wrappers, sprintf($str . '-%s.php', $templateSlug));
|
||||
}
|
||||
|
||||
/** {@inheritdoc} */
|
||||
public function getWrappers() {
|
||||
$this->wrappers = apply_filters('sage/wrap_' . $this->slug, $this->wrappers) ?: $this->wrappers;
|
||||
return $this->wrappers;
|
||||
}
|
||||
|
||||
/** {@inheritdoc} */
|
||||
public function getSlug() {
|
||||
return $this->slug;
|
||||
}
|
||||
|
||||
/** {@inheritdoc} */
|
||||
public function getTemplate()
|
||||
{
|
||||
return $this->template;
|
||||
}
|
||||
}
|
||||
27
src/lib/Sage/Template/WrapperInterface.php
Normal file
27
src/lib/Sage/Template/WrapperInterface.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php namespace Roots\Sage\Template;
|
||||
|
||||
/**
|
||||
* Interface WrapperInterface
|
||||
* @package Roots\Sage
|
||||
* @author QWp6t
|
||||
*/
|
||||
interface WrapperInterface {
|
||||
|
||||
/**
|
||||
* Get a list of potential wrappers
|
||||
* Useful for passing to WordPress's locate_template()
|
||||
*
|
||||
* @return string[] List of wrappers; e.g., `base-page.php`, `base.php`
|
||||
*/
|
||||
public function getWrappers();
|
||||
|
||||
/**
|
||||
* @return string Template file that is being wrapped; e.g., `page.php`, `single.php`, `singular.php`
|
||||
*/
|
||||
public function getTemplate();
|
||||
|
||||
/**
|
||||
* @return string Slug of the WrapperInterface; e.g., `base`
|
||||
*/
|
||||
public function getSlug();
|
||||
}
|
||||
Reference in New Issue
Block a user