Smutty
MVC Framework
View Code: Smutty_Utils
Browse: All · Classes · Plugins
1
<?php
2
3
/**
4
* this is a utility class that provides a number of static
5
* method for doing various things within Smutty
6
*
7
*/
8
9
class Smutty_Utils {
10
11
/** the base url cache */
12
private static $baseUrl = null;
13
14
/**
15
* returns the base URL for the appliation
16
*
17
* @return String the application base url
18
*
19
*/
20
22
23
if ( self::$baseUrl == null ) {
24
$data = Smutty_Data::getServerData();
25
self::$baseUrl = substr( $data->string('PHP_SELF'), 0,
26
strpos($data->string('PHP_SELF'), '/index.php') );
27
}
28
29
return self::$baseUrl;
30
31
}
32
33
/**
34
* gets the url for a controller/action/etc... it's assumed that
35
* the controller is the current controller unless you specify
36
* otherwise.
37
*
38
* this tries to make the url extra pretty by stripping off anything
39
* on the end of the url that isn't needed (default values, etc...)
40
*
41
* @param array $params hash of params
42
* @param array $args query string args
43
* @return String the url
44
*
45
*/
46
48
49
$router = Smutty_Router::getInstance();
50
// if a controller wan't specified then assume the
51
// user meant to use the current controller.
52
if ( !isset($params['controller']) )
53
$params['controller'] = strtolower($router->getControllerName());
54
55
$url = '';
56
$spec = Smutty_Router::getRouteSpec(
57
$params['controller'],
58
v($params,'action')
59
);
60
61
$parts = array_reverse(explode( '/', $spec ));
62
$valueAdded = false;
63
foreach ( $parts as $name ) {
64
65
switch ( $name ) {
66
// strip default values for the special params
67
// if we haven't added any values yet.
68
case 'controller':
69
case 'action':
70
$value = v($params,$name);
71
$defMethod = 'getDefault' . ucfirst($name) . 'Name';
72
$default = $router->$defMethod();
73
if ( $name == 'controller' ) {
74
$default = strtolower( $default );
75
$value = strtolower( $value );
76
}
77
if ( ($value == $default) && !$valueAdded )
78
$value = '';
79
break;
80
default:
81
$value = v($params,$name);
82
}
83
84
$url = $value . ( $valueAdded ? "/$url" : '' );
85
if ( $value ) $valueAdded = true;
86
87
}
88
89
$url = Smutty_Utils::getBaseUrl() . '/' . preg_replace( '/\/\//', '/', $url );
90
91
// now add any query string args we have
92
if ( is_array($args) ) {
93
$qs = '';
94
foreach ( $args as $name => $value )
95
$qs .= '&' . urlencode($name) . '=' . urlencode($value);
96
$url .= ( $qs ? '?' . substr($qs,1) : '' );
97
}
98
99
return $url;
100
101
}
102
103
/**
104
* this function is just like getUrl(), only it
105
* includes the protocol://server:port part to.
106
*
107
* @param array $params array of params
108
* @return String base url with proto://domain:port/
109
*
110
*/
111
113
114
$data = Smutty_Data::getServerData();
115
$port = $data->string('SERVER_PORT');
116
117
return 'http' .
118
'://' .
119
$data->string('SERVER_NAME') .
120
( $port == '80' ? '' : ":$port" ) .
121
Smutty_Utils::getUrl( $params );
122
123
}
124
125
/**
126
* this function turns a commer seperated string
127
* into a hash (associative array). it treats the
128
* string as having commer seperated values like...
129
*
130
* name,value,name,value
131
*
132
* @param String $str the string to convert
133
* @return hash assoc array
134
*
135
*/
136
138
139
$hash = array();
140
$args = explode( ',', $str );
141
$size = sizeof( $args );
142
143
for ( $i=0; $i<$size; $i+=2 )
144
if ( $args[$i] )
145
$hash[ $args[$i] ] = $args[ $i + 1 ];
146
147
return $hash;
148
149
}
150
151
/**
152
* this function returns the smutty url (ie. with any base
153
* url and query string stipped off)
154
*
155
* @return String the url
156
*
157
*/
158
160
161
static $url;
162
163
if ( $url == null ) {
164
$data = Smutty_Data::getServerData();
165
$baseUrl = Smutty_Utils::getBaseUrl();
166
$url = substr( $data->string('REQUEST_URI'), strlen($baseUrl) + 1 );
167
$url = preg_replace( '/^(.*)\?.*$/', '$1', $url );
168
}
169
170
return $url;
171
172
}
173
174
/**
175
* returns a unique 10 char id string
176
*
177
* @return String character string
178
*
179
*/
180
182
$id = '';
183
for ( $i=0; $i<10; $i++ )
184
$id .= chr( rand(65,90) );
185
return $id;
186
}
187
188
/**
189
* computes the difference of arrays composed of objects
190
*
191
* @param array $x main array
192
* @param array $y array to comp against
193
* @param String $p array index property
194
* @return array the difference of arrays
195
*
196
*/
197
199
$z = array();
200
$w = array();
201
// build index
202
foreach ( $y as $a )
203
$w[$a->$p] = $a;
204
// then compare
205
foreach ( $x as $a )
206
if ( !isset($w[$a->$p]) )
207
$z[] = $a;
208
return $z;
209
}
210
211
}
212
213
?>
The code shown here is the code that is currently running this site. If you want to view the latest SVN version of the code then go to the Subversion repository.