Skip to content

Commit 4af8b67

Browse files
committed
Adding columns defined in actAs-templates to the docblock of the generated model class.
1 parent 33bebf2 commit 4af8b67

File tree

1 file changed

+60
-11
lines changed

1 file changed

+60
-11
lines changed

lib/Doctrine/Import/Builder.php

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ public function buildTableDefinition(array $definition)
401401
* @param array $relations
402402
* @return string
403403
*/
404-
public function buildSetUp(array $definition)
404+
public function buildSetUp(array &$definition)
405405
{
406406
$ret = array();
407407
$i = 0;
@@ -483,7 +483,7 @@ public function buildSetUp(array $definition)
483483
}
484484

485485
if (isset($definition['actAs']) && is_array($definition['actAs']) && !empty($definition['actAs'])) {
486-
$ret[$i] = $this->buildActAs($definition['actAs']);
486+
$ret[$i] = $this->buildActAs($definition['actAs'], $definition);
487487
$i++;
488488
}
489489

@@ -865,7 +865,7 @@ public function buildPhpDocs(array $definition)
865865
* @param string $option
866866
* @return string assignation code
867867
*/
868-
private function emitAssign($level, $name, $option)
868+
private function emitAssign($level, $name, $option, &$classname)
869869
{
870870
// find class matching $name
871871
$classname = $name;
@@ -905,10 +905,10 @@ private function emitActAs($level, $name)
905905
* buildActAs: builds a complete actAs code. It supports hierarchy of plugins
906906
* @param array $actAs array of plugin definitions and options
907907
*/
908-
public function buildActAs($actAs)
908+
public function buildActAs($actAs, &$definition)
909909
{
910910
$emittedActAs = array();
911-
$build = $this->innerBuildActAs($actAs, 0, null, $emittedActAs);
911+
$build = $this->innerBuildActAs($actAs, 0, null, $emittedActAs, $definition);
912912
foreach($emittedActAs as $str) {
913913
$build .= $str;
914914
}
@@ -924,7 +924,7 @@ public function buildActAs($actAs)
924924
* @param array $emittedActAs contains on output an array of actAs command to be appended to output
925925
* @return string actAs full definition
926926
*/
927-
private function innerBuildActAs($actAs, $level = 0, $parent = null, array &$emittedActAs = array())
927+
private function innerBuildActAs($actAs, $level = 0, $parent = null, array &$emittedActAs = array(), array &$definition = array())
928928
{
929929
// rewrite special case of actAs: [Behavior] which gave [0] => Behavior
930930
if (is_array($actAs) && isset($actAs[0]) && !is_array($actAs[0])) {
@@ -945,7 +945,7 @@ private function innerBuildActAs($actAs, $level = 0, $parent = null, array &$emi
945945
foreach($actAs as $template => $options) {
946946
if ($template == 'actAs') {
947947
// found another actAs
948-
$build .= $this->innerBuildActAs($options, $level + 1, $parent, $emittedActAs);
948+
$build .= $this->innerBuildActAs($options, $level + 1, $parent, $emittedActAs, $definition);
949949
} else if (is_array($options)) {
950950
// remove actAs from options
951951
$realOptions = array();
@@ -959,17 +959,19 @@ private function innerBuildActAs($actAs, $level = 0, $parent = null, array &$emi
959959
}
960960

961961
$optionPHP = $this->varExport($realOptions);
962-
$build .= $this->emitAssign($level, $template, $optionPHP);
962+
$build .= $this->emitAssign($level, $template, $optionPHP, $className);
963+
$this->addActAsColumnsToDefinition($className, $realOptions, $definition);
963964
if ($level == 0) {
964965
$emittedActAs[] = $this->emitActAs($level, $template);
965966
} else {
966967
$build .= $this->emitAddChild($level, $currentParent, $template);
967968
}
968969
// descend for the remainings actAs
969970
$parent = $template;
970-
$build .= $this->innerBuildActAs($leftActAs, $level, $template, $emittedActAs);
971+
$build .= $this->innerBuildActAs($leftActAs, $level, $template, $emittedActAs, $definition);
971972
} else {
972-
$build .= $this->emitAssign($level, $template, null);
973+
$build .= $this->emitAssign($level, $template, null, $className);
974+
$this->addActAsColumnsToDefinition($className, array($options), $definition);
973975
if ($level == 0) {
974976
$emittedActAs[] = $this->emitActAs($level, $template);
975977
} else {
@@ -979,7 +981,8 @@ private function innerBuildActAs($actAs, $level = 0, $parent = null, array &$emi
979981
}
980982
}
981983
} else {
982-
$build .= $this->emitAssign($level, $actAs, null);
984+
$build .= $this->emitAssign($level, $actAs, null, $className);
985+
$this->addActAsColumnsToDefinition($className, array(), $definition);
983986
if ($level == 0) {
984987
$emittedActAs[] = $this->emitActAs($level, $actAs);
985988
} else {
@@ -990,6 +993,52 @@ private function innerBuildActAs($actAs, $level = 0, $parent = null, array &$emi
990993
return $build;
991994
}
992995

996+
/**
997+
* Adds the columns of the used actAs behaviors to the comment block.
998+
*
999+
* @param string $className
1000+
* @param array $instanceOptions
1001+
* @param array $definition
1002+
*
1003+
* @throws Doctrine_Import_Builder_Exception
1004+
*/
1005+
private function addActAsColumnsToDefinition($className, $instanceOptions, &$definition)
1006+
{
1007+
if ($className && class_exists($className)) {
1008+
$actAsInstance = new $className($instanceOptions);
1009+
$options = $actAsInstance->getOptions();
1010+
1011+
if (count($options) == 0) {
1012+
return;
1013+
}
1014+
1015+
// Some behaviors do not contain an array of columns, e.g. SoftDelete.
1016+
if (!is_array(reset($options))) {
1017+
$options = [$options];
1018+
}
1019+
1020+
foreach ($options as $name => $column) {
1021+
if (!is_array($column) || !array_key_exists('name', $column) || !array_key_exists('type', $column)) {
1022+
// 'name' or 'type' not found. Unfortunately there is no logger. What is the best way to abort here?
1023+
continue;
1024+
}
1025+
1026+
if (array_key_exists('disabled', $column) && $column['disabled']) {
1027+
// Column has been disabled.
1028+
continue;
1029+
}
1030+
1031+
// Add field, if it does not exist already.
1032+
if (!array_key_exists($name, $definition['columns']) && !array_key_exists($column['name'], $definition['columns'])) {
1033+
$definition['columns'][$name] = $column;
1034+
}
1035+
}
1036+
} else {
1037+
throw new Doctrine_Import_Builder_Exception('Missing class for actAs ' . $className . '.');
1038+
}
1039+
}
1040+
1041+
9931042
/**
9941043
* Build php code for adding record listeners
9951044
*

0 commit comments

Comments
 (0)