2007-09-21 06:48:13 +04:00
|
|
|
<?php
|
2007-09-23 02:02:58 +04:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This software consists of voluntary contributions made by many individuals
|
|
|
|
* and is licensed under the LGPL. For more information, see
|
|
|
|
* <http://www.phpdoctrine.com>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Doctrine_Resource_Server
|
|
|
|
*
|
|
|
|
* @package Doctrine
|
2007-10-04 01:43:22 +04:00
|
|
|
* @subpackage Resource
|
|
|
|
* @author Jonathan H. Wage <jwage@mac.com>
|
2007-09-23 02:02:58 +04:00
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
|
* @version $Revision$
|
|
|
|
* @link www.phpdoctrine.com
|
|
|
|
* @since 1.0
|
|
|
|
*/
|
2007-09-24 22:22:52 +04:00
|
|
|
class Doctrine_Resource_Server extends Doctrine_Resource
|
2007-09-25 18:09:05 +04:00
|
|
|
{
|
|
|
|
public function __construct($name = null, $config = null)
|
|
|
|
{
|
|
|
|
if ($name) {
|
|
|
|
$config['name'] = $name;
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::__construct($config);
|
|
|
|
}
|
|
|
|
|
|
|
|
static public function getInstance($name, $config = null)
|
2007-09-21 22:01:08 +04:00
|
|
|
{
|
2007-09-24 08:58:57 +04:00
|
|
|
static $instance;
|
|
|
|
|
2007-10-21 10:23:59 +04:00
|
|
|
if ( ! $instance) {
|
2007-09-25 18:09:05 +04:00
|
|
|
$instance = new Doctrine_Resource_Server($name, $config);
|
2007-09-24 08:58:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return $instance;
|
2007-09-21 22:01:08 +04:00
|
|
|
}
|
2007-09-27 01:12:52 +04:00
|
|
|
|
|
|
|
public function validate($errors)
|
|
|
|
{
|
2007-10-21 10:23:59 +04:00
|
|
|
if ( ! empty($errors)) {
|
2007-09-27 01:12:52 +04:00
|
|
|
throw new Doctrine_Resource_Exception(count($errors) . ' error(s) occurred: ' . implode('. ', $errors));
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function validateOpenRecord($request)
|
|
|
|
{
|
|
|
|
$errors = array();
|
|
|
|
|
2007-10-21 10:23:59 +04:00
|
|
|
if ( ! $request->has('model') || !$request->get('model')) {
|
2007-09-27 01:12:52 +04:00
|
|
|
$errors[] = 'You must specify the model/class name you are deleting';
|
|
|
|
}
|
|
|
|
|
2007-10-21 10:23:59 +04:00
|
|
|
if ( ! $request->has('identifier') || !is_array($request->get('identifier'))) {
|
2007-09-27 01:12:52 +04:00
|
|
|
$errors[] = 'You must specify an array containing the identifiers for the model you wish to delete';
|
|
|
|
}
|
2007-09-25 18:09:05 +04:00
|
|
|
|
2007-09-27 01:12:52 +04:00
|
|
|
return $errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function validateSave($request)
|
|
|
|
{
|
|
|
|
$errors = $this->validateOpenRecord($request);
|
|
|
|
|
2007-10-21 10:23:59 +04:00
|
|
|
if ( ! $request->has('data') || !$request->get('data')) {
|
2007-09-27 01:12:52 +04:00
|
|
|
$errors[] = 'You must specify an containing the changed data to save to the model';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $errors;
|
|
|
|
}
|
|
|
|
|
2007-09-22 05:32:48 +04:00
|
|
|
public function executeSave($request)
|
|
|
|
{
|
2007-09-24 08:58:57 +04:00
|
|
|
$model = $request->get('model');
|
|
|
|
$data = $request->get('data');
|
2007-09-25 01:46:05 +04:00
|
|
|
$identifier = $request->get('identifier');
|
2007-09-22 05:32:48 +04:00
|
|
|
|
2007-09-24 22:22:52 +04:00
|
|
|
$table = Doctrine_Manager::getInstance()->getTable($model);
|
|
|
|
|
|
|
|
$existing = true;
|
2007-09-25 01:46:05 +04:00
|
|
|
foreach ($identifier as $key => $value) {
|
2007-10-21 10:23:59 +04:00
|
|
|
if ( ! $value) {
|
2007-09-24 22:22:52 +04:00
|
|
|
$existing = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($existing) {
|
2007-09-25 01:46:05 +04:00
|
|
|
$record = $table->find($identifier);
|
2007-09-24 22:22:52 +04:00
|
|
|
} else {
|
|
|
|
$record = new $model();
|
|
|
|
}
|
|
|
|
|
2007-09-22 05:32:48 +04:00
|
|
|
$record->fromArray($data);
|
|
|
|
$record->save();
|
|
|
|
|
|
|
|
return $record->toArray(true, true);
|
|
|
|
}
|
|
|
|
|
2007-09-27 01:12:52 +04:00
|
|
|
public function validateDelete($request)
|
|
|
|
{
|
|
|
|
return $this->validateOpenRecord($request);
|
|
|
|
}
|
|
|
|
|
2007-09-25 01:46:05 +04:00
|
|
|
public function executeDelete($request)
|
|
|
|
{
|
|
|
|
$model = $request->get('model');
|
|
|
|
$identifier = $request->get('identifier');
|
|
|
|
|
|
|
|
$table = Doctrine_Manager::getInstance()->getTable($model);
|
|
|
|
|
|
|
|
$record = $table->find($identifier);
|
|
|
|
|
2007-09-27 01:12:52 +04:00
|
|
|
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();
|
|
|
|
|
2007-10-21 10:23:59 +04:00
|
|
|
if ( ! $request->has('dql') || !$request->get('dql')) {
|
2007-09-27 01:12:52 +04:00
|
|
|
$errors[] = 'You must specify a dql string in order to execute a query';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $errors;
|
2007-09-25 01:46:05 +04:00
|
|
|
}
|
|
|
|
|
2007-09-22 05:32:48 +04:00
|
|
|
public function executeQuery($request)
|
|
|
|
{
|
2007-09-24 08:58:57 +04:00
|
|
|
$dql = $request->get('dql');
|
|
|
|
$params = $request->get('params') ? $request->get('params'):array();
|
2007-09-22 05:32:48 +04:00
|
|
|
|
|
|
|
$conn = Doctrine_Manager::connection();
|
|
|
|
|
|
|
|
return $conn->query($dql, $params)->toArray(true, true);
|
|
|
|
}
|
|
|
|
|
2007-09-27 01:12:52 +04:00
|
|
|
public function validateLoad($request)
|
|
|
|
{
|
|
|
|
$errors = array();
|
|
|
|
|
|
|
|
return $errors;
|
|
|
|
}
|
|
|
|
|
2007-09-24 08:58:57 +04:00
|
|
|
public function executeLoad($request)
|
|
|
|
{
|
2007-09-27 01:12:52 +04:00
|
|
|
$path = '/tmp/' . rand();
|
2007-09-24 08:58:57 +04:00
|
|
|
|
|
|
|
$models = $this->getConfig('models') ? $this->getConfig('models'):array();
|
|
|
|
|
|
|
|
$export = new Doctrine_Export_Schema();
|
2007-09-28 02:02:13 +04:00
|
|
|
$export->exportSchema($path, $this->getFormat(), null, $models);
|
2007-09-24 08:58:57 +04:00
|
|
|
|
2007-09-28 02:02:13 +04:00
|
|
|
$schema = Doctrine_Parser::load($path, $this->getFormat());
|
2007-09-24 08:58:57 +04:00
|
|
|
|
|
|
|
unlink($path);
|
|
|
|
|
|
|
|
return $schema;
|
|
|
|
}
|
|
|
|
|
2007-09-24 22:22:52 +04:00
|
|
|
public function execute(array $r)
|
2007-09-21 06:48:13 +04:00
|
|
|
{
|
2007-10-21 10:23:59 +04:00
|
|
|
if ( ! isset($r['request'])) {
|
2007-09-28 02:02:13 +04:00
|
|
|
throw new Doctrine_Resource_Exception('You must specify a request '.$this->getFormat().' string in your request');
|
2007-09-27 01:12:52 +04:00
|
|
|
}
|
2007-09-24 22:22:52 +04:00
|
|
|
|
2007-09-28 02:02:13 +04:00
|
|
|
$requestArray = Doctrine_Parser::load($r['request'], $this->getFormat());
|
2007-09-21 06:48:13 +04:00
|
|
|
|
2007-09-27 01:12:52 +04:00
|
|
|
$request = new Doctrine_Resource_Request($requestArray);
|
2007-09-21 22:01:08 +04:00
|
|
|
|
2007-09-27 01:12:52 +04:00
|
|
|
$funcName = 'execute' . Doctrine::classify($request->get('action'));
|
2007-09-21 06:48:13 +04:00
|
|
|
|
2007-09-24 08:58:57 +04:00
|
|
|
if (method_exists($this, $funcName)) {
|
2007-09-27 01:12:52 +04:00
|
|
|
$validateFuncName = 'validate' . Doctrine::classify($request->get('action'));
|
|
|
|
|
|
|
|
$errors = $this->$validateFuncName($request);
|
|
|
|
|
|
|
|
if ($this->validate($errors)) {
|
|
|
|
$result = $this->$funcName($request);
|
|
|
|
|
2007-09-28 02:02:13 +04:00
|
|
|
return Doctrine_Parser::dump($result, $this->getFormat());
|
2007-09-27 01:12:52 +04:00
|
|
|
}
|
2007-09-24 08:58:57 +04:00
|
|
|
} else {
|
|
|
|
throw new Doctrine_Resource_Exception('Unknown Doctrine Resource Server function');
|
|
|
|
}
|
2007-09-21 06:48:13 +04:00
|
|
|
}
|
|
|
|
|
2007-09-21 22:01:08 +04:00
|
|
|
public function run($request)
|
2007-09-21 06:48:13 +04:00
|
|
|
{
|
2007-09-26 01:39:38 +04:00
|
|
|
try {
|
|
|
|
$result = $this->execute($request);
|
|
|
|
|
|
|
|
echo $result;
|
|
|
|
} catch(Exception $e) {
|
|
|
|
echo $this->exception($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function exception($e)
|
|
|
|
{
|
|
|
|
$error = array('error' => $e->getMessage());
|
|
|
|
|
2007-09-28 02:02:13 +04:00
|
|
|
return Doctrine_Parser::dump($error, $this->getFormat());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFormat()
|
|
|
|
{
|
|
|
|
$headers = getallheaders();
|
|
|
|
|
|
|
|
return isset($headers['Accept']) ? $headers['Accept']:Doctrine_Resource::FORMAT;
|
2007-09-21 06:48:13 +04:00
|
|
|
}
|
2007-09-24 08:58:57 +04:00
|
|
|
}
|