cleaned project, repaired router

This commit is contained in:
Logsol 2013-07-08 22:52:36 +02:00
parent 2156d3f5c6
commit bffe0f1660
16 changed files with 1172 additions and 1440 deletions

View file

@ -1,39 +1,29 @@
<?php
/**
* Autoloader
* Loads class files automatically when they are needed.
*
* @author Karl Pannek <info@katharsis.in>
* @version 0.5.2
* @package Katharsis
*/
class Katharsis_Autoload
{
/**
* @var array
*/
protected static $_classLocations = array(
'library',
'application/controller',
'application/model'
);
/**
* Registering autoload method
*
* @return void
*/
public static function init()
{
spl_autoload_register('Katharsis_Autoload::autoload');
}
/**
* Actual autoload method. Loads class files automatically when they are needed
*
* @return void
*/
public static function autoload($classname)
{
if($location = self::findClass($classname))
{
require_once $location;
return;
}
throw new exception('Autoload: could not load class "' . $classname . '"');
}
public static function findClass($classname)
{
$name = str_replace("_", DIRECTORY_SEPARATOR, $classname);
@ -41,11 +31,10 @@ class Katharsis_Autoload
{
if(file_exists($location . DIRECTORY_SEPARATOR . $name . ".php"))
{
require_once $location . DIRECTORY_SEPARATOR . $name . ".php";
return;
return $location . DIRECTORY_SEPARATOR . $name . ".php";
}
}
die('Autoload: could not load class "' . $classname . '"');
return false;
}
}
?>

View file

@ -1,25 +1,18 @@
<?php require_once('library/Katharsis/Autoload.php');
/**
* Bootstrap Class
* Central application routing entity
*
* @author Karl Pannek <info@katharsis.in>
* @version 0.5.2
* @package Katharsis
*/
<?php
require_once('library/Katharsis/Autoload.php');
class Katharsis_Bootstrap
{
/**
* Central application routing method
*
* @return void
*/
public static function init()
{
$router = Katharsis_ControllerRouting::getInstance();
$router->init();
}
public static function run()
{
$router = Katharsis_ControllerRouting::getInstance();
$router->init();
Katharsis_Controller_Plugin::preControllerHook();
$router->route();
@ -27,3 +20,4 @@ class Katharsis_Bootstrap
Katharsis_Controller_Plugin::postControllerHook();
}
}
?>

View file

@ -1,29 +1,9 @@
<?php
/**
* Abstract Controller
* All controllers must extend this class.
*
* @author Karl Pannek <info@katharsis.in>
* @version 0.5.2
* @package Katharsis
*/
abstract class Katharsis_Controller_Abstract
{
/**
* @var Katharsis_Db5
*/
protected $_con;
/**
* @var Katharsis_View
*/
protected $_view;
/**
* Instances class attributes, calles init method
*
* @return void
*/
public final function __construct()
{
$this->_con = Katharsis_DatabaseConnector::getConnection();
@ -31,31 +11,15 @@ abstract class Katharsis_Controller_Abstract
$this->init();
}
/**
* Overwrite this method instead of using a constructor
*
* @return void
*/
public function init()
{
}
/**
* If subclass hasn't got a __call method, this exception will be thrown
*
* @return void
*/
public function __call($action, $params)
{
throw new Katharsis_Exception('Action "' . $action . '" doesn\'t exist.');
throw new Katharsis_Exception('Die von Ihnen angeforderte Seite (Action) "' . substr($action, 0, -6) . '" konnte nicht gefunden werden.');
}
/**
* Returns a specific request parameter
*
* @param string $key
* @return mixed
*/
protected function _getParam($key)
{
$params = Katharsis_Request::getParams();
@ -66,24 +30,11 @@ abstract class Katharsis_Controller_Abstract
return null;
}
/**
* Returns all request parameters
*
* @return array
*/
protected function _getAllParams()
{
return Katharsis_Request::getParams();
}
/**
* Forward to an other action
*
* @param string $action
* @param string $controller
* @param array $getParams
* @return void
*/
protected function _location($action, $controller = null, $getParams = null)
{
if($controller === null)
@ -103,3 +54,4 @@ abstract class Katharsis_Controller_Abstract
header("location: " . $this->_view->base . "/" . $controller . "/" . $action . $paramstring);
}
}
?>

