Skip to content

Commit

Permalink
moved TwigExtension src from framework
Browse files Browse the repository at this point in the history
added Gravatar twig extension
  • Loading branch information
edwardteach42 committed Jul 19, 2018
1 parent b394355 commit c293035
Show file tree
Hide file tree
Showing 10 changed files with 432 additions and 2 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## [Unreleased]

## [1.1.0] - 2018-07-19
### Added
- Moved `TwigExtensions` src from Framework
- Added Gravatar Twig extension

## [1.0.6] - 2018-07-17
### Fixed
- Issue with email recipients it was an ID
Expand Down Expand Up @@ -31,7 +36,8 @@
- Separated Dappurware from the framework.


[Unreleased]: https://github.com/dappur/dappurware/compare/v1.0.6...HEAD
[Unreleased]: https://github.com/dappur/dappurware/compare/v1.1.0...HEAD
[1.1.0]: https://github.com/dappur/dappurware/compare/v1.0.6...v1.1.0
[1.0.6]: https://github.com/dappur/dappurware/compare/v1.0.5...v1.0.6
[1.0.5]: https://github.com/dappur/dappurware/compare/v1.0.4...v1.0.5
[1.0.4]: https://github.com/dappur/dappurware/compare/v1.0.3...v1.0.4
Expand Down
28 changes: 28 additions & 0 deletions app/src/TwigExtension/Asset.php
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;
}
}
89 changes: 89 additions & 0 deletions app/src/TwigExtension/Cloudinary.php
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);
}
}
59 changes: 59 additions & 0 deletions app/src/TwigExtension/Csrf.php
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() . '">';
}
}
46 changes: 46 additions & 0 deletions app/src/TwigExtension/Gravatar.php
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;
}
}
34 changes: 34 additions & 0 deletions app/src/TwigExtension/JsonDecode.php
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;
}
}
20 changes: 20 additions & 0 deletions app/src/TwigExtension/Md5.php
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";
}
}
Loading

0 comments on commit c293035

Please sign in to comment.