forked from KodiCMS-Kohana/cms
/
snippet.php
101 lines (88 loc) · 2.06 KB
/
snippet.php
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* @package KodiCMS/Snippet
* @author butschster <butschster@gmail.com>
* @link http://kodicms.ru
* @copyright (c) 2012-2014 butschster
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
*/
class Snippet {
/**
*
* @param string $snippet_name
* @param array $vars
* @param integer $cache_lifetime
* @param boolean $cache_by_uri
* @param array $tags
* @param boolean $i18n
* @return void
*/
public static function render($snippet_name, $vars = NULL, $cache_lifetime = NULL, $cache_by_uri = FALSE, array $tags = array(), $i18n = NULL)
{
$view = Snippet::get($snippet_name, $vars);
if ($view === NULL)
{
return NULL;
}
$cache_key = self::_cache_key($snippet_name, $cache_by_uri);
if (!in_array($snippet_name, $tags))
{
$tags[] = $snippet_name;
}
if (
Kohana::$caching === TRUE
AND
$cache_lifetime !== NULL
AND ! Fragment::load($cache_key, (int) $cache_lifetime, $i18n))
{
echo $view;
Fragment::save_with_tags((int) $cache_lifetime, $tags);
}
else if ($cache_lifetime === NULL)
{
echo $view;
}
}
/**
*
* @param string $snippet_name
* @param array $vars
* @return null|View_Front
*/
public static function get($snippet_name, $vars = NULL)
{
$snippet = new Model_File_Snippet($snippet_name);
if (!$snippet->is_exists())
{
if (($found_file = $snippet->find_file()) !== FALSE)
{
$snippet = new Model_File_Snippet($found_file);
}
else
{
return NULL;
}
}
$view = View_Front::factory($snippet->get_file(), $vars);
if (isset($view->page_object))
{
$view->page = $view->page_object;
}
return $view;
}
/**
*
* @param string $snippet_hash
* @param boolean $cache_by_uri
* @return string
*/
protected static function _cache_key($snippet_hash, $cache_by_uri = FALSE)
{
if ($cache_by_uri !== FALSE)
{
$snippet_hash .= '::';
$snippet_hash .= md5(Request::current()->uri() . serialize(Request::current()->query()));
}
return 'Snippet::' . $snippet_hash;
}
}