summaryrefslogtreecommitdiffstats
path: root/application/libraries/Output_cache.php
blob: 224e9f95aae9fc62ccd40b82fc3456cdcb496fd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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"]);
			}
		}
	}
}