Source for file Xml.php

Documentation is available at Xml.php

  1. <?php
  2. /*
  3.  * $Id: Xml.php 1838 2007-06-26 00:58:21Z nicobn $
  4.  *
  5.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9.  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15.  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16.  *
  17.  * This software consists of voluntary contributions made by many individuals
  18.  * and is licensed under the LGPL. For more information, see
  19.  * <http://www.phpdoctrine.com>.
  20.  */
  21.  
  22. /**
  23.  * class Doctrine_Import_Xml
  24.  *
  25.  * Different methods to import a XML schema. The logic behind using two different
  26.  * methods is simple. Some people will like the idea of producing Doctrine_Record
  27.  * objects directly, which is totally fine. But in fast and growing application,
  28.  * table definitions tend to be a little bit more volatile. importArr() can be used
  29.  * to output a table definition in a PHP file. This file can then be stored
  30.  * independantly from the object itself.
  31.  *
  32.  * @package     Doctrine
  33.  * @category    Object Relational Mapping
  34.  * @link        www.phpdoctrine.com
  35.  * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
  36.  * @version     $Revision: 1838 $
  37.  * @author      Nicolas BĂ©rard-Nault <nicobn@gmail.com>
  38.  * @author      Jonathan H. Wage <jonwage@gmail.com>
  39.  */
  40. {
  41.     /**
  42.      * parse
  43.      *
  44.      * @param string $schema 
  45.      * @return void 
  46.      */
  47.     public function parse($schema)
  48.     {
  49.         if (!is_readable($schema)) {
  50.             throw new Doctrine_Import_Exception('Could not read schema file '$schema);
  51.         }
  52.         
  53.         if (!($xmlString file_get_contents($schema))) {
  54.             throw new Doctrine_Import_Exception('Schema file '$schema ' is empty');
  55.         }
  56.         
  57.         return simplexml_load_string($xmlString);
  58.     }
  59.     
  60.     /**
  61.      * parseSchema
  62.      *
  63.      * A method to parse a XML Schema and translate it into a property array.
  64.      * The function returns that property array.
  65.      *
  66.      * @param  string $schema   Path to the file containing the XML schema
  67.      * @return array 
  68.      */
  69.     public function parseSchema($schema)
  70.     {        
  71.         $xmlObj $this->parse($schema);
  72.         
  73.         // Go through all tables...
  74.         foreach ($xmlObj->table as $table{
  75.             // Go through all columns... 
  76.             foreach ($table->declaration->field as $field{
  77.                 $colDesc array(
  78.                     'name'      => (string) $field->name,
  79.                     'type'      => (string) $field->type,
  80.                     'ptype'     => (string) $field->type,
  81.                     'length'    => (int) $field->length,
  82.                     'fixed'     => (int) $field->fixed,
  83.                     'unsigned'  => (bool) $field->unsigned,
  84.                     'primary'   => (bool) (isset($field->primary&& $field->primary),
  85.                     'default'   => (string) $field->default,
  86.                     'notnull'   => (bool) (isset($field->notnull&& $field->notnull),
  87.                     'autoinc'   => (bool) (isset($field->autoincrement&& $field->autoincrement),
  88.                 );
  89.             
  90.                 $columns[(string) $field->name$colDesc;
  91.             }
  92.             
  93.             $class $table->class ? (string) $table->class:(string) $table->name;
  94.             
  95.             $tables[(string) $table->name]['name'= (string) $table->name;
  96.             $tables[(string) $table->name]['class'= (string) $class;
  97.             
  98.             $tables[(string) $table->name]['columns'$columns;
  99.         }
  100.         
  101.         return $tables;
  102.     }
  103. }