Commit 39c3f2e0 authored by Jonathan.Wage's avatar Jonathan.Wage

Initial entry of Parser for yml/xml and made import schema code use the parser classes.

parent 15e2484b
......@@ -38,7 +38,15 @@
* @author Jonathan H. Wage <jonwage@gmail.com>
*/
abstract class Doctrine_Import_Schema
{
{
/**
* Parse the schema and return it in an array
*
* @param string $schema
* @access public
*/
abstract function parseSchema($schema);
/**
* parse
*
......@@ -48,17 +56,14 @@ abstract class Doctrine_Import_Schema
* @return void
* @author Jonathan H. Wage
*/
abstract function parse($schema);
/**
* Parse the schema and return it in an array
*
* @param string $schema
* @access public
*/
abstract function parseSchema($schema);
public function parse($schema)
{
$class = get_class($this);
$type = strtolower(str_replace('Doctrine_Import_Schema_', '', $class));
return Doctrine_Parser::load($schema, $type);
}
/**
* importSchema
*
......
......@@ -38,26 +38,7 @@
* @author Jonathan H. Wage <jonwage@gmail.com>
*/
class Doctrine_Import_Schema_Xml extends Doctrine_Import_Schema
{
/**
* parse
*
* @param string $schema
* @return void
*/
public function parse($schema)
{
if ( ! is_readable($schema)) {
throw new Doctrine_Import_Exception('Could not read schema file '. $schema);
}
if ( ! ($xmlString = file_get_contents($schema))) {
throw new Doctrine_Import_Exception('Schema file '. $schema . ' is empty');
}
return simplexml_load_string($xmlString);
}
{
/**
* parseSchema
*
......@@ -73,6 +54,8 @@ class Doctrine_Import_Schema_Xml extends Doctrine_Import_Schema
// Go through all tables...
foreach ($xmlObj->table as $table) {
$columns = array();
// Go through all columns...
foreach ($table->declaration->field as $field) {
$colDesc = array(
......@@ -101,4 +84,4 @@ class Doctrine_Import_Schema_Xml extends Doctrine_Import_Schema
return $tables;
}
}
}
\ No newline at end of file
......@@ -38,22 +38,7 @@
* @author Jonathan H. Wage <jonwage@gmail.com>
*/
class Doctrine_Import_Schema_Yml extends Doctrine_Import_Schema
{
/**
* parse
*
* @param string $schema
* @return void
*/
public function parse($schema)
{
if ( ! is_readable($schema)) {
throw new Doctrine_Import_Exception('Could not read schema file '. $schema);
}
return array();
}
{
/**
* parseSchema
*
......@@ -64,37 +49,38 @@ class Doctrine_Import_Schema_Yml extends Doctrine_Import_Schema
* @return array
*/
public function parseSchema($schema)
{
{
$array = $this->parse($schema);
$tables = array();
// Not working yet
/*
// Go through all tables...
foreach ($array['table'] as $table) {
// Go through all columns...
foreach ($table['declaration']['field'] as $field) {
foreach ($array['tables'] as $table) {
$columns = array();
foreach ($table['declaration'] as $field) {
$colDesc = array(
'name' => (string) $field['name'],
'type' => (string) $field['type'],
'ptype' => (string) $field['type'],
'length' => (int) $field['length'],
'fixed' => (int) $field['fixed'],
'unsigned' => (bool) $field['unsigned'],
'primary' => (bool) (isset($field['primary']) && $field['primary']),
'default' => (string) $field['default'],
'notnull' => (bool) (isset($field['notnull']) && $field['notnull']),
'autoinc' => (bool) (isset($field['autoincrement']) && $field['autoincrement']),
'name' => isset($field['name']) ? (string) $field['name']:null,
'type' => isset($field['type']) ? (string) $field['type']:null,
'ptype' => isset($field['type']) ? (string) $field['type']:null,
'length' => isset($field['length']) ? (int) $field['length']:null,
'fixed' => isset($field['fixed']) ? (int) $field['fixed']:null,
'unsigned' => isset($field['unsigned']) ? (bool) $field['unsigned']:null,
'primary' => isset($field['primary']) ? (bool) (isset($field['primary']) && $field['primary']):null,
'default' => isset($field['default']) ? (string) $field['default']:null,
'notnull' => isset($field['notnull']) ? (bool) (isset($field['notnull']) && $field['notnull']):null,
'autoinc' => isset($field['autoincrement']) ? (bool) (isset($field['autoincrement']) && $field['autoincrement']):null,
);
$columns[(string) $field['name']] = $colDesc;
}
$tables[(string) $table['name']] = $columns;
$class = $table['class'] ? (string) $table['class']:(string) $table['name'];
$tables[(string) $table['name']]['name'] = (string) $table['name'];
$tables[(string) $table['name']]['class'] = (string) $class;
$tables[(string) $table['name']]['columns'] = $columns;
}
*/
return $tables;
}
}
}
\ No newline at end of file
<?php
/*
* $Id: Parser.php 1080 2007-02-10 18:17:08Z jwage $
*
* 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_Parser
*
* @author Jonathan H. Wage <jwage@mac.com>
* @package Doctrine
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @category Object Relational Mapping
* @link www.phpdoctrine.com
* @since 1.0
* @version $Revision: 1080 $
*/
abstract class Doctrine_Parser
{
abstract public function loadData($array);
abstract public function dumpData($array, $path = null);
static public function getParser($type)
{
$class = 'Doctrine_Parser_'.ucfirst($type);
return new $class;
}
static public function load($path, $type = 'xml')
{
$parser = self::getParser($type);
return $parser->loadData($path);
}
static public function dump($array, $path = null, $type = 'xml')
{
$parser = self::getParser($type);
return $parser->dumpData($array, $path);
}
}
\ No newline at end of file
<?php
/*
* $Id: Exception.php 1080 2007-02-10 18:17:08Z romanb $
*
* 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::autoload('Doctrine_Exception');
/**
* Doctrine_Parser_Exception
*
* @package Doctrine
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @category Object Relational Mapping
* @link www.phpdoctrine.com
* @since 1.0
* @version $Revision: 1080 $
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
*/
class Doctrine_Parser_Exception extends Doctrine_Exception
{ }
\ No newline at end of file
<?php
/*
* $Id: Xml.php 1080 2007-02-10 18:17:08Z jwage $
*
* 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_Parser_Xml
*
* @author Jonathan H. Wage <jwage@mac.com>
* @package Doctrine
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @category Object Relational Mapping
* @link www.phpdoctrine.com
* @since 1.0
* @version $Revision: 1080 $
*/
class Doctrine_Parser_Xml extends Doctrine_Parser
{
public function arrayToXml($array)
{
$this->text = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>";
$this->text .= $this->arrayTransform($array);
return $this->text;
}
public function arrayTransform($array)
{
foreach ($array as $key => $value) {
if (!is_array($value)) {
$this->text .= "<$key>$value</$key>";
} else {
$this->text.="<$key>";
$this->arrayTransform($value);
$this->text.="</$key>";
}
}
}
public function dumpData($array, $path = null)
{
$xml = $this->arrayToXml($array);
if ($path) {
return file_put_contents($path, $xml);
} else {
return $xml;
}
}
public function loadData($path)
{
if ( !file_exists($path) OR !is_writeable($path) ) {
throw new Doctrine_Parser_Exception('Xml file '. $path .' could not be read');
}
if ( ! ($xmlString = file_get_contents($path))) {
throw new Doctrine_Parser_Exception('Xml file '. $path . ' is empty');
}
if (!file_exists($path) OR !is_readable($path) OR !($xmlString = file_get_contents($path))) {
$xmlString = $path;
}
return simplexml_load_string($xmlString);
}
}
\ No newline at end of file
<?php
require_once('spyc.php');
/*
* $Id: Yml.php 1080 2007-02-10 18:17:08Z jwage $
*
* 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_Parser_Yml
*
* @author Jonathan H. Wage <jwage@mac.com>
* @package Doctrine
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @category Object Relational Mapping
* @link www.phpdoctrine.com
* @since 1.0
* @version $Revision: 1080 $
*/
class Doctrine_Parser_Yml extends Doctrine_Parser
{
public function dumpData($array, $path = null)
{
$spyc = new Spyc();
$yml = $spyc->dump($array, false, false);
if ($path) {
return file_put_contents($path, $yml);
} else {
return $yml;
}
}
public function loadData($path)
{
$spyc = new Spyc();
$array = $spyc->load($path);
return $array;
}
}
\ No newline at end of file
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment