Passed
Push — master ( 8f9171...ee6060 )
by Brent
03:57
created

Stitcher::prepareCdn()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 4
nop 0
dl 0
loc 19
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace Brendt\Stitcher;
4
5
use Brendt\Stitcher\Exception\TemplateNotFoundException;
6
use Brendt\Stitcher\Site\Http\Htaccess;
7
use Brendt\Stitcher\Site\Site;
8
use Symfony\Component\Config\FileLocator;
9
use Symfony\Component\DependencyInjection\ContainerBuilder;
10
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
11
use Symfony\Component\Filesystem\Filesystem;
12
use Symfony\Component\Yaml\Yaml;
13
14
/**
15
 * The Stitcher class is the core compiler of every Stitcher application. This class takes care of all routes, pages,
16
 * templates and data, and "stitches" everything together.
17
 *
18
 * The stitching process is done in several steps, with the final result being a fully rendered website in the
19
 * `directories.public` folder.
20
 */
21
class Stitcher
22
{
23
    /**
24
     * @var string
25
     */
26
    private $srcDir;
27
28
    /**
29
     * @var string
30
     */
31
    private $publicDir;
32
33
    /**
34
     * @var string
35
     */
36
    private $templateDir;
37
38
    /**
39
     * @var array
40
     */
41
    private $cdn;
42
43
    /**
44
     * @var bool
45
     */
46
    private $cdnCache;
47
48
    /**
49
     * @var SiteParser
50
     */
51
    private $siteParser;
52
53
    /**
54
     * @var Htaccess
55
     */
56
    private $htaccess;
57
58
    /**
59
     * @param string     $srcDir
60
     * @param string     $publicDir
61
     * @param string     $templateDir
62
     * @param array      $cdn
63
     * @param bool       $cdnCache
64
     * @param SiteParser $siteParser
65
     * @param Htaccess   $htaccess
66
     */
67
    public function __construct(
68
        $srcDir,
69
        $publicDir,
70
        $templateDir,
71
        array $cdn,
72
        bool $cdnCache,
73
        SiteParser $siteParser,
74
        Htaccess $htaccess
75
    ) {
76
        $this->srcDir = $srcDir;
77
        $this->publicDir = $publicDir;
78
        $this->templateDir = $templateDir;
79
        $this->cdn = $cdn;
80
        $this->cdnCache = $cdnCache;
81
        $this->siteParser = $siteParser;
82
        $this->htaccess = $htaccess;
83
    }
84
85
    /**
86
     * The core stitcher function. This function will compile the configured site and return an array of parsed
87
     * data.
88
     *
89
     * Compiling a site is done in the following steps.
90
     *
91
     *      - Load the site configuration @see \Brendt\Stitcher\Stitcher::loadSite()
92
     *      - Load all available templates @see \Brendt\Stitcher\Stitcher::loadTemplates()
93
     *      - Loop over all pages and transform every page with the configured adapters (in any are set) @see
94
     *      \Brendt\Stitcher\Stitcher::parseAdapters()
95
     *      - Loop over all transformed pages and parse the variables which weren't parsed by the page's adapters. @see
96
     *      \Brendt\Stitcher\Stitcher::parseVariables()
97
     *      - Add all variables to the template engine and render the HTML for each page.
98
     *
99
     * This function takes two optional parameters which are used to render pages on the fly when using the
100
     * developer controller. The first one, `routes` will take a string or array of routes which should be rendered,
101
     * instead of all available routes. The second one, `filterValue` is used to provide a filter when the
102
     * CollectionAdapter is used, and only one entry page should be rendered.
103
     *
104
     * @param string|array $routes
105
     * @param string       $filterValue
106
     *
107
     * @return array
108
     * @throws TemplateNotFoundException
109
     *
110
     * @see \Brendt\Stitcher\Stitcher::save()
111
     * @see \Brendt\Stitcher\Application\DevController::run()
112
     * @see \Brendt\Stitcher\Adapter\CollectionAdapter::transform()
113
     */
114
    public function stitch($routes = [], string $filterValue = null) : array {
115
        if ($filterValue === null) {
116
            $this->htaccess->clearPageBlocks();
117
        }
118
119
        $this->prepareCdn();
120
121
        return $this->siteParser->parse((array) $routes, $filterValue);
122
    }
123
124
    /**
125
     * @param array $routes
126
     *
127
     * @return Site
128
     */
129
    public function loadSite(array $routes = []) : Site {
130
        return $this->siteParser->loadSite($routes);
131
    }
132
133
    /**
134
     * This function will save a stitched output to HTML files in the `directories.public` directory.
135
     *
136
     * @param array $blanket
137
     *
138
     * @see \Brendt\Stitcher\Stitcher::stitch()
139
     */
140
    public function save(array $blanket) {
141
        $fs = new Filesystem();
142
143
        foreach ($blanket as $path => $page) {
144
            if ($path === '/') {
145
                $path = 'index';
146
            }
147
148
            $fs->dumpFile($this->publicDir . "/{$path}.html", $page);
149
        }
150
151
        $fs->dumpFile("{$this->publicDir}/.htaccess", $this->htaccess->parse());
152
    }
153
154
    /**
155
     * Parse CDN resources and libraries
156
     */
157
    public function prepareCdn() {
158
        $fs = new Filesystem();
159
160
        foreach ($this->cdn as $resource) {
161
            $resource = trim($resource, '/');
162
            $publicResourcePath = "{$this->publicDir}/{$resource}";
163
164
            if ($this->cdnCache && $fs->exists($publicResourcePath)) {
165
                continue;
166
            }
167
168
            $sourceResourcePath = "{$this->srcDir}/{$resource}";
169
            if (is_dir($sourceResourcePath)) {
170
                $fs->mirror($sourceResourcePath, $publicResourcePath);
171
            } else {
172
                $fs->copy($sourceResourcePath, $publicResourcePath, true);
173
            }
174
        }
175
    }
176
}
177
178
179