@@ -74,7 +74,7 @@ class Html5MiniTemplate
7474 */
7575 public function __construct ()
7676 {
77- $ this ->markup = file_get_contents (__DIR__ . '/../resources/template.html ' );
77+ $ this ->markup = $ this -> getFileContent (__DIR__ . '/../resources/template.html ' );
7878 }
7979
8080 /**
@@ -87,7 +87,7 @@ protected function parseMarkup()
8787 $ markup = $ this ->markup ;
8888 $ content = $ this ->content ;
8989
90- // no message set = early return example template
90+ // No message set = early return example template
9191 if ($ content === null ) {
9292 return self ::getTemplate ();
9393 }
@@ -113,11 +113,27 @@ protected function parseMarkup()
113113 $ markup = preg_replace ('/<\/head>/ ' , $ this ->additionalMetadata . '</head> ' , $ markup );
114114
115115 // Content
116+ /** @var string $markup */
116117 $ markup = preg_replace ('/<body>(.*?)<\/body>/is ' , '<body> ' . $ content . '</body> ' , $ markup );
117118
119+ // That's it
118120 return $ markup ;
119121 }
120122
123+ /**
124+ * Wrap file_get_contents function and throw errors if needed
125+ *
126+ * @param string $file File-URI to read its content from
127+ * @return string
128+ */
129+ protected function getFileContent (string $ file ): string
130+ {
131+ if (($ content = file_get_contents ($ file )) === false ) {
132+ throw new \RuntimeException ('Can not access file to read its content ' );
133+ }
134+ return $ content ;
135+ }
136+
121137 /**
122138 * Get markup of HTML5 document
123139 *
@@ -135,7 +151,11 @@ public function getMarkup()
135151 */
136152 public static function getTemplate ()
137153 {
138- return file_get_contents (__DIR__ . '/../resources/template.html ' );
154+ if (($ content = file_get_contents (__DIR__ . '/../resources/template.html ' )) === false ) {
155+ throw new \RuntimeException ('Can not access file to read its content ' );
156+ }
157+ return $ content ;
158+
139159 }
140160
141161 /**
@@ -209,23 +229,23 @@ protected function getStylesheetUrl()
209229 }
210230
211231 /**
212- * Get stylesheet content
232+ * Get stylesheet content and cache it in filesystem
213233 *
214234 * @return string
215235 */
216236 protected function getStylesheetContent ()
217237 {
218238 $ stylesheetUrl = $ this ->getStylesheetUrl ();
219239
220- // try to get a cached version first
240+ // Try to get a cached version first
221241 $ cachefile = sys_get_temp_dir () . '/ ' . md5 ($ stylesheetUrl ) . '.css ' ;
222242 if (file_exists ($ cachefile ) && (filemtime ($ cachefile ) > (time () - 86400 ))) { // 1 day
223- return file_get_contents ($ cachefile );
243+ return $ this -> getFileContent ($ cachefile );
224244 }
225245
226246 try {
227- $ stylesheetContent = file_get_contents ($ stylesheetUrl ) ?: '' ;
228- // try to cache the file
247+ $ stylesheetContent = $ this -> getFileContent ($ stylesheetUrl ) ?: '' ;
248+ // Cache the file
229249 file_put_contents ($ cachefile , $ stylesheetContent , LOCK_EX );
230250 } catch (\Exception $ e ) {
231251 // Catch exception if resource is not reachable
0 commit comments