<?php
use Symfony\Component\Finder\Finder;
class BonesSingleton {
public static $instance;
public static function getInstance() {
if (null === static::$instance) {
static::$instance = new static();
}
return static::$instance;
}
protected function __construct() {
}
private function __clone() {
}
private function __wakeup() {
}
}
class TwigSingleton extends BonesSingleton {
public $twig = false;
public $templates = array();
protected function __construct() {
$this->getTemplates();
$loader = new Twig_Loader_Array($this->templates);
$twigEnv = new Twig_Environment($loader, array('debug' => true));
$imageFunc = new Twig_SimpleFunction('imagesrc', 'show_image', array('is_safe' => array('html')));
$doShortcodeFunc = new Twig_SimpleFunction('do_shortcode', function ($content, $ignore_html = false) {
return do_shortcode($content, $ignore_html);
}, array('is_safe' => array('html')));
$doBlockFunc = new Twig_SimpleFunction('localize_url', function ($url, $ignore_html = false) {
return localize_url($url);
}, array('is_safe' => array('html')));
$addEncoFunc = new Twig_SimpleFunction('add_enc',function($redirect_page){
return '<input type="hidden" name="uenc" value="'. Mage::helper('core')->urlEncode(Mage::helper('cms/page')->getPageUrl($redirect_page)).'"/>';
}, array('is_safe' => array('html')));
$twigEnv->addExtension(new Twig_Extension_Debug());
$twigEnv->addFunction($imageFunc);
$twigEnv->addFunction($doShortcodeFunc);
$twigEnv->addFunction($doBlockFunc);
$twigEnv->addFunction($addEncoFunc);
$this->twig = $twigEnv;
}
public function getTwig() {
return $this->twig;
}
public function getTemplates() {
//execute all the pattern portfolio parts
$this->get_templates_for('04-pages', 'pages');
$this->get_templates_for('03-templates', 'templates');
$this->get_templates_for('02-organisms', 'organisms');
$this->get_templates_for('01-molecules', 'molecules');
$this->get_templates_for('00-atoms', 'atoms');
}
private function get_templates_for($folder, $prefix) {
$pages = new Finder();
$pages->files()->in(PATTERNS_TEMPLATES . '/' . $folder)->name('*.twig');
foreach ($pages as $file) {
$this->templates[$this->prepare_name($file, $prefix . '-')] = $file->getContents();
}
}
private function prepare_name($file, $prefix = '') {
return $prefix . preg_replace('/^[_0-9\-]*/', '', $file->getBasename('.twig'));
}
}
define('PATTERNS_TEMPLATES', dirname(ABSPATH) . '/skin/frontend/sophiehulme/uk/styleguide/source/_patterns');
function create_sh_link($link) {
return $link;
}
function show_image($img, $size, $title = false, $classes = '', $animTarget = false, $lazyload) {
$imgHtml = '<img class="block image auto-height js-anim-image ' . $classes . '" width="' . $img['width'] . '" height="' . $img['height'] . '" alt="' . $img['name'] . '" srcset="' . get_image_group_srcset($img, $size) . '"' . ($animTarget ? ' data-anim-target="' . $animTarget . '"' : '') . ' sizes="'. get_image_group_size($size).'">';
if ($title && is_string($title)) {
return "<figure class='figure'>
$imgHtml
<figcaption class='figcaption t-header-small psy1'>$title</figcaption>
</figure>";
} else {
echo $imgHtml;
}
}
function date_to_unix($date) {
$dateTime = DateTime::createFromFormat('d/m/Y', $date);
return $dateTime->getTimestamp();
}
function should_be_visible($from, $to) {
$currentTime = strtotime('now');
return (date_to_unix($from) < $currentTime && $currentTime < date_to_unix($to));
}
//small singleton (Yeah I know)
//lazy person loader
function twig_render($name, $data, $debug) {
return TwigSingleton::getInstance()->getTwig()->render($name, $data);
}
preferences:
60.45 ms | 402 KiB | 5 Q