wallabag/src/Wallabag/CoreBundle/Twig/WallabagExtension.php
Jeremy Benoist 543da3e0b7
Instead of selecting the whole data, just count it
Instead of performing a complex select (to retrieve all data for entry, etc...) just select the counter and retrieve it.

Down from ~50ms to ~30ms on the unread page (with 500 items)
2016-09-03 18:11:07 +02:00

68 lines
2.1 KiB
PHP

<?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
{
private $tokenStorage;
private $repository;
public function __construct(EntryRepository $repository = null, TokenStorageInterface $tokenStorage = null)
{
$this->repository = $repository;
$this->tokenStorage = $tokenStorage;
}
public function getFilters()
{
return [
new \Twig_SimpleFilter('removeWww', [$this, 'removeWww']),
];
}
public function removeWww($url)
{
return preg_replace('/^www\./i', '', $url);
}
public function getGlobals()
{
$user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
if (null === $user || !is_object($user)) {
return [];
}
$unreadEntries = $this->repository->enableCache(
$this->repository->getBuilderForUnreadByUser($user->getId())->select('COUNT(e.id)')->getQuery()
);
$starredEntries = $this->repository->enableCache(
$this->repository->getBuilderForStarredByUser($user->getId())->select('COUNT(e.id)')->getQuery()
);
$archivedEntries = $this->repository->enableCache(
$this->repository->getBuilderForArchiveByUser($user->getId())->select('COUNT(e.id)')->getQuery()
);
$allEntries = $this->repository->enableCache(
$this->repository->getBuilderForAllByUser($user->getId())->select('COUNT(e.id)')->getQuery()
);
return [
'unreadEntries' => $unreadEntries->getSingleScalarResult(),
'starredEntries' => $starredEntries->getSingleScalarResult(),
'archivedEntries' => $archivedEntries->getSingleScalarResult(),
'allEntries' => $allEntries->getSingleScalarResult(),
];
}
public function getName()
{
return 'wallabag_extension';
}
}