template 엔진 '일단' 추가

This commit is contained in:
2018-01-11 21:40:21 +09:00
parent ee4f524437
commit 3a025cfa70
20 changed files with 1666 additions and 4 deletions
+85
View File
@@ -0,0 +1,85 @@
<?php
namespace League\Plates\Extension;
use League\Plates\Engine;
use League\Plates\Template\Template;
use LogicException;
/**
* Extension that adds the ability to create "cache busted" asset URLs.
*/
class Asset implements ExtensionInterface
{
/**
* Instance of the current template.
* @var Template
*/
public $template;
/**
* Path to asset directory.
* @var string
*/
public $path;
/**
* Enables the filename method.
* @var boolean
*/
public $filenameMethod;
/**
* Create new Asset instance.
* @param string $path
* @param boolean $filenameMethod
*/
public function __construct($path, $filenameMethod = false)
{
$this->path = rtrim($path, '/');
$this->filenameMethod = $filenameMethod;
}
/**
* Register extension function.
* @param Engine $engine
* @return null
*/
public function register(Engine $engine)
{
$engine->registerFunction('asset', array($this, 'cachedAssetUrl'));
}
/**
* Create "cache busted" asset URL.
* @param string $url
* @return string
*/
public function cachedAssetUrl($url)
{
$filePath = $this->path . '/' . ltrim($url, '/');
if (!file_exists($filePath)) {
throw new LogicException(
'Unable to locate the asset "' . $url . '" in the "' . $this->path . '" directory.'
);
}
$lastUpdated = filemtime($filePath);
$pathInfo = pathinfo($url);
if ($pathInfo['dirname'] === '.') {
$directory = '';
} elseif ($pathInfo['dirname'] === '/') {
$directory = '/';
} else {
$directory = $pathInfo['dirname'] . '/';
}
if ($this->filenameMethod) {
return $directory . $pathInfo['filename'] . '.' . $lastUpdated . '.' . $pathInfo['extension'];
}
return $directory . $pathInfo['filename'] . '.' . $pathInfo['extension'] . '?v=' . $lastUpdated;
}
}
@@ -0,0 +1,13 @@
<?php
namespace League\Plates\Extension;
use League\Plates\Engine;
/**
* A common interface for extensions.
*/
interface ExtensionInterface
{
public function register(Engine $engine);
}
+113
View File
@@ -0,0 +1,113 @@
<?php
namespace League\Plates\Extension;
use League\Plates\Engine;
use League\Plates\Template\Template;
use LogicException;
/**
* Extension that adds a number of URI checks.
*/
class URI implements ExtensionInterface
{
/**
* Instance of the current template.
* @var Template
*/
public $template;
/**
* The request URI.
* @var string
*/
protected $uri;
/**
* The request URI as an array.
* @var array
*/
protected $parts;
/**
* Create new URI instance.
* @param string $uri
*/
public function __construct($uri)
{
$this->uri = $uri;
$this->parts = explode('/', $this->uri);
}
/**
* Register extension functions.
* @param Engine $engine
* @return null
*/
public function register(Engine $engine)
{
$engine->registerFunction('uri', array($this, 'runUri'));
}
/**
* Perform URI check.
* @param null|integer|string $var1
* @param mixed $var2
* @param mixed $var3
* @param mixed $var4
* @return mixed
*/
public function runUri($var1 = null, $var2 = null, $var3 = null, $var4 = null)
{
if (is_null($var1)) {
return $this->uri;
}
if (is_numeric($var1) and is_null($var2)) {
return array_key_exists($var1, $this->parts) ? $this->parts[$var1] : null;
}
if (is_numeric($var1) and is_string($var2)) {
return $this->checkUriSegmentMatch($var1, $var2, $var3, $var4);
}
if (is_string($var1)) {
return $this->checkUriRegexMatch($var1, $var2, $var3);
}
throw new LogicException('Invalid use of the uri function.');
}
/**
* Perform a URI segment match.
* @param integer $key
* @param string $string
* @param mixed $returnOnTrue
* @param mixed $returnOnFalse
* @return mixed
*/
protected function checkUriSegmentMatch($key, $string, $returnOnTrue = null, $returnOnFalse = null)
{
if (array_key_exists($key, $this->parts) && $this->parts[$key] === $string) {
return is_null($returnOnTrue) ? true : $returnOnTrue;
}
return is_null($returnOnFalse) ? false : $returnOnFalse;
}
/**
* Perform a regular express match.
* @param string $regex
* @param mixed $returnOnTrue
* @param mixed $returnOnFalse
* @return mixed
*/
protected function checkUriRegexMatch($regex, $returnOnTrue = null, $returnOnFalse = null)
{
if (preg_match('#^' . $regex . '$#', $this->uri) === 1) {
return is_null($returnOnTrue) ? true : $returnOnTrue;
}
return is_null($returnOnFalse) ? false : $returnOnFalse;
}
}