Development

/branches/1.1/lib/plugins/sfPropelPlugin/lib/propel/builder/SfObjectBuilder.php

You must first sign up to be able to contribute.

root/branches/1.1/lib/plugins/sfPropelPlugin/lib/propel/builder/SfObjectBuilder.php

Revision 14378, 9.3 kB (checked in by Kris.Wallsmith, 5 years ago)

[1.1, 1.2] updated propel behavior inclusion logic to use only core classes

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Rev Date
Line 
1 <?php
2
3 require_once 'propel/engine/builder/om/php5/PHP5ComplexObjectBuilder.php';
4
5 /*
6  * This file is part of the symfony package.
7  * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
8  *
9  * For the full copyright and license information, please view the LICENSE
10  * file that was distributed with this source code.
11  */
12
13 /**
14  * @package    symfony
15  * @subpackage propel
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
18  */
19 class SfObjectBuilder extends PHP5ComplexObjectBuilder
20 {
21   public function build()
22   {
23     $objectCode = parent::build();
24     if (!DataModelBuilder::getBuildProperty('builderAddComments'))
25     {
26       $objectCode = sfToolkit::stripComments($objectCode);
27     }
28     if (!DataModelBuilder::getBuildProperty('builderAddIncludes'))
29     {
30        //remove all inline includes: object classes include the peers
31       $objectCode = preg_replace("/include_once\s*.*Base.*Peer\.php.*\s*/", "", $objectCode);
32     }
33     return $objectCode;
34   }
35
36   protected function addIncludes(&$script)
37   {
38     if (!DataModelBuilder::getBuildProperty('builderAddIncludes'))
39     {
40       return;
41     }
42
43     parent::addIncludes($script);
44
45     // include the i18n classes if needed
46     if ($this->getTable()->getAttribute('isI18N'))
47     {
48       $relatedTable   = $this->getDatabase()->getTable($this->getTable()->getAttribute('i18nTable'));
49
50       $script .= '
51 require_once \''.$this->getFilePath($this->getStubObjectBuilder()->getPackage().'.'.$relatedTable->getPhpName().'Peer').'\';
52 require_once \''.$this->getFilePath($this->getStubObjectBuilder()->getPackage().'.'.$relatedTable->getPhpName()).'\';
53 ';
54     }
55   }
56
57   protected function addClassBody(&$script)
58   {
59     parent::addClassBody($script);
60
61     if ($this->getTable()->getAttribute('isI18N'))
62     {
63       if (count($this->getTable()->getPrimaryKey()) > 1)
64       {
65         throw new Exception('i18n support only works with a single primary key');
66       }
67
68       $this->addCultureAccessorMethod($script);
69       $this->addCultureMutatorMethod($script);
70
71       $this->addI18nMethods($script);
72     }
73
74     if (DataModelBuilder::getBuildProperty('builderAddBehaviors'))
75     {
76       $this->addCall($script);
77     }
78   }
79
80   protected function addCall(&$script)
81   {
82     $script .= "
83
84   public function __call(\$method, \$arguments)
85   {
86     if (!\$callable = sfMixer::getCallable('{$this->getClassname()}:'.\$method))
87     {
88       throw new sfException(sprintf('Call to undefined method {$this->getClassname()}::%s', \$method));
89     }
90
91     array_unshift(\$arguments, \$this);
92
93     return call_user_func_array(\$callable, \$arguments);
94   }
95
96 ";
97   }
98
99   protected function addAttributes(&$script)
100   {
101     parent::addAttributes($script);
102
103     if ($this->getTable()->getAttribute('isI18N'))
104     {
105       $script .= '
106   /**
107    * The value for the culture field.
108    * @var string
109    */
110   protected $culture;
111 ';
112     }
113   }
114
115   protected function addCultureAccessorMethod(&$script)
116   {
117     $script .= '
118   public function getCulture()
119   {
120     return $this->culture;
121   }
122 ';
123   }
124
125   protected function addCultureMutatorMethod(&$script)
126   {
127     $script .= '
128   public function setCulture($culture)
129   {
130     $this->culture = $culture;
131   }
132 ';
133   }
134
135   protected function addI18nMethods(&$script)
136   {
137     $table = $this->getTable();
138     $pks = $table->getPrimaryKey();
139     $pk = $pks[0]->getPhpName();
140
141     foreach ($table->getReferrers() as $fk)
142     {
143       $tblFK = $fk->getTable();
144       if ($tblFK->getName() == $table->getAttribute('i18nTable'))
145       {
146         $className = $tblFK->getPhpName();
147         $culture = '';
148         $culture_peername = '';
149         foreach ($tblFK->getColumns() as $col)
150         {
151           if (("true" === strtolower($col->getAttribute('isCulture'))))
152           {
153             $culture = $col->getPhpName();
154             $culture_peername = PeerBuilder::getColumnName($col, $className);
155           }
156         }
157
158         foreach ($tblFK->getColumns() as $col)
159         {
160           if ($col->isPrimaryKey()) continue;
161
162           $script .= '
163   public function get'.$col->getPhpName().'($culture = null)
164   {
165     return $this->getCurrent'.$className.'($culture)->get'.$col->getPhpName().'();
166   }
167
168   public function set'.$col->getPhpName().'($value, $culture = null)
169   {
170     $this->getCurrent'.$className.'($culture)->set'.$col->getPhpName().'($value);
171   }
172 ';
173         }
174
175 $script .= '
176   protected $current_i18n = array();
177
178   public function getCurrent'.$className.'($culture = null)
179   {
180     if (is_null($culture))
181     {
182       $culture = is_null($this->culture) ? sfPropel::getDefaultCulture() : $this->culture;
183     }
184
185     if (!isset($this->current_i18n[$culture]))
186     {
187       $obj = '.$className.'Peer::retrieveByPK($this->get'.$pk.'(), $culture);
188       if ($obj)
189       {
190         $this->set'.$className.'ForCulture($obj, $culture);
191       }
192       else
193       {
194         $this->set'.$className.'ForCulture(new '.$className.'(), $culture);
195         $this->current_i18n[$culture]->set'.$culture.'($culture);
196       }
197     }
198
199     return $this->current_i18n[$culture];
200   }
201
202   public function set'.$className.'ForCulture($object, $culture)
203   {
204     $this->current_i18n[$culture] = $object;
205     $this->add'.$className.'($object);
206   }
207 ';
208       }
209     }
210   }
211
212   protected function addDoSave(&$script)
213   {
214     $tmp = '';
215     parent::addDoSave($tmp);
216     // add autosave to i18n object even if the base object is not changed
217     $tmp = preg_replace_callback('#(\$this\->(.+?)\->isModified\(\))#', array($this, 'i18nDoSaveCallback'), $tmp);
218
219     $script .= $tmp;
220   }
221
222   private function i18nDoSaveCallback($matches)
223   {
224     $value = $matches[1];
225
226     // get the related class to see if it is a i18n one
227     $table = $this->getTable();
228     $column = null;
229     foreach ($table->getForeignKeys() as $fk)
230     {
231       if ($matches[2] == $this->getFKVarName($fk))
232       {
233         $column = $fk;
234         break;
235       }
236     }
237     $foreign_table = $this->getDatabase()->getTable($fk->getForeignTableName());
238     if ($foreign_table->getAttribute('isI18N'))
239     {
240       $foreign_tables_i18n_table = $this->getDatabase()->getTable($foreign_table->getAttribute('i18nTable'));
241       $value .= ' || ($this->'.$matches[2].'->getCulture() && $this->'.$matches[2].'->getCurrent'.$foreign_tables_i18n_table->getPhpName().'()->isModified())';
242     }
243
244     return $value;
245   }
246
247   protected function addDelete(&$script)
248   {
249     $tmp = '';
250     parent::addDelete($tmp);
251
252     if (DataModelBuilder::getBuildProperty('builderAddBehaviors'))
253     {
254       // add sfMixer call
255       $pre_mixer_script = "
256
257     foreach (sfMixer::getCallables('{$this->getClassname()}:delete:pre') as \$callable)
258     {
259       \$ret = call_user_func(\$callable, \$this, \$con);
260       if (\$ret)
261       {
262         return;
263       }
264     }
265
266 ";
267       $post_mixer_script = "
268
269     foreach (sfMixer::getCallables('{$this->getClassname()}:delete:post') as \$callable)
270     {
271       call_user_func(\$callable, \$this, \$con);
272     }
273
274 ";
275       $tmp = preg_replace('/{/', '{'.$pre_mixer_script, $tmp, 1);
276       $tmp = preg_replace('/}\s*$/', $post_mixer_script.'  }', $tmp);
277     }
278
279     // update current script
280     $script .= $tmp;
281   }
282
283   protected function addSave(&$script)
284   {
285     $tmp = '';
286     parent::addSave($tmp);
287
288     // add support for created_(at|on) and updated_(at|on) columns
289     $date_script = '';
290     $updated = false;
291     $created = false;
292     foreach ($this->getTable()->getColumns() as $col)
293     {
294       $clo = strtolower($col->getName());
295
296       if (!$updated && in_array($clo, array('updated_at', 'updated_on')))
297       {
298         $updated = true;
299         $date_script .= "
300     if (\$this->isModified() && !\$this->isColumnModified(".$this->getColumnConstant($col)."))
301     {
302       \$this->set".$col->getPhpName()."(time());
303     }
304 ";
305       }
306       else if (!$created && in_array($clo, array('created_at', 'created_on')))
307       {
308         $created = true;
309         $date_script .= "
310     if (\$this->isNew() && !\$this->isColumnModified(".$this->getColumnConstant($col)."))
311     {
312       \$this->set".$col->getPhpName()."(time());
313     }
314 ";
315       }
316     }
317     $tmp = preg_replace('/{/', '{'.$date_script, $tmp, 1);
318
319     if (DataModelBuilder::getBuildProperty('builderAddBehaviors'))
320     {
321       // add sfMixer call
322       $pre_mixer_script = "
323
324     foreach (sfMixer::getCallables('{$this->getClassname()}:save:pre') as \$callable)
325     {
326       \$affectedRows = call_user_func(\$callable, \$this, \$con);
327       if (is_int(\$affectedRows))
328       {
329         return \$affectedRows;
330       }
331     }
332
333 ";
334       $post_mixer_script = <<<EOF
335
336     foreach (sfMixer::getCallables('{$this->getClassname()}:save:post') as \$callable)
337     {
338       call_user_func(\$callable, \$this, \$con, \$affectedRows);
339     }
340
341 EOF;
342       $tmp = preg_replace('/{/', '{'.$pre_mixer_script, $tmp, 1);
343       $tmp = preg_replace('/(\$con\->commit\(\);)/', '$1'.$post_mixer_script, $tmp);
344     }
345
346     // update current script
347     $script .= $tmp;
348   }
349
350   protected function addClassClose(&$script)
351   {
352     parent::addClassClose($script);
353
354     $behaviors = $this->getTable()->getAttribute('behaviors');
355     if ($behaviors)
356     {
357       $behavior_file_name = 'Base'.$this->getTable()->getPhpName().'Behaviors';
358       $behavior_file_path = $this->getFilePath($this->getStubObjectBuilder()->getPackage().'.om.'.$behavior_file_name);
359
360       $behavior_include_script = <<<EOF
361
362
363 if (sfProjectConfiguration::getActive() instanceof sfApplicationConfiguration)
364 {
365   include_once '%s';
366 }
367
368 EOF;
369       $script .= sprintf($behavior_include_script, $behavior_file_path);
370     }
371   }
372 }
373
Note: See TracBrowser for help on using the browser.