From bc363c03e7475c27062a830d2fef75ce81b1b0e9 Mon Sep 17 00:00:00 2001 From: Alexey Skobkin Date: Wed, 11 Jan 2017 21:03:32 +0300 Subject: [PATCH] Hardcoded URL's removed everywhere in favor of dynamically generated based on container parameters. Router now can generate absolute URL even when not processing HTTP request. Avatar-related functions moved from UserApi to PointUrlExtension. --- app/config/config.yml | 4 + app/config/parameters.yml.dist | 11 +- .../Controller/UserController.php | 4 - .../Bundle/PointToolsBundle/Entity/User.php | 4 + .../Resources/config/services.yml | 7 +- .../Resources/views/Post/show.html.twig | 2 +- .../Resources/views/Telegram/help.md.twig | 2 +- .../last_global_subscriptions.md.twig | 4 +- .../Telegram/last_user_subscriptions.md.twig | 5 +- .../Resources/views/Telegram/settings.md.twig | 2 +- .../Resources/views/Telegram/stats.md.twig | 2 +- .../views/Telegram/user_subscribers.md.twig | 3 +- ...r_subscribers_updated_notification.md.twig | 7 +- .../users_renamed_notification.md.twig | 2 +- .../Resources/views/User/show.html.twig | 3 +- .../PointToolsBundle/Service/UserApi.php | 40 +------ .../Twig/PointUrlExtension.php | 100 ++++++++++++++++++ .../Twig/PointUserExtension.php | 86 --------------- .../Controller/UserControllerTest.php | 4 +- 19 files changed, 141 insertions(+), 151 deletions(-) create mode 100644 src/Skobkin/Bundle/PointToolsBundle/Twig/PointUrlExtension.php delete mode 100644 src/Skobkin/Bundle/PointToolsBundle/Twig/PointUserExtension.php diff --git a/app/config/config.yml b/app/config/config.yml index 567d1ff..7aa85e9 100644 --- a/app/config/config.yml +++ b/app/config/config.yml @@ -7,6 +7,10 @@ imports: # http://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration parameters: locale: ru + # Defining default router parameters to be able to generate correct full URL's when not processing HTTP request + router.request_context.host: "%point_tools_domain%" + router.request_context.scheme: "%point_tools_scheme%" + router.request_context.base_url: "%point_tools_base_url%" framework: #esi: ~ diff --git a/app/config/parameters.yml.dist b/app/config/parameters.yml.dist index bee394c..9f6af9d 100644 --- a/app/config/parameters.yml.dist +++ b/app/config/parameters.yml.dist @@ -11,8 +11,15 @@ parameters: mailer_user: ~ mailer_password: ~ - point_base_url: https://point.im/ - point_api_base_url: https://point.im/api/ + point_tools_domain: point.skobk.in + point_tools_scheme: https + # Path to public root (e.g. 'path/to/app') + point_tools_base_url: '' + + point_domain: point.im + point_scheme: https + point_base_url: '%point_scheme%://%point_domain%/' + point_api_base_url: '%point_base_url%/api/' # Delay between calls when updating users subscribers (in microseconds) point_api_delay: 500000 point_use_https: true diff --git a/src/Skobkin/Bundle/PointToolsBundle/Controller/UserController.php b/src/Skobkin/Bundle/PointToolsBundle/Controller/UserController.php index 2721083..74ac438 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Controller/UserController.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Controller/UserController.php @@ -6,7 +6,6 @@ use Doctrine\ORM\EntityManager; use Skobkin\Bundle\PointToolsBundle\DTO\DailyEvents; use Skobkin\Bundle\PointToolsBundle\DTO\TopUserDTO; use Skobkin\Bundle\PointToolsBundle\Entity\User; -use Skobkin\Bundle\PointToolsBundle\Service\UserApi; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Ob\HighchartsBundle\Highcharts\Highchart; use Symfony\Component\HttpFoundation\Request; @@ -37,14 +36,11 @@ class UserController extends Controller 10 ); - $userApi = $this->get('app.point.api_user'); - return $this->render('SkobkinPointToolsBundle:User:show.html.twig', [ 'user' => $user, 'subscribers' => $em->getRepository('SkobkinPointToolsBundle:User')->findUserSubscribersById($user->getId()), 'subscriptions_log' => $subscriberEventsPagination, 'rename_log' => $em->getRepository('SkobkinPointToolsBundle:UserRenameEvent')->findBy(['user' => $user], ['date' => 'DESC'], 10), - 'avatar_url' => $userApi->getAvatarUrl($user, UserApi::AVATAR_SIZE_LARGE), ]); } diff --git a/src/Skobkin/Bundle/PointToolsBundle/Entity/User.php b/src/Skobkin/Bundle/PointToolsBundle/Entity/User.php index 3ace1ca..cf54201 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Entity/User.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Entity/User.php @@ -12,6 +12,10 @@ use Doctrine\ORM\Mapping as ORM; */ class User { + const AVATAR_SIZE_SMALL = '24'; + const AVATAR_SIZE_MEDIUM = '40'; + const AVATAR_SIZE_LARGE = '80'; + /** * @var int * diff --git a/src/Skobkin/Bundle/PointToolsBundle/Resources/config/services.yml b/src/Skobkin/Bundle/PointToolsBundle/Resources/config/services.yml index eae4105..46667a7 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Resources/config/services.yml +++ b/src/Skobkin/Bundle/PointToolsBundle/Resources/config/services.yml @@ -175,9 +175,12 @@ services: # Twig extensions app.twig.point_users_extension: - class: Skobkin\Bundle\PointToolsBundle\Twig\PointUserExtension + class: Skobkin\Bundle\PointToolsBundle\Twig\PointUrlExtension public: false - arguments: ['@app.point.api_user'] + arguments: + - '%point_domain%' + - '%point_scheme%' + - '%point_base_url%' tags: - { name: twig.extension } diff --git a/src/Skobkin/Bundle/PointToolsBundle/Resources/views/Post/show.html.twig b/src/Skobkin/Bundle/PointToolsBundle/Resources/views/Post/show.html.twig index e1e6a1e..eb4c022 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Resources/views/Post/show.html.twig +++ b/src/Skobkin/Bundle/PointToolsBundle/Resources/views/Post/show.html.twig @@ -43,7 +43,7 @@
- +
{% if post.comments|length > 0 %} diff --git a/src/Skobkin/Bundle/PointToolsBundle/Resources/views/Telegram/help.md.twig b/src/Skobkin/Bundle/PointToolsBundle/Resources/views/Telegram/help.md.twig index 88f0ab0..fe2d06a 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Resources/views/Telegram/help.md.twig +++ b/src/Skobkin/Bundle/PointToolsBundle/Resources/views/Telegram/help.md.twig @@ -10,4 +10,4 @@ /stats - Show some statistics about service /help shows this message -Visit [Point Tools](https://point.skobk.in/) for more info. \ No newline at end of file +Visit [Point Tools]({{ url('index') }}) for more info. \ No newline at end of file diff --git a/src/Skobkin/Bundle/PointToolsBundle/Resources/views/Telegram/last_global_subscriptions.md.twig b/src/Skobkin/Bundle/PointToolsBundle/Resources/views/Telegram/last_global_subscriptions.md.twig index 7882853..dfe83b2 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Resources/views/Telegram/last_global_subscriptions.md.twig +++ b/src/Skobkin/Bundle/PointToolsBundle/Resources/views/Telegram/last_global_subscriptions.md.twig @@ -5,7 +5,7 @@ {% for event in events %} {% set sub_login = event.subscriber.login %} {% set author_login = event.author.login %} -{{ event.date|date('d M H:i') }} [@{{ sub_login }}]({{ sub_login|point_user_url(true) }}){% if subscription == event.action %} -> {% else %} X {% endif %}[@{{ author_login }}]({{ author_login|point_user_url(true) }}) +{{ event.date|date('d M H:i') }} [@{{ sub_login }}]({{ sub_login|point_user_url }}){% if subscription == event.action %} -> {% else %} X {% endif %}[@{{ author_login }}]({{ author_login|point_user_url }}) {% endfor %} -See more events on [Point Tools](https://point.skobk.in/) site. \ No newline at end of file +See more events on [Point Tools]({{ url('index') }}) site. \ No newline at end of file diff --git a/src/Skobkin/Bundle/PointToolsBundle/Resources/views/Telegram/last_user_subscriptions.md.twig b/src/Skobkin/Bundle/PointToolsBundle/Resources/views/Telegram/last_user_subscriptions.md.twig index c1285ae..257c1d4 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Resources/views/Telegram/last_user_subscriptions.md.twig +++ b/src/Skobkin/Bundle/PointToolsBundle/Resources/views/Telegram/last_user_subscriptions.md.twig @@ -5,8 +5,7 @@ {# @var event \Skobkin\Bundle\PointToolsBundle\Entity\SubscriptionEvent #} {% for event in events %} {% set sub_login = event.subscriber.login %} -{{ event.date|date('d M y H:i') }} {% if subscription == event.action %} + {% else %} - {% endif %} [@{{ sub_login }}]({{ sub_login|point_user_url(true) }}) +{{ event.date|date('d M y H:i') }} {% if subscription == event.action %} + {% else %} - {% endif %} [@{{ sub_login }}]({{ sub_login|point_user_url }}) {% endfor %} -{# @todo remove hardcoded URL #} -See more events on [Point Tools](https://point.skobk.in{{ path('user_show', {'login': user.login}) }}) site. \ No newline at end of file +See more events on [Point Tools]({{ url('user_show', {'login': user.login}) }}) site. \ No newline at end of file diff --git a/src/Skobkin/Bundle/PointToolsBundle/Resources/views/Telegram/settings.md.twig b/src/Skobkin/Bundle/PointToolsBundle/Resources/views/Telegram/settings.md.twig index 6793fd0..18a834a 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Resources/views/Telegram/settings.md.twig +++ b/src/Skobkin/Bundle/PointToolsBundle/Resources/views/Telegram/settings.md.twig @@ -1,7 +1,7 @@ {# @var account \Skobkin\Bundle\PointToolsBundle\Entity\Telegram\Account #} *Your current settings:* -Account /link status: {% if account.user %}linked to [@{{ account.user.login }}]({{ account.user.login|point_user_url(true) }}){% else %}Not linked{% endif %} +Account /link status: {% if account.user %}linked to [@{{ account.user.login }}]({{ account.user.login|point_user_url }}){% else %}Not linked{% endif %} User renaming notifications: {% if account.renameNotification %}enabled{% else %}disabled{% endif %} diff --git a/src/Skobkin/Bundle/PointToolsBundle/Resources/views/Telegram/stats.md.twig b/src/Skobkin/Bundle/PointToolsBundle/Resources/views/Telegram/stats.md.twig index 132b86a..80e4d84 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Resources/views/Telegram/stats.md.twig +++ b/src/Skobkin/Bundle/PointToolsBundle/Resources/views/Telegram/stats.md.twig @@ -6,4 +6,4 @@ Telegram users: {{ telegram_accounts }} Telegram linked users: {{ telegram_linked_accounts }} 24 hours events: {{ today_events }} -Visit [Point Tools](https://point.skobk.in/) for more info. \ No newline at end of file +Visit [Point Tools]({{ url('statistics') }}) for more info. \ No newline at end of file diff --git a/src/Skobkin/Bundle/PointToolsBundle/Resources/views/Telegram/user_subscribers.md.twig b/src/Skobkin/Bundle/PointToolsBundle/Resources/views/Telegram/user_subscribers.md.twig index 47bd43e..5abcf17 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Resources/views/Telegram/user_subscribers.md.twig +++ b/src/Skobkin/Bundle/PointToolsBundle/Resources/views/Telegram/user_subscribers.md.twig @@ -7,5 +7,4 @@ No subscribers {% endif %} -{# @todo remove hardcoded URL #} -See more events on [Point Tools](https://point.skobk.in{{ path('user_show', {'login': user.login}) }}) site. \ No newline at end of file +See more events on [Point Tools]({{ url('user_show', {'login': user.login}) }}) site. \ No newline at end of file diff --git a/src/Skobkin/Bundle/PointToolsBundle/Resources/views/Telegram/user_subscribers_updated_notification.md.twig b/src/Skobkin/Bundle/PointToolsBundle/Resources/views/Telegram/user_subscribers_updated_notification.md.twig index 7d36248..b2f7ce1 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Resources/views/Telegram/user_subscribers_updated_notification.md.twig +++ b/src/Skobkin/Bundle/PointToolsBundle/Resources/views/Telegram/user_subscribers_updated_notification.md.twig @@ -5,16 +5,15 @@ {% if subscribed|length > 0 %} New subscribers: {% for subscriber in subscribed %} -[@{{ subscriber.login }}]({{ subscriber.login|point_user_url(true) }}) +[@{{ subscriber.login }}]({{ subscriber.login|point_user_url }}) {% endfor %} {% endif %} {% if unsubscribed|length > 0 %} Lost subscribers: {% for subscriber in unsubscribed %} -[@{{ subscriber.login }}]({{ subscriber.login|point_user_url(true) }}) +[@{{ subscriber.login }}]({{ subscriber.login|point_user_url }}) {% endfor %} {% endif %} -{# @todo remove hardcoded URL #} -See full subscribers history on [Point Tools](https://point.skobk.in{{ path('user_show', {'login': user.login}) }}) site. \ No newline at end of file +See full subscribers history on [Point Tools]({{ url('user_show', {'login': user.login}) }}) site. \ No newline at end of file diff --git a/src/Skobkin/Bundle/PointToolsBundle/Resources/views/Telegram/users_renamed_notification.md.twig b/src/Skobkin/Bundle/PointToolsBundle/Resources/views/Telegram/users_renamed_notification.md.twig index fa80c86..d7056c3 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Resources/views/Telegram/users_renamed_notification.md.twig +++ b/src/Skobkin/Bundle/PointToolsBundle/Resources/views/Telegram/users_renamed_notification.md.twig @@ -4,5 +4,5 @@ {% for event in events %} {% set login_old = event.oldLogin %} {% set login_new = event.user.login %} -@{{ login_old }} -> [@{{ login_new }}]({{ login_new|point_user_url(true) }}) +@{{ login_old }} -> [@{{ login_new }}]({{ login_new|point_user_url }}) {% endfor %} diff --git a/src/Skobkin/Bundle/PointToolsBundle/Resources/views/User/show.html.twig b/src/Skobkin/Bundle/PointToolsBundle/Resources/views/User/show.html.twig index 625ddda..f69cd20 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Resources/views/User/show.html.twig +++ b/src/Skobkin/Bundle/PointToolsBundle/Resources/views/User/show.html.twig @@ -4,7 +4,8 @@ {% block content %}

- + {# TODO: Add class with image size #} + {{ user.login }}

diff --git a/src/Skobkin/Bundle/PointToolsBundle/Service/UserApi.php b/src/Skobkin/Bundle/PointToolsBundle/Service/UserApi.php index 0ba9703..3048842 100644 --- a/src/Skobkin/Bundle/PointToolsBundle/Service/UserApi.php +++ b/src/Skobkin/Bundle/PointToolsBundle/Service/UserApi.php @@ -20,15 +20,6 @@ use Symfony\Component\HttpFoundation\Response; */ class UserApi extends AbstractApi { - const AVATAR_SIZE_SMALL = '24'; - const AVATAR_SIZE_MEDIUM = '40'; - const AVATAR_SIZE_LARGE = '80'; - - /** - * @var string Base URL for user avatars - */ - protected $avatarsBaseUrl = '//point.im/avatar/'; - /** * @var EntityManager */ @@ -50,6 +41,7 @@ class UserApi extends AbstractApi $this->em = $entityManager; $this->serializer = $serializer; + // @todo refactor $this->userRepository = $this->em->getRepository('SkobkinPointToolsBundle:User'); } @@ -355,34 +347,4 @@ class UserApi extends AbstractApi return $resultUsers; } - - /** - * Creates URL of avatar with specified size by User object - * - * @param User $user - * @param string $size - * - * @return string - */ - public function getAvatarUrl(User $user, string $size): string - { - return $this->getAvatarUrlByLogin($user->getLogin(), $size); - } - - /** - * Creates URL of avatar with specified size by login string - * - * @param string $login - * @param string $size - * - * @return string - */ - public function getAvatarUrlByLogin(string $login, string $size): string - { - 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.'); - } - - return $this->avatarsBaseUrl.urlencode($login).'/'.$size; - } } diff --git a/src/Skobkin/Bundle/PointToolsBundle/Twig/PointUrlExtension.php b/src/Skobkin/Bundle/PointToolsBundle/Twig/PointUrlExtension.php new file mode 100644 index 0000000..036e5fb --- /dev/null +++ b/src/Skobkin/Bundle/PointToolsBundle/Twig/PointUrlExtension.php @@ -0,0 +1,100 @@ +pointDomain = $pointDomain; + $this->pointScheme = $pointScheme; + $this->pointBaseUrl = $pointBaseUrl; + } + + public function getFunctions() + { + return [ + new \Twig_SimpleFunction('point_avatar', [$this, 'avatarFunction']), + new \Twig_SimpleFunction('point_avatar_small', [$this, 'avatarSmallFunction']), + new \Twig_SimpleFunction('point_avatar_medium', [$this, 'avatarMediumFunction']), + new \Twig_SimpleFunction('point_avatar_large', [$this, 'avatarLargeFunction']), + new \Twig_SimpleFunction('point_user_url', [$this, 'userUrl']), + new \Twig_SimpleFunction('point_user_blog_url', [$this, 'userBlogUrl']), + new \Twig_SimpleFunction('point_post_url', [$this, 'postUrl']), + ]; + } + + public function getFilters() + { + return [ + new \Twig_SimpleFilter('point_avatar', [$this, 'avatarFunction']), + new \Twig_SimpleFilter('point_avatar_small', [$this, 'avatarSmallFunction']), + new \Twig_SimpleFilter('point_avatar_medium', [$this, 'avatarMediumFunction']), + new \Twig_SimpleFilter('point_avatar_large', [$this, 'avatarLargeFunction']), + new \Twig_SimpleFilter('point_user_url', [$this, 'pointUserUrl']), + new \Twig_SimpleFilter('point_user_blog_url', [$this, 'userBlogUrl']), + new \Twig_SimpleFilter('point_post_url', [$this, 'postUrl']), + ]; + } + + public function avatarSmallFunction(string $login): string + { + return $this->avatarFunction($login, User::AVATAR_SIZE_SMALL); + } + + public function avatarMediumFunction(string $login): string + { + return $this->avatarFunction($login, User::AVATAR_SIZE_MEDIUM); + } + + public function avatarLargeFunction(string $login): string + { + return $this->avatarFunction($login, User::AVATAR_SIZE_LARGE); + } + + public function avatarFunction(string $login, $size): string + { + return $this->getAvatarUrlByLogin($login, $size); + } + + public function userUrl(string $login): string + { + return sprintf('%s://%s.%s/', $this->pointScheme, $login, $this->pointDomain); + } + + public function userBlogUrl(string $login): string + { + return sprintf('%s://%s.%s/blog/', $this->pointScheme, $login, $this->pointDomain); + } + + public function postUrl(string $postId): string + { + return sprintf('%s://%s/%s', $this->pointScheme, $this->pointDomain, $postId); + } + + private function getAvatarUrlByLogin(string $login, string $size): string + { + if (!in_array($size, [User::AVATAR_SIZE_SMALL, User::AVATAR_SIZE_MEDIUM, User::AVATAR_SIZE_LARGE], true)) { + throw new \InvalidArgumentException('Avatar size must be one of restricted variants. See User::AVATAR_SIZE_* constants.'); + } + + return sprintf('%s://%s/avatar/%s/%s', $this->pointScheme, $this->pointDomain, urlencode($login), $size); + } +} \ No newline at end of file diff --git a/src/Skobkin/Bundle/PointToolsBundle/Twig/PointUserExtension.php b/src/Skobkin/Bundle/PointToolsBundle/Twig/PointUserExtension.php deleted file mode 100644 index a8d5ee2..0000000 --- a/src/Skobkin/Bundle/PointToolsBundle/Twig/PointUserExtension.php +++ /dev/null @@ -1,86 +0,0 @@ -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']), - new \Twig_SimpleFunction('point_user_url', [$this, 'pointUserUrl']), - new \Twig_SimpleFunction('point_user_blog_url', [$this, 'pointUserBlogUrl']), - ]; - } - - public function getFilters() - { - return [ - new \Twig_SimpleFilter('point_avatar', [$this, 'pointAvatarFunction']), - new \Twig_SimpleFilter('point_avatar_small', [$this, 'pointAvatarSmallFunction']), - new \Twig_SimpleFilter('point_avatar_medium', [$this, 'pointAvatarMediumFunction']), - new \Twig_SimpleFilter('point_avatar_large', [$this, 'pointAvatarLargeFunction']), - new \Twig_SimpleFilter('point_user_url', [$this, 'pointUserUrl']), - new \Twig_SimpleFilter('point_user_blog_url', [$this, 'pointUserBlogUrl']), - ]; - } - - public function pointAvatarSmallFunction(string $login): string - { - return $this->pointAvatarFunction($login, UserApi::AVATAR_SIZE_SMALL); - } - - public function pointAvatarMediumFunction(string $login): string - { - return $this->pointAvatarFunction($login, UserApi::AVATAR_SIZE_MEDIUM); - } - - public function pointAvatarLargeFunction(string $login): string - { - return $this->pointAvatarFunction($login, UserApi::AVATAR_SIZE_LARGE); - } - - public function pointAvatarFunction(string $login, $size): string - { - return $this->userApi->getAvatarUrlByLogin($login, $size); - } - - /** - * @param string $login - * @param bool $forceHttps - * - * @return string - */ - public function pointUserUrl(string $login, bool $forceHttps = false): string - { - return sprintf('%s//%s.%s/', $forceHttps ? 'https:' : '', $login, self::POINT_HOST); - } - - /** - * @param string $login - * @param bool $forceHttps - * - * @return string - */ - public function pointUserBlogUrl(string $login, bool $forceHttps = false): string - { - return sprintf('%s//%s.%s/blog/', $forceHttps ? 'https:' : '', $login, self::POINT_HOST); - } -} \ No newline at end of file diff --git a/tests/Skobkin/PointToolsBundle/Controller/UserControllerTest.php b/tests/Skobkin/PointToolsBundle/Controller/UserControllerTest.php index 52e9be5..8c1e4c2 100644 --- a/tests/Skobkin/PointToolsBundle/Controller/UserControllerTest.php +++ b/tests/Skobkin/PointToolsBundle/Controller/UserControllerTest.php @@ -61,8 +61,10 @@ class UserControllerTest extends WebTestCase 'testuser page has no avatar' ); + $pointScheme = static::createClient()->getContainer()->getParameter('point_scheme'); + $this->assertEquals( - '//point.im/avatar/testuser/80', + $pointScheme.'://point.im/avatar/testuser/80', $userAvatar->attr('src'), 'testuser avatar image source is not correct' );