1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Brendt\Stitcher; |
4
|
|
|
|
5
|
|
|
use Brendt\Stitcher\Exception\TemplateNotFoundException; |
6
|
|
|
use Brendt\Stitcher\Lib\Browser; |
7
|
|
|
use Brendt\Stitcher\Lib\Cdn; |
8
|
|
|
use Brendt\Stitcher\Parser\Site\SiteParser; |
9
|
|
|
use Brendt\Stitcher\Site\Http\Htaccess; |
10
|
|
|
use Brendt\Stitcher\Site\Seo\SiteMap; |
11
|
|
|
use Brendt\Stitcher\Site\Site; |
12
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
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
|
|
|
private $browser; |
24
|
|
|
private $cdn; |
25
|
|
|
private $siteParser; |
26
|
|
|
private $htaccess; |
27
|
|
|
private $siteMap; |
28
|
|
|
|
29
|
|
|
public function __construct( |
30
|
|
|
Browser $browser, |
31
|
|
|
Cdn $cdn, |
32
|
|
|
SiteParser $siteParser, |
33
|
|
|
Htaccess $htaccess, |
34
|
|
|
SiteMap $siteMap |
35
|
|
|
) { |
36
|
|
|
$this->browser = $browser; |
37
|
|
|
$this->cdn = $cdn; |
38
|
|
|
$this->siteParser = $siteParser; |
39
|
|
|
$this->htaccess = $htaccess; |
40
|
|
|
$this->siteMap = $siteMap; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* The core stitcher function. This function will compile the configured site and return an array of parsed |
45
|
|
|
* data. |
46
|
|
|
* |
47
|
|
|
* Compiling a site is done in the following steps. |
48
|
|
|
* |
49
|
|
|
* - Load the site configuration @see \Brendt\Stitcher\Stitcher::loadSite() |
50
|
|
|
* - Load all available templates @see \Brendt\Stitcher\Stitcher::loadTemplates() |
51
|
|
|
* - Loop over all pages and transform every page with the configured adapters (in any are set) @see |
52
|
|
|
* \Brendt\Stitcher\Stitcher::parseAdapters() |
53
|
|
|
* - Loop over all transformed pages and parse the variables which weren't parsed by the page's adapters. @see |
54
|
|
|
* \Brendt\Stitcher\Stitcher::parseVariables() |
55
|
|
|
* - Add all variables to the template engine and render the HTML for each page. |
56
|
|
|
* |
57
|
|
|
* This function takes two optional parameters which are used to render pages on the fly when using the |
58
|
|
|
* developer controller. The first one, `routes` will take a string or array of routes which should be rendered, |
59
|
|
|
* instead of all available routes. The second one, `filterValue` is used to provide a filter when the |
60
|
|
|
* CollectionAdapter is used, and only one entry page should be rendered. |
61
|
|
|
* |
62
|
|
|
* @param string|array $routes |
63
|
|
|
* @param string $filterValue |
64
|
|
|
* |
65
|
|
|
* @return array |
66
|
|
|
* @throws TemplateNotFoundException |
67
|
|
|
* |
68
|
|
|
* @see \Brendt\Stitcher\Stitcher::save() |
69
|
|
|
* @see \Brendt\Stitcher\Application\DevController::run() |
70
|
|
|
* @see \Brendt\Stitcher\Adapter\CollectionAdapter::transform() |
71
|
|
|
*/ |
72
|
|
|
public function stitch($routes = [], string $filterValue = null) : array { |
73
|
|
|
if ($filterValue === null) { |
74
|
|
|
$this->htaccess->clearPageBlocks(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->cdn->save(); |
78
|
|
|
|
79
|
|
|
return $this->siteParser->parse((array) $routes, $filterValue); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function getSiteMap() : SiteMap { |
83
|
|
|
return $this->siteMap; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function loadSite(array $routes = []) : Site { |
87
|
|
|
return $this->siteParser->loadSite($routes); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* This function will save a stitched output to HTML files in the `directories.public` directory. |
92
|
|
|
* |
93
|
|
|
* @param array $blanket |
94
|
|
|
* |
95
|
|
|
* @see \Brendt\Stitcher\Stitcher::stitch() |
96
|
|
|
*/ |
97
|
|
|
public function save(array $blanket) { |
98
|
|
|
$fs = new Filesystem(); |
99
|
|
|
|
100
|
|
|
foreach ($blanket as $path => $page) { |
101
|
|
|
if ($path === '/') { |
102
|
|
|
$path = 'index'; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$fs->dumpFile($this->browser->getPublicDir() . "/{$path}.html", $page); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function saveHtaccess() : Stitcher { |
110
|
|
|
$fs = new Filesystem(); |
111
|
|
|
$fs->dumpFile("{$this->browser->getPublicDir()}/.htaccess", $this->htaccess->parse()); |
112
|
|
|
|
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function saveSitemap() : Stitcher { |
117
|
|
|
if (!$this->siteMap->isEnabled()) { |
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$fs = new Filesystem(); |
122
|
|
|
$fs->dumpFile("{$this->browser->getPublicDir()}/sitemap.xml", $this->siteMap->render()); |
123
|
|
|
|
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
|
129
|
|
|
|