wallabag/src/Wallabag/CoreBundle/Twig/WallabagExtension.php

68 lines
2.1 KiB
PHP
Raw Normal View History

2015-09-29 20:59:44 +00:00
<?php
namespace Wallabag\CoreBundle\Twig;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Wallabag\CoreBundle\Repository\EntryRepository;
class WallabagExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface
2015-09-29 20:59:44 +00:00
{
private $tokenStorage;
private $repository;
public function __construct(EntryRepository $repository = null, TokenStorageInterface $tokenStorage = null)
{
$this->repository = $repository;
$this->tokenStorage = $tokenStorage;
}
2015-09-29 20:59:44 +00:00
public function getFilters()
{
return [
new \Twig_SimpleFilter('removeWww', [$this, 'removeWww']),
];
2015-09-29 20:59:44 +00:00
}
public function removeWww($url)
{
2015-10-01 07:26:52 +00:00
return preg_replace('/^www\./i', '', $url);
2015-09-29 20:59:44 +00:00
}
public function getGlobals()
{
$user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
if (null === $user || !is_object($user)) {
return [];
}
2016-09-01 18:20:12 +00:00
$unreadEntries = $this->repository->enableCache(
$this->repository->getBuilderForUnreadByUser($user->getId())->select('COUNT(e.id)')->getQuery()
2016-09-01 18:20:12 +00:00
);
$starredEntries = $this->repository->enableCache(
$this->repository->getBuilderForStarredByUser($user->getId())->select('COUNT(e.id)')->getQuery()
2016-09-01 18:20:12 +00:00
);
$archivedEntries = $this->repository->enableCache(
$this->repository->getBuilderForArchiveByUser($user->getId())->select('COUNT(e.id)')->getQuery()
2016-09-01 18:20:12 +00:00
);
$allEntries = $this->repository->enableCache(
$this->repository->getBuilderForAllByUser($user->getId())->select('COUNT(e.id)')->getQuery()
2016-09-01 18:20:12 +00:00
);
return [
'unreadEntries' => $unreadEntries->getSingleScalarResult(),
'starredEntries' => $starredEntries->getSingleScalarResult(),
'archivedEntries' => $archivedEntries->getSingleScalarResult(),
'allEntries' => $allEntries->getSingleScalarResult(),
];
}
2015-09-29 20:59:44 +00:00
public function getName()
{
return 'wallabag_extension';
}
}