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

68 lines
2 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 array();
}
2016-09-01 18:20:12 +00:00
$unreadEntries = $this->repository->enableCache(
$this->repository->getBuilderForUnreadByUser($user->getId())->getQuery()
);
$starredEntries = $this->repository->enableCache(
$this->repository->getBuilderForStarredByUser($user->getId())->getQuery()
);
$archivedEntries = $this->repository->enableCache(
$this->repository->getBuilderForArchiveByUser($user->getId())->getQuery()
);
$allEntries = $this->repository->enableCache(
$this->repository->getBuilderForAllByUser($user->getId())->getQuery()
);
return array(
2016-09-01 18:20:12 +00:00
'unreadEntries' => count($unreadEntries->getResult()),
'starredEntries' => count($starredEntries->getResult()),
'archivedEntries' => count($archivedEntries->getResult()),
'allEntries' => count($allEntries->getResult()),
);
}
2015-09-29 20:59:44 +00:00
public function getName()
{
return 'wallabag_extension';
}
}