static function getInstance() { if (empty(self::$_instance)) { self::$_instance = new self(); } return self::$_instance; }
private function __construct() { Plank_Logger::log('Singleton', get_class($this) . ' singleton created.', L_TRACE); $config = Plank_Config::getInstance(); $config = $config->getArea('database'); if (!$config || !isset($config['connections'])) { throw new Plank_Exception_Database_Config('Databases not configured'); } $connections = explode(',', $config['connections']); foreach ($connections as $connection) { if (isset($config['dsn_' . $connection])) { $this->connections[$connection] = MDB2::factory($config['dsn_' . $connection]); if (PEAR::isError($this->connections[$connection])) { throw new Plank_Exception_Database('DB Error! ' . $this->connections[$connection]->getMessage() . ' ' . $this->connections[$connection]->getUserInfo()); } if (SHOWDEBUG) { $this->connections[$connection]->setOption('debug', 1); $this->connections[$connection]->setOption('log_line_break', "<br/>"); } $this->connections[$connection]->setOption('portability', MDB2_PORTABILITY_ALL ^ MDB2_PORTABILITY_FIX_CASE); $this->connections[$connection]->setFetchMode(MDB2_FETCHMODE_ASSOC); if (PEAR::isError($this->connections[$connection])) { throw new Plank_Exception_Database_Connection('Error connecting ' . $connection . ': ' . $this->connections[$connection]->getMessage() . ' ' . $this->connections[$connection]->getUserInfo()); } } else { throw new Plank_Exception_Database_Config('No DSN for DB Connection ' . $connection); } } }
function __construct($request, $response) { $config = Plank_Config::getInstance(); if (!($app_prefix = $config->get('system', 'application_prefix'))) { $app_prefix = 'Plank'; } $init = false; if (Plank_Autoload::findClass($app_prefix . '_Site_Initialise')) { Plank_Logger::log('Initialise', 'Loading ' . $app_prefix . '_Site_Initialise', L_DEBUG); $class = $app_prefix . '_Site_Initialise'; $init = new $class($request, $response); } else { $init = new Plank_Site_Initialise($request, $response); } if ($init) { $init->preroute(); } if (Plank_Autoload::findClass($app_prefix . '_Site_Routing')) { Plank_Logger::log('Router', 'Loading ' . $app_prefix . '_Routing as a routing file', L_DEBUG); $class_name = $app_prefix . '_Site_Routing'; } else { Plank_Logger::log('Router', 'Using Plank default routing table', L_DEBUG); $class_name = 'Plank_Routing'; } $routing = new $class_name($request); if (!$routing->foundRoute) { Plank_Logger::log('Router', 'Routing failed', L_FATAL); $response->setError('I couldn\'t find a route for that URL'); $response->setStatus(404); return; } $init->postroute($routing->controller, $routing->action); $controllername = $app_prefix . '_Controller_' . $routing->controller; if (Plank_Autoload::findClass($controllername)) { # Yay } elseif (Plank_Autoload::findClass('Plank_Controller_' . $routing->controller)) { $controllername = 'Plank_Controller_' . $routing->controller; } else { Plank_Logger::log('Router', 'There is no such thing as ' . $controllername, L_WARN); $response->setError('I couldn\'t load the controller \'' . $routing->controller . '\''); $response->setStatus(404); return; } $controller = new $controllername($request, $response); if (!is_a($controller, "Plank_Controller")) { throw new Plank_Exception("{$controllername} must be a subclass of Plank_Controller"); } $init->gotcontroller($controller, $routing->action); $method = $routing->action . 'Action'; $controller->{$method}(); $init->shutdown($routing->controller, $routing->action); }
static function findClass($class_name) { if (defined('INIT')) { $config = Plank_Config::getInstance(); if ($pathconfig = $config->get('system', "librarypath")) { $searchpath = PLANK_PATH . ':' . CODE_PATH . ':' . $pathconfig . ':' . ini_get('include_path'); $searchpath = explode(':', $searchpath); } else { $searchpath = array(explode(PLANK_PATH . ':' . CODE_PATH . ':' . ini_get('include_path'))); } } else { $searchpath = array(PLANK_PATH, CODE_PATH); } $found = 0; $filename = implode('/', explode('_', $class_name)) . '.php'; foreach ($searchpath as $path) { $path .= '/'; defined('INIT') ? Plank_Logger::log('Autoloader', 'Looking for ' . $path . $filename, L_TRACE) : false; if (file_exists($path . $filename)) { return $path . $filename; } } }
function IndexAction() { $view = new Plank_View('Errors', 'DefaultPage'); $view->config = Plank_Config::getInstance(); $this->response->setContent($view->render()); }
set_exception_handler("__exception_handler"); function __autoload($class_name) { Plank_Autoload::loadClass($class_name); } $request = new Plank_HTTP_Request(); $response = new Plank_HTTP_Response(); function handle_exceptions($e) { global $response; Plank_Error::Error503($e, $response); } set_exception_handler("handle_exceptions"); try { Plank_Logger::log('Init', 'Hello World', L_INFO); Plank_Logger::logStat('Init', 'Hello World'); Plank_Config::getInstance(); define('INIT', time()); Plank_Logger::logStat('Init', 'Finished init'); new Plank_Site($request, &$response); Plank_Logger::logStat('Init', 'Finished MVC'); } catch (Plank_Exception_NotFound $e) { Plank_Error::Error404($e->getMessage(), $response); #} catch ( Exception $e ){ # Plank_Error::Error503($e, $response); } $response->respond(); Plank_Logger::logStat('Init', 'Goodbye, Cruel World'); Plank_Logger::log('Init', 'Memory Use: ' . number_format(memory_get_usage()), L_INFO); echo Plank_Logger_Display::display(); define('DESTRUCT', true);