View file

@ -1,35 +1,13 @@
<?php
/**
* Central Plugin Class
*
* @author Karl Pannek <info@katharsis.in>
* @version 0.5.2
* @package Katharsis
*/
class Katharsis_Controller_Plugin
{
/**
* @var array
*/
protected static $_plugins;
/**
* Register all plugins with this method
* Plugins will be called in the same order as they have been registered
*
* @param object $object - an instance of your plugin
* @return void
*/
public static function registerPlugin($object)
{
self::$_plugins[] = $object;
}
/**
* Processes preController methods of all plugins
*
* @return void
*/
public static function preControllerHook()
{
foreach(self::$_plugins as $plugin)
@ -38,11 +16,6 @@ class Katharsis_Controller_Plugin
}
}
/**
* Processes postController methods of all plugins
*
* @return void
*/
public static function postControllerHook()
{
foreach(self::$_plugins as $plugin)
@ -51,3 +24,4 @@ class Katharsis_Controller_Plugin
}
}
}
?>

View file

@ -1,36 +1,19 @@
<?php
/**
* Abstract Controller Plugin
* All controller plugins must extend this class.
*
* @author Karl Pannek <info@katharsis.in>
* @version 0.5.2
* @package Katharsis
*/
abstract class Katharsis_Controller_Plugin_Abstract
{
protected $_con;
public function __construct()
{
$this->_con = Katharsis_DatabaseConnector::getConnection();
}
/**
* Overwrite this method, if you want something to be processed
* _before_ the controller is called
*
* @return void
*/
public function preController()
{
}
/**
* Overwrite this method, if you want something to be processed
* _after_ the controller was called
*
* @return void
*/
public function postController()
{
}
}
?>

View file

@ -0,0 +1,21 @@
<?php
class Katharsis_Controller_Plugin_AutoScriptControl extends Katharsis_Controller_Plugin_Abstract
{
public function preController()
{
$view = Katharsis_View::getInstance();
$view->autoScriptFile = false;
$scriptName = ucfirst(Katharsis_Request::getControllerName()) . '/' . strtolower(Katharsis_Request::getActionName());
$autoScriptFile = 'scripts/DidgeridooArtwork/' . $scriptName . '.js';
$sl = DIRECTORY_SEPARATOR;
if(file_exists(getcwd() . $sl . str_replace('/', $sl, $autoScriptFile)))
{
$view->autoScriptFile = $view->base . '/' . $autoScriptFile;
}
}
}

View file

@ -1,33 +1,20 @@
<?php
/**
* Autorender Controller Plugin
*
* @author Karl Pannek <info@katharsis.in>
* @version 0.5.2
* @package Katharsis
*/
class Katharsis_Controller_Plugin_Autorender extends Katharsis_Controller_Plugin_Abstract
{
/**
* Renders a controller/action.phtml template automaticly after processing controller
*
* @return void
*/
public function postController()
{
$view = Katharsis_View::getInstance();
$view->controllerAction = false;
$view->stageContent = false;
$templateName = strtolower(Katharsis_Request::getControllerName()) . DIRECTORY_SEPARATOR . strtolower(Katharsis_Request::getActionName());
$templateName = ucfirst(Katharsis_Request::getControllerName()) . DIRECTORY_SEPARATOR . strtolower(Katharsis_Request::getActionName());
if(file_exists('application/view' . DIRECTORY_SEPARATOR . $templateName . '.phtml'))
if(file_exists(getcwd() . '/application/view' . DIRECTORY_SEPARATOR . $templateName . '.phtml'))
{
$view->controllerAction = $templateName;
$view->stageContent = $view->render($templateName);
}
$view = Katharsis_View::getInstance();
echo $view->render('main');
}
}
?>

