1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* |
5
|
|
|
* This file is part of the Apix Project. |
6
|
|
|
* |
7
|
|
|
* (c) Franck Cassedanne <franck at ouarz.net> |
8
|
|
|
* |
9
|
|
|
* @license http://opensource.org/licenses/BSD-3-Clause New BSD License |
10
|
|
|
* |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Apix\View\ViewModel; |
14
|
|
|
|
15
|
|
|
use Apix\View\ViewModel; |
16
|
|
|
use Apix\Service; |
17
|
|
|
use Apix\Resource\Help; |
|
|
|
|
18
|
|
|
|
19
|
|
|
class Common extends ViewModel |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Returns the API resources. |
24
|
|
|
* |
25
|
|
|
* @return array |
26
|
|
|
*/ |
27
|
|
|
public function getResources() |
28
|
|
|
{ |
29
|
|
|
if ($this->items) { |
30
|
|
|
$items = $this->items; |
|
|
|
|
31
|
|
|
} else { |
32
|
|
|
$help = new Help(); |
33
|
|
|
$server = Service::get('server'); |
34
|
|
|
$items = $help->getResourcesDocs($server); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$resources = array(); |
38
|
|
|
foreach ($items as $resource) { |
39
|
|
|
foreach ($resource['methods'] as $v) { |
40
|
|
|
if( |
41
|
|
|
!isset($v['apix_man_toc_hidden']) |
42
|
|
|
) { |
43
|
|
|
$resources[] = array( |
44
|
|
|
'method' => $v['method'], |
45
|
|
|
'resource' => $resource['path'], |
46
|
|
|
'querystr' => $v['method'] !== 'GET' |
47
|
|
|
? '?method=' . $v['method'] |
48
|
|
|
: null |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $resources; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Deals with the resource groups definitions. |
59
|
|
|
* |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
|
|
public function getResourceGroups() |
63
|
|
|
{ |
64
|
|
|
#$ignore = array('internal', 'id', 'toc', 'todo', 'method'); |
|
|
|
|
65
|
|
|
$titles = array( |
66
|
|
|
'example' => $this->hasMany('example') ? 'Examples' : 'Example', |
67
|
|
|
'see' => 'See also', |
68
|
|
|
'link' => $this->hasMany('link') ? 'Links' : 'Link', |
69
|
|
|
'copyright' => 'Copyright', |
70
|
|
|
'license' => 'Licensing' |
71
|
|
|
); |
72
|
|
|
$groups = array(); |
73
|
|
|
|
74
|
|
|
foreach ($titles as $key => $title) { |
75
|
|
|
if( |
76
|
|
|
isset($this->{$key}) || isset($this->options[$key]) |
77
|
|
|
#&& !in_array($key, $ignore) |
|
|
|
|
78
|
|
|
) { |
79
|
|
|
$groups[] = array( |
80
|
|
|
'title' => $title, |
81
|
|
|
'items' => $this->get($key) |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $groups; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Returns the man index/section string. |
91
|
|
|
* |
92
|
|
|
* @return integer |
93
|
|
|
*/ |
94
|
|
|
public function getManTocSection() |
95
|
|
|
{ |
96
|
|
|
static $str; |
97
|
|
|
if (!$str) { |
98
|
|
|
switch($this->getLayout()): |
99
|
|
|
case 'man_error': $section = 7; break; |
|
|
|
|
100
|
|
|
case 'man_page': $section = 3; break; |
|
|
|
|
101
|
|
|
default: $section = 1; |
|
|
|
|
102
|
|
|
endswitch; |
103
|
|
|
$str = sprintf('%s(%s)', $this->config['output_rootNode'], $section); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $str; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* _def - view helper. |
111
|
|
|
* |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
public function _def() |
115
|
|
|
{ |
116
|
|
|
return function ($t) { |
117
|
|
|
return '<span class="default">' . $t . '</span>'; |
118
|
|
|
}; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function debug($data=null) |
122
|
|
|
{ |
123
|
|
|
echo '<pre>'; |
124
|
|
|
print_r( null !== $data ? $data : $this ); |
125
|
|
|
echo '</pre>'; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Assigns a property. |
130
|
|
|
* |
131
|
|
|
* // This value can be accessed as {{foo}} within the template |
132
|
|
|
* $view->set('foo', 'my value'); |
133
|
|
|
* |
134
|
|
|
* You can also use an array to set several values at once: |
135
|
|
|
* |
136
|
|
|
* // Create the values {{food}} and {{beverage}} in the template |
137
|
|
|
* $view->set(array('food' => 'bread', 'beverage' => 'water')); |
138
|
|
|
* |
139
|
|
|
* @param string|array $blob A string key or an associative array to set. |
140
|
|
|
* @param mixed $value The value to set if the blob is a string. |
141
|
|
|
* @return $this |
142
|
|
|
*/ |
143
|
|
View Code Duplication |
public function set($blob, $value = null) |
|
|
|
|
144
|
|
|
{ |
145
|
|
|
if (is_array($blob)) { |
146
|
|
|
foreach ($blob as $key => $value) { |
147
|
|
|
$this->{$key} = $value; |
148
|
|
|
} |
149
|
|
|
} else { |
150
|
|
|
$this->{$blob} = $value; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
View Code Duplication |
public static function htmlizer($string) |
|
|
|
|
157
|
|
|
{ |
158
|
|
|
$pattern = array( |
159
|
|
|
'/((?:[\w\d]+\:\/\/)?(?:[\w\-\d]+\.)+[\w\-\d]+(?:\/[\w\-\d]+)*(?:\/|\.[\w\-\d]+)?(?:\?[\w\-\d]+\=[\w\-\d]+\&?)?(?:\#[\w\-\d\.]*)?)/', # URL |
160
|
|
|
'/([\w\-\d]+\@[\w\-\d]+\.[\w\-\d]+)/', # email |
161
|
|
|
// '/\S{2}/', # line break |
162
|
|
|
); |
163
|
|
|
$replace = array( |
164
|
|
|
'<a href="$1">$1</a>', |
165
|
|
|
'<a href="mailto:$1">$1</a>', |
166
|
|
|
// '- $1' |
167
|
|
|
); |
168
|
|
|
|
169
|
|
|
return preg_replace($pattern, $replace, $string); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Assigns a value by reference. The benefit of binding is that values can |
174
|
|
|
* be altered without re-setting them. It is also possible to bind variables |
175
|
|
|
* before they have values. Assigned values will be available as a |
176
|
|
|
* variable within the template file: |
177
|
|
|
* |
178
|
|
|
* // This reference can be accessed as {{ref}} within the template |
179
|
|
|
* $view->bind('ref', $bar); |
180
|
|
|
* |
181
|
|
|
* @param string variable name |
182
|
|
|
* @param mixed referenced variable |
183
|
|
|
* @return $this |
184
|
|
|
*/ |
185
|
|
|
public function bind($key, & $value) |
186
|
|
|
{ |
187
|
|
|
$this->{$key} =& $value; |
188
|
|
|
|
189
|
|
|
return $this; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/* ---- generic helpers --- */ |
193
|
|
|
|
194
|
|
View Code Duplication |
public function hasMany($mix) |
|
|
|
|
195
|
|
|
{ |
196
|
|
|
if (is_string($mix) && isset($this->{$mix})) { |
197
|
|
|
return count($this->{$mix})>1; |
198
|
|
|
} elseif (is_array($mix)) { |
199
|
|
|
return count($mix)>1; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
return false; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Returns this view model layout. |
207
|
|
|
* |
208
|
|
|
* @return string |
209
|
|
|
*/ |
210
|
|
|
public function getLayout() |
211
|
|
|
{ |
212
|
|
|
return $this->_layout; |
|
|
|
|
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
} |
216
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: