Source for file Builder.php

Documentation is available at Builder.php

  1. <?php
  2. /*
  3.  *  $Id: Builder.php 2051 2007-07-23 20:28:46Z zYne $
  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.  * Doctrine_Import_Builder
  24.  * Import builder is responsible of building Doctrine ActiveRecord classes
  25.  * based on a database schema.
  26.  *
  27.  * @package     Doctrine
  28.  * @category    Object Relational Mapping
  29.  * @link        www.phpdoctrine.com
  30.  * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
  31.  * @since       1.0
  32.  * @version     $Revision: 2051 $
  33.  * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
  34.  * @author      Jukka Hassinen <Jukka.Hassinen@BrainAlliance.com>
  35.  * @author      Nicolas BĂ©rard-Nault <nicobn@php.net>
  36.  */
  37. {
  38.     /**
  39.      * @var string $path    the path where imported files are being generated
  40.      */
  41.     private $path = '';
  42.  
  43.     private $suffix = '.php';
  44.  
  45.     private static $tpl;
  46.  
  47.     public function __construct()
  48.     {
  49.         $this->loadTemplate();
  50.     }
  51.  
  52.     /**
  53.      * setTargetPath
  54.      *
  55.      * @param string path   the path where imported files are being generated
  56.      * @return 
  57.      */
  58.     public function setTargetPath($path)
  59.     {
  60.         if file_exists($path)) {
  61.             mkdir($path0777);
  62.         }
  63.  
  64.         $this->path = $path;
  65.     }
  66.     /**
  67.      * getTargetPath
  68.      *
  69.      * @return string       the path where imported files are being generated
  70.      */
  71.     public function getTargetPath()
  72.     {
  73.         return $this->path;
  74.     }
  75.  
  76.     /**
  77.      * This is a template that was previously in Builder/Record.tpl. Due to the fact
  78.      * that it was not bundled when compiling, it had to be moved here.
  79.      *
  80.      * @return void 
  81.      */
  82.     public function loadTemplate(
  83.     {
  84.         if (isset(self::$tpl)) {
  85.             return;
  86.         }
  87.  
  88.         self::$tpl =<<<END
  89. /**
  90.  * This class has been auto-generated by the Doctrine ORM Framework
  91.  */
  92. class %s extends Doctrine_Record
  93. {
  94.     public function setTableDefinition()
  95.     {
  96. %s
  97.     }
  98.     public function setUp()
  99.     {
  100. %s
  101.     }
  102. }
  103. END;
  104.  
  105.     }
  106.  
  107.     /*
  108.      * Build the table definition of a Doctrine_Record object
  109.      *
  110.      * @param  string $table
  111.      * @param  array  $tableColumns
  112.      */
  113.     public function buildColumnDefinition(array $tableColumns)
  114.     {
  115.         $columns   array();
  116.         $i 1;
  117.  
  118.         foreach ($tableColumns as $name => $column{
  119.             $columns[$i'        $this->hasColumn(\'' $name '\', \'' $column['type''\'';
  120.             if ($column['length']{
  121.                 $columns[$i.= ', ' $column['length'];
  122.             else {
  123.                 $columns[$i.= ', null';
  124.             }
  125.  
  126.             $a array();
  127.  
  128.             if (isset($column['default']&& $column['default']{
  129.                 $a['\'default\' => ' var_export($column['default']true);
  130.             }
  131.             if (isset($column['notnull']&& $column['notnull']{
  132.                 $a['\'notnull\' => true';
  133.             }
  134.             if (isset($column['primary']&& $column['primary']{
  135.                 $a['\'primary\' => true';
  136.             }
  137.             if (isset($column['autoinc']&& $column['autoinc']{
  138.                 $a['\'autoincrement\' => true';
  139.             }
  140.             if (isset($column['unique']&& $column['unique']{
  141.                 $a['\'unique\' => true';
  142.             }
  143.             if (isset($column['unsigned']&& $column['unsigned']{
  144.                 $a['\'unsigned\' => true';
  145.             }
  146.             if ($column['type'== 'enum' && isset($column['values']&& $column['values']{
  147.                 $a['\'values\' => array(' implode(','$column['values']')';
  148.             }
  149.  
  150.             if empty($a)) {
  151.                 $columns[$i.= ', ' 'array(';
  152.                 $length strlen($columns[$i]);
  153.                 $columns[$i.= implode(',' PHP_EOL str_repeat(' '$length)$a')';
  154.             }
  155.             $columns[$i.= ');';
  156.  
  157.             if ($i (count($tableColumns1)) {
  158.                 $columns[$i.= PHP_EOL;
  159.             }
  160.             $i++;
  161.         }
  162.         
  163.         return implode("\n"$columns);
  164.     }
  165.     public function buildRelationDefinition(array $relations)
  166.     {
  167.         $ret array();
  168.         $i 0;
  169.         foreach ($relations as $name => $relation{
  170.             $alias (isset($relation['alias']&& $relation['alias'!== $name' as ' $relation['alias''';
  171.  
  172.             if isset($relation['type'])) {
  173.                 $relation['type'Doctrine_Relation::ONE;
  174.             }
  175.  
  176.             if ($relation['type'=== Doctrine_Relation::ONE || 
  177.                 $relation['type'=== Doctrine_Relation::ONE_COMPOSITE{
  178.                 $ret[$i'        $this->hasOne(\'' $name $alias '\'';
  179.             else {
  180.                 $ret[$i'        $this->hasMany(\'' $name $alias '\'';
  181.             }
  182.             $a array();
  183.  
  184.             if (isset($relation['deferred']&& $relation['deferred']{
  185.                 $a['\'default\' => ' var_export($relation['deferred']true);
  186.             }
  187.             if (isset($relation['local']&& $relation['local']{
  188.                 $a['\'local\' => ' var_export($relation['local']true);
  189.             }
  190.             if (isset($relation['foreign']&& $relation['foreign']{
  191.                 $a['\'foreign\' => ' var_export($relation['foreign']true);
  192.             }
  193.             if (isset($relation['onDelete']&& $relation['onDelete']{
  194.                 $a['\'onDelete\' => ' var_export($relation['onDelete']true);
  195.             }
  196.             if (isset($relation['onUpdate']&& $relation['onUpdate']{
  197.                 $a['\'onUpdate\' => ' var_export($relation['onUpdate']true);
  198.             }
  199.             if empty($a)) {
  200.                 $ret[$i.= ', ' 'array(';
  201.                 $length strlen($ret[$i]);
  202.                 $ret[$i.= implode(',' PHP_EOL str_repeat(' '$length)$a')';
  203.             }
  204.             $ret[$i.= ');';
  205.             $i++;
  206.         }
  207.         return implode("\n"$ret);
  208.     }
  209.     
  210.  
  211.     public function buildDefinition(array $optionsarray $columnsarray $relations array())
  212.     {
  213.         if isset($options['className'])) {
  214.             throw new Doctrine_Import_Builder_Exception('Missing class name.');
  215.         }
  216.  
  217.         //$opt     = array(0 => str_repeat(' ', 8) . '$this->setTableName(\''. $table .'\');');
  218.  
  219.         $content sprintf(self::$tpl$options['className'],
  220.                           $this->buildColumnDefinition($columns),
  221.                           $this->buildRelationDefinition($relations));
  222.                           
  223.         return $content;
  224.     }
  225.  
  226.     public function buildRecord($options$columns$relations)
  227.     {
  228.         if isset($options['className'])) {
  229.             throw new Doctrine_Import_Builder_Exception('Missing class name.');
  230.         }
  231.  
  232.         if isset($options['fileName'])) {
  233.             if (empty($this->path)) {
  234.                 $errMsg 'No build target directory set.';
  235.                 throw new Doctrine_Import_Builder_Exception($errMsg);
  236.             }
  237.             
  238.  
  239.             if (is_writable($this->path=== false{
  240.                 $errMsg 'Build target directory ' $this->path ' is not writable.';
  241.                 throw new Doctrine_Import_Builder_Exception($errMsg);
  242.             }
  243.  
  244.             $options['fileName']  $this->path DIRECTORY_SEPARATOR $options['className'$this->suffix;
  245.         }
  246.  
  247.         $content $this->buildDefinition($options$columns$relations);
  248.  
  249.         $bytes file_put_contents($options['fileName']'<?php' PHP_EOL $content);
  250.  
  251.         if ($bytes === false{
  252.             throw new Doctrine_Import_Builder_Exception("Couldn't write file " $options['fileName']);
  253.         }
  254.     }
  255. }