Passed
Pull Request — master (#8)
by Jitendra
01:46
created

TwigGenerator::shouldGenerate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Ahc\Phint\Generator;
4
5
use Ahc\Phint\Util\Path;
6
use Symfony\Component\Finder\Finder;
7
8
class TwigGenerator implements GeneratorInterface
9
{
10
    /** @var \Twig_Environment */
11
    protected $twig;
12
13
    /** @var Path */
14
    protected $pathUtil;
15
16
    /** @var string|array */
17
    protected $templatePath;
18
19
    /** @var string */
20
    protected $cachePath;
21
22
    /** @var array Templates required for type 'project' only */
23
    protected $projectTemplates = [
24
        '.env.example' => true,
25
        'package.json' => true,
26
    ];
27
28
    public function __construct($templatePath, $cachePath)
29
    {
30
        $this->templatePath = $templatePath;
31
        $this->cachePath    = $cachePath;
32
        $this->pathUtil     = new Path;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function generate($targetPath, array $parameters, CollisionHandlerInterface $handler = null)
39
    {
40
        if (!$this->twig) {
41
            $this->initTwig();
42
        }
43
44
        $templates = $this->findTemplates($this->templatePath);
45
        foreach ($templates as $template) {
46
            if ($this->shouldGenerate($template, $parameters)) {
47
                $this->doGenerate($template, $targetPath, $parameters, $handler);
48
            }
49
        }
50
    }
51
52
    protected function initTwig()
53
    {
54
        $this->pathUtil->ensureDir($this->cachePath);
55
56
        $options = [
57
            'auto_reload' => true,
58
            'cache'       => $this->cachePath,
59
        ];
60
61
        $this->twig = new \Twig_Environment(
62
            new \Twig_Loader_Filesystem($this->templatePath),
63
            $options
64
        );
65
    }
66
67
    protected function findTemplates($templatePath)
68
    {
69
        $finder    = new Finder;
70
        $templates = [];
71
72
        $finder->files()->ignoreDotFiles(false)->filter(function ($file) {
73
            return \substr($file, -5) === '.twig';
74
        });
75
76
        foreach ($finder->in($templatePath) as $file) {
77
            $templates[] = (string) $file;
78
        }
79
80
        return $templates;
81
    }
82
83
    protected function doGenerate($template, $targetPath, array $parameters, CollisionHandlerInterface $handler = null)
84
    {
85
        $relativePath = $this->pathUtil->getRelativePath($template, $this->templatePath);
86
        $targetFile   = $targetPath . '/' . str_replace('.twig', '', $relativePath);
87
        $targetDir    = \dirname($targetFile);
88
        $content      = $this->twig->render($relativePath, $parameters);
89
90
        if (\is_file($targetFile) && $handler) {
91
            $handler->handle($targetFile, $content, $parameters);
92
93
            return;
94
        }
95
96
        $this->pathUtil->ensureDir($targetDir);
97
98
        $this->pathUtil->writeFile($targetFile, $content);
99
    }
100
101
    protected function shouldGenerate($template, array $parameters)
102
    {
103
        $name = basename($template, '.twig');
104
105
        if (isset($this->projectTemplates[$name])) {
106
            return $parameters['type'] === 'project';
107
        }
108
109
        return true;
110
    }
111
}
112