Rewrite of the data / model structure
This commit is contained in:
parent
fe13754bc0
commit
3a0d27cb59
13 changed files with 202 additions and 107 deletions
7
TODO
7
TODO
|
|
@ -1,3 +1,6 @@
|
|||
- Add mongoDB support
|
||||
- Implement gravatar support
|
||||
-
|
||||
- User management
|
||||
- Blog
|
||||
- Administration UI
|
||||
- Implement form decorators
|
||||
- Do not connect models and datapoints. If datapoints want to return a model object thats fine but the model is not a part of the data storage(!)
|
||||
|
|
@ -4,13 +4,6 @@
|
|||
*/
|
||||
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
|
||||
{
|
||||
protected function _initDev()
|
||||
{
|
||||
// TODO: Move this out of dev.
|
||||
Zend_Controller_Action_HelperBroker::addPath(APPLICATION_PATH . '/../library/Fiktiv/Controller/Action/Helper', 'Fiktiv_Controller_Action_Helper');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Setup navigaion
|
||||
*
|
||||
|
|
@ -181,6 +174,20 @@ class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
|
|||
return $translate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Make sure we find our datapoints
|
||||
*/
|
||||
protected function _initDatapoints()
|
||||
{
|
||||
$this->bootstrap('autoloader');
|
||||
|
||||
// Include datapoints directory
|
||||
set_include_path(implode(PATH_SEPARATOR, array(
|
||||
APPLICATION_PATH . '/datapoints',
|
||||
get_include_path()
|
||||
)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup a sweet and simple database
|
||||
* connection.
|
||||
|
|
@ -198,10 +205,14 @@ class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
|
|||
// Use the adapter by default
|
||||
Zend_Db_Table::setDefaultAdapter($database);
|
||||
|
||||
// Fire up the db service for easier access to the tables
|
||||
// TODO: Better definition of tables in the database, maybe read meta?
|
||||
$dbLayer = Fiktiv_Db_Service::getInstance();
|
||||
$dbLayer->setTables(array('users'));
|
||||
/*
|
||||
* Fire up the data service
|
||||
* This is not needed as the data service will be
|
||||
* started when it is first accessed. Also, the
|
||||
* data service will automaticly load datapoints
|
||||
* on demand.
|
||||
*/
|
||||
$dbLayer = Fiktiv_Data_Service::getInstance();
|
||||
|
||||
return $database;
|
||||
}
|
||||
|
|
@ -220,4 +231,15 @@ class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
|
|||
|
||||
return $defaultNamespace;
|
||||
}
|
||||
|
||||
/**
|
||||
* This is just a temporary.
|
||||
*
|
||||
*/
|
||||
protected function _initOther()
|
||||
{
|
||||
// Add action helpers. Maybe should this should be in the frontController setup?
|
||||
Zend_Controller_Action_HelperBroker::addPath(APPLICATION_PATH . '/../library/Fiktiv/Controller/Action/Helper',
|
||||
'Fiktiv_Controller_Action_Helper');
|
||||
}
|
||||
}
|
||||
|
|
@ -3,7 +3,7 @@
|
|||
*
|
||||
*
|
||||
*/
|
||||
class Users extends Zend_Db_Table_Abstract
|
||||
class Users extends Zend_Db_Table_Abstract implements Fiktiv_Data_Storage
|
||||
{
|
||||
protected $_schema = 'fiktivkod';
|
||||
protected $_name = 'User';
|
||||
|
|
@ -12,7 +12,7 @@ class Users extends Zend_Db_Table_Abstract
|
|||
|
||||
/**
|
||||
* Authenticate user
|
||||
*
|
||||
* TODO: Evaluate, should this be here? Datapoint = data access only?
|
||||
* @param string $email
|
||||
* @param string $password
|
||||
*/
|
||||
|
|
@ -3,17 +3,91 @@
|
|||
* User
|
||||
*
|
||||
*/
|
||||
class User extends Zend_Db_Table_Row_Abstract
|
||||
class User
|
||||
{
|
||||
|
||||
protected $_email;
|
||||
|
||||
protected $_firstName;
|
||||
|
||||
protected $_lastName;
|
||||
|
||||
protected $_regDate;
|
||||
|
||||
protected $_isDeleted = false;
|
||||
|
||||
|
||||
public function __construct(array $data = array()) {
|
||||
|
||||
|
||||
if (isset($data['table']) && $data['table'] instanceof Users) {
|
||||
|
||||
$this->setFirstName($data['data']['firstName']);
|
||||
$this->setLastName($data['data']['lastName']);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Defines field not to show on __toString()
|
||||
*
|
||||
* @var array
|
||||
* Set user lastname
|
||||
*
|
||||
* @param string $name
|
||||
* @return boolean
|
||||
*/
|
||||
protected $_hiddenFields = array('password', 'salt');
|
||||
public function setLastName($name)
|
||||
{
|
||||
if (is_string($name) && preg_match('/^[A-ö\ \.\-]{0,20}$/', $name)) {
|
||||
$this->_lastName = $name;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set user firstname
|
||||
*
|
||||
* @param string $name
|
||||
* @return boolean
|
||||
*/
|
||||
public function setFirstName($name)
|
||||
{
|
||||
if (is_string($name) && preg_match('/^[A-ö\-]{0,10}$/', $name)) {
|
||||
$this->_firstName = $name;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the user registration date
|
||||
*
|
||||
* @return string $_regDate
|
||||
*/
|
||||
public function userSince()
|
||||
{
|
||||
return (!empty($_regDate)) ?: null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Controll wheter the user is currently active on the
|
||||
* site.
|
||||
* TODO: Make sure we have a way to see if the user is logged in.
|
||||
* @return boolean
|
||||
*/
|
||||
public function isActive()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
return '{' . join(',',array_diff_key($this->toArray(), array_flip($this->_hiddenFields))) . '}';
|
||||
return '{' . $this->_firstName . ' ' . $this->_lastName . '}';
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
class Blog_IndexController extends Zend_Controller_Action
|
||||
class Blog_IndexController extends Fiktiv_Controller_Action
|
||||
{
|
||||
public function indexAction()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
class ErrorController extends Zend_Controller_Action
|
||||
class ErrorController extends Fiktiv_Controller_Action
|
||||
{
|
||||
public function errorAction()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -5,11 +5,13 @@ class IndexController extends Fiktiv_Controller_Action
|
|||
public function indexAction()
|
||||
{
|
||||
// Here be dragons
|
||||
$m = new MyModel();
|
||||
echo $m->name;
|
||||
//$m = new MyModel();
|
||||
//echo $m->name;
|
||||
|
||||
$me = $this->dbService->users->findRandom();
|
||||
// Quick access
|
||||
$me = $this->dbService->Users->findRandom();
|
||||
echo $me;
|
||||
|
||||
}
|
||||
|
||||
public function aboutAction()
|
||||
|
|
@ -18,12 +20,7 @@ class IndexController extends Fiktiv_Controller_Action
|
|||
//$me = $dbLayer->users->findByEmail('fredric@fiktivkod.org');
|
||||
|
||||
$dbLayer = $this->_helper->dbService();
|
||||
$me = $dbLayer->users->findByEmail('fredric@fiktivkod.org');
|
||||
/*
|
||||
$users = new Users();
|
||||
$me = $users->findByEmail('fredric@fiktivkod.org');
|
||||
$dbLayer->users->findByEmail();
|
||||
*/
|
||||
$me = $dbLayer->Users->findByEmail('fredric@fiktivkod.org');
|
||||
echo $me;
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ function get_gravatar( $email, $s = 80, $d = '404', $r = 'g', $img = false, $att
|
|||
return $url;
|
||||
}
|
||||
|
||||
echo '<img src="'. get_gravatar('someone@somewhere.com') .'" />'
|
||||
//echo '<img src="'. get_gravatar('someone@somewhere.com') .'" />'
|
||||
|
||||
?>
|
||||
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ class Fiktiv_Controller_Action extends Zend_Controller_Action
|
|||
public function __get($name)
|
||||
{
|
||||
if ($this->_helper->$name instanceof Zend_Controller_Action_Helper_Abstract) {
|
||||
return call_user_func(array($this->_helper->$name, 'dbService'));
|
||||
return call_user_func(array($this->_helper, $name));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -7,7 +7,7 @@ class Fiktiv_Controller_Action_Helper_DbService extends Zend_Controller_Action_H
|
|||
{
|
||||
public function dbService()
|
||||
{
|
||||
return Fiktiv_Db_Service::getInstance();
|
||||
return Fiktiv_Data_Service::getInstance();
|
||||
}
|
||||
|
||||
public function direct()
|
||||
|
|
|
|||
65
library/Fiktiv/Data/Service.php
Normal file
65
library/Fiktiv/Data/Service.php
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
<?php
|
||||
/**
|
||||
* Data service to allow easy access to data storage
|
||||
*
|
||||
*/
|
||||
class Fiktiv_Data_Service
|
||||
{
|
||||
/**
|
||||
* Hold the singleton instance
|
||||
*
|
||||
* @var Fiktiv_Data_Service
|
||||
*/
|
||||
protected static $_instance = null;
|
||||
|
||||
/**
|
||||
* Set of datapoints that the service can access
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_dataStorage = array();
|
||||
|
||||
/**
|
||||
* Returns the current singleton instance
|
||||
*
|
||||
* @return Fiktiv_Data_Service $_instance
|
||||
*/
|
||||
public static function getInstance()
|
||||
{
|
||||
if (self::$_instance === null) {
|
||||
self::$_instance = new Fiktiv_Data_Service();
|
||||
}
|
||||
|
||||
return self::$_instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides access to the underlaying datapoint objects.
|
||||
* TODO: Better / faster loading of datapoints?
|
||||
*
|
||||
* @param $name
|
||||
* @return Fiktiv_Data_Point
|
||||
*/
|
||||
public function __get($name) {
|
||||
|
||||
/*
|
||||
* Two ways to go, if we already have the datapoint object we
|
||||
* will choose a faster route
|
||||
*/
|
||||
if (array_key_exists($name, $this->_dataStorage)) {
|
||||
|
||||
// If we for some very odd reason have lost the object, re-create it!
|
||||
if (!is_object($this->_dataStorage[$name])) {
|
||||
$this->_dataStorage[$name] = new $name();
|
||||
}
|
||||
|
||||
|
||||
} else if (class_exists($name) && array_key_exists('Fiktiv_Data_Storage', class_implements($name))) {
|
||||
|
||||
$this->_dataStorage[$name] = new $name();
|
||||
}
|
||||
|
||||
return $this->_dataStorage[$name];
|
||||
}
|
||||
|
||||
}
|
||||
6
library/Fiktiv/Data/Storage.php
Normal file
6
library/Fiktiv/Data/Storage.php
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
<?php
|
||||
/**
|
||||
* Fiktiv_Data_Storage interface
|
||||
*
|
||||
*/
|
||||
interface Fiktiv_Data_Storage {}
|
||||
|
|
@ -1,72 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* Database service to allow easy access to the tables in the database
|
||||
* This is a wrapper for all the table classes used.
|
||||
*
|
||||
*/
|
||||
class Fiktiv_Db_Service
|
||||
{
|
||||
/**
|
||||
* Hold the singleton instance
|
||||
*
|
||||
* @var Fiktiv_Db_Service
|
||||
*/
|
||||
protected static $_instance = null;
|
||||
|
||||
/**
|
||||
* Set of tables that the service can access
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_tables = array();
|
||||
|
||||
/**
|
||||
* Returns the current singleton instance
|
||||
*
|
||||
* @return Fiktiv_Db_Service $_instance
|
||||
*/
|
||||
public static function getInstance()
|
||||
{
|
||||
if (self::$_instance === null) {
|
||||
self::$_instance = new Fiktiv_Db_Service();
|
||||
}
|
||||
|
||||
return self::$_instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set tables the service can access.
|
||||
* This only defines the table, it does not
|
||||
* connect to the table. Connections are made
|
||||
* "at runtime" to the needed tables.
|
||||
*
|
||||
* @param array $options
|
||||
*/
|
||||
public function setTables(array $options) {
|
||||
|
||||
foreach ($options as $tbl) {
|
||||
$this->_tables[$tbl] = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides access to the underlaying table objects.
|
||||
*
|
||||
* @param $name
|
||||
* @return Zend_Db_Table_Abstract
|
||||
*/
|
||||
public function __get($name) {
|
||||
|
||||
if (array_key_exists($name, $this->_tables)) {
|
||||
|
||||
if (!($this->_tables[$name] instanceof Zend_Db_Table_Abstract)) {
|
||||
|
||||
$class = ucfirst($name);
|
||||
$this->_tables[$name] = new $class();
|
||||
}
|
||||
}
|
||||
|
||||
return $this->_tables[$name];
|
||||
}
|
||||
|
||||
}
|
||||
Reference in a new issue