Completed
Push — master ( bae8f6...ca8f6d )
by Rougin
06:54
created

Website   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 259
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 30
lcom 2
cbo 8
dl 0
loc 259
ccs 76
cts 76
cp 1
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 3
A compile() 0 20 3
A content() 0 4 1
A filter() 0 6 1
A helper() 0 6 1
A helpers() 0 4 1
A page() 0 6 1
A renderer() 0 4 1
A transfer() 0 18 4
A clear() 0 14 4
A folder() 0 14 4
A html() 0 18 3
A path() 0 14 3
1
<?php
2
3
namespace Staticka;
4
5
use Staticka\Content\ContentInterface;
6
use Staticka\Content\MarkdownContent;
7
use Staticka\Filter\FilterInterface;
8
use Staticka\Helper\HelperInterface;
9
use Zapheus\Provider\Configuration;
10
use Zapheus\Renderer\Renderer;
11
use Zapheus\Renderer\RendererInterface;
12
13
/**
14
 * Website
15
 *
16
 * @package Staticka
17
 * @author  Rougin Royce Gutib <[email protected]>
18
 */
19
class Website extends Configuration
20
{
21
    /**
22
     * @var \Staticka\Content\ContentInterface
23
     */
24
    protected $content;
25
26
    /**
27
     * @var \Staticka\Helper\HelperInterface[]
28
     */
29
    protected $helpers = array();
30
31
    /**
32
     * @var \Staticka\Filter\FilterInterface[]
33
     */
34
    protected $filters = array();
35
36
    /**
37
     * @var string
38
     */
39
    protected $output = '';
40
41
    /**
42
     * @var \Staticka\Page[]
43
     */
44
    protected $pages = array();
45
46
    /**
47
     * @var \Zapheus\Renderer\RendererInterface
48
     */
49
    protected $renderer;
50
51
    /**
52
     * Initializes the Staticka instance.
53
     *
54
     * @param \Zapheus\Renderer\RendererInterface|null $renderer
55
     * @param \Staticka\Content\ContentInterface|null  $content
56
     */
57 21
    public function __construct(RendererInterface $renderer = null, ContentInterface $content = null)
58
    {
59 21
        $this->renderer = $renderer === null ? new Renderer(getcwd()) : $renderer;
60
61 21
        $this->content = $content === null ? new MarkdownContent : $content;
62 21
    }
63
64
    /**
65
     * Compiles the specified pages into HTML output.
66
     *
67
     * @param  string $output
68
     * @return self
69
     */
70 15
    public function compile($output)
71
    {
72 15
        file_exists($output) || mkdir($output);
73
74 15
        $this->clear((string) $output);
75
76 15
        $this->output = (string) $output;
77
78 15
        foreach ((array) $this->pages as $page) {
79 15
            $folder = $this->folder($output, $page->uris());
80
81 15
            $html = (string) $this->html($page);
82
83 15
            $path = $this->path($output . '/' . $folder);
84
85 15
            file_put_contents($path . 'index.html', $html);
86 10
        }
87
88 15
        return $this;
89
    }
90
91
    /**
92
     * Returns the content instance.
93
     *
94
     * @return \Staticka\Content\ContentInterface
95
     */
96 6
    public function content()
97
    {
98 6
        return $this->content;
99
    }
100
101
    /**
102
     * Adds a filter instance.
103
     *
104
     * @param  \Staticka\Filter\FilterInterface $filter
105
     * @return self
106
     */
107 3
    public function filter(FilterInterface $filter)
108
    {
109 3
        $this->filters[] = $filter;
110
111 3
        return $this;
112
    }
113
114
    /**
115
     * Adds a helper instance.
116
     *
117
     * @param  \Staticka\Helper\HelperInterface $helper
118
     * @return self
119
     */
120 3
    public function helper(HelperInterface $helper)
121
    {
122 3
        $this->helpers[$helper->name()] = $helper;
123
124 3
        return $this;
125
    }
126
127
    /**
128
     * Returns an array of helpers.
129
     *
130
     * @return \Staticka\Helper\HelperInterface[]
131
     */
132 3
    public function helpers()
133
    {
134 3
        return $this->helpers;
135
    }
136
137
    /**
138
     * Creates a new page.
139
     *
140
     * @param  string $file
141
     * @param  array  $data
142
     * @return self
143
     */
144 21
    public function page($file, array $data = array())
145
    {
146 21
        $this->pages[] = new Page($file, $data);
147
148 21
        return $this;
149
    }
150
151
    /**
152
     * Returns the renderer instance.
153
     *
154
     * @return \Zapheus\Renderer\RendererInterface
155
     */
156 3
    public function renderer()
157
    {
158 3
        return $this->renderer;
159
    }
160
161
    /**
162
     * Transfers files from a directory into another path.
163
     *
164
     * @param  string      $source
165
     * @param  string|null $path
166
     * @return void
167
     */
168 3
    public function transfer($source, $path = null)
169
    {
170 3
        $path = $path === null ? $this->output : (string) $path;
171
172 3
        $source = str_replace('/', DIRECTORY_SEPARATOR, $source);
173
174 3
        $path = str_replace('/', DIRECTORY_SEPARATOR, $path);
175
176 3
        $directory = new \RecursiveDirectoryIterator($source, 4096);
177
178 3
        $iterator = new \RecursiveIteratorIterator($directory, 1);
179
180 3
        foreach ($iterator as $file) {
181 3
            $to = str_replace($source, $path, $from = $file->getRealPath());
182
183 3
            $file->isDir() ? $this->transfer($from, $to) : copy($from, $to);
184 2
        }
185 3
    }
186
187
    /**
188
     * Removes the files recursively from the specified directory.
189
     *
190
     * @param  string $path
191
     * @return void
192
     */
193 15
    protected function clear($path)
194
    {
195 15
        $directory = new \RecursiveDirectoryIterator($path, 4096);
196
197 15
        $iterator = new \RecursiveIteratorIterator($directory, 2);
198
199 15
        foreach ($iterator as $file) {
200 12
            $git = strpos($file->getRealPath(), '.git') !== false;
201
202 12
            $path = (string) $file->getRealPath();
203
204 12
            $git || ($file->isDir() ? rmdir($path) : unlink($path));
205 10
        }
206 15
    }
207
208
    /**
209
     * Returns the whole folder path based from specified URIs.
210
     * Also creates the specified folder if it doesn't exists.
211
     *
212
     * @param  string $output
213
     * @param  array  $uris
214
     * @return string
215
     */
216 15
    protected function folder($output, array $uris)
217
    {
218 15
        $folder = (string) '';
219
220 15
        foreach ((array) $uris as $uri) {
221 6
            $directory = $output . '/' . (string) $folder;
222
223 6
            file_exists($directory) ?: mkdir($directory);
224
225 6
            $folder === $uri ?: $folder .= '/' . $uri;
226 10
        }
227
228 15
        return $folder;
229
    }
230
231
    /**
232
     * Converts the specified page into HTML.
233
     *
234
     * @param  \Staticka\Page $page
235
     * @return string
236
     */
237 15
    protected function html(Page $page)
238
    {
239 15
        $html = $this->content->make($content = $page->content());
240
241 15
        if (($name = $page->layout()) !== null) {
242 3
            $data = array_merge($this->helpers(), (array) $page->data());
243
244 3
            $layout = new Layout($this->renderer, $this, $data);
245
246 3
            $html = (string) $layout->render($name, (string) $content);
247 2
        }
248
249 15
        foreach ($this->filters as $filter) {
250 3
            $html = $filter->filter($html);
251 10
        }
252
253 15
        return $html;
254
    }
255
256
    /**
257
     * Replaces the slashes with the DIRECTORY_SEPARATOR.
258
     * Also creates the directory if it doesn't exists.
259
     *
260
     * @param  string $folder
261
     * @return string
262
     */
263 15
    protected function path($folder)
264
    {
265 15
        $separator = (string) DIRECTORY_SEPARATOR;
266
267 15
        $search = array('\\', '/', (string) '\\\\');
268
269 15
        $path = str_replace($search, $separator, $folder);
270
271 15
        file_exists($path) || mkdir((string) $path);
272
273 15
        $exists = in_array(substr($path, -1), $search);
274
275 15
        return $exists ? $path : $path . $separator;
276
    }
277
}
278