summaryrefslogtreecommitdiffstats
path: root/application/libraries/Output_cache.php
diff options
context:
space:
mode:
authorFlorian Pritz <bluewind@xinu.at>2014-08-29 17:41:33 +0200
committerFlorian Pritz <bluewind@xinu.at>2014-08-29 17:43:19 +0200
commitd7621c2c01074bdcabd6a8c1a8e95c95d00319bf (patch)
treebfcff5599f26939f185bd0208538b56f39a3dc57 /application/libraries/Output_cache.php
parent08c68f84f4a8519912e31f85823694186de804cf (diff)
add multipaste support
Signed-off-by: Florian Pritz <bluewind@xinu.at>
Diffstat (limited to 'application/libraries/Output_cache.php')
-rw-r--r--application/libraries/Output_cache.php80
1 files changed, 80 insertions, 0 deletions
diff --git a/application/libraries/Output_cache.php b/application/libraries/Output_cache.php
new file mode 100644
index 000000000..224e9f95a
--- /dev/null
+++ b/application/libraries/Output_cache.php
@@ -0,0 +1,80 @@
+<?php
+/*
+ * Copyright 2014 Florian "Bluewind" Pritz <bluewind@server-speed.net>
+ *
+ * Licensed under AGPLv3
+ * (see COPYING for full license text)
+ *
+ */
+
+class Output_cache {
+ private $output_cache = array();
+
+ /**
+ * Combine multiple objects for the same view into one
+ * @param data data to pass to the view
+ * @param view view path
+ */
+ public function add_merge($data, $view)
+ {
+ assert($view !== NULL);
+
+ // combine multiple objects for the same view into one
+ $count = count($this->output_cache);
+ if ($count > 0 && $this->output_cache[$count - 1]["view"] === $view) {
+ $this->output_cache[$count - 1]["data"] = array_merge_recursive($this->output_cache[$count - 1]["data"], $data);
+ } else {
+ $this->add($data, $view);
+ }
+ }
+
+ /**
+ * Add some data that will be output directly if view is NULL or passed
+ * to the view otherweise.
+ *
+ * @param data data to pass to view or output
+ * @param view view path or NULL
+ */
+ public function add($data, $view = NULL)
+ {
+ $this->output_cache[] = array(
+ "view" => $view,
+ "data" => $data,
+ );
+ }
+
+ /**
+ * Add a function that will be excuted when render() is called.
+ * This function is supposed to use render_now() to output data.
+ *
+ * @param data_function
+ */
+ public function add_function($data_function)
+ {
+ $this->output_cache[] = array(
+ "view" => NULL,
+ "data_function" => $data_function,
+ );
+ }
+
+ public function render_now($data, $view = NULL)
+ {
+ if ($view !== NULL) {
+ echo get_instance()->load->view($view, $data, true);
+ } else {
+ echo $data;
+ }
+ }
+
+ public function render()
+ {
+ while ($output = array_shift($this->output_cache)) {
+ if (isset($output["data_function"])) {
+ $output["data_function"]();
+ } else {
+ $data = $output["data"];
+ $this->render_now($data, $output["view"]);
+ }
+ }
+ }
+}