sfDoctrineSchemaPropelLoader.class.php 2.26 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
<?php
/*
 * This file is part of the sfDoctrine package.
 * (c) 2006-2007 Olivier Verdier <Olivier.Verdier@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * @package    symfony.plugins
 * @subpackage sfDoctrine
 * @author     Olivier Verdier <Olivier.Verdier@gmail.com>
 * @version    SVN: $Id: sfDoctrineSchemaPropelLoader.class.php 3455 2007-02-14 16:17:48Z chtito $
 */

class sfDoctrineSchemaPropelLoader extends sfDoctrineDatabaseSchema
{
  // get the attributes parsed by the sfPropelDatabaseSchema class
  protected function getAttribute($tag, $attribute)
  {
    return isset($tag['_attributes'][$attribute]) ? $tag['_attributes'][$attribute] : null;
  }
  
  public function load($file, $package = null)
  {
    // we figure out what kind of file we are given
    $type = array_pop(explode('.', $file));
    $type2method = array('yml'=>'loadYAML', 'xml'=>'loadXML');
    if (isset($type2method[$type]))
      $method = $type2method[$type];
    else
      throw new sfDoctrineSchemaException(sprintf('Unkwnown method for extension "%s"', $type));
    
    $propelDatabaseSchema = new sfPropelDatabaseSchema();
    $propelDatabaseSchema->$method($file);
    $data = $propelDatabaseSchema->asArray();
          
    foreach ($propelDatabaseSchema->getTables() as $tb_name => $tableDesc)
    {
      // special table class
      // propel has only such classes (no inheritance support)
      $table = new sfDoctrineTableSchema($tb_name, $package);
      $this->tables[$tb_name] = $table;

      if (!($className = $this->getAttribute($tableDesc, 'phpName')))
        $className = sfInflector::camelize($tb_name); // wild guess

      $class = new sfDoctrineClassSchema($className);
      $table->addClass($class);

      // columns
      foreach ($propelDatabaseSchema->getChildren($tableDesc) as $col_name => $columnDescription)
      {
        if (($col_name == 'id')) // id is automatically generated in doctrine
          continue;
      
        $docCol = new sfDoctrineColumnSchema($col_name, $columnDescription, true);
        $class->addColumn($docCol);
      }

      $this->classes[$class->getPhpName()] = $class;
    }
  }

  public function process()
  {
    $this->fixRelationships();
  }
}