Test Failed
Push — develop ( 4544b5...b9d221 )
by Brent
03:13
created

Stitcher::getSiteMap()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
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->prepareCdn();
126
127
        return $this->siteParser->parse((array) $routes, $filterValue);
128
    }
129
130
    /**
131
     * @param array $routes
132
     *
133
     * @return Site
134
     */
135
    public function loadSite(array $routes = []) : Site {
136
        return $this->siteParser->loadSite($routes);
137
    }
138
139
    /**
140
     * This function will save a stitched output to HTML files in the `directories.public` directory.
141
     *
142
     * @param array $blanket
143
     *
144
     * @see \Brendt\Stitcher\Stitcher::stitch()
145
     */
146
    public function save(array $blanket) {
147
        $fs = new Filesystem();
148
149
        foreach ($blanket as $path => $page) {
150
            if ($path === '/') {
151
                $path = 'index';
152
            }
153
154
            $fs->dumpFile($this->publicDir . "/{$path}.html", $page);
155
        }
156
    }
157
158
    /**
159
     * Save .htaccess
160
     *
161
     * @return Stitcher
162
     */
163
    public function saveHtaccess() : Stitcher {
164
        $fs = new Filesystem();
165
        $fs->dumpFile("{$this->publicDir}/.htaccess", $this->htaccess->parse());
166
167
        return $this;
168
    }
169
170
    /**
171
     * Save the sitemap if enabled
172
     *
173
     * @return Stitcher
174
     */
175
    public function saveSitemap() : Stitcher {
176
        if (!$this->siteMap->isEnabled()) {
177
            return $this;
178
        }
179
180
        $fs = new Filesystem();
181
        $fs->dumpFile("{$this->publicDir}/sitemap.xml", $this->siteMap->render());
182
183
        return $this;
184
    }
185
186
    /**
187
     * Parse CDN resources and libraries
188
     */
189
    public function prepareCdn() {
190
        $fs = new Filesystem();
191
192
        foreach ($this->cdn as $resource) {
193
            $resource = trim($resource, '/');
194
            $publicResourcePath = "{$this->publicDir}/{$resource}";
195
196
            if ($this->cdnCache && $fs->exists($publicResourcePath)) {
197
                continue;
198
            }
199
200
            $sourceResourcePath = "{$this->srcDir}/{$resource}";
201
            if (is_dir($sourceResourcePath)) {
202
                $fs->mirror($sourceResourcePath, $publicResourcePath);
203
            } else {
204
                $fs->copy($sourceResourcePath, $publicResourcePath, true);
205
            }
206
        }
207
    }
208
209
    /**
210
     * @return SiteMap
211
     */
212
    public function getSiteMap() : SiteMap {
213
        return $this->siteMap;
214
    }
215
}
216
217
218