/
LegacyStylesheetPreviewHandler.php
100 lines (83 loc) · 3.73 KB
/
LegacyStylesheetPreviewHandler.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
namespace Concrete\Core\StyleCustomizer\Preview;
use Concrete\Core\Http\Request;
use Concrete\Core\Http\Response;
use Concrete\Core\Page\CustomStyle;
use Concrete\Core\Page\Page;
use Concrete\Core\Page\Theme\Theme;
use Concrete\Core\StyleCustomizer\Customizer\Customizer;
use Concrete\Core\StyleCustomizer\Normalizer\NormalizedVariableCollection;
use Concrete\Core\StyleCustomizer\Normalizer\NormalizedVariableCollectionFactory;
use Concrete\Core\StyleCustomizer\Preset\PresetInterface;
use Concrete\Core\StyleCustomizer\Style\StyleValueListFactory;
use Concrete\Core\StyleCustomizer\Traits\ExtractPresetFontsFileStyleFromLegacyPresetTrait;
/**
* Used by the legacy customizer, this preview handler loops through all customizable style sheets in the theme's root
* and saves them into the cache, which is then used by the getStylesheet method in the page.
* @deprecated
*/
class LegacyStylesheetPreviewHandler implements PreviewHandlerInterface
{
use ExtractPresetFontsFileStyleFromLegacyPresetTrait;
/**
* @var StyleValueListFactory
*/
protected $styleValueListFactory;
/**
* @var NormalizedVariableCollectionFactory
*/
protected $variableCollectionFactory;
public function __construct(
StyleValueListFactory $styleValueListFactory,
NormalizedVariableCollectionFactory $variableCollectionFactory
) {
$this->styleValueListFactory = $styleValueListFactory;
$this->variableCollectionFactory = $variableCollectionFactory;
}
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);
$this->addPresetFontsFileStyleToStyleValueList($customizer->getType(), $preset, $styleValueList);
$collection = $this->variableCollectionFactory->createFromStyleValueList($styleValueList);
return $this->deliverResponse($customizer, $page, $collection);
}
protected function deliverResponse(Customizer $customizer, Page $page, NormalizedVariableCollection $collection): Response
{
$theme = $customizer->getTheme();
$theme->enablePreviewRequest();
$sheets = $theme->getThemeCustomizableStylesheets();
foreach ($sheets as $sheet) {
$sheet->setVariableCollection($collection);
// we save each sheet to the preview location.
$sheet->output();
}
$request = Request::getInstance();
$request->setCustomRequestUser(-1);
$request->setCurrentPage($page);
$controller = $page->getPageController();
$view = $controller->getViewObject();
$view->setCustomPageTheme($theme);
$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);
return $this->deliverResponse($customizer, $page, $collection);
}
/**
* Used when a page level or custom theme customizer set has been saved and now needs to be re-previewed.
*/
public function getCustomStylePreviewResponse(Customizer $customizer, Page $page, CustomStyle $customStyle): Response
{
$collection = $this->variableCollectionFactory->createFromStyleValueList($customStyle->getValueList());
return $this->deliverResponse($customizer, $page, $collection);
}
}