| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * File containing the Trax class for framework configs |
|---|
| 4 | * |
|---|
| 5 | * (PHP 5) |
|---|
| 6 | * |
|---|
| 7 | * @package PHPonTrax |
|---|
| 8 | * @version $Id:$ |
|---|
| 9 | * @copyright (c) 2005 John Peterson |
|---|
| 10 | * |
|---|
| 11 | * Permission is hereby granted, free of charge, to any person obtaining |
|---|
| 12 | * a copy of this software and associated documentation files (the |
|---|
| 13 | * "Software"), to deal in the Software without restriction, including |
|---|
| 14 | * without limitation the rights to use, copy, modify, merge, publish, |
|---|
| 15 | * distribute, sublicense, and/or sell copies of the Software, and to |
|---|
| 16 | * permit persons to whom the Software is furnished to do so, subject to |
|---|
| 17 | * the following conditions: |
|---|
| 18 | * |
|---|
| 19 | * The above copyright notice and this permission notice shall be |
|---|
| 20 | * included in all copies or substantial portions of the Software. |
|---|
| 21 | * |
|---|
| 22 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|---|
| 23 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
|---|
| 24 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|---|
| 25 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
|---|
| 26 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
|---|
| 27 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
|---|
| 28 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|---|
| 29 | */ |
|---|
| 30 | |
|---|
| 31 | /** |
|---|
| 32 | * @todo Document this class |
|---|
| 33 | * @package PHPonTrax |
|---|
| 34 | */ |
|---|
| 35 | class Trax { |
|---|
| 36 | |
|---|
| 37 | const |
|---|
| 38 | MAJOR = 0, |
|---|
| 39 | MINOR = 16, |
|---|
| 40 | TINY = 0; |
|---|
| 41 | |
|---|
| 42 | public static |
|---|
| 43 | $models_path = null, |
|---|
| 44 | $views_path = null, |
|---|
| 45 | $controllers_path = null, |
|---|
| 46 | $helpers_path = null, |
|---|
| 47 | $layouts_path = null, |
|---|
| 48 | $config_path = null, |
|---|
| 49 | $environments_path = null, |
|---|
| 50 | $lib_path = null, |
|---|
| 51 | $app_path = null, |
|---|
| 52 | $log_path = null, |
|---|
| 53 | $vendor_path = null, |
|---|
| 54 | $public_path = null, |
|---|
| 55 | $tmp_path = null, |
|---|
| 56 | $url_prefix = null, |
|---|
| 57 | $views_extension = 'phtml', |
|---|
| 58 | $path_seperator = ":", # default is Unix |
|---|
| 59 | $current_controller_path = null, |
|---|
| 60 | $current_controller_name = null, |
|---|
| 61 | $current_action_name = null, |
|---|
| 62 | $current_controller_object = null, |
|---|
| 63 | $session_store = "file_store", |
|---|
| 64 | $session_class_name = "ActiveRecordStore", |
|---|
| 65 | $session_save_path = "", |
|---|
| 66 | $session_name = "TRAXSESSID", |
|---|
| 67 | $session_cookie_domain = null, |
|---|
| 68 | $session_lifetime = "0", |
|---|
| 69 | $session_maxlifetime_minutes = "20", |
|---|
| 70 | $version = null, |
|---|
| 71 | $show_trax_errors = false, |
|---|
| 72 | $server_default_include_path = null, |
|---|
| 73 | $include_paths = array(); |
|---|
| 74 | |
|---|
| 75 | function initialize() { |
|---|
| 76 | |
|---|
| 77 | self::$version = self::version(); |
|---|
| 78 | |
|---|
| 79 | if(substr(PHP_OS, 0, 3) == 'WIN') { |
|---|
| 80 | # Windows |
|---|
| 81 | self::$path_seperator = ";"; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | # Set include paths |
|---|
| 85 | self::$models_path = TRAX_ROOT."/app/models"; |
|---|
| 86 | self::$views_path = TRAX_ROOT."/app/views"; |
|---|
| 87 | self::$controllers_path = TRAX_ROOT."/app/controllers"; |
|---|
| 88 | self::$helpers_path = TRAX_ROOT."/app/helpers"; |
|---|
| 89 | self::$layouts_path = TRAX_ROOT."/app/views/layouts"; |
|---|
| 90 | self::$config_path = TRAX_ROOT."/config"; |
|---|
| 91 | self::$environments_path = TRAX_ROOT."/config/environments"; |
|---|
| 92 | self::$lib_path = TRAX_ROOT."/lib"; |
|---|
| 93 | self::$app_path = TRAX_ROOT."/app"; |
|---|
| 94 | self::$log_path = TRAX_ROOT."/log"; |
|---|
| 95 | self::$vendor_path = TRAX_ROOT."/vendor"; |
|---|
| 96 | self::$public_path = TRAX_ROOT."/public"; |
|---|
| 97 | self::$tmp_path = TRAX_ROOT."/tmp"; |
|---|
| 98 | |
|---|
| 99 | # Set which file to log php errors to for this application |
|---|
| 100 | # As well in your application you can do error_log("whatever") and it will go to this log file. |
|---|
| 101 | ini_set("log_errors", "On"); |
|---|
| 102 | ini_set("error_log", self::$log_path."/".TRAX_ENV.".log"); |
|---|
| 103 | |
|---|
| 104 | if(TRAX_ENV == "development") { |
|---|
| 105 | # Display errors to browser if in development mode for debugging |
|---|
| 106 | ini_set("display_errors", "On"); |
|---|
| 107 | } else { |
|---|
| 108 | # Hide errors from browser if not in development mode |
|---|
| 109 | ini_set("display_errors", "Off"); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | # Get the include_path so we know what the original path was |
|---|
| 113 | self::$server_default_include_path = ini_get("include_path"); |
|---|
| 114 | # Set the include_paths |
|---|
| 115 | self::set_default_include_paths(); |
|---|
| 116 | |
|---|
| 117 | # Include Trax library files. |
|---|
| 118 | include_once("session.php"); |
|---|
| 119 | include_once("input_filter.php"); |
|---|
| 120 | include_once("trax_exceptions.php"); |
|---|
| 121 | include_once("inflector.php"); |
|---|
| 122 | include_once("active_record.php"); |
|---|
| 123 | include_once("action_controller.php"); |
|---|
| 124 | include_once("action_view.php"); |
|---|
| 125 | include_once("action_mailer.php"); |
|---|
| 126 | include_once("dispatcher.php"); |
|---|
| 127 | include_once("router.php"); |
|---|
| 128 | |
|---|
| 129 | self::load_active_record_connections_config(); |
|---|
| 130 | |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | function add_include_path($path, $prepend = false, $use_trax_root = false) { |
|---|
| 134 | if(is_array($path)) { |
|---|
| 135 | foreach($path as $new_path) { |
|---|
| 136 | if(!in_array($new_path, self::$include_paths)) { |
|---|
| 137 | $new_paths[] = $use_trax_root ? TRAX_ROOT."/".$new_path : $new_path; |
|---|
| 138 | } |
|---|
| 139 | } |
|---|
| 140 | } elseif(!in_array($path, self::$include_paths)) { |
|---|
| 141 | $new_paths[] = $use_trax_root ? TRAX_ROOT."/".$path : $path; |
|---|
| 142 | } |
|---|
| 143 | if(is_array($new_paths) && is_array(self::$include_paths)) { |
|---|
| 144 | foreach($new_paths as $path) { |
|---|
| 145 | if($prepend) { |
|---|
| 146 | array_unshift(self::$include_paths, $path); |
|---|
| 147 | } else { |
|---|
| 148 | array_push(self::$include_paths, $path); |
|---|
| 149 | } |
|---|
| 150 | } |
|---|
| 151 | ini_set("include_path", implode(self::$path_seperator, self::$include_paths)); |
|---|
| 152 | } |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | function set_default_include_paths() { |
|---|
| 156 | # first clear out all the current paths |
|---|
| 157 | self::$include_paths = array(); |
|---|
| 158 | # now add the default paths |
|---|
| 159 | self::add_include_path(array( |
|---|
| 160 | ".", # current directory |
|---|
| 161 | TRAX_LIB_ROOT, # trax libs (vendor/trax or server trax libs) |
|---|
| 162 | PHP_LIB_ROOT, # php libs dir (ex: /usr/local/lib/php) |
|---|
| 163 | self::$lib_path, # app specific libs extra libs to include |
|---|
| 164 | self::$vendor_path, # 3rd party libs to include in vendor (vendor/third_party) |
|---|
| 165 | self::$server_default_include_path # tack on the old include_path to the end |
|---|
| 166 | )); |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | function load_active_record_connections_config() { |
|---|
| 170 | # Make sure database settings are cleared out |
|---|
| 171 | ActiveRecord::$database_settings = array(); |
|---|
| 172 | ActiveRecord::clear_all_connections(); |
|---|
| 173 | if(file_exists(self::$config_path."/database.ini")) { |
|---|
| 174 | # Load databse settings |
|---|
| 175 | ActiveRecord::$database_settings = parse_ini_file(self::$config_path."/database.ini", true); |
|---|
| 176 | #error_log("db settings:".print_r(ActiveRecord::$database_settings, true)); |
|---|
| 177 | } |
|---|
| 178 | ActiveRecord::$environment = TRAX_ENV; |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | function include_env_config() { |
|---|
| 182 | # Include the application environment specific config file |
|---|
| 183 | if(file_exists(self::$environments_path."/".TRAX_ENV.".php")) { |
|---|
| 184 | include_once(self::$environments_path."/".TRAX_ENV.".php"); |
|---|
| 185 | } |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | function version() { |
|---|
| 189 | return implode('.', array(self::MAJOR, self::MINOR, self::TINY)); |
|---|
| 190 | } |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | ################################################################### |
|---|
| 195 | # Auto include model / controller / other app specific libs files |
|---|
| 196 | ################################################################### |
|---|
| 197 | function __autoload($class_name) { |
|---|
| 198 | $file = Inflector::underscore($class_name).".php"; |
|---|
| 199 | $file_org = $class_name.".php"; |
|---|
| 200 | |
|---|
| 201 | if(file_exists(Trax::$models_path."/$file")) { |
|---|
| 202 | # Include model classes |
|---|
| 203 | include_once(Trax::$models_path."/$file"); |
|---|
| 204 | } elseif(file_exists(Trax::$controllers_path."/$file")) { |
|---|
| 205 | # Include extra controller classes |
|---|
| 206 | include_once(Trax::$controllers_path."/$file"); |
|---|
| 207 | } elseif(file_exists(Trax::$lib_path."/$file")) { |
|---|
| 208 | # Include users application libs |
|---|
| 209 | include_once(Trax::$lib_path."/$file"); |
|---|
| 210 | } elseif(file_exists(Trax::$lib_path."/$file_org")) { |
|---|
| 211 | # Include users application libs |
|---|
| 212 | include_once(Trax::$lib_path."/$file_org"); |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | # add to the __autoload function from Trax |
|---|
| 216 | # just define _autoload() |
|---|
| 217 | if(function_exists('_autoload')) { |
|---|
| 218 | call_user_func('_autoload', $class_name); |
|---|
| 219 | } |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | ?> |
|---|