View file

@ -1,24 +1,8 @@
<?php
/**
* Controller Router
* Specific routing entity
*
* @author Karl Pannek <info@katharsis.in>
* @version 0.5.2
* @package Katharsis
*/
class Katharsis_ControllerRouting
{
/**
* @var Katharsis_ControllerRouting
*/
protected static $_instance = null;
/**
* Singleton. Returns the same instance every time
*
* @return Katharsis_ControllerRouting
*/
public static function getInstance()
{
if(self::$_instance === null)
@ -28,28 +12,18 @@ class Katharsis_ControllerRouting
return self::$_instance;
}
/**
* Sets default controller and action names
*
* @return void
*/
protected function __construct()
{
Katharsis_Request::setControllerName('index');
Katharsis_Request::setActionName('index');
}
/**
* Initiates routing process
*
* @return void
*/
public function init()
{
$paramstring = "";
$baseUrl = preg_replace('#(.*/)[^/]+#', '\1', $_SERVER['SCRIPT_NAME']);
if(preg_match("~.*" . $baseUrl . "([^/\?]+)/([^/\?]+)/*([^\?]*)~", $_SERVER['REQUEST_URI'], $matches))
if(preg_match("~/([^/\?]+)/([^/\?]+)/*([^\?]*)~", $_SERVER['REQUEST_URI'], $matches))
{
$controller = $matches[1];
$action = $matches[2];
@ -58,14 +32,16 @@ class Katharsis_ControllerRouting
Katharsis_Request::setControllerName($controller);
Katharsis_Request::setActionName($action);
} else if(preg_match("~.*" . $baseUrl . "([^/\?]+)/*([^\?]*)~", $_SERVER['REQUEST_URI'], $matches))
}
else if(preg_match("~/([^/\?]+)/*([^\?]*)~", $_SERVER['REQUEST_URI'], $matches))
{
$controller = $matches[1];
$paramstring = $matches[2];
$params = $this->_buildParams($paramstring);
Katharsis_Request::setControllerName($controller);
} else
}
else
{
if(array_key_exists('controller', $_GET))
{
@ -87,12 +63,6 @@ class Katharsis_ControllerRouting
Katharsis_View::getInstance()->requestHook();
}
/**
* Routing processing method
*
* @return void
*/
public function route()
{
$controllerName = ucfirst(Katharsis_Request::getControllerName()) . 'Controller';
@ -104,14 +74,9 @@ class Katharsis_ControllerRouting
$controllerObject->$action();
}
}
/**
* Splits parameters to an array and returns them
*
* @return array
*/
protected function _buildParams($string)
{
$params = array();
@ -134,3 +99,4 @@ class Katharsis_ControllerRouting
return $params;
}
}
?>

View file

