2014-06-26 17:32:12 +10:00
|
|
|
<?php namespace System\Twig;
|
|
|
|
|
2017-06-02 23:34:50 +10:00
|
|
|
use Url;
|
2014-06-26 17:32:12 +10:00
|
|
|
use Twig_Extension;
|
|
|
|
use Twig_SimpleFilter;
|
2015-01-28 18:03:35 +11:00
|
|
|
use ApplicationException;
|
2017-10-18 15:56:52 +02:00
|
|
|
use System\Classes\MediaLibrary;
|
2014-06-28 21:23:13 +10:00
|
|
|
use System\Classes\MarkupManager;
|
2014-06-26 17:32:12 +10:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The System Twig extension class implements common Twig functions and filters.
|
|
|
|
*
|
|
|
|
* @package october\system
|
|
|
|
* @author Alexey Bobkov, Samuel Georges
|
|
|
|
*/
|
|
|
|
class Extension extends Twig_Extension
|
|
|
|
{
|
2014-06-28 21:23:13 +10:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \System\Classes\MarkupManager A reference to the markup manager instance.
|
|
|
|
*/
|
2014-08-01 18:20:55 +10:00
|
|
|
protected $markupManager;
|
2014-06-28 21:23:13 +10:00
|
|
|
|
2014-06-26 17:32:12 +10:00
|
|
|
/**
|
|
|
|
* Creates the extension instance.
|
|
|
|
*/
|
2014-06-28 21:23:13 +10:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->markupManager = MarkupManager::instance();
|
|
|
|
}
|
2014-06-26 17:32:12 +10:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a list of functions to add to the existing list.
|
|
|
|
*
|
|
|
|
* @return array An array of functions
|
|
|
|
*/
|
|
|
|
public function getFunctions()
|
|
|
|
{
|
2014-06-28 21:23:13 +10:00
|
|
|
$functions = [];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Include extensions provided by plugins
|
|
|
|
*/
|
2014-07-16 18:28:15 +10:00
|
|
|
$functions = $this->markupManager->makeTwigFunctions($functions);
|
2014-06-28 21:23:13 +10:00
|
|
|
|
|
|
|
return $functions;
|
2014-06-26 17:32:12 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a list of filters this extensions provides.
|
|
|
|
*
|
|
|
|
* @return array An array of filters
|
|
|
|
*/
|
|
|
|
public function getFilters()
|
|
|
|
{
|
|
|
|
$filters = [
|
|
|
|
new Twig_SimpleFilter('app', [$this, 'appFilter'], ['is_safe' => ['html']]),
|
2017-09-27 22:15:01 -06:00
|
|
|
new Twig_SimpleFilter('media', [$this, 'mediaFilter'], ['is_safe' => ['html']]),
|
2014-06-26 17:32:12 +10:00
|
|
|
];
|
|
|
|
|
2014-06-28 21:23:13 +10:00
|
|
|
/*
|
|
|
|
* Include extensions provided by plugins
|
|
|
|
*/
|
2014-07-16 18:28:15 +10:00
|
|
|
$filters = $this->markupManager->makeTwigFilters($filters);
|
2014-06-28 21:23:13 +10:00
|
|
|
|
2014-06-26 17:32:12 +10:00
|
|
|
return $filters;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a list of token parsers this extensions provides.
|
|
|
|
*
|
|
|
|
* @return array An array of token parsers
|
|
|
|
*/
|
|
|
|
public function getTokenParsers()
|
|
|
|
{
|
2014-06-28 21:23:13 +10:00
|
|
|
$parsers = [];
|
|
|
|
|
2014-07-16 18:28:15 +10:00
|
|
|
/*
|
|
|
|
* Include extensions provided by plugins
|
|
|
|
*/
|
|
|
|
$parsers = $this->markupManager->makeTwigTokenParsers($parsers);
|
2014-06-28 21:23:13 +10:00
|
|
|
|
|
|
|
return $parsers;
|
2014-06-26 17:32:12 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts supplied URL to one relative to the website root.
|
|
|
|
* @param mixed $url Specifies the application-relative URL
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function appFilter($url)
|
|
|
|
{
|
2017-06-02 23:34:50 +10:00
|
|
|
return Url::to($url);
|
2014-06-26 17:32:12 +10:00
|
|
|
}
|
2017-09-27 22:15:01 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts supplied file to a URL relative to the media library.
|
|
|
|
* @param string $file Specifies the media-relative file
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function mediaFilter($file)
|
|
|
|
{
|
|
|
|
return MediaLibrary::url($file);
|
|
|
|
}
|
2014-10-18 11:58:50 +02:00
|
|
|
}
|