Render::renderFile()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 0
cts 9
cp 0
rs 9.7
c 0
b 0
f 0
cc 1
nc 1
nop 4
crap 2
1
<?php
2
3
/**
4
 * This file is part of the Clover to Html package.
5
 *
6
 * (c) Stéphane Demonchaux <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace CloverToHtml;
12
13
class Render
14
{
15
    /**
16
     * @var TwigCreator
17
     */
18
    private $twigCreator;
19
    /**
20
     * @var ConfigDAO
21
     */
22
    private $configDAO;
23
24
    /**
25
     * Construct.
26
     *
27
     * @param TwigCreator $twig
28
     * @param ConfigDAO   $configDAO
29
     */
30 4
    public function __construct(TwigCreator $twig, ConfigDAO $configDAO)
31
    {
32 4
        $this->twigCreator = $twig;
33 4
        $this->configDAO   = $configDAO;
34 4
    }
35
36
    /**
37
     * @param Root $root
38
     * @param $target
39
     * @param $templatePath
40
     * @throws \Twig_Error_Loader
41
     * @throws \Twig_Error_Runtime
42
     * @throws \Twig_Error_Syntax
43
     */
44
    public function render(Root $root, $target, $templatePath = null): void
45
    {
46
        if ($templatePath === null) {
47
            $templatePath = __DIR__.'/Template/';
48
        }
49
50
        $twig = $this->twigCreator->createInstance($templatePath);
51
52
        foreach ($root->getFileCollection() as $file) {
53
            $this->renderFile($file, $twig, $target, $root->getBasePath());
54
        }
55
56
        foreach ($root->getDirectoryCollection() as $directory) {
57
            $this->renderDirectory($directory, $root, $twig, $target);
58
            foreach ($directory->getFileCollection() as $file) {
59
                $this->renderFile($file, $twig, $target, $root->getBasePath());
60
            }
61
        }
62
63
        $this->copyAssets($templatePath. 'assets/', $target. '/assets');
64
    }
65
66
    /**
67
     * @param string $templatePath
68
     * @param string $target
69
     */
70
    private function copyAssets($templatePath, $target): void
71
    {
72
        try {
73
            $files = $this->configDAO->findConfig($templatePath, 'files');
74
            foreach ($files as $file) {
75
                $this->createDirIfNotExist($target.'/'.$file);
76
                copy($templatePath.$file, $target.'/'.$file);
77
            }
78
        } catch (\InvalidArgumentException $e) {
79
            // Nothing to do
80
        }
81
    }
82
83
    /**
84
     * @param string $path
85
     */
86
    private function createDirIfNotExist($path): void
87
    {
88
        if (!is_dir(dirname($path)) && !mkdir(dirname($path), 0777, true) && !is_dir(dirname($path))) {
89
            return;
90
        }
91
    }
92
93
    /**
94
     * @param string $base
95
     * @param string $actual
96
     *
97
     * @return string
98
     */
99
    private function assetsPath($base, $actual): string
100
    {
101
        return str_repeat('../', (substr_count($actual, '/') - substr_count($base, '/')) - 2);
102
    }
103
104
    /**
105
     * @param File $file
106
     * @param \Twig_Environment $twig
107
     * @param $target
108
     * @param $basePath
109
     * @throws \Twig_Error_Loader
110
     * @throws \Twig_Error_Runtime
111
     * @throws \Twig_Error_Syntax
112
     */
113
    private function renderFile(File $file, \Twig_Environment $twig, $target, $basePath): void
114
    {
115
        $path = $target.'/'.$file->getDestination($basePath);
116
117
        $this->createDirIfNotExist($path);
118
119
        file_put_contents(
120
            $path,
121
            $twig->render(
122
                'file.twig',
123
                array(
124
                    'file' => $file,
125
                    'assets' => $this->assetsPath($target, $path),
126
                )
127
            )
128
         );
129
    }
130
131
    /**
132
     * @param Directory $directory
133
     * @param Root $root
134
     * @param \Twig_Environment $twig
135
     * @param $target
136
     * @throws \Twig_Error_Loader
137
     * @throws \Twig_Error_Runtime
138
     * @throws \Twig_Error_Syntax
139
     */
140
    private function renderDirectory(Directory $directory, Root $root, \Twig_Environment $twig, $target): void
141
    {
142
        $path = $target.'/'.$directory->getDestination();
143
144
        $this->createDirIfNotExist($path);
145
146
        file_put_contents(
147
            $path,
148
            $twig->render(
149
                'directory.twig',
150
                array(
151
                    'directory' => $directory,
152
                    'directoryCollection' => $root->getAllDirIn($directory),
153
                    'assets' => $this->assetsPath($target, $path),
154
                )
155
            )
156
        );
157
    }
158
}
159