/
StandardPreviewHandler.php
85 lines (69 loc) · 3.01 KB
/
StandardPreviewHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
namespace Concrete\Core\StyleCustomizer\Preview;
use Concrete\Core\Http\Request;
use Concrete\Core\Http\Response;
use Concrete\Core\Page\Page;
use Concrete\Core\Page\View\Preview\ThemeCustomizerRequest;
use Concrete\Core\StyleCustomizer\Compiler\Compiler;
use Concrete\Core\StyleCustomizer\Customizer\Customizer;
use Concrete\Core\StyleCustomizer\Normalizer\NormalizedVariableCollectionFactory;
use Concrete\Core\StyleCustomizer\Preset\PresetInterface;
use Concrete\Core\StyleCustomizer\Style\StyleValueListFactory;
class StandardPreviewHandler implements PreviewHandlerInterface
{
/**
* @var Compiler
*/
protected $compiler;
/**
* @var StyleValueListFactory
*/
protected $styleValueListFactory;
/**
* @var NormalizedVariableCollectionFactory
*/
protected $variableCollectionFactory;
public function __construct(
Compiler $compiler,
StyleValueListFactory $styleValueListFactory,
NormalizedVariableCollectionFactory $variableCollectionFactory
) {
$this->compiler = $compiler;
$this->styleValueListFactory = $styleValueListFactory;
$this->variableCollectionFactory = $variableCollectionFactory;
}
protected function deliverResponse(Page $page, string $css): Response
{
$request = Request::getInstance();
$request->setCustomRequestUser(-1);
$request->setCurrentPage($page);
$previewRequest = new ThemeCustomizerRequest();
$previewRequest->setCustomCss($css);
$controller = $page->getPageController();
$view = $controller->getViewObject();
$view->setCustomPreviewRequest($previewRequest);
$content = $view->render();
$response = new Response();
$response->setContent($content);
return $response;
}
public function getPresetPreviewResponse(Customizer $customizer, Page $page, PresetInterface $preset): Response
{
$collection = $this->variableCollectionFactory->createFromPreset($customizer, $preset);
$result = $this->compiler->compileFromPreset($customizer, $preset, $collection);
return $this->deliverResponse($page, $result);
}
public function getCustomPreviewResponse(Customizer $customizer, Page $page, PresetInterface $preset, array $requestData): Response
{
$styles = json_decode($requestData['styles'], true);
$styleValueList = $this->styleValueListFactory->createFromRequestArray($customizer->getThemeCustomizableStyleList($preset), $styles);
$collection = $this->variableCollectionFactory->createFromStyleValueList($styleValueList);
$result = $this->compiler->compileFromPreset($customizer, $preset, $collection);
if (isset($requestData['customCss'])) {
// This is custom CSS from the "Custom CSS" button at the bottom of the customizer. This just gets
// appended to the SCSS generated from the theme.
$result .= $requestData['customCss'];
}
return $this->deliverResponse($page, $result);
}
}