@ -1,25 +1,8 @@
<?php
/**
* Database Connector
* controls database connections
*
* @author Karl Pannek <info@katharsis.in>
* @version 0.5.2
* @package Katharsis
*/
class Katharsis_DatabaseConnector
{
/**
* @var array
*/
public static $_conns = array();
/**
* Reading ini file information and connecting
*
* @param string $requestedName
* @return Katharsis_Db5
*/
protected static function connect($requestedName = null)
{
$ini = parse_ini_file('config/database.config.ini', true);
@ -33,13 +16,7 @@ class Katharsis_DatabaseConnector
}
}
/**
* Connects to all connections in config file
*
* @param string $requestedName
* @return void
*/
public static function connectAll()
public static function connectAll($requestedName = null)
{
$groups = parse_ini_file('config/database.config.ini', true);
@ -52,14 +29,11 @@ class Katharsis_DatabaseConnector
}
}
/**
* Calling Katharsis Db connecting method
*
* @param string $requestedName
* @return Katharsis_Db5
*/
protected static function _realConnect($conInformation)
{
//$con = new PDO('mysql:host=' . $conInformation['host'] . ';dbname=' . $conInformation['database'], $conInformation['user'], $conInformation['password']);
$con = new Katharsis_Db5($conInformation['host'], $conInformation['user'], $conInformation['password'], $conInformation['database']);
self::$_conns[$conInformation['name']]['connection'] = $con;
@ -68,12 +42,6 @@ class Katharsis_DatabaseConnector
return $con;
}
/**
* Returns specified or default connection
*
* @param string $requestedName
* @return Katharsis_Db5
*/
public static function getConnection($requestedName = null)
{
if($requestedName === null)
@ -96,13 +64,6 @@ class Katharsis_DatabaseConnector
}
}
/**
* Returns an array of connection information
*
* @param array $ini
* @param string $requestedName
* @return array
*/
protected static function _selectConnection($ini, $requestedName = null)
{
foreach($ini as $name => $connectionInfo)
@ -128,3 +89,4 @@ class Katharsis_DatabaseConnector
throw new Katharsis_Exception('Could not find database connection information for "' . $requestedName . '"');
}
}
?>

View file

