1
0
mirror of synced 2024-12-14 23:26:04 +03:00

General fixes/work.

This commit is contained in:
Jonathan.Wage 2007-09-27 22:02:13 +00:00
parent 900df377c8
commit 90541334e1
5 changed files with 58 additions and 21 deletions

View File

@ -34,7 +34,7 @@
class Doctrine_Resource class Doctrine_Resource
{ {
protected $_config = null; protected $_config = null;
protected $_defaultFormat = 'xml'; const FORMAT = 'json';
public function __construct($config) public function __construct($config)
{ {
@ -48,10 +48,6 @@ class Doctrine_Resource
$this->loadDoctrine = true; $this->loadDoctrine = true;
} }
} }
if (!$this->getConfig()->has('format') OR !$this->getConfig()->get('format')) {
$this->getConfig()->set('format', $this->_defaultFormat);
}
} }
public function getConfig($key = null) public function getConfig($key = null)

View File

@ -61,7 +61,7 @@ class Doctrine_Resource_Client extends Doctrine_Resource
public function loadDoctrine() public function loadDoctrine()
{ {
$path = '/tmp/' . md5(serialize($this->getConfig())); $path = '/tmp/' . $this->getClientKey();
$classesPath = $path.'.classes.php'; $classesPath = $path.'.classes.php';
if (file_exists($path)) { if (file_exists($path)) {
@ -73,13 +73,13 @@ class Doctrine_Resource_Client extends Doctrine_Resource
$schema = $request->execute(); $schema = $request->execute();
if ($schema) { if ($schema) {
file_put_contents($path, Doctrine_Parser::dump($schema, 'xml')); file_put_contents($path, Doctrine_Parser::dump($schema, Doctrine_Resource::FORMAT));
} }
} }
if (file_exists($path) && $schema) { if (file_exists($path) && $schema) {
$import = new Doctrine_Import_Schema(); $import = new Doctrine_Import_Schema();
$schema = $import->buildSchema($path, 'xml'); $schema = $import->buildSchema($path, Doctrine_Resource::FORMAT);
if (!file_exists($classesPath)) { if (!file_exists($classesPath)) {
$build = "<?php\n"; $build = "<?php\n";
@ -98,6 +98,11 @@ class Doctrine_Resource_Client extends Doctrine_Resource
} }
} }
public function getClientKey()
{
return md5(Doctrine_Resource::FORMAT.serialize($this->getConfig()));
}
public function getTable($table) public function getTable($table)
{ {
static $instance; static $instance;

View File

@ -44,13 +44,15 @@ class Doctrine_Resource_Request extends Doctrine_Resource_Params
{ {
$url = $this->getConfig()->get('url'); $url = $this->getConfig()->get('url');
$request = array('xml' => Doctrine_Parser::dump($this->getAll(), 'xml')); $request = array('request' => Doctrine_Parser::dump($this->getAll(), $this->getFormat()));
$header[0] = 'Accept: ' . $this->getFormat();
$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_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request); curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
$response = curl_exec($ch); $response = curl_exec($ch);
@ -63,7 +65,7 @@ class Doctrine_Resource_Request extends Doctrine_Resource_Params
$array = array(); $array = array();
if ($response) { if ($response) {
$array = Doctrine_Parser::load($response, $this->getConfig()->get('format')); $array = Doctrine_Parser::load($response, $this->getFormat());
} }
if (isset($array['error'])) { if (isset($array['error'])) {
@ -72,4 +74,9 @@ class Doctrine_Resource_Request extends Doctrine_Resource_Params
return $array; return $array;
} }
public function getFormat()
{
return ($this->getConfig()->has('format') && $this->getConfig()->get('format')) ? $this->getConfig()->get('format'):Doctrine_Resource::FORMAT;
}
} }

View File

@ -171,9 +171,9 @@ class Doctrine_Resource_Server extends Doctrine_Resource
$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, 'xml', null, $models); $export->exportSchema($path, $this->getFormat(), null, $models);
$schema = Doctrine_Parser::load($path, 'xml'); $schema = Doctrine_Parser::load($path, $this->getFormat());
unlink($path); unlink($path);
@ -182,11 +182,11 @@ class Doctrine_Resource_Server extends Doctrine_Resource
public function execute(array $r) public function execute(array $r)
{ {
if (!isset($r['xml'])) { if (!isset($r['request'])) {
throw new Doctrine_Resource_Exception('You must specify an xml string in your request'); throw new Doctrine_Resource_Exception('You must specify a request '.$this->getFormat().' string in your request');
} }
$requestArray = Doctrine_Parser::load($r['xml']); $requestArray = Doctrine_Parser::load($r['request'], $this->getFormat());
$request = new Doctrine_Resource_Request($requestArray); $request = new Doctrine_Resource_Request($requestArray);
@ -200,7 +200,7 @@ class Doctrine_Resource_Server extends Doctrine_Resource
if ($this->validate($errors)) { if ($this->validate($errors)) {
$result = $this->$funcName($request); $result = $this->$funcName($request);
return Doctrine_Parser::dump($result, 'xml'); return Doctrine_Parser::dump($result, $this->getFormat());
} }
} else { } else {
throw new Doctrine_Resource_Exception('Unknown Doctrine Resource Server function'); throw new Doctrine_Resource_Exception('Unknown Doctrine Resource Server function');
@ -222,6 +222,13 @@ class Doctrine_Resource_Server extends Doctrine_Resource
{ {
$error = array('error' => $e->getMessage()); $error = array('error' => $e->getMessage());
return Doctrine_Parser::dump($error); return Doctrine_Parser::dump($error, $this->getFormat());
}
public function getFormat()
{
$headers = getallheaders();
return isset($headers['Accept']) ? $headers['Accept']:Doctrine_Resource::FORMAT;
} }
} }

View File

@ -1,5 +1,27 @@
<?php <?php
require_once('playground.php'); require_once('playground.php');
if (isset($_REQUEST['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');
$name = 'Doctrine_Resource_Playground';
$config = array('models' => $tables);
$server = Doctrine_Resource_Server::getInstance($name, $config);
$server->run($_REQUEST);
} else {
$url = 'http://localhost/~jwage/doctrine_trunk/playground/index.php?server';
$config = array('format' => 'json');
// Instantiate a new client
$client = Doctrine_Resource_Client::getInstance($url, $config);
$query = new Doctrine_Resource_Query();
$users = $query->from('User u, u.Phonenumber p, u.Email e, u.Address a')->execute();
print_r($users->toArray(true));
}