X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;ds=inline;f=src%2FWallabag%2FCoreBundle%2FTwig%2FWallabagExtension.php;h=02f17f50a58b52b2dc5d0b6c2af765e58506ecef;hb=3c5aa05311d057c8fed83f085e6fb6ecc874fca4;hp=351172c460f2fc24ba76c37e85e76b14ac958c57;hpb=71e1cbc8eb5928d393b0772055d6b711e90a09b3;p=github%2Fwallabag%2Fwallabag.git diff --git a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php index 351172c4..02f17f50 100644 --- a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php +++ b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php @@ -4,10 +4,14 @@ namespace Wallabag\CoreBundle\Twig; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; use Symfony\Component\Translation\TranslatorInterface; +use Twig\Extension\AbstractExtension; +use Twig\Extension\GlobalsInterface; +use Twig\TwigFilter; +use Twig\TwigFunction; use Wallabag\CoreBundle\Repository\EntryRepository; use Wallabag\CoreBundle\Repository\TagRepository; -class WallabagExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface +class WallabagExtension extends AbstractExtension implements GlobalsInterface { private $tokenStorage; private $entryRepository; @@ -24,19 +28,26 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa $this->translator = $translator; } + public function getGlobals() + { + return []; + } + public function getFilters() { return [ - new \Twig_SimpleFilter('removeWww', [$this, 'removeWww']), + new TwigFilter('removeWww', [$this, 'removeWww']), + new TwigFilter('removeScheme', [$this, 'removeScheme']), + new TwigFilter('removeSchemeAndWww', [$this, 'removeSchemeAndWww']), ]; } public function getFunctions() { return [ - new \Twig_SimpleFunction('count_entries', [$this, 'countEntries']), - new \Twig_SimpleFunction('count_tags', [$this, 'countTags']), - new \Twig_SimpleFunction('display_stats', [$this, 'displayStats']), + new TwigFunction('count_entries', [$this, 'countEntries']), + new TwigFunction('count_tags', [$this, 'countTags']), + new TwigFunction('display_stats', [$this, 'displayStats']), ]; } @@ -45,6 +56,16 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa return preg_replace('/^www\./i', '', $url); } + public function removeScheme($url) + { + return preg_replace('#^https?://#i', '', $url); + } + + public function removeSchemeAndWww($url) + { + return $this->removeWww($this->removeScheme($url)); + } + /** * Return number of entries depending of the type (unread, archive, starred or all). * @@ -56,7 +77,7 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa { $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; - if (null === $user || !is_object($user)) { + if (null === $user || !\is_object($user)) { return 0; } @@ -88,7 +109,7 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa $query->useResultCache(true); $query->setResultCacheLifetime($this->lifeTime); - return count($query->getArrayResult()); + return \count($query->getArrayResult()); } /** @@ -100,7 +121,7 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa { $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; - if (null === $user || !is_object($user)) { + if (null === $user || !\is_object($user)) { return 0; } @@ -116,7 +137,7 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa { $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; - if (null === $user || !is_object($user)) { + if (null === $user || !\is_object($user)) { return 0; } @@ -129,7 +150,7 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa $query->useResultCache(true); $query->setResultCacheLifetime($this->lifeTime); - $nbArchives = count($query->getArrayResult()); + $nbArchives = \count($query->getArrayResult()); $interval = $user->getCreatedAt()->diff(new \DateTime('now')); $nbDays = (int) $interval->format('%a') ?: 1;