ViewEngine::__invoke()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 9.344
c 0
b 0
f 0
cc 4
nc 6
nop 1
1
<?php
2
3
/**
4
 * @author Jared King <[email protected]>
5
 *
6
 * @link http://jaredtking.com
7
 *
8
 * @copyright 2015 Jared King
9
 * @license MIT
10
 */
11
namespace Infuse\Services;
12
13
use Infuse\View;
14
15
class ViewEngine
16
{
17
    public function __construct($app)
18
    {
19
        View::inject($app);
20
    }
21
22
    public function __invoke($app)
23
    {
24
        $config = $app['config'];
25
26
        // get the view-related directories
27
        $assetsDir = $config->get('dirs.assets');
28
        $tempDir = $config->get('dirs.temp');
29
        $viewsDir = $config->get('dirs.views');
30
31
        // use PHP view engine by default
32
        $class = $config->get('views.engine');
33
        if (!$class) {
34
            $class = 'Infuse\ViewEngine\PHP';
35
        }
36
37
        // Instantiate the engine, and pass in any special configuration
38
        if ($class === 'Infuse\ViewEngine\Smarty') {
39
            $smartyTemp = "$tempDir/smarty";
40
            $engine = new $class($viewsDir, $smartyTemp, "$smartyTemp/cache");
41
        } else if ($class == 'Infuse\ViewEngine\Twig') {
42
            $twigTemp = "$tempDir/twig";
43
            $twigParams = (array) $config->get('views.twigConfig');
44
            $twigParams = array_replace(['cache' => $twigTemp], $twigParams);
45
            $engine = new $class($viewsDir, $twigParams);
46
        } else {
47
            $engine = new $class($viewsDir);
48
        }
49
50
        // static assets
51
        $assetsUrl = $config->get('assets.base_url');
52
        $engine->setAssetMapFile("$assetsDir/static.assets.json")
53
            ->setAssetBaseUrl($assetsUrl)
54
            ->setGlobalParameters(['app' => $app]);
55
56
        return $engine;
57
    }
58
}
59