-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
moved TwigExtension src from framework
added Gravatar twig extension
- Loading branch information
1 parent
b394355
commit c293035
Showing
10 changed files
with
432 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace Dappur\TwigExtension; | ||
|
||
use Psr\Http\Message\RequestInterface; | ||
|
||
class Asset extends \Twig_Extension { | ||
|
||
protected $request; | ||
|
||
public function __construct(RequestInterface $request) { | ||
$this->request = $request; | ||
} | ||
|
||
public function getName() { | ||
return 'asset'; | ||
} | ||
|
||
public function getFunctions() { | ||
return [ | ||
new \Twig_SimpleFunction('asset', [$this, 'asset']) | ||
]; | ||
} | ||
|
||
public function asset($path) { | ||
return '/asset?path=' . $path; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
namespace Dappur\TwigExtension; | ||
|
||
/** | ||
* Cloudinary twig extension. | ||
* | ||
* adapted from @author Stefan Gotre <[email protected]> | ||
*/ | ||
class Cloudinary extends \Twig_Extension | ||
{ | ||
public function getName() | ||
{ | ||
return 'cloudinary'; | ||
} | ||
|
||
public function getFunctions() | ||
{ | ||
return array( | ||
'clUploadUrl' => new \Twig_SimpleFunction( | ||
'clUploadUrl', | ||
[$this, 'clUploadUrl'] | ||
), | ||
'clImageTag' => new \Twig_SimpleFunction( | ||
'clImageTag', | ||
[$this, 'clImageTag'], | ||
array('is_safe' => array('html')) | ||
), | ||
'cloudinaryJsConfig' => new \Twig_SimpleFunction( | ||
'cloudinaryJsConfig', | ||
[$this, 'cloudinaryJsConfig'], | ||
array('is_safe' => array('html', 'js')) | ||
), | ||
'clUrl' => new \Twig_SimpleFunction( | ||
'clUrl', | ||
[$this, 'clUrl'] | ||
), | ||
'clVideoPath' => new \Twig_SimpleFunction( | ||
'clVideoPath', | ||
[$this, 'clVideoPath'] | ||
), | ||
'clVideoThumb' => new \Twig_SimpleFunction( | ||
'clVideoThumb', | ||
[$this, 'clVideoThumb'] | ||
), | ||
'clVideoTag' => new \Twig_SimpleFunction( | ||
'clVideoTag', | ||
[$this, 'clVideoTag'], | ||
array('is_safe' => array('html')) | ||
), | ||
new \Twig_SimpleFunction('tinymce_init', [$this, 'tinymceInit'], array('is_safe' => array('html'))) | ||
); | ||
} | ||
|
||
public function clUploadUrl($options = array()) | ||
{ | ||
return cl_upload_url($options); | ||
} | ||
|
||
public function clImageTag($source, $options = array()) | ||
{ | ||
return cl_image_tag($source, $options); | ||
} | ||
|
||
public function cloudinaryJsConfig() | ||
{ | ||
return cloudinary_js_config(); | ||
} | ||
|
||
public function clUrl($source, $options = array()) | ||
{ | ||
return cloudinary_url($source, $options); | ||
} | ||
|
||
public function clVideoPath($source, $options = array()) | ||
{ | ||
return cl_video_path($source, $options = array()); | ||
} | ||
|
||
public function clVideoThumb($source, $options = array()) | ||
{ | ||
return cl_video_thumbnail_path($source, $options); | ||
} | ||
|
||
public function clVideoTag($source, $options = array()) | ||
{ | ||
return cl_video_tag($source, $options); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace Dappur\TwigExtension; | ||
|
||
use Psr\Http\Message\RequestInterface as Request; | ||
|
||
class Csrf extends \Twig_Extension | ||
{ | ||
|
||
/** | ||
* @var \Slim\Csrf\Guard | ||
*/ | ||
protected $csrf; | ||
|
||
public function __construct($csrf) | ||
{ | ||
$this->csrf = $csrf; | ||
} | ||
|
||
public function getGlobals() | ||
{ | ||
// CSRF token name and value | ||
$csrfNameKey = $this->csrf->getTokenNameKey(); | ||
$csrfValueKey = $this->csrf->getTokenValueKey(); | ||
$csrfName = $this->csrf->getTokenName(); | ||
$csrfValue = $this->csrf->getTokenValue(); | ||
|
||
return [ | ||
'csrf' => [ | ||
'keys' => [ | ||
'name' => $csrfNameKey, | ||
'value' => $csrfValueKey | ||
], | ||
'name' => $csrfName, | ||
'value' => $csrfValue | ||
] | ||
]; | ||
} | ||
|
||
public function getName() | ||
{ | ||
return 'csrf'; | ||
} | ||
|
||
public function getFunctions() | ||
{ | ||
return [ | ||
new \Twig_SimpleFunction('csrf', array($this, 'csrf'), array('is_safe' => array('html'))) | ||
]; | ||
} | ||
|
||
public function csrf() | ||
{ | ||
return '<input type="hidden" name="' . $this->csrf->getTokenNameKey() . | ||
'" value="' . $this->csrf->getTokenName() . | ||
'"><input type="hidden" name="' . $this->csrf->getTokenValueKey() . | ||
'" value="' . $this->csrf->getTokenValue() . '">'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace Dappur\TwigExtension; | ||
|
||
class Gravatar extends \Twig_Extension | ||
{ | ||
|
||
public function getName() | ||
{ | ||
return 'gravatar'; | ||
} | ||
|
||
public function getFunctions() | ||
{ | ||
return array( | ||
new \Twig_SimpleFunction('gravatar', array($this, 'gravatar')), | ||
); | ||
} | ||
|
||
/** | ||
* Get either a Gravatar URL or complete image tag for a specified email address. | ||
* | ||
* @param string $email The email address | ||
* @param string $s Size in pixels, defaults to 80px [ 1 - 2048 ] | ||
* @param string $d Default imageset to use [ 404 | mp | identicon | monsterid | wavatar ] | ||
* @param string $r Maximum rating (inclusive) [ g | pg | r | x ] | ||
* @param boole $img True to return a complete IMG tag False for just the URL | ||
* @param array $atts Optional, additional key/value attributes to include in the IMG tag | ||
* @return String containing either just a URL or a complete image tag | ||
* @source https://gravatar.com/site/implement/images/php/ | ||
*/ | ||
public function gravatar($email, $s = 160, $d = 'mp', $r = 'g', $img = false, $atts = array()) | ||
{ | ||
$url = 'https://www.gravatar.com/avatar/'; | ||
$url .= md5(strtolower(trim($email))); | ||
$url .= "?s=$s&d=$d&r=$r"; | ||
if ($img) { | ||
$url = '<img src="' . $url . '"'; | ||
foreach ($atts as $key => $val) { | ||
$url .= ' ' . $key . '="' . $val . '"'; | ||
} | ||
$url .= ' />'; | ||
} | ||
return $url; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace Dappur\TwigExtension; | ||
|
||
use Psr\Http\Message\RequestInterface; | ||
|
||
class JsonDecode extends \Twig_Extension | ||
{ | ||
protected $request; | ||
|
||
public function __construct(RequestInterface $request) | ||
{ | ||
$this->request = $request; | ||
} | ||
|
||
public function getName() | ||
{ | ||
return 'jsonDecode'; | ||
} | ||
|
||
public function getFilters() | ||
{ | ||
return array( | ||
new \Twig_SimpleFilter('jsonDecode', array($this, 'jsonDecode')), | ||
); | ||
} | ||
|
||
public function jsonDecode($str, $options = 1) | ||
{ | ||
$array = json_decode($str, $options); | ||
|
||
return $array; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace Dappur\TwigExtension; | ||
|
||
use Psr\Http\Message\RequestInterface; | ||
|
||
class Md5 extends \Twig_Extension | ||
{ | ||
public function getFilters() | ||
{ | ||
return array( | ||
new \Twig_SimpleFilter('md5', 'md5') | ||
); | ||
} | ||
|
||
public function getName() | ||
{ | ||
return "md5_hash"; | ||
} | ||
} |
Oops, something went wrong.