]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Wallabag\CoreBundle\Twig; | |
4 | ||
5 | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; | |
6 | use Wallabag\CoreBundle\Repository\EntryRepository; | |
7 | ||
8 | class WallabagExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface | |
9 | { | |
10 | private $tokenStorage; | |
11 | private $repository; | |
12 | ||
13 | public function __construct(EntryRepository $repository = null, TokenStorageInterface $tokenStorage = null) | |
14 | { | |
15 | $this->repository = $repository; | |
16 | $this->tokenStorage = $tokenStorage; | |
17 | } | |
18 | ||
19 | public function getFilters() | |
20 | { | |
21 | return [ | |
22 | new \Twig_SimpleFilter('removeWww', [$this, 'removeWww']), | |
23 | ]; | |
24 | } | |
25 | ||
26 | public function removeWww($url) | |
27 | { | |
28 | return preg_replace('/^www\./i', '', $url); | |
29 | } | |
30 | ||
31 | public function getGlobals() | |
32 | { | |
33 | $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; | |
34 | ||
35 | if (null === $user || !is_object($user)) { | |
36 | return array(); | |
37 | } | |
38 | ||
39 | $unreadEntries = $this->repository->enableCache( | |
40 | $this->repository->getBuilderForUnreadByUser($user->getId())->getQuery() | |
41 | ); | |
42 | ||
43 | $starredEntries = $this->repository->enableCache( | |
44 | $this->repository->getBuilderForStarredByUser($user->getId())->getQuery() | |
45 | ); | |
46 | ||
47 | $archivedEntries = $this->repository->enableCache( | |
48 | $this->repository->getBuilderForArchiveByUser($user->getId())->getQuery() | |
49 | ); | |
50 | ||
51 | $allEntries = $this->repository->enableCache( | |
52 | $this->repository->getBuilderForAllByUser($user->getId())->getQuery() | |
53 | ); | |
54 | ||
55 | return array( | |
56 | 'unreadEntries' => count($unreadEntries->getResult()), | |
57 | 'starredEntries' => count($starredEntries->getResult()), | |
58 | 'archivedEntries' => count($archivedEntries->getResult()), | |
59 | 'allEntries' => count($allEntries->getResult()), | |
60 | ); | |
61 | } | |
62 | ||
63 | public function getName() | |
64 | { | |
65 | return 'wallabag_extension'; | |
66 | } | |
67 | } |