|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the trefoil application. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Miguel Angel Gabriel <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace Trefoil\Util; |
|
11
|
|
|
|
|
12
|
|
|
use Easybook\DependencyInjection\Application as EasybookApplication; |
|
13
|
|
|
use Easybook\Util\Toolkit as EasybookToolkit; |
|
14
|
|
|
|
|
15
|
|
|
class Toolkit extends EasybookToolkit |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @param array|EasybookApplication $app |
|
19
|
|
|
* |
|
20
|
|
|
* @return null|string |
|
21
|
|
|
*/ |
|
22
|
34 |
|
public static function getCurrentThemeDir(EasybookApplication $app) |
|
23
|
|
|
{ |
|
24
|
34 |
|
$theme = ucfirst($app->edition('theme')); |
|
25
|
|
|
|
|
26
|
|
|
// the custom theme set for this book publishing |
|
27
|
34 |
|
$localThemesDir = realpath($app['trefoil.publishing.dir.themes']); |
|
28
|
|
|
|
|
29
|
|
|
// the default trefoil themes |
|
30
|
34 |
|
$defaultThemesDir = $app['trefoil.app.dir.resources'] . '/Themes'; |
|
31
|
|
|
|
|
32
|
|
|
$paths = array( |
|
33
|
34 |
|
$localThemesDir, |
|
34
|
|
|
$defaultThemesDir |
|
35
|
34 |
|
); |
|
36
|
|
|
|
|
37
|
34 |
|
$existing = $app->getFirstExistingFile($theme, $paths); |
|
38
|
|
|
|
|
39
|
34 |
|
return $existing; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Return the current resources dir. |
|
44
|
|
|
* |
|
45
|
|
|
* @param EasybookApplication $app |
|
46
|
|
|
* @param null $format |
|
47
|
|
|
* |
|
48
|
|
|
* @return null|string |
|
49
|
|
|
*/ |
|
50
|
34 |
|
public static function getCurrentResourcesDir(EasybookApplication $app, $format = null) |
|
51
|
|
|
{ |
|
52
|
34 |
|
$paths = self::getCurrentResourceDirs($app, $format); |
|
53
|
|
|
|
|
54
|
34 |
|
$existing = $app->getFirstExistingFile('', $paths); |
|
55
|
34 |
|
$existing = (substr($existing, -1) === '/') ? substr($existing, 0, -1) : $existing; |
|
56
|
|
|
|
|
57
|
34 |
|
return $existing; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Return first existing resource by name. |
|
62
|
|
|
* |
|
63
|
|
|
* @param EasybookApplication $app |
|
64
|
|
|
* @param $resourceName |
|
65
|
|
|
* @param null $format |
|
66
|
|
|
* |
|
67
|
|
|
* @return null|string |
|
68
|
|
|
*/ |
|
69
|
34 |
|
public static function getCurrentResource(EasybookApplication $app, $resourceName, $format = null) |
|
70
|
|
|
{ |
|
71
|
34 |
|
$paths = self::getCurrentResourceDirs($app, $format); |
|
72
|
|
|
|
|
73
|
34 |
|
$existing = $app->getFirstExistingFile($resourceName, $paths); |
|
74
|
|
|
|
|
75
|
34 |
|
return $existing; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Return the list of current resource directories, ordered by precedence. |
|
80
|
|
|
* |
|
81
|
|
|
* @param EasybookApplication $app |
|
82
|
|
|
* @param null $format |
|
83
|
|
|
* |
|
84
|
|
|
* @return array |
|
85
|
|
|
*/ |
|
86
|
34 |
|
public static function getCurrentResourceDirs(EasybookApplication $app, $format = null) |
|
87
|
|
|
{ |
|
88
|
34 |
|
$theme = ucfirst($app->edition('theme')); |
|
89
|
|
|
|
|
90
|
34 |
|
if (!$format) { |
|
91
|
34 |
|
$format = self::getCurrentFormat($app); |
|
92
|
34 |
|
} |
|
93
|
|
|
|
|
94
|
|
|
// the custom theme set for this book and format |
|
95
|
34 |
|
$localResourcesDir = realpath($app['trefoil.publishing.dir.themes']) . '/' . $theme . '/' . $format; |
|
96
|
|
|
|
|
97
|
|
|
// the custom theme set for this book and the Common format |
|
98
|
34 |
|
$localCommonResourcesDir = realpath($app['trefoil.publishing.dir.themes']) . '/' . $theme . '/Common'; |
|
99
|
|
|
|
|
100
|
|
|
// the default trefoil themes for the format |
|
101
|
34 |
|
$defaultResourcesDir = $app['trefoil.app.dir.resources'] . '/Themes' . '/' . $theme . '/' . $format; |
|
102
|
|
|
|
|
103
|
|
|
// the Common format into the default trefoil themes |
|
104
|
34 |
|
$defaultCommonResourcesDir = $app['trefoil.app.dir.resources'] . '/Themes' . '/' . $theme . '/Common'; |
|
105
|
|
|
|
|
106
|
|
|
$paths = array( |
|
107
|
34 |
|
$localResourcesDir . '/Resources', |
|
108
|
34 |
|
$localCommonResourcesDir . '/Resources', |
|
109
|
34 |
|
$defaultResourcesDir . '/Resources', |
|
110
|
|
|
$defaultCommonResourcesDir . '/Resources' |
|
111
|
34 |
|
); |
|
112
|
|
|
|
|
113
|
34 |
|
return $paths; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
34 |
|
public static function getCurrentFormat(EasybookApplication $app) |
|
117
|
|
|
{ |
|
118
|
34 |
|
$format = Toolkit::camelize($app->edition('format'), true); |
|
119
|
|
|
|
|
120
|
34 |
|
return $format; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public static function sprintfUTF8($format) |
|
124
|
|
|
{ |
|
125
|
|
|
$args = func_get_args(); |
|
126
|
|
|
|
|
127
|
|
|
$count = count($args); |
|
128
|
|
|
for ($i = 1; $i < $count; $i++) { |
|
129
|
|
|
$args[$i] = iconv('UTF-8', 'ISO-8859-15', $args[$i]); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
return iconv('ISO-8859-15', 'UTF-8', call_user_func_array('sprintf', $args)); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Extract the attributes of an HTML tag |
|
137
|
|
|
* |
|
138
|
|
|
* @param string $string |
|
139
|
|
|
* |
|
140
|
|
|
* @return array of attributes |
|
141
|
|
|
*/ |
|
142
|
13 |
View Code Duplication |
public static function parseHTMLAttributes($string) |
|
143
|
|
|
{ |
|
144
|
13 |
|
$regExp = '/(?<attr>.*)="(?<value>.*)"/Us'; |
|
145
|
13 |
|
preg_match_all($regExp, $string, $attrMatches, PREG_SET_ORDER); |
|
146
|
|
|
|
|
147
|
13 |
|
$attributes = array(); |
|
148
|
13 |
|
foreach ($attrMatches as $attrMatch) { |
|
149
|
8 |
|
$attributes[trim($attrMatch['attr'])] = $attrMatch['value']; |
|
150
|
13 |
|
} |
|
151
|
|
|
|
|
152
|
13 |
|
return $attributes; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Render the attributes of an HTML tag |
|
157
|
|
|
* |
|
158
|
|
|
* @param array $attributes |
|
159
|
|
|
* |
|
160
|
|
|
* @return string |
|
161
|
|
|
*/ |
|
162
|
13 |
View Code Duplication |
public static function renderHTMLAttributes(array $attributes) |
|
163
|
|
|
{ |
|
164
|
13 |
|
$html = ''; |
|
165
|
|
|
|
|
166
|
13 |
|
foreach ($attributes as $name => $value) { |
|
167
|
13 |
|
$html .= sprintf('%s="%s" ', $name, $value); |
|
168
|
13 |
|
} |
|
169
|
|
|
|
|
170
|
13 |
|
return trim($html); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Render the HTML tag |
|
175
|
|
|
* |
|
176
|
|
|
* @param string $tag |
|
177
|
|
|
* @param string $contents |
|
178
|
|
|
* @param array $attributes |
|
179
|
|
|
* |
|
180
|
|
|
* @return string |
|
181
|
|
|
*/ |
|
182
|
13 |
|
public static function renderHTMLTag($tag, $contents = '', array $attributes = array()) |
|
183
|
|
|
{ |
|
184
|
13 |
|
$strAttributes = static::renderHTMLAttributes($attributes); |
|
185
|
13 |
|
$strAttributes = $strAttributes ? ' ' . $strAttributes : ''; |
|
186
|
|
|
|
|
187
|
13 |
|
if ($contents) { |
|
188
|
12 |
|
return sprintf('<%s%s>%s</%s>', $tag, $strAttributes, $contents, $tag); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
1 |
|
return sprintf('<%s%s />', $tag, $strAttributes); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
} |
|
195
|
|
|
|