This repository was archived by the owner on Mar 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from helmer/enable_ssl
Gravatar URL to match app scheme
- Loading branch information
Showing
8 changed files
with
110 additions
and
66 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 |
---|---|---|
|
@@ -40,23 +40,23 @@ All you have to do is use the helper like this example: | |
|
||
Or with parameters: | ||
|
||
<img src="<?php echo $view['gravatar']->getUrl('[email protected]', '80', 'g', 'defaultimage.png') ?>" /> | ||
<img src="<?php echo $view['gravatar']->getUrl('[email protected]', '80', 'g', 'defaultimage.png', true) ?>" /> | ||
|
||
The only required parameter is the email adress. The rest have default values. | ||
|
||
If you use twig you can use the helper like this exemple: | ||
|
||
{{ gravatar('[email protected]') }} | ||
|
||
Or if you want to check if a gravatar email exists: | ||
Or if you want to check if a gravatar email exists: | ||
|
||
{% if gravatar_exists('[email protected]') %} | ||
The email is an gravatar email | ||
{% endif %} | ||
|
||
Or with parameters: | ||
|
||
{{ gravatar('[email protected]', size, rating, default) }} | ||
{{ gravatar('[email protected]', size, rating, default, secure) }} | ||
|
||
For more information [look at the gravatar implementation pages][gravatar]. | ||
|
||
|
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 |
---|---|---|
|
@@ -2,7 +2,8 @@ | |
|
||
namespace Bundle\GravatarBundle\Templating\Helper; | ||
|
||
use Symfony\Component\Templating\Helper\HelperInterface; | ||
use Symfony\Component\DependencyInjection\ContainerInterface, | ||
Symfony\Component\Templating\Helper\Helper; | ||
use Bundle\GravatarBundle\GravatarApi; | ||
|
||
/** | ||
|
@@ -11,95 +12,73 @@ | |
* @author Thibault Duplessis | ||
* @author Henrik Bjornskov <[email protected]> | ||
*/ | ||
class GravatarHelper implements HelperInterface | ||
class GravatarHelper extends Helper implements GravatarHelperInterface | ||
{ | ||
/** | ||
* @var string $charset | ||
* @var Bundle\GravatarBundle\GravatarApi $api | ||
*/ | ||
protected $charset = 'UTF-8'; | ||
protected $api; | ||
|
||
/** | ||
* @var Bundle\GravatarBundle\GravatarApi $api | ||
* @var ContainerInterface $container | ||
*/ | ||
protected $api; | ||
protected $container; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param Bundle\GravatarBundle\GravatarApi $api | ||
* @return void | ||
*/ | ||
public function __construct(GravatarApi $api) | ||
public function __construct(GravatarApi $api, ContainerInterface $container = null) | ||
{ | ||
$this->api = $api; | ||
$this->container = $container; | ||
} | ||
|
||
/** | ||
* Returns a url for a gravatar | ||
* | ||
* @param string $email | ||
* @param integer $size | ||
* @param string $rating | ||
* @param string $default | ||
* @return string | ||
* {@inheritDoc} | ||
*/ | ||
public function getUrl($email, $size = null, $rating = null, $default = null) | ||
public function getUrl($email, $size = null, $rating = null, $default = null, $secure = null) | ||
{ | ||
return $this->api->getUrl($email, $size, $rating, $default); | ||
return $this->api->getUrl($email, $size, $rating, $default, $this->isSecure($secure)); | ||
} | ||
|
||
/** | ||
* Returns a url for a gravatar for a given hash | ||
* | ||
* @param string $hash | ||
* @param integer $size | ||
* @param string $rating | ||
* @param string $default | ||
* @return string | ||
* {@inheritDoc} | ||
*/ | ||
public function getUrlForHash($hash, $size = null, $rating = null, $default = null) | ||
public function getUrlForHash($hash, $size = null, $rating = null, $default = null, $secure = null) | ||
{ | ||
return $this->api->getUrlForHash($hash, $size, $rating, $default); | ||
return $this->api->getUrlForHash($hash, $size, $rating, $default, $this->isSecure($secure)); | ||
} | ||
|
||
public function render($email, array $options = array()) | ||
{ | ||
$size = isset($options['size'])?$options['size']:null; | ||
$rating = isset($options['rating'])?$options['rating']:null; | ||
$default = isset($options['default'])?$options['default']:null; | ||
$secure = $this->isSecure(); | ||
|
||
return $this->api->getUrl($email, $size, $rating, $default); | ||
return $this->api->getUrl($email, $size, $rating, $default, $secure); | ||
} | ||
|
||
/** | ||
* Returns true if a avatar could be found for the email | ||
* | ||
* @param string $email | ||
* @return boolean | ||
* {@inheritDoc} | ||
*/ | ||
public function exists($email) | ||
{ | ||
return $this->api->exists($email); | ||
} | ||
|
||
/** | ||
* Sets the default charset. | ||
* | ||
* @param string $charset The charset | ||
*/ | ||
public function setCharset($charset) | ||
{ | ||
$this->charset = $charset; | ||
} | ||
|
||
/** | ||
* Gets the default charset. | ||
* Returns true if avatar should be fetched over secure connection | ||
* | ||
* @return string The default charset | ||
* @param mixed $preset | ||
* @return Boolean | ||
*/ | ||
public function getCharset() | ||
protected function isSecure($preset = null) | ||
{ | ||
return $this->charset; | ||
return (null === $preset && $this->container && $this->container->has('request') ? $this->container->get('request')->isSecure() : !!$preset); | ||
} | ||
|
||
/** | ||
|
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,38 @@ | ||
<?php | ||
|
||
namespace Bundle\GravatarBundle\Templating\Helper; | ||
|
||
interface GravatarHelperInterface | ||
{ | ||
/** | ||
* Returns a url for a gravatar | ||
* | ||
* @param string $email | ||
* @param integer $size | ||
* @param string $rating | ||
* @param string $default | ||
* @param Boolean $secure | ||
* @return string | ||
*/ | ||
function getUrl($email, $size = null, $rating = null, $default = null, $secure = null); | ||
|
||
/** | ||
* Returns a url for a gravatar for a given hash | ||
* | ||
* @param string $hash | ||
* @param integer $size | ||
* @param string $rating | ||
* @param string $default | ||
* @param Boolean $secure | ||
* @return string | ||
*/ | ||
function getUrlForHash($hash, $size = null, $rating = null, $default = null, $secure = null); | ||
|
||
/** | ||
* Returns true if a avatar could be found for the email | ||
* | ||
* @param string $email | ||
* @return Boolean | ||
*/ | ||
function exists($email); | ||
} |
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 |
---|---|---|
|
@@ -13,6 +13,12 @@ public function testGravatarUrlWithDefaultOptions() | |
$this->assertEquals('http://www.gravatar.com/avatar/0aa61df8e35327ac3b3bc666525e0bee?s=80&r=g', $api->getUrl('[email protected]')); | ||
} | ||
|
||
public function testGravatarSecureUrlWithDefaultOptions() | ||
{ | ||
$api = new GravatarApi(); | ||
$this->assertEquals('https://secure.gravatar.com/avatar/0aa61df8e35327ac3b3bc666525e0bee?s=80&r=g', $api->getUrl('[email protected]', null, null, null, true)); | ||
} | ||
|
||
public function testGravatarUrlWithDefaultImage() | ||
{ | ||
$api = new GravatarApi(); | ||
|
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 |
---|---|---|
|
@@ -20,6 +20,14 @@ public function testGetUrlReturnsTheCorrectUrl() | |
$this->assertEquals('http://www.gravatar.com/avatar/0aa61df8e35327ac3b3bc666525e0bee?s=80&r=g', $this->helper->getUrl('[email protected]')); | ||
} | ||
|
||
public function testGetUrlReturnsTheCorrectSecureUrl() | ||
{ | ||
$this->assertEquals( | ||
'https://secure.gravatar.com/avatar/0aa61df8e35327ac3b3bc666525e0bee?s=80&r=g', | ||
$this->helper->getUrl('[email protected]', null, null, null, true) | ||
); | ||
} | ||
|
||
public function testCheckForAvatarExistance() | ||
{ | ||
$this->assertTrue($this->helper->exists('[email protected]')); | ||
|
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 |
---|---|---|
|
@@ -2,25 +2,26 @@ | |
|
||
namespace Bundle\GravatarBundle\Twig; | ||
|
||
use Bundle\GravatarBundle\GravatarApi; | ||
use Bundle\GravatarBundle\Templating\Helper\GravatarHelper, | ||
Bundle\GravatarBundle\Templating\Helper\GravatarHelperInterface; | ||
|
||
/** | ||
* @author Thibault Duplessis | ||
* @author Henrik Bjornskov <[email protected]> | ||
*/ | ||
class GravatarExtension extends \Twig_Extension | ||
class GravatarExtension extends \Twig_Extension implements GravatarHelperInterface | ||
{ | ||
/** | ||
* @var GravatarApi $api | ||
* @var GravatarHelper $baseHelper | ||
*/ | ||
protected $api; | ||
protected $baseHelper; | ||
|
||
/** | ||
* @param GravatarApi $api | ||
*/ | ||
public function __construct(GravatarApi $api) | ||
public function __construct(GravatarHelper $helper) | ||
{ | ||
$this->api = $api; | ||
$this->baseHelper = $helper; | ||
} | ||
|
||
public function getFunctions() | ||
|
@@ -32,19 +33,28 @@ public function getFunctions() | |
); | ||
} | ||
|
||
public function getUrl($email, $size = null, $rating = null, $default = null) | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getUrl($email, $size = null, $rating = null, $default = null, $secure = null) | ||
{ | ||
return $this->api->getUrl($email, $size, $rating, $default); | ||
return $this->baseHelper->getUrl($email, $size, $rating, $default, $secure); | ||
} | ||
|
||
public function getUrlForHash($hash, $size = null, $rating = null, $default = null) | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getUrlForHash($hash, $size = null, $rating = null, $default = null, $secure = null) | ||
{ | ||
return $this->api->getUrlForHash($hash, $size, $rating, $default); | ||
return $this->baseHelper->getUrlForHash($hash, $size, $rating, $default, $secure); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function exists($email) | ||
{ | ||
return $this->api->exists($email); | ||
return $this->baseHelper->exists($email); | ||
} | ||
|
||
/** | ||
|