Commit 09ba0c35 authored by Jonathan.Wage's avatar Jonathan.Wage

Refactored some more stuff, cleaned things up, adding request validation to the server.

parent c9298919
...@@ -68,24 +68,23 @@ class Doctrine_Resource_Client extends Doctrine_Resource ...@@ -68,24 +68,23 @@ class Doctrine_Resource_Client extends Doctrine_Resource
$schema = file_get_contents($path); $schema = file_get_contents($path);
} else { } else {
$request = new Doctrine_Resource_Request(); $request = new Doctrine_Resource_Request();
$request->set('type', 'load'); $request->set('action', 'load');
$request->set('format', $this->getConfig()->get('format'));
$schema = $request->execute(); $schema = $request->execute();
if ($schema) { if ($schema) {
file_put_contents($path, Doctrine_Parser::dump($schema, $this->getConfig()->get('format'))); file_put_contents($path, Doctrine_Parser::dump($schema, 'xml'));
} }
} }
if (file_exists($path) && $schema) { if (file_exists($path) && $schema) {
$import = new Doctrine_Import_Schema(); $import = new Doctrine_Import_Schema();
$schema = $import->buildSchema($path, $this->getConfig()->get('format')); $schema = $import->buildSchema($path, 'xml');
if (!file_exists($classesPath)) { if (!file_exists($classesPath)) {
$build = "<?php\n"; $build = "<?php\n";
foreach ($schema['schema'] as $className => $details) { foreach ($schema['schema'] as $className => $details) {
$build .= "class " . $className . " extends Doctrine_Resource_Record { protected \$_model = '".$className."'; public function __construct(\$loadRelations = true) { parent::__construct(\$this->_model, \$loadRelations); } }\n"; $build .= "class " . $className . " extends Doctrine_Resource_Record { protected \$_model = '".$className."'; public function __construct() { parent::__construct(\$this->_model); } }\n";
$schema['schema'][$className]['relations'] = isset($schema['relations'][$className]) ? $schema['relations'][$className]:array(); $schema['schema'][$className]['relations'] = isset($schema['relations'][$className]) ? $schema['relations'][$className]:array();
} }
......
...@@ -36,18 +36,12 @@ class Doctrine_Resource_Collection extends Doctrine_Resource_Access implements C ...@@ -36,18 +36,12 @@ class Doctrine_Resource_Collection extends Doctrine_Resource_Access implements C
protected $_data = array(); protected $_data = array();
protected $_config = array(); protected $_config = array();
protected $_model = null; protected $_model = null;
protected $_parent = null;
public function __construct($model) public function __construct($model)
{ {
$this->_model = $model; $this->_model = $model;
} }
public function setParent($parent)
{
$this->_parent = $parent;
}
public function getConfig($key = null) public function getConfig($key = null)
{ {
return Doctrine_Resource_Client::getInstance()->getConfig($key); return Doctrine_Resource_Client::getInstance()->getConfig($key);
...@@ -60,7 +54,7 @@ class Doctrine_Resource_Collection extends Doctrine_Resource_Access implements C ...@@ -60,7 +54,7 @@ class Doctrine_Resource_Collection extends Doctrine_Resource_Access implements C
public function get($key) public function get($key)
{ {
if (!$key || !isset($this->_data[$key])) { if (!isset($key) || !isset($this->_data[$key])) {
return $this->add(); return $this->add();
} else { } else {
return $this->_data[$key]; return $this->_data[$key];
...@@ -69,7 +63,7 @@ class Doctrine_Resource_Collection extends Doctrine_Resource_Access implements C ...@@ -69,7 +63,7 @@ class Doctrine_Resource_Collection extends Doctrine_Resource_Access implements C
public function set($key, $value) public function set($key, $value)
{ {
if (!$key || !isset($this->_data[$key])) { if (!isset($key) || !isset($this->_data[$key])) {
$this->_data[$key] = $value; $this->_data[$key] = $value;
} else { } else {
$val = $this->add(); $val = $this->add();
...@@ -78,29 +72,17 @@ class Doctrine_Resource_Collection extends Doctrine_Resource_Access implements C ...@@ -78,29 +72,17 @@ class Doctrine_Resource_Collection extends Doctrine_Resource_Access implements C
} }
} }
public function add($value = null) public function add(Doctrine_Resource_Record $value = null)
{ {
if (!$value) { if (!$value) {
$model = $this->_model; $value = new $this->_model;
$value = new $model(false);
$table = $value->getTable();
$relation = $table->getRelationByClassName(get_class($this->_parent));
$alias = $relation['alias'];
if ($relation['type'] === Doctrine_Relation::ONE) {
$value->set($alias, $this->_parent);
} else {
$collection = new Doctrine_Resource_Collection($relation['class']);
$collection[] = $this->_parent;
$value->set($alias, $collection);
}
} }
$this->_data[] = $value; if ($value) {
$this->_data[] = $value;
return $value;
return $value;
}
} }
public function getIterator() public function getIterator()
...@@ -115,15 +97,20 @@ class Doctrine_Resource_Collection extends Doctrine_Resource_Access implements C ...@@ -115,15 +97,20 @@ class Doctrine_Resource_Collection extends Doctrine_Resource_Access implements C
public function toArray($deep = false) public function toArray($deep = false)
{ {
$array = array(); $data = array();
foreach ($this->_data as $key => $record) { foreach ($this->_data as $key => $record) {
if ($record->exists() || $record->hasChanges()) { $data[$key] = $record->toArray($deep);
$array[$this->_model . '_' .$key] = $record->toArray($deep);
}
} }
return $array; return $data;
}
public function fromArray(array $array)
{
foreach ($array as $key => $record) {
$this->add()->fromArray($record);
}
} }
public function save() public function save()
......
...@@ -55,25 +55,23 @@ class Doctrine_Resource_Query ...@@ -55,25 +55,23 @@ class Doctrine_Resource_Query
$request = new Doctrine_Resource_Request(); $request = new Doctrine_Resource_Request();
$request->set('dql', $this->getDql()); $request->set('dql', $this->getDql());
$request->set('params', $params); $request->set('params', $params);
$request->set('format', $this->getConfig()->get('format')); $request->set('action', 'query');
$request->set('type', 'query');
$request->set('model', $this->getModel()); $request->set('model', $this->getModel());
$response = $request->execute(); $response = $request->execute();
// If we have a response then lets parse it and hydrate it
if (!empty($response)) { if (!empty($response)) {
return $request->hydrate($response, $this->getModel());
// Otherwise lets return an empty collection for the queried for model
} else {
$model = $this->getModel();
$collection = new Doctrine_Resource_Collection($this->getModel()); $collection = new Doctrine_Resource_Collection($this->getModel());
$collection->fromArray($response);
} else {
$model = $this->getModel();
$collection[] = new $model(false); $collection = new Doctrine_Resource_Collection($model);
$collection[] = new $model();
return $collection;
} }
return $collection;
} }
public function getDql() public function getDql()
......
...@@ -37,11 +37,11 @@ class Doctrine_Resource_Record extends Doctrine_Resource_Access implements Count ...@@ -37,11 +37,11 @@ class Doctrine_Resource_Record extends Doctrine_Resource_Access implements Count
protected $_model = null; protected $_model = null;
protected $_changes = array(); protected $_changes = array();
public function __construct($model, $loadRelations = true) public function __construct($model)
{ {
$this->_model = $model; $this->_model = $model;
$this->initialize($loadRelations); $this->initialize();
} }
public function clearChanges() public function clearChanges()
...@@ -63,19 +63,6 @@ class Doctrine_Resource_Record extends Doctrine_Resource_Access implements Count ...@@ -63,19 +63,6 @@ class Doctrine_Resource_Record extends Doctrine_Resource_Access implements Count
} }
} }
} }
if (isset($schema['relations']) && $loadRelations) {
$relations = $schema['relations'];
foreach ($relations as $relation) {
if ($relation['type'] === Doctrine_Relation::ONE) {
$this->_data[$relation['alias']] = new $relation['class'](false);
} else {
$this->_data[$relation['alias']] = new Doctrine_Resource_Collection($relation['class']);
$this->_data[$relation['alias']]->setParent($this);
}
}
}
} }
public function getConfig($key = null) public function getConfig($key = null)
...@@ -85,7 +72,7 @@ class Doctrine_Resource_Record extends Doctrine_Resource_Access implements Count ...@@ -85,7 +72,7 @@ class Doctrine_Resource_Record extends Doctrine_Resource_Access implements Count
public function get($key) public function get($key)
{ {
if (!$key) { if (!isset($key)) {
return; return;
} }
...@@ -102,7 +89,7 @@ class Doctrine_Resource_Record extends Doctrine_Resource_Access implements Count ...@@ -102,7 +89,7 @@ class Doctrine_Resource_Record extends Doctrine_Resource_Access implements Count
public function set($key, $value) public function set($key, $value)
{ {
if (!$key) { if (!isset($key)) {
return; return;
} }
...@@ -127,18 +114,9 @@ class Doctrine_Resource_Record extends Doctrine_Resource_Access implements Count ...@@ -127,18 +114,9 @@ class Doctrine_Resource_Record extends Doctrine_Resource_Access implements Count
$class = $relation['class']; $class = $relation['class'];
if ($relation['type'] === Doctrine_Relation::ONE) { if ($relation['type'] === Doctrine_Relation::ONE) {
$return = new $class(false); $return = new $class();
$table = $return->getTable();
$returnRelation = $table->getRelationByClassName(get_class($this));
if ($returnRelation) {
$returnClass = new $returnRelation['class'](false);
$return->set($returnRelation['alias'], $returnClass);
}
} else { } else {
$return = new Doctrine_Resource_Collection($class); $return = new Doctrine_Resource_Collection($class);
$return->setParent($this);
} }
return $return; return $return;
...@@ -154,31 +132,6 @@ class Doctrine_Resource_Record extends Doctrine_Resource_Access implements Count ...@@ -154,31 +132,6 @@ class Doctrine_Resource_Record extends Doctrine_Resource_Access implements Count
return new ArrayIterator($this->_data); return new ArrayIterator($this->_data);
} }
public function sameAs(Doctrine_Resource_Record $record)
{
// If we have same class name
if (get_class($this) == get_class($record)) {
// If we have 2 records that exist and are persistant
if ($record->exists() && $this->exists()) {
if ($record->identifier() === $this->identifier()) {
return true;
} else {
return false;
}
// If we have unsaved records then lets compare the data
} else {
if ($record->toArray(false) === $this->toArray(false)) {
return true;
} else {
return false;
}
}
} else {
return false;
}
}
public function getChanges() public function getChanges()
{ {
global $gotten; global $gotten;
...@@ -232,29 +185,21 @@ class Doctrine_Resource_Record extends Doctrine_Resource_Access implements Count ...@@ -232,29 +185,21 @@ class Doctrine_Resource_Record extends Doctrine_Resource_Access implements Count
public function save() public function save()
{ {
$format = $this->getConfig('format');
$request = new Doctrine_Resource_Request(); $request = new Doctrine_Resource_Request();
$request->set('format', $format); $request->set('action', 'save');
$request->set('type', 'save');
$request->set('model', $this->getModel()); $request->set('model', $this->getModel());
$request->set('data', $this->getChanges());
$request->set('identifier', $this->identifier()); $request->set('identifier', $this->identifier());
$request->set('data', $this->getChanges());
$response = $request->execute(); $response = $request->execute();
$this->_data = $request->hydrate(array($response), $this->_model, array($this))->getFirst()->_data; $this->fromArray($response);
$this->clearChanges();
} }
public function delete() public function delete()
{ {
$format = $this->getConfig('format');
$request = new Doctrine_Resource_Request(); $request = new Doctrine_Resource_Request();
$request->set('format', $format); $request->set('action', 'delete');
$request->set('type', 'delete');
$request->set('model', $this->getModel()); $request->set('model', $this->getModel());
$request->set('identifier', $this->identifier()); $request->set('identifier', $this->identifier());
...@@ -306,42 +251,28 @@ class Doctrine_Resource_Record extends Doctrine_Resource_Access implements Count ...@@ -306,42 +251,28 @@ class Doctrine_Resource_Record extends Doctrine_Resource_Access implements Count
public function toArray($deep = false) public function toArray($deep = false)
{ {
global $gotten; $data = array();
if (!$gotten) {
$gotten = array();
}
$md5Hash = $this->getMd5Hash();
if (!in_array($md5Hash, $gotten)) {
$gotten[] = $md5Hash;
}
$array = array();
foreach ($this->_data as $key => $value) { foreach ($this->_data as $key => $value) {
if ($deep && $this->getTable()->hasRelation($key)) { if ($deep && $this->getTable()->hasRelation($key)) {
if ($value instanceof Doctrine_Resource_Collection) { $data[$key] = $this->$key->toArray($deep);
if ($value->count() > 0) { } else if ($this->getTable()->hasColumn($key)) {
foreach ($value as $key2 => $record) { $data[$key] = $value;
if (($record->exists() || $record->hasChanges()) && !in_array($record->getMd5Hash(), $gotten)) {
$array[$key][get_class($record) . '_' . $key2] = $record->toArray($deep);
}
}
}
} else if ($value instanceof Doctrine_Resource_Record) {
if (($value->exists() || $value->hasChanges()) && !in_array($value->getMd5Hash(), $gotten)) {
$array[$key] = $value->toArray($deep);
}
}
} else if (!$this->getTable()->hasRelation($key) && $this->getTable()->hasColumn($key)) {
$array[$key] = $value;
} }
} }
return $array; return $data;
}
public function fromArray(array $array)
{
foreach ($array as $key => $value) {
if ($this->getTable()->hasRelation($key) && is_array($value)) {
$this->$key->fromArray($value);
} else if ($this->getTable()->hasColumn($key)) {
$this->$key = $value;
}
}
} }
public function getMd5Hash() public function getMd5Hash()
......
...@@ -43,14 +43,15 @@ class Doctrine_Resource_Request extends Doctrine_Resource_Params ...@@ -43,14 +43,15 @@ class Doctrine_Resource_Request extends Doctrine_Resource_Params
public function execute() public function execute()
{ {
$url = $this->getConfig()->get('url'); $url = $this->getConfig()->get('url');
$data = array('type' => $this->get('type'), 'format' => $this->getConfig()->get('format'), 'data' => Doctrine_Parser::dump($this->getAll(), $this->getConfig()->get('format')));
$request = array('xml' => Doctrine_Parser::dump($this->getAll(), 'xml'));
$ch = curl_init(); $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
$response = curl_exec($ch); $response = curl_exec($ch);
if (curl_errno($ch)) { if (curl_errno($ch)) {
...@@ -71,66 +72,4 @@ class Doctrine_Resource_Request extends Doctrine_Resource_Params ...@@ -71,66 +72,4 @@ class Doctrine_Resource_Request extends Doctrine_Resource_Params
return $array; return $array;
} }
public function hydrate(array $array, $model, $records = array())
{
$collection = new Doctrine_Resource_Collection($model);
foreach ($array as $recordKey => $record) {
if (isset($records[$recordKey])) {
$r = $records[$recordKey];
} else {
$r = new $model(false);
}
foreach ($record as $key => $value) {
if ($r->getTable()->hasRelation($key) && !empty($value)) {
$relation = $r->getTable()->getRelation($key);
if ($relation['type'] === Doctrine_Relation::MANY) {
$relationCollection = $this->hydrate($value, $relation['class']);
$relationCollection->setParent($r);
foreach ($relationCollection as $relationRecord) {
$relationTable = $relationRecord->getTable();
if ($relation = $relationTable->getRelationByClassName($model)) {
if ($relation['type'] === Doctrine_Relation::ONE) {
$relationRecord->set($relation['alias'], $r);
$relationRecord->clearChanges();
} else {
$coll = new Doctrine_Resource_Collection($relation['class']);
$coll[] = $r;
$relationRecord->set($relation['alias'], $coll);
}
$relationRecord->clearChanges();
}
}
$r->set($key, $relationCollection);
} else {
$relationRecord = $this->hydrate(array($value), $relation['class'])->getFirst();
$relationTable = $relationRecord->getTable();
if ($relation = $relationTable->getRelationByClassName($model)) {
$relationRecord->set($relation['alias'], $r);
$relationRecord->clearChanges();
}
$r->set($key, $relationRecord);
}
} else if($r->getTable()->hasColumn($key)) {
$r->set($key, $value);
}
$r->clearChanges();
}
$collection[] = $r;
}
return $collection;
}
} }
\ No newline at end of file
...@@ -52,7 +52,42 @@ class Doctrine_Resource_Server extends Doctrine_Resource ...@@ -52,7 +52,42 @@ class Doctrine_Resource_Server extends Doctrine_Resource
return $instance; return $instance;
} }
public function validate($errors)
{
if (!empty($errors)) {
throw new Doctrine_Resource_Exception(count($errors) . ' error(s) occurred: ' . implode('. ', $errors));
} else {
return true;
}
}
public function validateOpenRecord($request)
{
$errors = array();
if (!$request->has('model') || !$request->get('model')) {
$errors[] = 'You must specify the model/class name you are deleting';
}
if (!$request->has('identifier') || !is_array($request->get('identifier'))) {
$errors[] = 'You must specify an array containing the identifiers for the model you wish to delete';
}
return $errors;
}
public function validateSave($request)
{
$errors = $this->validateOpenRecord($request);
if (!$request->has('data') || !$request->get('data')) {
$errors[] = 'You must specify an containing the changed data to save to the model';
}
return $errors;
}
public function executeSave($request) public function executeSave($request)
{ {
$model = $request->get('model'); $model = $request->get('model');
...@@ -80,6 +115,11 @@ class Doctrine_Resource_Server extends Doctrine_Resource ...@@ -80,6 +115,11 @@ class Doctrine_Resource_Server extends Doctrine_Resource
return $record->toArray(true, true); return $record->toArray(true, true);
} }
public function validateDelete($request)
{
return $this->validateOpenRecord($request);
}
public function executeDelete($request) public function executeDelete($request)
{ {
$model = $request->get('model'); $model = $request->get('model');
...@@ -89,7 +129,22 @@ class Doctrine_Resource_Server extends Doctrine_Resource ...@@ -89,7 +129,22 @@ class Doctrine_Resource_Server extends Doctrine_Resource
$record = $table->find($identifier); $record = $table->find($identifier);
$record->delete(); if ($record) {
$record->delete();
} else {
throw new Doctrine_Resource_Exception('Record could not be deleted because it is not a valid record');
}
}
public function validateQuery($request)
{
$errors = array();
if (!$request->has('dql') || !$request->get('dql')) {
$errors[] = 'You must specify a dql string in order to execute a query';
}
return $errors;
} }
public function executeQuery($request) public function executeQuery($request)
...@@ -102,16 +157,23 @@ class Doctrine_Resource_Server extends Doctrine_Resource ...@@ -102,16 +157,23 @@ class Doctrine_Resource_Server extends Doctrine_Resource
return $conn->query($dql, $params)->toArray(true, true); return $conn->query($dql, $params)->toArray(true, true);
} }
public function validateLoad($request)
{
$errors = array();
return $errors;
}
public function executeLoad($request) public function executeLoad($request)
{ {
$path = '/tmp/' . rand() . '.' . $request->get('format'); $path = '/tmp/' . rand();
$models = $this->getConfig('models') ? $this->getConfig('models'):array(); $models = $this->getConfig('models') ? $this->getConfig('models'):array();
$export = new Doctrine_Export_Schema(); $export = new Doctrine_Export_Schema();
$export->exportSchema($path, $request->get('format'), null, $models); $export->exportSchema($path, 'xml', null, $models);
$schema = Doctrine_Parser::load($path, $request->get('format')); $schema = Doctrine_Parser::load($path, 'xml');
unlink($path); unlink($path);
...@@ -120,29 +182,29 @@ class Doctrine_Resource_Server extends Doctrine_Resource ...@@ -120,29 +182,29 @@ class Doctrine_Resource_Server extends Doctrine_Resource
public function execute(array $r) public function execute(array $r)
{ {
if (!isset($r['data'])) { if (!isset($r['xml'])) {
throw new Doctrine_Resource_Exception('You must specify a data xml string in your request'); throw new Doctrine_Resource_Exception('You must specify an xml string in your request');
} }
$type = $r['type']; $requestArray = Doctrine_Parser::load($r['xml']);
$format = isset($r['format']) ? $r['format']:'xml';
$data = Doctrine_Parser::load($r['data'], $format);
$funcName = 'execute' . Doctrine::classify($type); $request = new Doctrine_Resource_Request($requestArray);
$requestObj = new Doctrine_Resource_Request($data); $funcName = 'execute' . Doctrine::classify($request->get('action'));
if (method_exists($this, $funcName)) { if (method_exists($this, $funcName)) {
$result = $this->$funcName($requestObj); $validateFuncName = 'validate' . Doctrine::classify($request->get('action'));
$errors = $this->$validateFuncName($request);
if ($this->validate($errors)) {
$result = $this->$funcName($request);
return Doctrine_Parser::dump($result, 'xml');
}
} else { } else {
throw new Doctrine_Resource_Exception('Unknown Doctrine Resource Server function'); throw new Doctrine_Resource_Exception('Unknown Doctrine Resource Server function');
} }
if ($result) {
return Doctrine_Parser::dump($result, $format);
} else {
return null;
}
} }
public function run($request) public function run($request)
......
...@@ -119,19 +119,6 @@ class Doctrine_Resource_Table ...@@ -119,19 +119,6 @@ class Doctrine_Resource_Table
} }
} }
public function getRelationByClassName($name)
{
$relations = $this->getRelations();
foreach ($relations as $relation) {
if ($relation['class'] === $name) {
return $relation;
}
}
return false;
}
public function getIdentifier() public function getIdentifier()
{ {
$identifier = array(); $identifier = array();
......
<?php <?php
require_once('playground.php'); require_once('playground.php');
$action = isset($_REQUEST['action']) ? $_REQUEST['action']:'client'; if (isset($_REQUEST['server'])) {
if ($action == 'server') {
require_once('connection.php'); require_once('connection.php');
require_once('models.php'); require_once('models.php');
require_once('data.php'); require_once('data.php');
...@@ -15,62 +13,15 @@ if ($action == 'server') { ...@@ -15,62 +13,15 @@ if ($action == 'server') {
$server->run($_REQUEST); $server->run($_REQUEST);
} else { } else {
$url = 'http://localhost/~jwage/doctrine_trunk/playground/index.php?action=server'; $url = 'http://localhost/~jwage/doctrine_trunk/playground/index.php?server';
$config = array('format' => 'yml'); $config = array('format' => 'xml');
// Instantiate a new client // Instantiate a new client
$client = Doctrine_Resource_Client::getInstance($url, $config); $client = Doctrine_Resource_Client::getInstance($url, $config);
/*
$query = new Doctrine_Resource_Query(); $query = new Doctrine_Resource_Query();
$users = $query->from('User u, u.Group g')->execute();
print_r($users->toArray(true));
*/
/*
$group = new Group();
$group->name = 'Jon';
$group->save();
print_r($group->toArray());
*/
//$client->printSchema();
/*
// Retrieve a models table object
$table = $client->getTable('User');
$user = new User();
$user->name = 'Jon Wage';
$user->Email->address = 'jonwage@gmail.com';
$phone = $user->Phonenumber[0];
$phone->phonenumber = '555-5555';
$phone = $user->Phonenumber[1];
$phone->phonenumber = '555-55555';
$user->Phonenumber[2]->phonenumber = '555';
$user->Account->amount = 50.00;
$user->Account->amount = 25.25;
$address = $user->Address[0];
$address->address = '112 2nd Ave North';
$album = $user->Album[0];
$album->name = 'test album';
$song = $album->Song[0];
$song->title = 'test author';
$user->save(); $users = $query->from('User u')->execute();
print_r($user->toArray(true)); print_r($users->toArray());
*/
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment