Passed
Push — develop ( 54bae0...c2e20e )
by Brent
03:18
created

Stitcher::saveCdn()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 3
nop 0
dl 0
loc 17
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace Brendt\Stitcher;
4
5
use Brendt\Stitcher\Exception\TemplateNotFoundException;
6
use Brendt\Stitcher\Parser\Site\SiteParser;
7
use Brendt\Stitcher\Site\Http\Htaccess;
8
use Brendt\Stitcher\Site\Seo\SiteMap;
9
use Brendt\Stitcher\Site\Site;
10
use Symfony\Component\Filesystem\Filesystem;
11
12
/**
13
 * The Stitcher class is the core compiler of every Stitcher application. This class takes care of all routes, pages,
14
 * templates and data, and "stitches" everything together.
15
 *
16
 * The stitching process is done in several steps, with the final result being a fully rendered website in the
17
 * `directories.public` folder.
18
 */
19
class Stitcher
20
{
21
    /**
22
     * @var string
23
     */
24
    private $srcDir;
25
26
    /**
27
     * @var string
28
     */
29
    private $publicDir;
30
31
    /**
32
     * @var string
33
     */
34
    private $templateDir;
35
36
    /**
37
     * @var array
38
     */
39
    private $cdn;
40
41
    /**
42
     * @var bool
43
     */
44
    private $cdnCache;
45
46
    /**
47
     * @var SiteParser
48
     */
49
    private $siteParser;
50
51
    /**
52
     * @var Htaccess
53
     */
54
    private $htaccess;
55
56
    /**
57
     * @var SiteMap
58
     */
59
    private $siteMap;
60
61
    /**
62
     * @param string     $srcDir
63
     * @param string     $publicDir
64
     * @param string     $templateDir
65
     * @param array      $cdn
66
     * @param bool       $cdnCache
67
     * @param SiteParser $siteParser
68
     * @param Htaccess   $htaccess
69
     * @param SiteMap    $siteMap
70
     */
71 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
        $srcDir,
73
        $publicDir,
74
        $templateDir,
75
        array $cdn,
76
        bool $cdnCache,
77
        SiteParser $siteParser,
78
        Htaccess $htaccess,
79
        SiteMap $siteMap
80
    ) {
81
        $this->srcDir = $srcDir;
82
        $this->publicDir = $publicDir;
83
        $this->templateDir = $templateDir;
84
        $this->cdn = $cdn;
85
        $this->cdnCache = $cdnCache;
86
        $this->siteParser = $siteParser;
87
        $this->htaccess = $htaccess;
88
        $this->siteMap = $siteMap;
89
    }
90
91
    /**
92
     * The core stitcher function. This function will compile the configured site and return an array of parsed
93
     * data.
94
     *
95
     * Compiling a site is done in the following steps.
96
     *
97
     *      - Load the site configuration @see \Brendt\Stitcher\Stitcher::loadSite()
98
     *      - Load all available templates @see \Brendt\Stitcher\Stitcher::loadTemplates()
99
     *      - Loop over all pages and transform every page with the configured adapters (in any are set) @see
100
     *      \Brendt\Stitcher\Stitcher::parseAdapters()
101
     *      - Loop over all transformed pages and parse the variables which weren't parsed by the page's adapters. @see
102
     *      \Brendt\Stitcher\Stitcher::parseVariables()
103
     *      - Add all variables to the template engine and render the HTML for each page.
104
     *
105
     * This function takes two optional parameters which are used to render pages on the fly when using the
106
     * developer controller. The first one, `routes` will take a string or array of routes which should be rendered,
107
     * instead of all available routes. The second one, `filterValue` is used to provide a filter when the
108
     * CollectionAdapter is used, and only one entry page should be rendered.
109
     *
110
     * @param string|array $routes
111
     * @param string       $filterValue
112
     *
113
     * @return array
114
     * @throws TemplateNotFoundException
115
     *
116
     * @see \Brendt\Stitcher\Stitcher::save()
117
     * @see \Brendt\Stitcher\Application\DevController::run()
118
     * @see \Brendt\Stitcher\Adapter\CollectionAdapter::transform()
119
     */
120
    public function stitch($routes = [], string $filterValue = null) : array {
121
        if ($filterValue === null) {
122
            $this->htaccess->clearPageBlocks();
123
        }
124
125
        $this->saveCdn();
126
127
        return $this->siteParser->parse((array) $routes, $filterValue);
128
    }
129
130
    public function getSiteMap() : SiteMap {
131
        return $this->siteMap;
132
    }
133
134
    public function loadSite(array $routes = []) : Site {
135
        return $this->siteParser->loadSite($routes);
136
    }
137
138
    /**
139
     * This function will save a stitched output to HTML files in the `directories.public` directory.
140
     *
141
     * @param array $blanket
142
     *
143
     * @see \Brendt\Stitcher\Stitcher::stitch()
144
     */
145
    public function save(array $blanket) {
146
        $fs = new Filesystem();
147
148
        foreach ($blanket as $path => $page) {
149
            if ($path === '/') {
150
                $path = 'index';
151
            }
152
153
            $fs->dumpFile($this->publicDir . "/{$path}.html", $page);
154
        }
155
    }
156
157
    public function saveHtaccess() : Stitcher {
158
        $fs = new Filesystem();
159
        $fs->dumpFile("{$this->publicDir}/.htaccess", $this->htaccess->parse());
160
161
        return $this;
162
    }
163
164
    public function saveSitemap() : Stitcher {
165
        if (!$this->siteMap->isEnabled()) {
166
            return $this;
167
        }
168
169
        $fs = new Filesystem();
170
        $fs->dumpFile("{$this->publicDir}/sitemap.xml", $this->siteMap->render());
171
172
        return $this;
173
    }
174
175
    public function saveCdn() : Stitcher {
176
        $fs = new Filesystem();
177
178
        foreach ($this->cdn as $resource) {
179
            $resource = trim($resource, '/');
180
            $publicResourcePath = "{$this->publicDir}/{$resource}";
181
182
            if ($this->cdnCache && $fs->exists($publicResourcePath)) {
183
                continue;
184
            }
185
186
            $sourceResourcePath = "{$this->srcDir}/{$resource}";
187
            $this->copyCdnFiles($sourceResourcePath, $publicResourcePath);
188
        }
189
190
        return $this;
191
    }
192
193
    private function copyCdnFiles($sourcePath, $publicPath) {
194
        $fs = new Filesystem();
195
196
        if (is_dir($sourcePath)) {
197
            $fs->mirror($sourcePath, $publicPath);
198
        } else {
199
            $fs->copy($sourcePath, $publicPath, true);
200
        }
201
    }
202
}
203
204
205