Skip to content
This repository was archived by the owner on Mar 14, 2020. It is now read-only.

Commit

Permalink
Merge pull request #13 from helmer/enable_ssl
Browse files Browse the repository at this point in the history
Gravatar URL to match app scheme
  • Loading branch information
ornicar committed Aug 18, 2011
2 parents fc5a035 + 2917817 commit 3bb04d9
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 66 deletions.
14 changes: 8 additions & 6 deletions GravatarApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
class GravatarApi
{
/**
* @var array $default array of default options that can be overriden with getters and in the construct.
* @var array $defaults Array of default options that can be overriden with getters and in the construct.
*/
protected $defaults = array(
'size' => 80,
Expand All @@ -41,13 +41,14 @@ public function __construct(array $options = array())
* @param integer $size
* @param string $rating
* @param string $default
* @param Boolean $secure
* @return string
*/
public function getUrl($email, $size = null, $rating = null, $default = null)
public function getUrl($email, $size = null, $rating = null, $default = null, $secure = false)
{
$hash = md5(strtolower($email));

return $this->getUrlForHash($hash, $size, $rating, $default);
return $this->getUrlForHash($hash, $size, $rating, $default, $secure);
}

/**
Expand All @@ -57,25 +58,26 @@ public function getUrl($email, $size = null, $rating = null, $default = null)
* @param integer $size
* @param string $rating
* @param string $default
* @param Boolean $secure
* @return string
*/
public function getUrlForHash($hash, $size = null, $rating = null, $default = null)
public function getUrlForHash($hash, $size = null, $rating = null, $default = null, $secure = false)
{
$map = array(
's' => $size ?: $this->defaults['size'],
'r' => $rating ?: $this->defaults['rating'],
'd' => $default ?: $this->defaults['default'],
);

return 'http://www.gravatar.com/avatar/' . $hash . '?' . http_build_query(array_filter($map));
return ($secure ? 'https://secure' : 'http://www') . '.gravatar.com/avatar/' . $hash . '?' . http_build_query(array_filter($map));
}

/**
* Checks if a gravatar exists for the email. It does this by checking for 404 Not Found in the
* body returned.
*
* @param string $email
* @return boolean
* @return Boolean
*/
public function exists($email)
{
Expand Down
8 changes: 4 additions & 4 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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].

Expand Down
3 changes: 2 additions & 1 deletion Resources/config/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
<service id="templating.helper.gravatar" class="%templating.helper.gravatar.class%">
<tag name="templating.helper" alias="gravatar" />
<argument type="service" id="gravatar.api" />
<argument type="service" id="service_container" />
</service>

<service id="twig.extension.gravatar" class="%twig.extension.gravatar.class%">
<tag name="twig.extension" alias="gravatar" />
<argument type="service" id="gravatar.api" />
<argument type="service" id="templating.helper.gravatar" />
</service>
</services>
</container>
67 changes: 23 additions & 44 deletions Templating/Helper/GravatarHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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);
}

/**
Expand Down
38 changes: 38 additions & 0 deletions Templating/Helper/GravatarHelperInterface.php
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);
}
6 changes: 6 additions & 0 deletions Tests/GravatarApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 8 additions & 0 deletions Tests/Templating/Helper/GravatarHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]'));
Expand Down
32 changes: 21 additions & 11 deletions Twig/GravatarExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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);
}

/**
Expand Down

0 comments on commit 3bb04d9

Please sign in to comment.