@@ -9,20 +9,25 @@ class CssInlinerPlugin implements \Swift_Events_SendListener
99 /**
1010 * @var CssToInlineStyles
1111 */
12- private $ converter ;
12+ protected $ converter ;
1313
1414 /**
15- * @var string
15+ * @var string[]
1616 */
17- protected $ css ;
17+ protected $ cssCache ;
18+
19+ /**
20+ * @var array
21+ */
22+ protected $ options ;
1823
1924 /**
2025 * @param array $options options defined in the configuration file.
2126 */
2227 public function __construct (array $ options )
2328 {
2429 $ this ->converter = new CssToInlineStyles ();
25- $ this ->loadOptions ( $ options) ;
30+ $ this ->options = $ options ;
2631 }
2732
2833 /**
@@ -36,14 +41,16 @@ public function beforeSendPerformed(\Swift_Events_SendEvent $evt)
3641 || ($ message ->getContentType () === 'multipart/alternative ' && $ message ->getBody ())
3742 || ($ message ->getContentType () === 'multipart/mixed ' && $ message ->getBody ())
3843 ) {
39- $ body = $ this ->loadCssFilesFromLinks ($ message ->getBody ());
40- $ message ->setBody ($ this ->converter ->convert ($ body , $ this ->css ));
44+ [$ body , $ cssResources ] = $ this ->messageSieve ($ message ->getBody ());
45+ $ css = $ this ->concatCss ($ cssResources );
46+ $ message ->setBody ($ this ->converter ->convert ($ body , $ css ));
4147 }
4248
4349 foreach ($ message ->getChildren () as $ part ) {
4450 if (strpos ($ part ->getContentType (), 'text/html ' ) === 0 ) {
45- $ body = $ this ->loadCssFilesFromLinks ($ part ->getBody ());
46- $ part ->setBody ($ this ->converter ->convert ($ body , $ this ->css ));
51+ [$ body , $ cssResources ] = $ this ->messageSieve ($ part ->getBody ());
52+ $ css = $ this ->concatCss ($ cssResources );
53+ $ part ->setBody ($ this ->converter ->convert ($ body , $ css ));
4754 }
4855 }
4956 }
@@ -58,37 +65,46 @@ public function sendPerformed(\Swift_Events_SendEvent $evt)
5865 // Do Nothing
5966 }
6067
61- /**
62- * Load the options
63- * @param array $options Options array
64- */
65- public function loadOptions ($ options )
68+ protected function concatCss (array $ cssResources ): string
6669 {
67- if (isset ($ options ['css-files ' ]) && count ($ options ['css-files ' ]) > 0 ) {
68- $ this ->css = '' ;
69- foreach ($ options ['css-files ' ] as $ fileUrl ) {
70- // Fix relative protocols on hrefs. Assume https.
71- if (substr ($ fileUrl , 0 , 2 ) === '// ' ) {
72- $ fileUrl = 'https: ' . $ fileUrl ;
73- }
74-
75- $ this ->css .= file_get_contents ($ fileUrl );
76- }
70+ $ output = '' ;
71+ foreach ($ cssResources as $ cssResource ) {
72+ $ output .= $ this ->fetchCss ($ cssResource );
7773 }
74+
75+ return $ output ;
7876 }
7977
80- /**
81- * Find CSS stylesheet links and load them
82- *
83- * Loads the body of the message and passes
84- * any link stylesheets to $this->css
85- * Removes any link elements
86- *
87- * @return string $message The message
88- */
89- public function loadCssFilesFromLinks ($ message )
78+ protected function fetchCss (string $ filename ): string
79+ {
80+ if (isset ($ this ->cssCache [$ filename ])) {
81+ return $ this ->cssCache [$ filename ];
82+ }
83+
84+ $ fixedFilename = $ filename ;
85+ // Fix relative protocols on hrefs. Assume https.
86+ if (substr ($ filename , 0 , 2 ) === '// ' ) {
87+ $ fixedFilename = 'https: ' . $ filename ;
88+ }
89+
90+ $ content = file_get_contents ($ fixedFilename );
91+ if (! $ content ) {
92+ return '' ;
93+ }
94+
95+ $ this ->cssCache [$ filename ] = $ content ;
96+
97+ return $ content ;
98+ }
99+
100+ protected function messageSieve (string $ message ): array
90101 {
91- $ options ['css-files ' ] = [];
102+ $ cssResources = [];
103+
104+ // Initialize with config defaults, if any
105+ if (isset ($ this ->options ['css-files ' ])) {
106+ $ cssResources = $ this ->options ['css-files ' ];
107+ }
92108
93109 $ dom = new \DOMDocument ();
94110 // set error level
@@ -103,7 +119,7 @@ public function loadCssFilesFromLinks($message)
103119 /** @var \DOMElement $link */
104120 foreach ($ link_tags as $ link ) {
105121 if ($ link ->getAttribute ('rel ' ) === 'stylesheet ' ) {
106- $ options [ ' css-files ' ][] = $ link ->getAttribute ('href ' );
122+ array_push ( $ cssResources , $ link ->getAttribute ('href ' ) );
107123 }
108124 }
109125
@@ -115,12 +131,10 @@ public function loadCssFilesFromLinks($message)
115131 }
116132 }
117133
118- if (count ($ options ['css-files ' ])) {
119- $ this ->loadOptions ($ options );
120-
121- return $ dom ->saveHTML ();
134+ if (count ($ cssResources )) {
135+ return [$ dom ->saveHTML (), $ cssResources ];
122136 }
123137
124- return $ message ;
138+ return [ $ message, []] ;
125139 }
126140}
0 commit comments