diff options
author | Matt Butcher <[email protected]> | 2013-04-03 21:34:13 -0500 |
---|---|---|
committer | Matt Butcher <[email protected]> | 2013-04-03 21:34:13 -0500 |
commit | 326f71c3747145b5618788f429559cbac453f114 (patch) | |
tree | 2b26194feea1b0a33ac8a249290ff48880e8bb9b /src/HTML5/Serializer.php | |
parent | 49d2982bf2daef3017539f8cc5ba393ad9f52bf9 (diff) |
Created Serializer methods.
Diffstat (limited to 'src/HTML5/Serializer.php')
-rw-r--r-- | src/HTML5/Serializer.php | 27 |
1 files changed, 25 insertions, 2 deletions
diff --git a/src/HTML5/Serializer.php b/src/HTML5/Serializer.php index a8bc784..207f848 100644 --- a/src/HTML5/Serializer.php +++ b/src/HTML5/Serializer.php @@ -32,16 +32,39 @@ class Serializer { /** * Save to a file. * - * @param string $filename - * The full name to the file. This will overwrite the contents of + * @param mixed $filename + * A file handle resource or the + * full name to the file. This will overwrite the contents of * any file that it finds. */ public function save($filename) { + $close = TRUE; + if (is_resource($filename)) { + $file = $filename; + $close = FALSE; + } + else { + $file = fopen($filename, 'w'); + } + $trav = new Traverser($this->dom, $file); + + $trav->walk(); + + if ($close) { + fclose($file); + } } /** * Return the DOM as an HTML5 string. */ public function saveHTML() { + // We buffer into a temp-file backed memory map. This may or may not be + // faster than writing directly to a string, but it makes the interface + // consistant and will keep memory consumption lower (2MB max for the file + // buffer). + $stream = fopen('php://temp'); + $this->save($stream); + return stream_get_contents($stream, -1, 0); } } |