Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Twig
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
2 / 2
3
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 getEnvironment
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Miniframe\Middleware;
4
5use Miniframe\Core\AbstractMiddleware;
6use Miniframe\Core\Config;
7use Miniframe\Core\Request;
8use Twig\Environment;
9use Twig\Loader\FilesystemLoader;
10
11class Twig extends AbstractMiddleware
12{
13    /**
14     * Reference to the Twig Environment
15     *
16     * @var Environment
17     */
18    private $twigEnvironment;
19
20    /**
21     * Initializes the Twig Bundle
22     *
23     * @param Request $request Reference to the Request object.
24     * @param Config  $config  Reference to the Config object.
25     */
26    public function __construct(Request $request, Config $config)
27    {
28        parent::__construct($request, $config);
29
30        // Fetch required config values
31        $baseHref = $config->get('framework', 'base_href');
32        $templatePaths = $config->getPath('twig', 'template_path');
33        $cachePath = $config->has('twig', 'cache_path') ? $config->getPath('twig', 'cache_path') : false;
34
35        // Initialize Twig
36        $twig = new Environment(new FilesystemLoader($templatePaths), [
37            'cache' => $cachePath,
38        ]);
39        $twig->addGlobal('baseHref', $baseHref);
40        $this->twigEnvironment = $twig;
41    }
42
43    /**
44     * Gets the Twig Environment
45     *
46     * @return Environment
47     */
48    public function getEnvironment(): Environment
49    {
50        return $this->twigEnvironment;
51    }
52}