summaryrefslogtreecommitdiff
path: root/src/HTML5/Serializer.php
diff options
context:
space:
mode:
authorMatt Butcher <[email protected]>2013-04-03 21:34:13 -0500
committerMatt Butcher <[email protected]>2013-04-03 21:34:13 -0500
commit326f71c3747145b5618788f429559cbac453f114 (patch)
tree2b26194feea1b0a33ac8a249290ff48880e8bb9b /src/HTML5/Serializer.php
parent49d2982bf2daef3017539f8cc5ba393ad9f52bf9 (diff)
Created Serializer methods.
Diffstat (limited to 'src/HTML5/Serializer.php')
-rw-r--r--src/HTML5/Serializer.php27
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);
}
}