vendor/kunstmaan/article-bundle/Twig/ArticleTwigExtension.php line 140

Open in your IDE?
  1. <?php
  2. namespace Kunstmaan\ArticleBundle\Twig;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  6. use Symfony\Component\Routing\RouterInterface;
  7. use Twig\Extension\AbstractExtension;
  8. use Twig\TwigFunction;
  9. /**
  10.  * Extension for article bundle.
  11.  */
  12. final class ArticleTwigExtension extends AbstractExtension
  13. {
  14.     /**
  15.      * @var EntityManagerInterface
  16.      */
  17.     private $em;
  18.     /**
  19.      * @var RouterInterface
  20.      */
  21.     private $router;
  22.     public function __construct(EntityManagerInterface $emRouterInterface $router)
  23.     {
  24.         $this->em $em;
  25.         $this->router $router;
  26.     }
  27.     /**
  28.      * Returns a list of functions to add to the existing list.
  29.      *
  30.      * @return array An array of functions
  31.      */
  32.     public function getFunctions(): array
  33.     {
  34.         return [
  35.             new TwigFunction(
  36.                 'get_article_tag_path', [$this'getArticleTagRouterPath']
  37.             ),
  38.             new TwigFunction(
  39.                 'get_article_category_path', [$this'getArticleCategoryRouterPath']
  40.             ),
  41.             new TwigFunction(
  42.                 'get_article_categories', [$this'getCategories']
  43.             ),
  44.             new TwigFunction(
  45.                 'get_article_tags', [$this'getTags']
  46.             ),
  47.         ];
  48.     }
  49.     /**
  50.      * Get tags array for view.
  51.      *
  52.      * @param string $className
  53.      */
  54.     public function getTags(Request $request$className): array
  55.     {
  56.         $context = [];
  57.         $tagRepository $this->em->getRepository($className);
  58.         $context['tags'] = $tagRepository->findBy([], ['name' => 'ASC']);
  59.         $searchTag $request->query->get('tag') ? explode(','$request->query->get('tag')) : null;
  60.         if ($searchTag) {
  61.             $context['activeTag'] = true;
  62.             $context['activeTags'] = $searchTag;
  63.         }
  64.         return $context;
  65.     }
  66.     /**
  67.      * Get categories array for view.
  68.      *
  69.      * @param string $className
  70.      */
  71.     public function getCategories(Request $request$className): array
  72.     {
  73.         $context = [];
  74.         $categoryRepository $this->em->getRepository($className);
  75.         $context['categories'] = $categoryRepository->findBy([], ['name' => 'ASC']);
  76.         $searchCategory $request->query->get('category') ? explode(','$request->query->get('category')) : null;
  77.         if ($searchCategory) {
  78.             $context['activeCategory'] = true;
  79.             $context['activeCategories'] = $searchCategory;
  80.         }
  81.         return $context;
  82.     }
  83.     /**
  84.      * @param string $slug
  85.      * @param string $tag
  86.      * @param string $locale
  87.      * @param array  $parameters
  88.      * @param int    $referenceType
  89.      */
  90.     public function getArticleTagRouterPath($slug$tag$locale$parameters = [], $referenceType UrlGeneratorInterface::ABSOLUTE_PATH): string
  91.     {
  92.         $routeName sprintf('_slug_tag_%s'$locale);
  93.         return $this->getArticleRouterPath($routeName'tag'$slug$tag$locale$parameters$referenceType);
  94.     }
  95.     /**
  96.      * @param string $slug
  97.      * @param string $category
  98.      * @param string $locale
  99.      * @param array  $parameters
  100.      * @param int    $referenceType
  101.      */
  102.     public function getArticleCategoryRouterPath($slug$category$locale$parameters = [], $referenceType UrlGeneratorInterface::ABSOLUTE_PATH): string
  103.     {
  104.         $routeName sprintf('_slug_category_%s'$locale);
  105.         return $this->getArticleRouterPath($routeName'category'$slug$category$locale$parameters$referenceType);
  106.     }
  107.     public function getName(): string
  108.     {
  109.         return 'article_twig_extension';
  110.     }
  111.     /**
  112.      * @param string $routeName
  113.      * @param string $type
  114.      * @param string $slug
  115.      * @param string $tagOrCategory
  116.      * @param string $locale
  117.      * @param array  $parameters
  118.      * @param int    $referenceType
  119.      */
  120.     protected function getArticleRouterPath($routeName$type$slug$tagOrCategory$locale$parameters = [], $referenceType UrlGeneratorInterface::ABSOLUTE_PATH): string
  121.     {
  122.         if (!$this->articleRouteExists($type$locale)) {
  123.             $routeName '_slug';
  124.         }
  125.         if (!isset($parameters[$type])) {
  126.             $parameters[$type] = $tagOrCategory;
  127.         }
  128.         if (!isset($parameters['url'])) {
  129.             $parameters['url'] = $slug;
  130.         }
  131.         if (!isset($parameters['_locale'])) {
  132.             $parameters['_locale'] = $locale;
  133.         }
  134.         return $this->router->generate($routeName$parameters$referenceType);
  135.     }
  136.     /**
  137.      * @param string $type
  138.      * @param string $locale
  139.      */
  140.     protected function articleRouteExists($type$locale): bool
  141.     {
  142.         $routeName sprintf('_slug_%s_%s'$type$locale);
  143.         try {
  144.             return !\is_null($this->router->getRouteCollection()->get($routeName));
  145.         } catch (\Exception $e) {
  146.             return false;
  147.         }
  148.     }
  149. }