Exemplo n.º 1
0
 /**
  * backend action for save/load from schema.yml
  */
 public function executeBackend($request)
 {
     // get ?action argument (save, load or import)
     $get = $request->getGetParameters();
     $action = isset($get['action']) ? $get['action'] : FALSE;
     $this->logMessage("Backend called with {$action}", 'info');
     // set response format
     $response = $this->getResponse();
     switch ($action) {
         case "save":
             try {
                 $xml = file_get_contents("php://input");
                 // attempt to save to schema.yml
                 sfSqlDesignerLib::saveToSchema($xml, $this->_ymlPath);
                 // attempt to save to sfSqlDesignerPlugin.xml
                 if (!file_put_contents($this->_xmlPath, $xml)) {
                     throw new Exception('Could not save to sfSqlDesignerPlugin.xml');
                 }
                 // all good! :)
                 $response->setStatusCode(201);
                 return $this->renderText("HTTP/1.0 201 Created");
             } catch (Exception $e) {
                 $response->setStatusCode(500);
                 $this->logMessage('Error when saving: ' . $e->getMessage());
                 return $this->renderText("HTTP/1.0 500 Internal Server Error: " . $e->getMessage());
             }
             break;
         case "load":
             try {
                 // first try to load the sfSqlDesignerPlugin.xml file we previously saved, there
                 // is no need to do the conversion work again
                 if (file_exists($this->_xmlPath)) {
                     $xml = file_get_contents($this->_xmlPath);
                 } else {
                     if (file_exists($this->_ymlPath)) {
                         //here the code to convert schema.yml to xml
                         //$xml=....
                         $response->setStatusCode(501);
                         return $this->renderText("HTTP/1.0 501 Not Implemented");
                     } else {
                         throw new Exception('No schema file found');
                     }
                 }
                 $response->setContentType('text/xml');
                 return $this->renderText($xml);
             } catch (Exception $e) {
                 $response->setStatusCode(500);
                 $this->logMessage('Error when loading: ' . $e->getMessage());
                 return $this->renderText("HTTP/1.0 500 Internal Server Error: " . $e->getMessage());
             }
             break;
         case "import":
             // here the code to import from the DB
             $response->setStatusCode(501);
             return $this->renderText("HTTP/1.0 501 Not Implemented");
             break;
         default:
             $response->setStatusCode(501);
             return $this->renderText("HTTP/1.0 501 Not Implemented");
             break;
     }
 }
catch (Exception $e)
{
	$t->fail('failed to convert xml to yml, error was: '.$e->getMessage());
}*/
// this test the converter from yml to xml and regenerate the yml to match
// the original. this way, no need to do a comparison on the xml (which can
// slightly differ)
try {
    $xml = sfSqlDesignerLib::loadFromSchema(dirname(__FILE__) . '/books.yml');
    if (!$xml) {
        throw new Exception('empty xml in return');
    }
} catch (Exception $e) {
    $t->fail('failed to convert xml to yml, error was: ' . $e->getMessage());
}
$xmlHeader = '/^<\\?xml version="1.0" encoding="utf-8" \\?>\\n<sql>\\n<datatypes db="symfony-doctrine">/';
$t->like($xml, $xmlHeader, 'xml as a proper header');
$xmlFooter = '/<\\/table>\\n<\\/sql>/';
$t->like($xml, $xmlFooter, 'xml as a proper footer');
try {
    sfSqlDesignerLib::saveToSchema($xml, '/tmp/test.yml');
    if (!file_exists('/tmp/test.yml')) {
        throw new Exception('yml file as not been saved to disk');
    }
} catch (Exception $e) {
    $t->fail('failed to convert xml to yml, error was: ' . $e->getMessage());
}
$result = file_get_contents('/tmp/test.yml');
unlink('/tmp/test.yml');
$books = file_get_contents(dirname(__FILE__) . '/books.yml');
$t->cmp_ok($result, '===', $books, 'generated xml was as expected and yml generated as original');