Point avatars twig extension.

This commit is contained in:
Alexey Skobkin 2016-03-29 07:07:58 +03:00
parent 9b052cc97e
commit cf08978c58
3 changed files with 76 additions and 2 deletions

View file

@ -65,3 +65,11 @@ services:
class: Skobkin\Bundle\PointToolsBundle\EventListener\UserRenameSubscriber class: Skobkin\Bundle\PointToolsBundle\EventListener\UserRenameSubscriber
tags: tags:
- { name: doctrine.event_subscriber, connection: default } - { name: doctrine.event_subscriber, connection: default }
# Twig extensions
point_tools.twig.point_avatar_extension:
class: Skobkin\Bundle\PointToolsBundle\Twig\PointAvatarExtension
public: false
arguments: ['@skobkin_point_tools.api_user']
tags:
- { name: twig.extension }

View file

@ -294,18 +294,31 @@ class UserApi extends AbstractApi
} }
/** /**
* Creates avatar with specified size URL for user * Creates URL of avatar with specified size by User object
* *
* @param User $user * @param User $user
* @param int $size * @param int $size
* @return string * @return string
*/ */
public function getAvatarUrl(User $user, $size) public function getAvatarUrl(User $user, $size)
{
return $this->getAvatarUrlByLogin($user->getLogin(), $size);
}
/**
* Creates URL of avatar with specified size by login string
*
* @param $login
* @param $size
*
* @return string
*/
public function getAvatarUrlByLogin($login, $size)
{ {
if (!in_array($size, [self::AVATAR_SIZE_SMALL, self::AVATAR_SIZE_MEDIUM, self::AVATAR_SIZE_LARGE], true)) { if (!in_array($size, [self::AVATAR_SIZE_SMALL, self::AVATAR_SIZE_MEDIUM, self::AVATAR_SIZE_LARGE], true)) {
throw new \InvalidArgumentException('Avatar size must be one of restricted variants. See UserApi class AVATAR_SIZE_* constants.'); throw new \InvalidArgumentException('Avatar size must be one of restricted variants. See UserApi class AVATAR_SIZE_* constants.');
} }
return $this->avatarsBaseUrl.urlencode($user->getLogin()).'/'.$size; return $this->avatarsBaseUrl.urlencode($login).'/'.$size;
} }
} }

View file

@ -0,0 +1,53 @@
<?php
namespace Skobkin\Bundle\PointToolsBundle\Twig;
use Skobkin\Bundle\PointToolsBundle\Service\UserApi;
class PointAvatarExtension extends \Twig_Extension
{
/**
* @var UserApi
*/
private $userApi;
public function __construct(UserApi $userApi)
{
$this->userApi = $userApi;
}
public function getFunctions()
{
return [
new \Twig_SimpleFunction('point_avatar', [$this, 'pointAvatarFunction']),
new \Twig_SimpleFunction('point_avatar_small', [$this, 'pointAvatarSmallFunction']),
new \Twig_SimpleFunction('point_avatar_medium', [$this, 'pointAvatarMediumFunction']),
new \Twig_SimpleFunction('point_avatar_large', [$this, 'pointAvatarLargeFunction']),
];
}
public function pointAvatarSmallFunction($login)
{
return $this->pointAvatarFunction($login, UserApi::AVATAR_SIZE_SMALL);
}
public function pointAvatarMediumFunction($login)
{
return $this->pointAvatarFunction($login, UserApi::AVATAR_SIZE_MEDIUM);
}
public function pointAvatarLargeFunction($login)
{
return $this->pointAvatarFunction($login, UserApi::AVATAR_SIZE_LARGE);
}
public function pointAvatarFunction($login, $size)
{
return $this->userApi->getAvatarUrlByLogin($login, $size);
}
public function getName()
{
return 'point_tools_avatars';
}
}