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 @@ ...@@ -38,7 +38,15 @@
* @author Jonathan H. Wage <jonwage@gmail.com> * @author Jonathan H. Wage <jonwage@gmail.com>
*/ */
abstract class Doctrine_Import_Schema abstract class Doctrine_Import_Schema
{ {
/**
* Parse the schema and return it in an array
*
* @param string $schema
* @access public
*/
abstract function parseSchema($schema);
/** /**
* parse * parse
* *
...@@ -48,17 +56,14 @@ abstract class Doctrine_Import_Schema ...@@ -48,17 +56,14 @@ abstract class Doctrine_Import_Schema
* @return void * @return void
* @author Jonathan H. Wage * @author Jonathan H. Wage
*/ */
public function parse($schema)
abstract function parse($schema); {
$class = get_class($this);
/** $type = strtolower(str_replace('Doctrine_Import_Schema_', '', $class));
* Parse the schema and return it in an array
* return Doctrine_Parser::load($schema, $type);
* @param string $schema }
* @access public
*/
abstract function parseSchema($schema);
/** /**
* importSchema * importSchema
* *
......
...@@ -38,26 +38,7 @@ ...@@ -38,26 +38,7 @@
* @author Jonathan H. Wage <jonwage@gmail.com> * @author Jonathan H. Wage <jonwage@gmail.com>
*/ */
class Doctrine_Import_Schema_Xml extends Doctrine_Import_Schema 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 * parseSchema
* *
...@@ -73,6 +54,8 @@ class Doctrine_Import_Schema_Xml extends Doctrine_Import_Schema ...@@ -73,6 +54,8 @@ class Doctrine_Import_Schema_Xml extends Doctrine_Import_Schema
// Go through all tables... // Go through all tables...
foreach ($xmlObj->table as $table) { foreach ($xmlObj->table as $table) {
$columns = array();
// Go through all columns... // Go through all columns...
foreach ($table->declaration->field as $field) { foreach ($table->declaration->field as $field) {
$colDesc = array( $colDesc = array(
...@@ -101,4 +84,4 @@ class Doctrine_Import_Schema_Xml extends Doctrine_Import_Schema ...@@ -101,4 +84,4 @@ class Doctrine_Import_Schema_Xml extends Doctrine_Import_Schema
return $tables; return $tables;
} }
} }
\ No newline at end of file
...@@ -38,22 +38,7 @@ ...@@ -38,22 +38,7 @@
* @author Jonathan H. Wage <jonwage@gmail.com> * @author Jonathan H. Wage <jonwage@gmail.com>
*/ */
class Doctrine_Import_Schema_Yml extends Doctrine_Import_Schema 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 * parseSchema
* *
...@@ -64,37 +49,38 @@ class Doctrine_Import_Schema_Yml extends Doctrine_Import_Schema ...@@ -64,37 +49,38 @@ class Doctrine_Import_Schema_Yml extends Doctrine_Import_Schema
* @return array * @return array
*/ */
public function parseSchema($schema) public function parseSchema($schema)
{ {
$array = $this->parse($schema); $array = $this->parse($schema);
$tables = array();
// Not working yet
/*
// Go through all tables... // Go through all tables...
foreach ($array['table'] as $table) { foreach ($array['tables'] as $table) {
// Go through all columns... $columns = array();
foreach ($table['declaration']['field'] as $field) {
foreach ($table['declaration'] as $field) {
$colDesc = array( $colDesc = array(
'name' => (string) $field['name'], 'name' => isset($field['name']) ? (string) $field['name']:null,
'type' => (string) $field['type'], 'type' => isset($field['type']) ? (string) $field['type']:null,
'ptype' => (string) $field['type'], 'ptype' => isset($field['type']) ? (string) $field['type']:null,
'length' => (int) $field['length'], 'length' => isset($field['length']) ? (int) $field['length']:null,
'fixed' => (int) $field['fixed'], 'fixed' => isset($field['fixed']) ? (int) $field['fixed']:null,
'unsigned' => (bool) $field['unsigned'], 'unsigned' => isset($field['unsigned']) ? (bool) $field['unsigned']:null,
'primary' => (bool) (isset($field['primary']) && $field['primary']), 'primary' => isset($field['primary']) ? (bool) (isset($field['primary']) && $field['primary']):null,
'default' => (string) $field['default'], 'default' => isset($field['default']) ? (string) $field['default']:null,
'notnull' => (bool) (isset($field['notnull']) && $field['notnull']), 'notnull' => isset($field['notnull']) ? (bool) (isset($field['notnull']) && $field['notnull']):null,
'autoinc' => (bool) (isset($field['autoincrement']) && $field['autoincrement']), 'autoinc' => isset($field['autoincrement']) ? (bool) (isset($field['autoincrement']) && $field['autoincrement']):null,
); );
$columns[(string) $field['name']] = $colDesc; $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; 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