@ -1,22 +1,12 @@
<?php
/**
* Katharsis Database Class
* A mysql query class, that is based on native php functionality (PHP5)
* KatharsisDb5 is a mysql query class, that is based on native php functionality (PHP5)
*
* @author Karl Pannek <info@katharsis.in>
* @version 0.5.2
* @package Katharsis
* @author Karl Pannek
*/
class Katharsis_Db5
{
/**
* @var string
*/
const FETCHMODE_ASSOC = 'ASSOC';
/**
* @var string
*/
const FETCHMODE_ARRAY = 'ARRAY';
/**
@ -184,7 +174,7 @@ class Katharsis_Db5
*/
public function connect()
{
$this->_connection = @mysql_connect(
$this->_connection = mysql_connect(
$this->getHost(),
$this->getUser(),
$this->getPassword(),
@ -281,6 +271,10 @@ class Katharsis_Db5
{
$value = "'" . mysql_real_escape_string($value, $this->_connection) . "'";
}
if($value === null)
{
$value = 'NULL';
}
$sets[] = "`" . $key . "` = " . $value;
}
@ -297,6 +291,13 @@ class Katharsis_Db5
return $this->run($sql);
}
public function simpleDelete($table, $fieldvalue, $fieldname = 'id')
{
$sql = "DELETE FROM " . $table . " WHERE " . $fieldname . " = :field";
$sql = $this->createStatement($sql, array('field' => $fieldvalue));
return $this->run($sql);
}
/**
* Executes Query and returns number of rows
*
@ -334,21 +335,24 @@ class Katharsis_Db5
return $this->_fetch($statement, $fetchmode, true);
}
public function fetchField ($statement, $field = null)
/**
* Returns a fetched result (One rows)
*
* @param $statement
* @param $fetchmode
* @return array
*/
public function fetchField ($statement, $field = 0)
{
if($field === null)
if(intval($field) === $field)
{
$result = $this->_fetch($statement, self::FETCHMODE_ARRAY, true);
return isset($result[0]) ? $result[0] : null;
} else
{
$result = $this->_fetch($statement, self::FETCHMODE_ASSOC, true);
if(array_key_exists($field, $result))
{
return $result[$field];
}
return null;
}
return array_key_exists($field, $result) ? $result[$field] : null;
}
/**
@ -363,9 +367,9 @@ class Katharsis_Db5
$file = $file[count($file)-1];
echo '<pre style="margin: 10px; padding: 0px; background-color: #eee">';
echo '<pre style="position: absolute; z-index: 10000; margin: 10px; padding: 0px; background-color: #eee">';
echo '<table style="width: 100%"><tr><td style="font-size: 0.9em; padding-left: 10px; color: #aaa;">QUERY ANALYSIS | from ' . $file . ' on line ' . $debug[0]['line'] . '</td>';
echo '<table style="width: 1100px"><tr><td style="font-size: 0.9em; padding-left: 10px; color: #aaa;">QUERY ANALYSIS | from ' . $file . ' on line ' . $debug[0]['line'] . '</td>';
echo '<td align="right" style="padding: 5px;">';
echo '<button style="background-color: #ddd; border: 0px solid #bbb;" onclick="parentNode.parentNode.parentNode.parentNode.parentNode.style.display=\'none\';">X</button></td></tr></table>';
echo '<div style="margin: 1px; background-color: #FFFCE6; padding: 5px; color: #3F0808;">';
@ -408,6 +412,12 @@ class Katharsis_Db5
{
foreach($values as $key => $value)
{
if($value === null)
{
$statement = str_replace(":" . $key, 'NULL', $statement);
continue;
}
$wasString = false;
if(is_string($value))
{
@ -435,6 +445,18 @@ class Katharsis_Db5
return $statement;
}
public function getEmptyColumnArray($table)
{
$sql = $this->createStatement("SHOW COLUMNS FROM " . $table);
$columns = $this->fetchAll($sql);
$result = array();
foreach($columns as $column)
{
$result[$column['Field']] = $column['Default'];
}
return $result;
}
/**
* Last primary key that has been inserted
*
@ -486,17 +508,19 @@ class Katharsis_Db5
}
if($result = mysql_query($statement, $this->_connection))
{
$this->_lastStatement = $statement;
$this->_lastRowCount = mysql_affected_rows($this->_connection);
}
$this->_lastStatement = $statement;
if(mysql_error($this->_connection))
{
$this->_lastError['number'] = mysql_errno($this->_connection);
$this->_lastError['message'] = mysql_error($this->_connection);
$this->analyseLast();
} else
{
$this->_lastError = array();
$this->_lastResult = " ";
}
return $result;
@ -566,47 +590,25 @@ class Katharsis_Db5
/**
* KatharsisDb exception spicification
*
* @author Karl Pannek <info@katharsis.in>
* @version 0.5.2
* @package Katharsis
* @author Karl Pannek
*/
class KatharsisDb5_Exception extends Exception {}
/**
* KatharsisDb Result Set
*
* @author Karl Pannek <info@katharsis.in>
* @version 0.5.2
* @package Katharsis
* @author Karl Pannek
*/
class KatharsisDb5_ResultSet
{
/**
* @var Mysql Resource
*/
private $_resultSet;
/**
* @var Katharsis_Db5
*/
private $_connection;
/**
* Sets class attributes
*
* @param Mysql Resource $resultSet
*/
public function __construct($resultSet)
{
$this->_resultSet = $resultSet;
}
/**
* Fetching next row
*
* @param Mysql Resource $resultSet
*/
public function fetchNext ($fetchmode = Katharsis_Db5::FETCHMODE_ASSOC)
{
switch ($fetchmode)
@ -624,4 +626,6 @@ class KatharsisDb5_ResultSet
break;
}
}
}
?>

View file

@ -1,11 +1,4 @@
<?php
/**
* Katharsis Exception
*
* @author Karl Pannek <info@katharsis.in>
* @version 0.5.2
* @package Katharsis
*/
class Katharsis_Exception extends Exception
{
}

View file

@ -1,36 +1,16 @@
<?php
/**
* Abstract Model
* All models must extend this class.
*
* @author Karl Pannek <info@katharsis.in>
* @version 0.5.2
* @package Katharsis
*/
abstract class Katharsis_Model_Abstract
{
/**
* @var Katharsis_Db5
*/
protected $_con;
/**
* Instances class attributes, calles init method
*
* @return void
*/
public final function __construct()
{
$this->_con = Katharsis_DatabaseConnector::getConnection();
$this->init();
}
/**
* Overwrite this method instead of using a constructor
*
* @return void
*/
public function init()
{
}
}
?>

View file

@ -1,57 +1,20 @@
<?php
/**
* Katharsis Request
* Represents a http call
*
* @author Karl Pannek <info@katharsis.in>
* @version 0.5.2
* @package Katharsis
*/
class Katharsis_Request
{
/**
* @var string
*/
protected static $_controller;
/**
* @var string
*/
protected static $_action;
/**
* @var array
*/
protected static $_controller = null;
protected static $_action = null;
protected static $_params = array();
/**
* Set name of controller
*
* @param string $name
* @return void
*/
public static function setControllerName($name)
{
self::$_controller = $name;
}
/**
* Set name of action
*
* @param string $name
* @return void
*/
public static function setActionName($name)
{
self::$_action = $name;
}
/**
* Set parameters
*
* @param array $params
* @return void
*/
public static function setParams($params)
{
foreach($_POST as $key => $value)
@ -59,35 +22,23 @@ class Katharsis_Request
$params[$key] = $value;
}
self::$_params = $params;
}
/**
* Get controller name
*
* @return string
*/
public static function getControllerName()
{
return self::$_controller;
}
/**
* Get action name
*
* @return string
*/
public static function getActionName()
{
return self::$_action;
}
/**
* Get parameter array
*
* @return array
*/
public static function getParams()
{
return self::$_params;
}
}
?>

