Skip to content

Commit

Permalink
Remove Template class
Browse files Browse the repository at this point in the history
  • Loading branch information
QWp6t authored and retlehs committed Mar 11, 2016
1 parent 12d6ac3 commit 1df3fee
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 190 deletions.
6 changes: 6 additions & 0 deletions src/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# editorconfig.org

root = false

[*.php]
indent_size = 2
3 changes: 1 addition & 2 deletions src/filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,5 @@
if (!is_string($main) || !(string) $main) {
return $main;
}
$main = basename($main, '.php');
return Template::wrap(new Wrapper($main, 'layouts/base.php'))->locate();
return template_wrap(new Wrapper(basename($main)));
}, 109);
19 changes: 9 additions & 10 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@

use Roots\Sage\Asset;
use Roots\Sage\Assets\JsonManifest;
use Roots\Sage\Template;
use Roots\Sage\Template\WrapperCollection;
use Roots\Sage\Template\WrapperInterface;

/**
* @param string $slug
* @param array $context
*/
function template_unwrap($slug = '', $context = []) {
if ($file = Template::unwrap($slug, $context)->locate()) {
/** @noinspection PhpIncludeInspection */
include $file;
}
function template_wrap(WrapperInterface $wrapper, $slug = 'base') {
WrapperCollection::add($wrapper, $slug);
return $wrapper->getWrapper();
}

function template_unwrap($slug = 'base') {
return WrapperCollection::get($slug)->getTemplate();
}

/**
Expand Down
162 changes: 0 additions & 162 deletions src/lib/Sage/Template.php

This file was deleted.

32 changes: 22 additions & 10 deletions src/lib/Sage/Template/Wrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,37 @@ class Wrapper implements WrapperInterface {
protected $template = '';

/** @var string[] Array of template wrappers; e.g., `base-singular.php`, `base-page.php`, `base.php` */
protected $wrappers = [];
protected $wrapper = [];

/** @var string[] Cache template locations */
protected static $locations = [];

/**
* Wrapper constructor
*
* @param string $templateSlug Template slug, typically from Template Heirarchy; e.g., `page`, `single`, `singular`
* @param string $template Template file, as from Template Heirarchy; e.g., `page.php`, `single.php`, `singular.php`
* @param string $base Wrapper's base template, this is what will wrap around $template
*/
public function __construct($templateSlug, $base = 'layouts/base.php') {
public function __construct($template, $base = 'layouts/base.php') {
$this->slug = sanitize_title(basename($base, '.php'));
$this->wrappers = [$base];
$this->template = $templateSlug;
$this->wrapper = [$base];
$this->template = $template;
$str = substr($base, 0, -4);
array_unshift($this->wrappers, sprintf($str . '-%s.php', $templateSlug));
array_unshift($this->wrapper, sprintf($str . '-%s.php', basename($template, '.php')));
}

/**
* @return string
* @see getTemplate
*/
public function __toString() {
return $this->getTemplate();
}

/** {@inheritdoc} */
public function getWrappers() {
$this->wrappers = apply_filters('sage/wrap_' . $this->slug, $this->wrappers) ?: $this->wrappers;
return $this->wrappers;
public function getWrapper() {
$wrappers = apply_filters('sage/wrap_' . $this->slug, $this->wrapper) ?: $this->wrapper;
return locate_template($wrappers);
}

/** {@inheritdoc} */
Expand All @@ -42,6 +53,7 @@ public function getSlug() {

/** {@inheritdoc} */
public function getTemplate() {
return $this->template;
$template = apply_filters('sage/unwrap_' . $this->slug, $this->template) ?: $this->template;
return locate_template($template);
}
}
73 changes: 73 additions & 0 deletions src/lib/Sage/Template/WrapperCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php namespace Roots\Sage\Template;

/**
* Class Wrapper
* @package Roots\Sage
* @author QWp6t
*/
class WrapperCollection {
/** @var $this */
protected static $instance;
/** @var WrapperInterface[] $wrappers */
protected $wrappers = [];

/** Singleton */
private function __construct() {}
private function __clone() {}

/**
* @return static
*/
public static function instance() {
isset(self::$instance) || self::$instance = new static;
return self::$instance;
}

/**
* @param WrapperInterface $wrapper
* @param string $slug
* @param bool $overwriteIfExists
* @return $this
* @throws \Exception
*/
public static function add(WrapperInterface $wrapper, $slug = '', $overwriteIfExists = false) {
$slug = $slug ?: $wrapper->getSlug();
if (self::instance()->exists($slug) && !$overwriteIfExists) {
throw new \Exception("Wrapper $slug already exists.");
}
self::instance()->wrappers[$slug] = $wrapper;
return self::instance();
}

/**
* @param string $slug
* @return $this
*/
public static function remove($slug) {
unset(self::instance()->wrappers[$slug]);
return self::instance();
}

/**
* @param string $slug
* @return null|WrapperInterface
*/
public static function get($slug) {
return isset(self::instance()->wrappers[$slug]) ? self::instance()->wrappers[$slug] : null;
}

/**
* @return string[] Slugs of wrappers in collection
*/
public static function wrappers() {
return array_keys(self::instance()->wrappers);
}

/**
* @param $slug
* @return bool
*/
public static function exists($slug) {
return isset(self::instance()->wrappers[$slug]);
}
}
10 changes: 5 additions & 5 deletions src/lib/Sage/Template/WrapperInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
* @author QWp6t
*/
interface WrapperInterface {

/**
* Get a list of potential wrappers
* Useful for passing to WordPress's locate_template()
* Get wrapper template file
*
* @return string[] List of wrappers; e.g., `base-page.php`, `base.php`
* @return string Wrapper template (FQPN of, e.g., `base-page.php`, `base.php`)
*/
public function getWrappers();
public function getWrapper();

/**
* @return string Template file that is being wrapped; e.g., `page.php`, `single.php`, `singular.php`
* @return string Wrapped template (FQPN of, e.g., `page.php`, `single.php`, `singular.php`)
*/
public function getTemplate();

Expand Down
2 changes: 1 addition & 1 deletion templates/layouts/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<div class="wrap container" role="document">
<div class="content row">
<main class="main">
<?php App\template_unwrap(); ?>
<?php include App\template_unwrap(); ?>
</main><!-- /.main -->
<?php if (App\display_sidebar()) : ?>
<aside class="sidebar">
Expand Down

0 comments on commit 1df3fee

Please sign in to comment.