1
0
mirror of synced 2024-12-05 03:06:05 +03:00
This commit is contained in:
doctrine 2006-04-16 08:41:45 +00:00
parent 842fee428e
commit b6e9365743
10 changed files with 320 additions and 16 deletions

View File

@ -50,6 +50,8 @@ class Doctrine_DataDict {
return "C($length)";
elseif($length < 4000)
return "X";
else
return "X2";
break;
case "mbstring":
if($length < 255)

View File

@ -214,6 +214,7 @@ final class Doctrine {
case "Exception":
case "Session":
case "DQL":
case "Sensei":
$a[] = self::$path.DIRECTORY_SEPARATOR.$entry;
break;
default:

View File

@ -385,19 +385,17 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
if($this->state != Doctrine_Record::STATE_TDIRTY &&
$this->state != Doctrine_Record::STATE_TCLEAN &&
$this->state != Doctrine_Record::STATE_CLEAN) {
$this->loaded = true;
if( ! empty($this->collections)) {
foreach($this->collections as $collection) {
$collection->load($this);
}
} else {
$this->refresh();
}
$this->state = Doctrine_Record::STATE_CLEAN;
}
$this->loaded = true;
}
if(is_array($this->data[$name]))
@ -458,6 +456,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
$this->modified[] = $name;
switch($this->state):
case Doctrine_Record::STATE_CLEAN:
case Doctrine_Record::STATE_PROXY:

View File

@ -0,0 +1,192 @@
<?php
class Sensei_Group extends Doctrine_Record { }
//class Sensei_Company extends Sensei_Group { }
class Sensei_User extends Doctrine_Record { }
//class Sensei_Customer extends Sensei_User { }
class Sensei_Entity extends Doctrine_Record {
public function setTableDefinition() {
$this->hasColumn("loginname","string",32,"unique");
$this->hasColumn("password","string",32);
}
}
class Sensei_Variable extends Doctrine_Record {
public function setTableDefinition() {
$this->hasColumn("name","string",50);
$this->hasColumn("value","string",10000);
$this->hasColumn("session_id","integer");
}
}
class Sensei_Entity_Var extends Sensei_Variable { }
class Sensei_Session_Var extends Doctrine_Record { }
class Sensei_Session extends Doctrine_Record {
public function setUp() {
$this->ownsMany("Sensei_variable","Sensei_variable.session_id");
$this->hasOne("Sensei_entity","Sensei_session.entity_id");
}
public function setTableDefinition() {
$this->hasColumn("session_id","string",32);
$this->hasColumn("logged_in","integer",1);
$this->hasColumn("entity_id","integer");
$this->hasColumn("user_agent","string",200);
$this->hasColumn("updated","integer");
$this->hasColumn("created","integer");
}
}
class Sensei_Exception extends Exception { }
class Sensei extends Doctrine_Access {
const ATTR_LIFESPAN = 0;
/**
* @var Sensei_Session $record
*/
private $record;
/**
* @var Doctrine_Session $session
*/
private $session;
/**
* @var Doctrine_Table $table
*/
private $table;
/**
* @var array $attributes
*/
private $attributes = array();
/**
* @var Doctrine_Collection $vars
*/
private $vars;
public function __construct() {
if(headers_sent())
throw new Sensei_Exception("Headers already sent. Couldn't initialize session.");
$this->session = Doctrine_Manager::getInstance()->getCurrentSession();
$this->table = $this->session->getTable("Sensei_session");
$this->init();
$this->gc(1);
if( ! isset($_SESSION))
session_start();
}
/**
* getRecord
*/
public function getRecord() {
return $this->record;
}
/**
* init
*/
private function init() {
session_set_save_handler(
array($this,"open"),
array($this,"close"),
array($this,"read"),
array($this,"write"),
array($this,"destroy"),
array($this,"gc")
);
}
/**
* @param string $username
* @param string $password
* @return boolean
*/
public function login($username,$password) {
$coll = $this->session->query("FROM Sensei_Entity WHERE Sensei_Entity.loginname = ? && Sensei_Entity.password = ?",array($username,$password));
if(count($coll) > 0) {
$this->record->logged_in = 1;
$this->record->entity_id = $coll[0]->getID();
$this->record->save();
return true;
}
return false;
}
/**
* logout
* @return boolean
*/
public function logout() {
if( $this->record->logged_in == true) {
$this->record->logged_in = 0;
$this->record->entity_id = 0;
return true;
} else {
return false;
}
}
public function get($name) {
foreach($this->vars as $var) {
if($var->name == $name) {
return $var->value;
}
}
}
public function set($name,$value) {
foreach($this->vars as $var) {
if($var->name == $name) {
$var->value = $value;
return true;
}
}
return false;
}
public function setAttribute($attr, $value) {
switch($attr):
case Sensei::ATTR_LIFESPAN:
break;
default:
throw new Sensei_Exception("Unknown attribute");
endswitch;
$this->attributes[$attr] = $value;
}
private function open($save_path,$session_name) {
return true;
}
public function close() {
return true;
}
private function read($id) {
$coll = $this->session->query("FROM Sensei_Session WHERE Sensei_Session.session_id = ?",array($id));
$this->record = $coll[0];
$this->record->user_agent = $_SERVER['HTTP_USER_AGENT'];
$this->record->updated = time();
$this->record->session_id = $id;
if($this->record->getState() == Doctrine_Record::STATE_TDIRTY) {
$this->record->created = time();
$this->record->save();
}
$this->vars = $this->record->Sensei_variable;
return "";
}
public function write($id,$sess_data) {
return true;
}
private function destroy($id) {
$this->record->delete();
return $r;
}
private function gc($maxlifetime) {
return true;
}
public function flush() {
$this->record->save();
}
public function __destruct() {
$this->flush();
}
}
?>

View File

@ -169,13 +169,15 @@ abstract class Doctrine_Session extends Doctrine_Configurable implements Countab
if(isset($this->tables[$name]))
return $this->tables[$name];
$class = $name."Table";
if(class_exists($class, false) && in_array("Doctrine_Table", class_parents($class)))
if(class_exists($class, false) && in_array("Doctrine_Table", class_parents($class))) {
return new $class($name);
else
} else {
return new Doctrine_Table($name);
}
}
/**
* @return array -- an array of all initialized tables

View File

@ -145,8 +145,6 @@ class Doctrine_Table extends Doctrine_Configurable {
if(isset($this->columns)) {
$method = new ReflectionMethod($this->name,"setTableDefinition");
$class = $method->getDeclaringClass();
print $class->getName();
if( ! isset($this->tableName))
$this->tableName = strtolower($class->getName());

View File

@ -1,5 +1,5 @@
<?php
class Doctrine_Validator_Blank {
class Doctrine_Validator_Notblank {
/**
* @param Doctrine_Record $record
* @param string $key

View File

@ -0,0 +1,109 @@
<?php
require_once("../classes/Doctrine.class.php");
Doctrine::loadAll();
class Sensei_UnitTestCase extends UnitTestCase {
protected $manager;
protected $session;
protected $dbh;
protected $listener;
protected $users;
protected $tables;
private $init = false;
public function init() {
$this->manager = Doctrine_Manager::getInstance();
$this->manager->setAttribute(Doctrine::ATTR_CACHE, Doctrine::CACHE_NONE);
if($this->manager->count() > 0) {
$this->session = $this->manager->getSession(0);
$this->session->clear();
$this->dbh = $this->session->getDBH();
$this->listener = $this->manager->getAttribute(Doctrine::ATTR_LISTENER);
} else {
$this->dbh = Doctrine_DB::getConnection();
$this->session = $this->manager->openSession($this->dbh);
}
$this->tables = array("sensei_group","sensei_user","sensei_entity","sensei_session","sensei_variable");
$tables = $this->tables;
foreach($tables as $name) {
$this->dbh->query("DROP TABLE IF EXISTS $name");
}
$this->sensei = new Sensei();
$entity = new Sensei_Entity();
$entity->loginname = "Chuck Norris";
$entity->password = "toughguy";
$entity->save();
$this->init = true;
$this->record = $this->sensei->getRecord();
}
public function setUp() {
if( ! $this->init)
$this->init();
}
public function testConstructor() {
$this->assertTrue($this->record instanceof Sensei_Session);
if(isset($_COOKIE["PHPSESSID"])) {
$this->assertEqual($this->record->session_id, $_COOKIE["PHPSESSID"]);
}
$updated = $this->record->updated;
$this->assertFalse(empty($updated));
$created = $this->record->created;
$this->assertFalse(empty($created));
$this->assertEqual($this->record->user_agent, $_SERVER['HTTP_USER_AGENT']);
// make the changes persistent
$this->sensei->flush();
if(isset($_COOKIE["PHPSESSID"])) {
$this->assertEqual($this->record->session_id, $_COOKIE["PHPSESSID"]);
}
$updated = $this->record->updated;
$this->assertFalse(empty($updated));
$created = $this->record->created;
$this->assertFalse(empty($created));
$this->assertEqual($this->record->user_agent, $_SERVER['HTTP_USER_AGENT']);
}
public function testLogin() {
$this->assertFalse($this->sensei->login('Chuck Norris','unknown'));
$this->assertEqual($this->record->logged_in, null);
$this->assertEqual($this->record->entity_id, null);
$this->assertTrue($this->sensei->login('Chuck Norris','toughguy'));
$this->assertEqual($this->record->logged_in, 1);
$this->assertEqual($this->record->entity_id, 1);
}
public function testLogout() {
$this->assertTrue($this->sensei->logout());
$this->assertEqual($this->record->logged_in, 0);
$this->assertEqual($this->record->entity_id, 0);
$this->sensei->flush();
$this->assertEqual($this->record->logged_in, 0);
$this->assertEqual($this->record->entity_id, 0);
}
}
?>

View File

@ -30,13 +30,10 @@ class Doctrine_UnitTestCase extends UnitTestCase {
protected $users;
protected $tables;
private static $instances;
private $init = false;
public function init() {
$name = get_class($this);
if( ! isset($instances[$name]))
$instances[$name] = $this;
$this->manager = Doctrine_Manager::getInstance();
$this->manager->setAttribute(Doctrine::ATTR_CACHE, Doctrine::CACHE_NONE);
@ -61,6 +58,7 @@ class Doctrine_UnitTestCase extends UnitTestCase {
foreach($tables as $name) {
$table = $this->session->getTable($name);
$table->getCache()->deleteAll();
}

View File

@ -1,5 +1,5 @@
<?php
ob_start();
require_once("ConfigurableTestCase.class.php");
require_once("ManagerTestCase.class.php");
require_once("SessionTestCase.class.php");
@ -13,6 +13,7 @@ require_once("AccessTestCase.class.php");
require_once("ValidatorTestCase.class.php");
require_once("CacheSqliteTestCase.class.php");
require_once("SenseiTestCase.class.php");
print "<pre>";
@ -23,7 +24,7 @@ $test = new GroupTest("Doctrine Framework Unit Tests");
/**
$test->addTestCase(new Doctrine_RecordTestCase());
$test->addTestCase(new Doctrine_SessionTestCase());
@ -40,6 +41,8 @@ $test->addTestCase(new Doctrine_EventListenerTestCase());
$test->addTestCase(new Doctrine_DQL_ParserTestCase());
$test->addTestCase(new Doctrine_BatchIteratorTestCase());
*/
$test->addTestCase(new Sensei_UnitTestCase());
//$test->addTestCase(new Doctrine_Cache_FileTestCase());
//$test->addTestCase(new Doctrine_Cache_SqliteTestCase());
@ -71,5 +74,5 @@ print "Executed queries: ".count($a)."\n";
foreach($a as $query) {
print $query."\n";
}
ob_end_flush();
?>