View file

@ -1,29 +1,9 @@
<?php
/**
* Katharsis View
* Controls anything related to the display level
*
* @author Karl Pannek <info@katharsis.in>
* @version 0.5.2
* @package Katharsis
*/
class Katharsis_View
{
/**
* @var Katharsis_View
*/
protected static $_instance = null;
protected $_items = array();
/**
* @var array
*/
protected $_params = array();
/**
* Singleton. Returns the same instance every time
*
* @return Katharsis_View
*/
public static function getInstance()
{
if(self::$_instance === null)
@ -33,49 +13,30 @@ class Katharsis_View
return self::$_instance;
}
/**
* Sets base application path
*
* @return void
*/
protected function __construct()
{
$base = preg_replace('/(.+)\/[^\/]+/', '\1', $_SERVER['SCRIPT_NAME']);
$this->_params['base'] = $base != $_SERVER['SCRIPT_NAME'] ? $base : '';
$this->_items['base'] = $base != $_SERVER['SCRIPT_NAME'] ? $base : '';
}
/**
* Magical get method, gets specific param
*
* @param string $name
* @return string
*/
public function __get($name)
{
if(array_key_exists($name, $this->_params))
if(array_key_exists($name, $this->_items))
{
return $this->_params[$name];
if(is_array($this->_items[$name]))
{
return (array) $this->_items[$name];
}
return $this->_items[$name];
}
return null;
}
/**
* Magical set method, sets specific param
*
* @param string name
* @param string value
*/
public function __set($name, $value)
{
$this->_params[$name] = $value;
$this->_items[$name] = $value;
}
/**
* Template rendering method
*
* @param string $template
* @return string
*/
public function render($template)
{
ob_start();
@ -89,13 +50,24 @@ class Katharsis_View
return $output;
}
/**
* Sets Request params into View params
*
* @return void
*/
public function requestHook()
{
$this->_params['params'] = Katharsis_Request::getParams();
}
public function _getParam($key)
{
$params = Katharsis_Request::getParams();
if(array_key_exists($key,$params))
{
return $params[$key];
}
return null;
}
public function formatDate($date)
{
$date = explode("-", $date);
return $date[2] . '.' . $date[1] . '.' . $date[0];
}
}
?>

View file

@ -1,4 +1,7 @@
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d

View file

@ -23,6 +23,7 @@ Katharsis_Controller_Plugin::registerPlugin(new DidgeridooArtwork_Controller_Plu
try {
Katharsis_Bootstrap::init();
Katharsis_Bootstrap::run();
} catch(Exception $e)
{