NavBuilder.php 954 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. namespace demo\Services;
  3. use Silex\Application;
  4. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  5. class NavBuilder {
  6. /**
  7. * @var \Twig_Environment
  8. */
  9. protected $twig;
  10. /**
  11. * @var \Symfony\Component\Routing\Generator\UrlGeneratorInterface
  12. */
  13. protected $urlGenerator;
  14. public function __construct(
  15. UrlGeneratorInterface $urlGenerator,
  16. \Twig_Environment $twig
  17. ) {
  18. $this->twig = $twig;
  19. $this->urlGenerator = $urlGenerator;
  20. }
  21. public static function create(Application $app) {
  22. $urlGenerator = $app['url_generator'];
  23. $twig = $app['twig'];
  24. return new static($urlGenerator, $twig);
  25. }
  26. public function build() {
  27. $home = $this->urlGenerator->generate('homepage');
  28. $blog = $this->urlGenerator->generate('blog_list');
  29. $result = [
  30. 'home' => ['url' => $home, 'text' => 'Accueil'],
  31. 'blog' => ['url' => $blog, 'text' => 'Blogs'],
  32. ];
  33. return $result;
  34. }
  35. }