Skip to content

Commit c293035

Browse files
committed
moved TwigExtension src from framework
added Gravatar twig extension
1 parent b394355 commit c293035

File tree

10 files changed

+432
-2
lines changed

10 files changed

+432
-2
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## [Unreleased]
44

5+
## [1.1.0] - 2018-07-19
6+
### Added
7+
- Moved `TwigExtensions` src from Framework
8+
- Added Gravatar Twig extension
9+
510
## [1.0.6] - 2018-07-17
611
### Fixed
712
- Issue with email recipients it was an ID
@@ -31,7 +36,8 @@
3136
- Separated Dappurware from the framework.
3237

3338

34-
[Unreleased]: https://github.com/dappur/dappurware/compare/v1.0.6...HEAD
39+
[Unreleased]: https://github.com/dappur/dappurware/compare/v1.1.0...HEAD
40+
[1.1.0]: https://github.com/dappur/dappurware/compare/v1.0.6...v1.1.0
3541
[1.0.6]: https://github.com/dappur/dappurware/compare/v1.0.5...v1.0.6
3642
[1.0.5]: https://github.com/dappur/dappurware/compare/v1.0.4...v1.0.5
3743
[1.0.4]: https://github.com/dappur/dappurware/compare/v1.0.3...v1.0.4

app/src/TwigExtension/Asset.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Dappur\TwigExtension;
4+
5+
use Psr\Http\Message\RequestInterface;
6+
7+
class Asset extends \Twig_Extension {
8+
9+
protected $request;
10+
11+
public function __construct(RequestInterface $request) {
12+
$this->request = $request;
13+
}
14+
15+
public function getName() {
16+
return 'asset';
17+
}
18+
19+
public function getFunctions() {
20+
return [
21+
new \Twig_SimpleFunction('asset', [$this, 'asset'])
22+
];
23+
}
24+
25+
public function asset($path) {
26+
return '/asset?path=' . $path;
27+
}
28+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
namespace Dappur\TwigExtension;
4+
5+
/**
6+
* Cloudinary twig extension.
7+
*
8+
* adapted from @author Stefan Gotre <[email protected]>
9+
*/
10+
class Cloudinary extends \Twig_Extension
11+
{
12+
public function getName()
13+
{
14+
return 'cloudinary';
15+
}
16+
17+
public function getFunctions()
18+
{
19+
return array(
20+
'clUploadUrl' => new \Twig_SimpleFunction(
21+
'clUploadUrl',
22+
[$this, 'clUploadUrl']
23+
),
24+
'clImageTag' => new \Twig_SimpleFunction(
25+
'clImageTag',
26+
[$this, 'clImageTag'],
27+
array('is_safe' => array('html'))
28+
),
29+
'cloudinaryJsConfig' => new \Twig_SimpleFunction(
30+
'cloudinaryJsConfig',
31+
[$this, 'cloudinaryJsConfig'],
32+
array('is_safe' => array('html', 'js'))
33+
),
34+
'clUrl' => new \Twig_SimpleFunction(
35+
'clUrl',
36+
[$this, 'clUrl']
37+
),
38+
'clVideoPath' => new \Twig_SimpleFunction(
39+
'clVideoPath',
40+
[$this, 'clVideoPath']
41+
),
42+
'clVideoThumb' => new \Twig_SimpleFunction(
43+
'clVideoThumb',
44+
[$this, 'clVideoThumb']
45+
),
46+
'clVideoTag' => new \Twig_SimpleFunction(
47+
'clVideoTag',
48+
[$this, 'clVideoTag'],
49+
array('is_safe' => array('html'))
50+
),
51+
new \Twig_SimpleFunction('tinymce_init', [$this, 'tinymceInit'], array('is_safe' => array('html')))
52+
);
53+
}
54+
55+
public function clUploadUrl($options = array())
56+
{
57+
return cl_upload_url($options);
58+
}
59+
60+
public function clImageTag($source, $options = array())
61+
{
62+
return cl_image_tag($source, $options);
63+
}
64+
65+
public function cloudinaryJsConfig()
66+
{
67+
return cloudinary_js_config();
68+
}
69+
70+
public function clUrl($source, $options = array())
71+
{
72+
return cloudinary_url($source, $options);
73+
}
74+
75+
public function clVideoPath($source, $options = array())
76+
{
77+
return cl_video_path($source, $options = array());
78+
}
79+
80+
public function clVideoThumb($source, $options = array())
81+
{
82+
return cl_video_thumbnail_path($source, $options);
83+
}
84+
85+
public function clVideoTag($source, $options = array())
86+
{
87+
return cl_video_tag($source, $options);
88+
}
89+
}

app/src/TwigExtension/Csrf.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Dappur\TwigExtension;
4+
5+
use Psr\Http\Message\RequestInterface as Request;
6+
7+
class Csrf extends \Twig_Extension
8+
{
9+
10+
/**
11+
* @var \Slim\Csrf\Guard
12+
*/
13+
protected $csrf;
14+
15+
public function __construct($csrf)
16+
{
17+
$this->csrf = $csrf;
18+
}
19+
20+
public function getGlobals()
21+
{
22+
// CSRF token name and value
23+
$csrfNameKey = $this->csrf->getTokenNameKey();
24+
$csrfValueKey = $this->csrf->getTokenValueKey();
25+
$csrfName = $this->csrf->getTokenName();
26+
$csrfValue = $this->csrf->getTokenValue();
27+
28+
return [
29+
'csrf' => [
30+
'keys' => [
31+
'name' => $csrfNameKey,
32+
'value' => $csrfValueKey
33+
],
34+
'name' => $csrfName,
35+
'value' => $csrfValue
36+
]
37+
];
38+
}
39+
40+
public function getName()
41+
{
42+
return 'csrf';
43+
}
44+
45+
public function getFunctions()
46+
{
47+
return [
48+
new \Twig_SimpleFunction('csrf', array($this, 'csrf'), array('is_safe' => array('html')))
49+
];
50+
}
51+
52+
public function csrf()
53+
{
54+
return '<input type="hidden" name="' . $this->csrf->getTokenNameKey() .
55+
'" value="' . $this->csrf->getTokenName() .
56+
'"><input type="hidden" name="' . $this->csrf->getTokenValueKey() .
57+
'" value="' . $this->csrf->getTokenValue() . '">';
58+
}
59+
}

app/src/TwigExtension/Gravatar.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Dappur\TwigExtension;
4+
5+
class Gravatar extends \Twig_Extension
6+
{
7+
8+
public function getName()
9+
{
10+
return 'gravatar';
11+
}
12+
13+
public function getFunctions()
14+
{
15+
return array(
16+
new \Twig_SimpleFunction('gravatar', array($this, 'gravatar')),
17+
);
18+
}
19+
20+
/**
21+
* Get either a Gravatar URL or complete image tag for a specified email address.
22+
*
23+
* @param string $email The email address
24+
* @param string $s Size in pixels, defaults to 80px [ 1 - 2048 ]
25+
* @param string $d Default imageset to use [ 404 | mp | identicon | monsterid | wavatar ]
26+
* @param string $r Maximum rating (inclusive) [ g | pg | r | x ]
27+
* @param boole $img True to return a complete IMG tag False for just the URL
28+
* @param array $atts Optional, additional key/value attributes to include in the IMG tag
29+
* @return String containing either just a URL or a complete image tag
30+
* @source https://gravatar.com/site/implement/images/php/
31+
*/
32+
public function gravatar($email, $s = 160, $d = 'mp', $r = 'g', $img = false, $atts = array())
33+
{
34+
$url = 'https://www.gravatar.com/avatar/';
35+
$url .= md5(strtolower(trim($email)));
36+
$url .= "?s=$s&d=$d&r=$r";
37+
if ($img) {
38+
$url = '<img src="' . $url . '"';
39+
foreach ($atts as $key => $val) {
40+
$url .= ' ' . $key . '="' . $val . '"';
41+
}
42+
$url .= ' />';
43+
}
44+
return $url;
45+
}
46+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Dappur\TwigExtension;
4+
5+
use Psr\Http\Message\RequestInterface;
6+
7+
class JsonDecode extends \Twig_Extension
8+
{
9+
protected $request;
10+
11+
public function __construct(RequestInterface $request)
12+
{
13+
$this->request = $request;
14+
}
15+
16+
public function getName()
17+
{
18+
return 'jsonDecode';
19+
}
20+
21+
public function getFilters()
22+
{
23+
return array(
24+
new \Twig_SimpleFilter('jsonDecode', array($this, 'jsonDecode')),
25+
);
26+
}
27+
28+
public function jsonDecode($str, $options = 1)
29+
{
30+
$array = json_decode($str, $options);
31+
32+
return $array;
33+
}
34+
}

app/src/TwigExtension/Md5.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Dappur\TwigExtension;
4+
5+
use Psr\Http\Message\RequestInterface;
6+
7+
class Md5 extends \Twig_Extension
8+
{
9+
public function getFilters()
10+
{
11+
return array(
12+
new \Twig_SimpleFilter('md5', 'md5')
13+
);
14+
}
15+
16+
public function getName()
17+
{
18+
return "md5_hash";
19+
}
20+
}

0 commit comments

Comments
 (0)