1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Burzum\ZurbInky\View; |
5
|
|
|
|
6
|
|
|
use Cake\View\View; |
7
|
|
|
use Pelago\Emogrifier; |
8
|
|
|
use Pinky; |
|
|
|
|
9
|
|
|
use RuntimeException; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* InkyView |
13
|
|
|
* |
14
|
|
|
* @link https://github.com/zurb/inky |
15
|
|
|
* @link https://github.com/lorenzo/pinky |
16
|
|
|
* @link https://github.com/MyIntervals/emogrifier |
17
|
|
|
*/ |
18
|
|
|
class InkyView extends View |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* CSS |
22
|
|
|
* |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $_css; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Emogrifier CSS Parser Instance |
29
|
|
|
* |
30
|
|
|
* @link https://github.com/MyIntervals/emogrifier |
31
|
|
|
* @var \Pelago\Emogrifier |
32
|
|
|
*/ |
33
|
|
|
protected $_emogriefer; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @inheritDoc |
37
|
|
|
*/ |
38
|
2 |
|
public function initialize() |
39
|
|
|
{ |
40
|
2 |
|
parent::initialize(); |
41
|
|
|
|
42
|
2 |
|
$this->_emogriefer = new Emogrifier(); |
|
|
|
|
43
|
2 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @inheritDoc |
47
|
|
|
*/ |
48
|
2 |
|
public function render($view = null, $layout = null) |
49
|
|
|
{ |
50
|
2 |
|
$output = parent::render($view, $layout); |
51
|
2 |
|
$output = Pinky\transformString($output)->saveHTML(); |
|
|
|
|
52
|
|
|
|
53
|
2 |
|
if (!empty($this->_css)) { |
54
|
1 |
|
$this->getEmogriefer()->setCss($this->_css); |
|
|
|
|
55
|
1 |
|
$this->getEmogriefer()->setHtml($output); |
56
|
|
|
|
57
|
1 |
|
return $this->getEmogriefer()->emogrify(); |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
return $output; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Gets the Emogriefer instance |
65
|
|
|
* |
66
|
|
|
* @return \Pelago\Emogrifier |
67
|
|
|
*/ |
68
|
1 |
|
public function getEmogriefer() |
69
|
|
|
{ |
70
|
1 |
|
return $this->_emogriefer; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Sets multiple CSS files |
75
|
|
|
* |
76
|
|
|
* @param array $cssFiles A list of CSS files to read |
77
|
|
|
* @return $this |
78
|
|
|
*/ |
79
|
1 |
|
public function setCssFiles(array $cssFiles) |
80
|
|
|
{ |
81
|
1 |
|
$css = ''; |
82
|
1 |
|
foreach ($cssFiles as $file) { |
83
|
1 |
|
$css .= $this->_getCssFromFile($file); |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
$this->setCss($css); |
87
|
|
|
|
88
|
1 |
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Sets the CSS |
93
|
|
|
* |
94
|
|
|
* @param string $css CSS File |
95
|
|
|
* @return $this |
96
|
|
|
*/ |
97
|
1 |
|
public function setCss(string $css) |
98
|
|
|
{ |
99
|
1 |
|
$this->_css .= $css; |
100
|
|
|
|
101
|
1 |
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
|
|
|
|
105
|
|
|
* Gets the CSS from a file |
106
|
|
|
* |
107
|
|
|
* @param string $css CSS File |
|
|
|
|
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
1 |
|
protected function _getCssFromFile(string $file) : string |
|
|
|
|
111
|
|
|
{ |
112
|
1 |
|
$file = WWW_ROOT . $this->Url->css($file, ['timestamp' => false]); |
113
|
|
|
|
114
|
1 |
|
if (!file_exists($file)) { |
115
|
|
|
throw new RuntimeException(sprintf('The CSS file `%s` does not exist.', $file)); |
116
|
|
|
} |
117
|
|
|
|
118
|
1 |
|
$css = file_get_contents($file); |
119
|
|
|
|
120
|
|
|
// Emogriefer works only with UTF-8! |
121
|
1 |
|
if (!mb_check_encoding($css, 'UTF-8')) { |
122
|
|
|
throw new RuntimeException(sprintf('The CSS file `$css` must be UTF-8 encoded!', $file)); |
123
|
|
|
} |
124
|
|
|
|
125
|
1 |
|
return $css; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|