diff --git a/lib/Doctrine/Resource.php b/lib/Doctrine/Resource.php index f433dfd38..486296586 100644 --- a/lib/Doctrine/Resource.php +++ b/lib/Doctrine/Resource.php @@ -34,7 +34,7 @@ class Doctrine_Resource { protected $_config = null; - protected $_defaultFormat = 'xml'; + const FORMAT = 'json'; public function __construct($config) { @@ -48,10 +48,6 @@ class Doctrine_Resource $this->loadDoctrine = true; } } - - if (!$this->getConfig()->has('format') OR !$this->getConfig()->get('format')) { - $this->getConfig()->set('format', $this->_defaultFormat); - } } public function getConfig($key = null) diff --git a/lib/Doctrine/Resource/Client.php b/lib/Doctrine/Resource/Client.php index c2bd95a55..a7a0d643a 100644 --- a/lib/Doctrine/Resource/Client.php +++ b/lib/Doctrine/Resource/Client.php @@ -61,7 +61,7 @@ class Doctrine_Resource_Client extends Doctrine_Resource public function loadDoctrine() { - $path = '/tmp/' . md5(serialize($this->getConfig())); + $path = '/tmp/' . $this->getClientKey(); $classesPath = $path.'.classes.php'; if (file_exists($path)) { @@ -73,13 +73,13 @@ class Doctrine_Resource_Client extends Doctrine_Resource $schema = $request->execute(); 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) { $import = new Doctrine_Import_Schema(); - $schema = $import->buildSchema($path, 'xml'); + $schema = $import->buildSchema($path, Doctrine_Resource::FORMAT); if (!file_exists($classesPath)) { $build = "getConfig())); + } + public function getTable($table) { static $instance; diff --git a/lib/Doctrine/Resource/Request.php b/lib/Doctrine/Resource/Request.php index 0f2148a14..f0f4ed826 100644 --- a/lib/Doctrine/Resource/Request.php +++ b/lib/Doctrine/Resource/Request.php @@ -44,13 +44,15 @@ class Doctrine_Resource_Request extends Doctrine_Resource_Params { $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(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); - + curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_POSTFIELDS, $request); $response = curl_exec($ch); @@ -63,7 +65,7 @@ class Doctrine_Resource_Request extends Doctrine_Resource_Params $array = array(); if ($response) { - $array = Doctrine_Parser::load($response, $this->getConfig()->get('format')); + $array = Doctrine_Parser::load($response, $this->getFormat()); } if (isset($array['error'])) { @@ -72,4 +74,9 @@ class Doctrine_Resource_Request extends Doctrine_Resource_Params return $array; } + + public function getFormat() + { + return ($this->getConfig()->has('format') && $this->getConfig()->get('format')) ? $this->getConfig()->get('format'):Doctrine_Resource::FORMAT; + } } \ No newline at end of file diff --git a/lib/Doctrine/Resource/Server.php b/lib/Doctrine/Resource/Server.php index 1b3e9ad4c..17d1c36a0 100644 --- a/lib/Doctrine/Resource/Server.php +++ b/lib/Doctrine/Resource/Server.php @@ -171,9 +171,9 @@ class Doctrine_Resource_Server extends Doctrine_Resource $models = $this->getConfig('models') ? $this->getConfig('models'):array(); $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); @@ -182,11 +182,11 @@ class Doctrine_Resource_Server extends Doctrine_Resource public function execute(array $r) { - if (!isset($r['xml'])) { - throw new Doctrine_Resource_Exception('You must specify an xml string in your request'); + if (!isset($r['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); @@ -200,7 +200,7 @@ class Doctrine_Resource_Server extends Doctrine_Resource if ($this->validate($errors)) { $result = $this->$funcName($request); - return Doctrine_Parser::dump($result, 'xml'); + return Doctrine_Parser::dump($result, $this->getFormat()); } } else { 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()); - 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; } } \ No newline at end of file diff --git a/playground/index.php b/playground/index.php index 764990286..84188199f 100644 --- a/playground/index.php +++ b/playground/index.php @@ -1,5 +1,27 @@ $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)); +} \ No newline at end of file