Smutty

MVC Framework

View Code: Smutty_Main

Browse: All · Classes · Plugins

1
<?php
2
3
/**
4
* this is the main class in the application. it provides a
5
* few methods of use like loadClass(),
6
*
7
*/
8
9
class Smutty_Main extends Smutty_Object {
10
11
/**
12
* loads a class by name. some classes may have special
13
* things done when loaded (like user models)
14
*
15
* @param String $class class name
16
* @return boolean indicating success
17
*
18
*/
19
20
public static function loadClass( $class ) {
21
22
// first try the smutty library
23
$path = preg_replace( '/_/', '/', $class );
24
$path = "library/$path.class.php";
25
if ( file_exists($path) )
26
return include( $path );
27
28
else {
29
30
// then maybe a model?
31
$path = "application/models/$class.php";
32
if ( file_exists($path) ) {
33
// this data gets added to the class dynamically.
34
// need this cause of php's static lmitation thing...
35
// this is a ***VERY*** dirty hack, but it works and i
36
// like the syntax it allows so it's stayin for now!
37
$replace = "
38
39
public static function getFields() {
40
return parent::getFields( '$class' );
41
}
42
43
public static function find( \$id, \$name = 'id' ) {
44
return parent::find( '$class', \$id, \$name );
45
}
46
47
public static function fetchAll( \$order = false, \$params = array(), \$limit = false, \$whereSql = false, \$joins = false ) {
48
return parent::fetchAll( '$class', \$order, \$params, \$limit, \$whereSql, \$joins );
49
}
50
51
public static function deleteWhere( \$params ) {
52
return parent::deleteWhere( '$class', \$params );
53
}
54
55
public static function getTotal() {
56
return parent::getTotal( '$class' );
57
}
58
59
";
60
// read in model class data to alter then eval
61
$data = file_get_contents( $path );
62
$data = preg_replace( '/<\?/', '', $data );
63
$data = preg_replace( '/\?>/', '', $data );
64
$data = preg_replace( '/extends Smutty_Model {/', "extends Smutty_Model { $replace ", $data );
65
eval( $data );
66
return true;
67
}
68
69
}
70
71
// class not found
72
return false;
73
74
}
75
76
/**
77
* this is the entry point of the application, call this
78
* to get everything going!!!
79
*
80
* @return nothing
81
*
82
*/
83
84
public static function run() {
85
86
$cfg =& Smutty_Config::getInstance();
87
$cfg->loadDefaults();
88
89
// set error handler and error reporting level
90
set_error_handler( 'smutty_error_handler' );
91
switch ( $cfg->get('errors') ) {
92
case 'all':
93
Smutty_Error::setErrorReporting( ERR_ALL );
94
}
95
96
self::setLanguage();
97
98
// first check the resource to see if it's a public
99
// resource that the user has requested.
100
$resource = Smutty_Utils::getSmuttyUrl();
101
if ( substr($resource,0,6) != 'smutty' ) {
102
$path = "application/public/$resource";
103
if ( $resource && file_exists($path) && !is_dir($path) )
104
Smutty_Resource::output( $path );
105
}
106
107
// we should now be able to allow the error
108
// reporting to use smarty templates
109
$e =& Smutty_Error::getInstance();
110
$e->enableTemplates();
111
112
// then create the router and dispatch the request
113
$router = Smutty_Router::getInstance();
114
$router->dispatch();
115
116
self::completeRequest();
117
118
}
119
120
/**
121
* includes the correct langauge file
122
*
123
* @return nothing
124
*
125
*/
126
127
public static function setLanguage() {
128
129
$cfg = Smutty_Config::getInstance();
130
131
$lang = $cfg->get('smutty.lang') ? $cfg->get('smutty.lang') : 'en';
132
include "library/lang/$lang.php";
133
134
}
135
136
/**
137
* this function does any cleanup/saving needed
138
* before the request is done.
139
*
140
* @return nothing
141
*
142
*/
143
144
public static function completeRequest() {
145
146
$session =& Smutty_Session::getInstance();
147
$session->save();
148
149
}
150
151
/**
152
* loads any model classes the user has created
153
*
154
* @return nothing
155
*
156
*/
157
158
public static function loadModelClasses() {
159
160
$d = opendir( 'application/models/' );
161
162
while ( $f = readdir($d) )
163
if ( preg_match('/(.*)\.php$/',$f,$matches) )
164
Smutty_Main::loadClass( $matches[1] );
165
166
closedir( $d );
167
168
}
169
170
}
171
172
?>

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.