Server.php 4.23 KB
Newer Older
1
<?php
zYne's avatar
zYne committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 *  $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
 *
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
26
 * @author      Jonathan H. Wage <jwage@mac.com>
zYne's avatar
zYne committed
27 28 29 30 31 32 33
 * @package     Doctrine
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @version     $Revision$
 * @category    Object Relational Mapping
 * @link        www.phpdoctrine.com
 * @since       1.0
 */
34
class Doctrine_Resource_Server extends Doctrine_Resource
35
{
36
    static public function getInstance($config = null)
37
    {
38 39 40 41 42 43 44
        static $instance;
        
        if (!$instance) {
            $instance = new Doctrine_Resource_Server($config);
        }
        
        return $instance;
45 46
    }
    
47 48
    public function executeSave($request)
    {
49 50
        $model = $request->get('model');
        $data = $request->get('data');
51
        
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
        $table = Doctrine_Manager::getInstance()->getTable($model);
        $identifier = $table->getIdentifier();
        
        if (!is_array($identifier)) {
            $identifier = array($identifier);
        }
        
        $existing = true;
        $pks = array();
        foreach ($identifier as $name) {
            if (isset($data[$name])) {
                $pks[$name] = $data[$name];
            } else {
                $existing = false;
            }
        }
        
        if ($existing) {
            $record = $table->find($pks);
        } else {
            $record = new $model();
        }
        
75 76 77 78 79 80 81 82
        $record->fromArray($data);
        $record->save();
        
        return $record->toArray(true, true);
    }
    
    public function executeQuery($request)
    {
83 84
        $dql = $request->get('dql');
        $params = $request->get('params') ? $request->get('params'):array();
85 86 87 88 89 90
        
        $conn = Doctrine_Manager::connection();
        
        return $conn->query($dql, $params)->toArray(true, true);
    }
    
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
    public function executeLoad($request)
    {
        $path = '/tmp/' . rand() . '.' . $request->get('format');
        
        $models = $this->getConfig('models') ? $this->getConfig('models'):array();
        
        $export = new Doctrine_Export_Schema();
        $export->exportSchema($path, $request->get('format'), null, $models);
        
        $schema = Doctrine_Parser::load($path, $request->get('format'));
        
        unlink($path);
        
        return $schema;
    }
    
107
    public function execute(array $r)
108
    {
109 110 111 112 113 114 115
        if (!isset($r['data'])) {
            throw new Doctrine_Resource_Exception('You must specify a data xml string in your request');
        }                        
        
        $type = $r['type'];
        $format = isset($r['format']) ? $r['format']:'xml';
        $data = Doctrine_Parser::load($r['data'], $format);
116
        
117
        $funcName = 'execute' . Doctrine::classify($type);
118
        
119
        $requestObj = new Doctrine_Resource_Request($data);
120
        
121
        if (method_exists($this, $funcName)) {
122
            $result = $this->$funcName($requestObj);
123 124 125 126 127 128 129 130 131
        } else {
            throw new Doctrine_Resource_Exception('Unknown Doctrine Resource Server function');
        }
        
        if ($result) {
            return Doctrine_Parser::dump($result, $format);
        } else {
            return false;
        }
132 133
    }
    
134
    public function run($request)
135 136 137
    {
        echo $this->execute($request);
    }
138
}