-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathframework.php
More file actions
184 lines (145 loc) · 4.48 KB
/
framework.php
File metadata and controls
184 lines (145 loc) · 4.48 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
namespace sakiv\framework;
// Should be included on top of each framewrok file.
if(!defined('sakiv.framework')){
Trace::write("External Access to File Denied", TraceMessageTypes::error, TRUE);
}
// Namespaces referred
use sakiv\framework\instrumentation\Trace;
use sakiv\framework\instrumentation\TraceMessageTypes;
/**
* sakiv.framework: constant for framework config file name.
* @var unknown_type
*/
define('SF_CONFIG_FILE_NAME', 'framework.inc.php');
/**
* sakiv.framework entry point.
*
* @example
* <?php
* use sakiv\framework\sf;
*
* define('sakiv.framework', 1);
* require_once 'framework.php';
* sf::loadFramework();
* ?>
*
* @uses
* sakiv\framework\instrumentation\Trace
* sakiv\framework\instrumentation\TraceMessageTypes
*
* @author sakiv
* @copyright Copyright (C) 2005-2009, Sakiv Inc., India
* @license Licensed under the GNU GPL v3
*/
class sf {
/**
* sakiv.framework: Use this method to load Sakiv Framework.
* @param unknown_type $configPath
*/
public static function loadFramework($configPath = null) {
// Verify if configPath is passed or not
// If not, then try to locate config file using locateConfigFile()
if (!isset($configPath)) {
$configPath = self::locateConfigFile();
}
// If configPath is still not set then stop processing
if(!isset($configPath))
die("Could not find Sakiv Framework config file ['$configPath']");
// TODO: Try to use ErrorHandler or some common thing here.
// Load the framework
self::load($configPath);
}
/**
* Locates Sakiv Framework config file within the given path
* @return string
*/
private static function locateConfigFile($lookupPath = null) {
/**
* sakiv.framework: List of path to locate the framework config file.
* @var unknown_type
*/
$defaultLookupPath = array('./', './config');
// Verify if lookupPath is passed or not
// If passed, then process it based on its type
if(isset($lookupPath)) {
switch (gettype($lookupPath)) {
// If array then merge it into defaultLookupPath
case 'array':
$defaultLookupPath = array_merge($lookupPath, $defaultLookupPath);
break;
// If string then add it to beginning of defaultLookupPath
case 'string':
array_unshift($defaultLookupPath, $lookupPath);
break;
// Print warning for any other data type
default:
Trace::write("Lookup path ['$lookupPath'] not supported.", TraceMessageTypes::warning);
break;
}
}
// Try to locate framework config file in each of the lookup Path
foreach ($defaultLookupPath as $path) {
$path = self::joinPath($path,SF_CONFIG_FILE_NAME);
// If file found then return the path
if (file_exists($path)) {
return $path;
}
}
}
/**
* Joins the given path by eliminating any extra forward slashes "/".
* @param string $a
* @param string $b
* @return string
*/
private static function joinPath($a,$b) {
return rtrim($a, '/') .'/'. ltrim($b, '/');
}
/**
* Loads the framework using framework config file from given path.
* @param unknown_type $configPath
*/
private static function load($configPath) {
// Start buffer
ob_start();
// Define the version for framework
define('SF_VERSION', '0.1b');
// If framework path is not defined then set to default value
if(!defined(SF_PATH))
define('SF_PATH', './framework');
// Load the framework config file
require_once $configPath;
// Validate framework path or stop processing
if(!file_exists(SF_PATH))
die("Could not find Sakiv Framework path ['SF_PATH']");
// TODO: Try to use ErrorHandler here.
////////////////////////////////////////////////////////////
// Load core library
////////////////////////////////////////////////////////////
// Resolve core library path by taking framework path constant
// and core string
$sf_core = self::joinPath(SF_PATH, 'core');
// If framework core directory exits and handle is returned
// then first load framework Auto Loader class
if($handle = opendir($sf_core)) {
$loaderPath = self::joinPath($sf_core, 'Loader.php');
// print("Loading [$loaderPath]...<br/>");
require_once $loaderPath;
while (false !== ($file = readdir($handle))) {
if(strtolower(end(explode('.', $file))) == 'php') {
$filePath = self::joinPath($sf_core, $file);
if (is_file($filePath)) {
// print("Loading [$filePath]...<br/>");
require_once $filePath;
}
}
}
closedir($handle);
}
////////////////////////////////////////////////////////////
// Release the output
ob_flush();
}
}
?>