-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSingletonViewBlockHtmlHelper.php
More file actions
162 lines (145 loc) · 5.16 KB
/
SingletonViewBlockHtmlHelper.php
File metadata and controls
162 lines (145 loc) · 5.16 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
/**
* Singleton ViewBlock Html Helper class file.
*
* @copyright Copyright 2014, NetCommons Project
* @author Kohei Teraguchi <kteraguchi@commonsnet.org>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
*/
App::uses('HtmlHelper', 'View/Helper');
/**
* SingletonViewBlockHtmlHelper
*
*
*/
class SingletonViewBlockHtmlHelper extends HtmlHelper {
/**
* The same ViewBlock instance this helper is attached to
*
* @var View
*/
private static $__staticViewBlock = null;
/**
* Names of script & css files that have been included once
* If CakePHP less than 2.6.0, css is not implemented
*
* @var array
*/
private static $__staticIncludedAssets = array();
/**
* Constructor
*
* Set static View instance and _includedAssets property after parent::__construct
*
* @param View $View The View this helper is being attached to.
* @param array $settings Configuration settings for the helper.
*/
public function __construct(View $View, $settings = array()) {
parent::__construct($View, $settings);
if (!isset(self::$__staticViewBlock)) {
self::$__staticViewBlock = $this->_View->Blocks;
}
if (!empty($this->request->params['requested'])) {
$this->__copyBlockValue(self::$__staticViewBlock, $this->_View->Blocks);
$this->_includedAssets = self::$__staticIncludedAssets;
}
}
/**
* Copy block value
*
* @param ViewBlock $sourceBlock Source of copy block
* @param ViewBlock $destinationBlock Destination of copy block
* @param array|string $blockKeys Copy key
* @return void
*/
private function __copyBlockValue(ViewBlock $sourceBlock,
ViewBlock $destinationBlock, $blockKeys = array()) {
if (!is_array($blockKeys)) {
$blockKeys = array($blockKeys);
}
if (empty($blockKeys)) {
// Change to Helper option, if customize
$blockKeys = array(
'meta',
'css',
'script'
);
}
foreach ($blockKeys as $name) {
$destinationBlock->set($name, $sourceBlock->get($name));
}
}
/**
* View object changed to static View instance for used same BlockView instance
*
* @param string $type The title of the external resource
* @param string|array $url The address of the external resource or string for content attribute
* @param array $options Other attributes for the generated tag. If the type attribute is html,
* rss, atom, or icon, the mime-type is returned.
* @return string A completed `<link />` element.
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::meta
*/
public function meta($type, $url = null, $options = array()) {
$out = parent::meta($type, $url, $options);
if (strlen($out) > 0) {
return $out;
}
$this->__copyBlockValue($this->_View->Blocks, self::$__staticViewBlock, 'meta');
}
/**
* View object changed to static View instance for used same BlockView instance
*
* @param string|array $path The name of a CSS style sheet or an array containing names of
* CSS stylesheets. If `$path` is prefixed with '/', the path will be relative to the webroot
* of your application. Otherwise, the path will be relative to your CSS path, usually webroot/css.
* @param array $options Array of options and HTML arguments.
* @return string CSS <link /> or <style /> tag, depending on the type of link.
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::css
*/
public function css($path, $options = array()) {
$out = parent::css($path, $options);
self::$__staticIncludedAssets = array_merge(
self::$__staticIncludedAssets, $this->_includedAssets
);
if (strlen($out) > 0) {
return $out;
}
$this->__copyBlockValue($this->_View->Blocks, self::$__staticViewBlock, 'css');
}
/**
* View object changed to static View instance for used same BlockView instance
*
* @param string|array $url String or array of javascript files to include
* @param array|bool $options Array of options, and html attributes see above. If boolean sets $options['inline'] = value
* @return mixed String of `<script />` tags or null if $inline is false or if $once is true and the file has been
* included before.
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::script
*/
public function script($url, $options = array()) {
$out = parent::script($url, $options);
self::$__staticIncludedAssets = array_merge(
self::$__staticIncludedAssets, $this->_includedAssets
);
if (strlen($out) > 0) {
return $out;
}
$this->__copyBlockValue($this->_View->Blocks, self::$__staticViewBlock, 'script');
}
/**
* View object changed to static View instance for used same BlockView instance
*
* @param string $script The script to wrap
* @param array $options The options to use. Options not listed above will be
* treated as HTML attributes.
* @return mixed string or null depending on the value of `$options['block']`
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::scriptBlock
*/
public function scriptBlock($script, $options = array()) {
$out = parent::scriptBlock($script, $options);
if (strlen($out) > 0) {
return $out;
}
$this->__copyBlockValue($this->_View->Blocks, self::$__staticViewBlock, 'script');
}
}