Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Outsourced some code to templates

  • Loading branch information...
commit b3904998d0d03d5aca70c84cf876718d5b7693b5 1 parent 7adcc1e
@mpscholten authored
Showing with 222 additions and 316 deletions.
  1. +2 −0  src/Propel/Generator/Builder/Om/AbstractOMBuilder.php
  2. +4 −1 src/Propel/Generator/Builder/Om/AbstractObjectBuilder.php
  3. +12 −34 src/Propel/Generator/Builder/Om/ExtensionObjectBuilder.php
  4. +22 −281 src/Propel/Generator/Builder/Om/ObjectBuilder.php
  5. +1 −0  src/Propel/Generator/Builder/Om/templates/ExtensionObject/_classClose.php.twig
  6. +24 −0 src/Propel/Generator/Builder/Om/templates/ExtensionObject/_classOpen.php.twig
  7. +20 −0 src/Propel/Generator/Builder/Om/templates/Object/_applyDefaultValues.php.twig
  8. +7 −0 src/Propel/Generator/Builder/Om/templates/Object/_attribute/_alreadyInSaveAttribute.php.twig
  9. +32 −0 src/Propel/Generator/Builder/Om/templates/Object/_attribute/_columnAttributes.php.twig
  10. +4 −0 src/Propel/Generator/Builder/Om/templates/Object/_attribute/_foreignKeyAttribute.php.twig
  11. +13 −0 src/Propel/Generator/Builder/Om/templates/Object/_attribute/_refFkAttributes.php.twig
  12. +5 −0 src/Propel/Generator/Builder/Om/templates/Object/_attribute/_scheduledForDeletionAttribute.php.twig
  13. +45 −0 src/Propel/Generator/Builder/Om/templates/Object/_attributes.php.twig
  14. +27 −0 src/Propel/Generator/Builder/Om/templates/Object/_classBody.php.twig
  15. +4 −0 src/Propel/Generator/Builder/Om/templates/Object/_constants.php.twig
View
2  src/Propel/Generator/Builder/Om/AbstractOMBuilder.php
@@ -96,6 +96,8 @@ public function build()
* This method may emit warnings for code which may cause problems
* and will throw exceptions for errors that will definitely cause
* problems.
+ *
+ * Todo: Why is this not abstract?!
*/
protected function validateModel()
{
View
5 src/Propel/Generator/Builder/Om/AbstractObjectBuilder.php
@@ -140,7 +140,10 @@ protected function isAddGenericAccessors()
return (!$table->isAlias() && $this->getBuildProperty('addGenericAccessors'));
}
- protected function hasDefaultValues()
+ /**
+ * todo: made this public because view need this
+ */
+ public function hasDefaultValues()
{
foreach ($this->getTable()->getColumns() as $col) {
if (null !== $col->getDefaultValue()) {
View
46 src/Propel/Generator/Builder/Om/ExtensionObjectBuilder.php
@@ -10,6 +10,8 @@
namespace Propel\Generator\Builder\Om;
+use Propel\Generator\Model\Table;
+
/**
* Generates the empty PHP5 stub object class for user object model (OM).
*
@@ -20,6 +22,14 @@
*/
class ExtensionObjectBuilder extends AbstractObjectBuilder
{
+ private $twig;
+ public function __construct(Table $table)
+ {
+ parent::__construct($table);
+
+ $loader = new \Twig_Loader_Filesystem(__DIR__ . '/templates/');
+ $this->twig = new \Twig_Environment($loader);
+ }
/**
* Returns the name of the current class being built.
@@ -36,37 +46,7 @@ public function getUnprefixedClassName()
*/
protected function addClassOpen(&$script)
{
- $table = $this->getTable();
- $tableName = $table->getName();
- $tableDesc = $table->getDescription();
- $baseClassName = $this->getClassNameFromBuilder($this->getObjectBuilder());
-
- if ($this->getBuildProperty('addClassLevelComment')) {
- $script .= "
-
-/**
- * Skeleton subclass for representing a row from the '$tableName' table.
- *
- * $tableDesc
- *";
- if ($this->getBuildProperty('addTimeStamp')) {
- $now = strftime('%c');
- $script .= "
- * This class was autogenerated by Propel " . $this->getBuildProperty('version') . " on:
- *
- * $now
- *";
- }
- $script .= "
- * You should add additional methods to this class to meet the
- * application requirements. This class will only be generated as
- * long as it does not already exist in the output directory.
- *
- */";
- }
- $script .= PHP_EOL . ($table->isAbstract() ? "abstract " : "")."class ".$this->getUnqualifiedClassName()." extends $baseClassName
-{
-";
+ $script .= $this->twig->render('ExtensionObject/_classOpen.php.twig', ['builder' => $this]);
}
/**
@@ -87,9 +67,7 @@ protected function addClassBody(&$script)
*/
protected function addClassClose(&$script)
{
- $script .= "
-}
-";
+ $script .= $this->twig->render('ExtensionObject/_classClose.php.twig', ['builder' => $this]);
$this->applyBehaviorModifier('extensionObjectFilter', $script, "");
}
}
View
303 src/Propel/Generator/Builder/Om/ObjectBuilder.php
@@ -15,6 +15,7 @@
use Propel\Generator\Model\ForeignKey;
use Propel\Generator\Model\IdMethod;
use Propel\Generator\Model\PropelTypes;
+use Propel\Generator\Model\Table;
use Propel\Generator\Platform\MssqlPlatform;
use Propel\Generator\Platform\MysqlPlatform;
use Propel\Generator\Platform\OraclePlatform;
@@ -31,6 +32,17 @@
*/
class ObjectBuilder extends AbstractObjectBuilder
{
+ private $twig;
+
+ public function __construct(Table $table)
+ {
+ parent::__construct($table);
+ $loader = new \Twig_Loader_Filesystem(__DIR__ . '/templates/');
+ $this->twig = new \Twig_Environment($loader, ['autoescape' => false, 'strict_variables' => true, 'cache' => __DIR__ . '/cache/']);
+ $this->twig->addFilter('addSlashes', new \Twig_SimpleFilter('addSlashes', 'addslashes'));
+ $this->twig->addFilter('lcfirst', new \Twig_SimpleFilter('lcfirst', 'lcfirst'));
+ }
+
/**
* Returns the package for the base object classes.
@@ -150,11 +162,13 @@ protected function getTemporalFormatter(Column $column)
* Returns the type-casted and stringified default value for the specified
* Column. This only works for scalar default values currently.
*
+ * TODO: made this public because template
+ *
* @param Column $column
* @throws EngineException
* @return string
*/
- protected function getDefaultValueString(Column $column)
+ public function getDefaultValueString(Column $column)
{
$defaultValue = var_export(null, true);
$val = $column->getPhpDefaultValue();
@@ -279,35 +293,9 @@ protected function addClassBody(&$script)
'\Propel\Runtime\Map\TableMap'
);
- $table = $this->getTable();
- if (!$table->isAlias()) {
- $this->addConstants($script);
- $this->addAttributes($script);
- }
+ $script .= $this->twig->render('Object/_classBody.php.twig', ['builder' => $this]);
- if ($table->hasCrossForeignKeys()) {
- /* @var $refFK ForeignKey */
- foreach ($table->getCrossFks() as $fkList) {
- list($refFK, $crossFK) = $fkList;
- $fkName = $this->getFKPhpNameAffix($crossFK, true);
-
- if (!$refFK->isLocalPrimaryKey()) {
- $this->addScheduledForDeletionAttribute($script, $fkName);
- }
- }
- }
-
- foreach ($table->getReferrers() as $refFK) {
- $fkName = $this->getRefFKPhpNameAffix($refFK, true);
-
- if (!$refFK->isLocalPrimaryKey()) {
- $this->addScheduledForDeletionAttribute($script, $fkName);
- }
- }
-
- if ($this->hasDefaultValues()) {
- $this->addApplyDefaultValues($script);
- }
+ $table = $this->getTable();
$this->addConstructor($script);
$this->addBaseObjectMethods($script);
@@ -376,56 +364,14 @@ protected function addClassClose(&$script)
}
/**
- * Adds any constants to the class.
- *
- * @param string &$script
- */
- protected function addConstants(&$script)
- {
- $script .= "
- /**
- * TableMap class name
- */
- const TABLE_MAP = '" . addslashes($this->getTableMapBuilder()->getFullyQualifiedClassName()) . "';
-";
- }
-
- /**
- * Adds class attributes.
- *
- * @param string &$script
+ * Todo: find a way to handle behaviours
*/
- protected function addAttributes(&$script)
+ public function addAttributes()
{
- $table = $this->getTable();
-
- $script .= "
-";
-
- $script .= $this->renderTemplate('baseObjectAttributes');
-
- if (!$table->isAlias()) {
- $this->addColumnAttributes($script);
- }
-
- foreach ($table->getForeignKeys() as $fk) {
- $this->addFKAttributes($script, $fk);
- }
-
- foreach ($table->getReferrers() as $refFK) {
- $this->addRefFKAttributes($script, $refFK);
- }
-
- // many-to-many relationships
- foreach ($table->getCrossFks() as $fkList) {
- $crossFK = $fkList[1];
- $this->addCrossFKAttributes($script, $crossFK);
- }
-
- $this->addAlreadyInSaveAttribute($script);
-
- // apply behaviors
+ $script = $this->twig->render('Object/_attributes.php.twig', ['builder' => $this]);
$this->applyBehaviorModifier('objectAttributes', $script, " ");
+
+ return $script;
}
/**
@@ -439,111 +385,12 @@ protected function addColumnAttributes(&$script)
$table = $this->getTable();
foreach ($table->getColumns() as $col) {
- $this->addColumnAttributeComment($script, $col);
- $this->addColumnAttributeDeclaration($script, $col);
- if ($col->isLazyLoad() ) {
- $this->addColumnAttributeLoaderComment($script, $col);
- $this->addColumnAttributeLoaderDeclaration($script, $col);
- }
if ($col->getType() == PropelTypes::OBJECT || $col->getType() == PropelTypes::PHP_ARRAY) {
$this->addColumnAttributeUnserializedComment($script, $col);
$this->addColumnAttributeUnserializedDeclaration($script, $col);
}
}
}
-
- /**
- * Adds comment about the attribute (variable) that stores column values.
- *
- * @param string &$script
- * @param Column $column
- */
- protected function addColumnAttributeComment(&$script, Column $column)
- {
- $cptype = $column->getPhpType();
- $clo = $column->getLowercasedName();
-
- $script .= "
- /**
- * The value for the $clo field.";
- if ($column->getDefaultValue()) {
- if ($column->getDefaultValue()->isExpression()) {
- $script .= "
- * Note: this column has a database default value of: (expression) ".$column->getDefaultValue()->getValue();
- } else {
- $script .= "
- * Note: this column has a database default value of: ". $this->getDefaultValueString($column);
- }
- }
- $script .= "
- * @var $cptype
- */";
- }
-
- /**
- * Adds the declaration of a column value storage attribute.
- *
- * @param string &$script
- * @param Column $column
- */
- protected function addColumnAttributeDeclaration(&$script, Column $column)
- {
- $clo = $column->getLowercasedName();
- $script .= "
- protected \$" . $clo . ";
-";
- }
-
- /**
- * Adds the comment about the attribute keeping track if an attribute value
- * has been loaded.
- *
- * @param string &$script
- * @param Column $column
- */
- protected function addColumnAttributeLoaderComment(&$script, Column $column)
- {
- $clo = $column->getLowercasedName();
- $script .= "
- /**
- * Whether the lazy-loaded \$$clo value has been loaded from database.
- * This is necessary to avoid repeated lookups if \$$clo column is NULL in the db.
- * @var boolean
- */";
- }
-
- /**
- * Adds the declaration of the attribute keeping track of an attribute
- * loaded state.
- *
- * @param string &$script
- * @param Column $column
- */
- protected function addColumnAttributeLoaderDeclaration(&$script, Column $column)
- {
- $clo = $column->getLowercasedName();
- $script .= "
- protected \$".$clo."_isLoaded = false;
-";
- }
-
- /**
- * Adds the comment about the serialized attribute.
- *
- * @param string &$script
- * @param Column $column
- */
- protected function addColumnAttributeUnserializedComment(&$script, Column $column)
- {
- $clo = $column->getLowercasedName();
- $script .= "
- /**
- * The unserialized \$$clo value - i.e. the persisted object.
- * This is necessary to avoid repeated calls to unserialize() at runtime.
- * @var object
- */";
- }
-
/**
* Adds the declaration of the serialized attribute.
*
@@ -653,95 +500,6 @@ protected function addHookMethods(&$script)
}
/**
- * Adds the applyDefaults() method, which is called from the constructor.
- *
- * @param string &$script
- */
- protected function addApplyDefaultValues(&$script)
- {
- $this->addApplyDefaultValuesComment($script);
- $this->addApplyDefaultValuesOpen($script);
- $this->addApplyDefaultValuesBody($script);
- $this->addApplyDefaultValuesClose($script);
- }
-
- /**
- * Adds the comment for the applyDefaults method.
- *
- * @param string &$script
- */
- protected function addApplyDefaultValuesComment(&$script)
- {
- $script .= "
- /**
- * Applies default values to this object.
- * This method should be called from the object's constructor (or
- * equivalent initialization method).
- * @see __construct()
- */";
- }
-
- /**
- * Adds the function declaration for the applyDefaults method.
- *
- * @param string &$script
- */
- protected function addApplyDefaultValuesOpen(&$script)
- {
- $script .= "
- public function applyDefaultValues()
- {";
- }
-
- /**
- * Adds the function body of the applyDefault method.
- *
- * @param string &$script
- */
- protected function addApplyDefaultValuesBody(&$script)
- {
- $table = $this->getTable();
- // FIXME - Apply support for PHP default expressions here
- // see: http://propel.phpdb.org/trac/ticket/378
-
- $colsWithDefaults = array();
- foreach ($table->getColumns() as $column) {
- $def = $column->getDefaultValue();
- if ($def !== null && !$def->isExpression()) {
- $colsWithDefaults[] = $column;
- }
- }
-
- foreach ($colsWithDefaults as $column) {
- $clo = $column->getLowercasedName();
- $defaultValue = $this->getDefaultValueString($column);
- if ($column->isTemporalType()) {
- $dateTimeClass = $this->getBuildProperty('dateTimeClass');
- if (!$dateTimeClass) {
- $dateTimeClass = '\DateTime';
- }
- $script .= "
- \$this->".$clo." = PropelDateTime::newInstance($defaultValue, null, '$dateTimeClass');";
- } else {
- $script .= "
- \$this->".$clo." = $defaultValue;";
- }
- }
- }
-
- /**
- * Adds the function close for the applyDefaults method.
- *
- * @param string &$script
- */
- protected function addApplyDefaultValuesClose(&$script)
- {
- $script .= "
- }
-";
- }
-
- /**
* Adds a date/time/timestamp getter method.
*
* @param string &$script
@@ -4713,23 +4471,6 @@ protected function doUpdate(ConnectionInterface \$con)
}
/**
- * Adds the $alreadyInSave attribute, which prevents attempting to re-save the same object.
- * @param string &$script The script will be modified in this method.
- */
- protected function addAlreadyInSaveAttribute(&$script)
- {
- $script .= "
- /**
- * Flag to prevent endless save loop, if this object is referenced
- * by another object which falls in this transaction.
- *
- * @var boolean
- */
- protected \$alreadyInSave = false;
-";
- }
-
- /**
* Adds the save() method.
* @param string &$script The script will be modified in this method.
*/
View
1  src/Propel/Generator/Builder/Om/templates/ExtensionObject/_classClose.php.twig
@@ -0,0 +1 @@
+}
View
24 src/Propel/Generator/Builder/Om/templates/ExtensionObject/_classOpen.php.twig
@@ -0,0 +1,24 @@
+
+{% if builder.buildProperty('addClassLevelComment') %}
+ /**
+ * Skeleton subclass for representing a row from the '{{ builder.table.name }}' table.
+ *
+ * {{ builder.table.description }}
+ *
+ {% if builder.buildProperty('addTimeStamp') %}
+ * This class was autogenerated by Propel {{ builder.buildProperty('version') }} on:
+ *
+ * {{ now|date('m/d/Y') }}
+ *
+ {% endif %}
+ * You should add additional methods to this class to meet the
+ * application requirements. This class will only be generated as
+ * long as it does not already exist in the output directory.
+ *
+ */
+{% endif %}
+{% if builder.table.abstract %}
+ abstract
+{% endif %}
+class {{ builder.unqualifiedClassName }} extends {{ builder.classNameFromBuilder(builder.objectBuilder) }}
+{
View
20 src/Propel/Generator/Builder/Om/templates/Object/_applyDefaultValues.php.twig
@@ -0,0 +1,20 @@
+/**
+ * Applies default values to this object.
+ * This method should be called from the object's constructor (or
+ * equivalent initialization method).
+ * @see __construct()
+ */
+
+public function applyDefaultValues()
+{
+ {% for column in builder.table.columns %}
+ {% set default = column.defaultValue %}
+ {% if default != null and not default.expression %}
+ {% if column.temporalType %}
+ $this->{{ column.name|lower }} = PropelDateTime::newInstance({{ builder.defaultValueString(column) }}, null, '{{ builder.buildProperty('dateTimeClass')|default('\DateTime') }}');
+ {% else %}
+ $this->{{ column.name|lower }} = {{ builder.defaultValueString(column) }};
+ {% endif %}
+ {% endif %}
+ {% endfor %}
+}
View
7 src/Propel/Generator/Builder/Om/templates/Object/_attribute/_alreadyInSaveAttribute.php.twig
@@ -0,0 +1,7 @@
+/**
+ * Flag to prevent endless save loop, if this object is referenced
+ * by another object which falls in this transaction.
+ *
+ * @var boolean
+ */
+protected $alreadyInSave = false;
View
32 src/Propel/Generator/Builder/Om/templates/Object/_attribute/_columnAttributes.php.twig
@@ -0,0 +1,32 @@
+{% for column in builder.table.columns %}
+ /**
+ * The value for the {{ column.name|lower }} field.
+ {% if column.defaultvalue %}
+ {% if column.defaultValue.expression %}
+ * Note: this column has a database default value of: (expression) {{ column.defaultValue.value }}
+ {% else %}
+ * Note: this column has a database default value of: {{ builder.defaultValueString(column) }}
+ {% endif %}
+ {% endif %}
+ * @var {{ column.phpType }}
+ */
+ protected ${{ column.name|lower }};
+
+ {% if column.lazyLoad %}
+ /**
+ * Whether the lazy-loaded ${{ column.name|lower }} value has been loaded from database.
+ * This is necessary to avoid repeated lookups if ${{ column.name|lower }} column is NULL in the db.
+ * @var boolean
+ */
+ protected ${{ column.name|lower }}_isLoaded = false;
+ {% endif %}
+
+ {% if column.type == 'OBJECT' or column.type == 'ARRAY' %}{# TODO: find a way to access the PropelType::-const vars via propel #}
+ /**
+ * The unserialized ${{ column.name|lower }} value - i.e. the persisted object.
+ * This is necessary to avoid repeated calls to unserialize() at runtime.
+ * @var object
+ */
+ protected ${{ column.name|lower }}_unserialized;
+ {% endif %}
+{% endfor %}
View
4 src/Propel/Generator/Builder/Om/templates/Object/_attribute/_foreignKeyAttribute.php.twig
@@ -0,0 +1,4 @@
+/**
+ * @var {{ foreignKey.phpName }}
+ */
+protected ${{ builder.fkVarName(foreignKey) }};
View
13 src/Propel/Generator/Builder/Om/templates/Object/_attribute/_refFkAttributes.php.twig
@@ -0,0 +1,13 @@
+{% set className = builder.newObjectBuilder(refFk.table).objectClassName %}
+{% if refFk.localPrimaryKey %}
+ /**
+ * @var {{ className }} one-to-one related {{ className }} object
+ */
+ protected ${{ builder.pkRefFKVarName(refFk) }};
+{% else %}
+ /**
+ * @var ObjectCollection|{{ className }}[] Collection to store aggregation of {{ className }} objects.
+ */
+ protected ${{ builder.refFKCollVarName(refFk) }};
+ protected ${{ builder.refFKCollVarName(refFk) }}Partial;
+{% endif %}
View
5 src/Propel/Generator/Builder/Om/templates/Object/_attribute/_scheduledForDeletionAttribute.php.twig
@@ -0,0 +1,5 @@
+/**
+ * An array of objects scheduled for deletion.
+ * @var ObjectCollection
+ */
+protected ${{ fkName|lcfirst }}ScheduledForDeletion = null;
View
45 src/Propel/Generator/Builder/Om/templates/Object/_attributes.php.twig
@@ -0,0 +1,45 @@
+
+/**
+ * attribute to determine if this object has previously been saved.
+ * @var boolean
+ */
+protected $new = true;
+
+/**
+ * attribute to determine whether this object has been deleted.
+ * @var boolean
+ */
+protected $deleted = false;
+
+/**
+ * The columns that have been modified in current object.
+ * Tracking modified columns allows us to only update modified columns.
+ * @var array
+ */
+protected $modifiedColumns = array();
+
+/**
+ * The (virtual) columns that are added at runtime
+ * The formatters can add supplementary columns based on a resultset
+ * @var array
+ */
+protected $virtualColumns = array();
+
+
+{% if not builder.table.alias %}
+ {% include 'Object/_attribute/_columnAttributes.php.twig' %}
+{% endif %}
+
+{% for key in builder.table.foreignKeys %}
+ {% include 'Object/_attribute/_foreignKeyAttribute.php.twig' with {'foreignKey': key} %}
+{% endfor %}
+
+{% for refFk in builder.table.referrers %}
+ {% include 'Object/_attribute/_refFkAttributes.php.twig' with {'refFk': refFk} %}
+{% endfor %}
+
+{% for fkList in builder.table.crossFks %}
+ {% include 'Object/_crossFkAttributes.php.twig' with {'key': fkList[1]} %}
+{% endfor %}
+
+{% include 'Object/_attribute/_alreadyInSaveAttribute.php.twig' %}
View
27 src/Propel/Generator/Builder/Om/templates/Object/_classBody.php.twig
@@ -0,0 +1,27 @@
+{% if not builder.table.isAlias %}
+ {% include 'Object/_constants.php.twig' %}
+ {{ builder.addAttributes }} {# Todo: remove and use inlcude instead #}
+{% endif %}
+{% if builder.table.hasCrossForeignKeys %}
+ {% for fkList in builder.table.crossFKs %} {# todo: add getters to the fklist #}
+ {% set refFk = fkList[0] %}
+ {% set crossFk = fkList[1] %}
+ {% set fkName = builder.getFKPhpNameAffix(crossFk, true) %}
+
+ {% if not refFk.localPrimaryKey %}
+ {% include 'Object/_scheduledForDeletionAttribute.php.twig' with {'fkName': fkName} %}
+ {% endif %}
+ {% endfor %}
+{% endif %}
+
+{% for ref in builder.table.referrers %}
+ {% set fkName = builder.refFKPhpNameAffix(ref, true) %}
+
+ {% if not ref.localPrimaryKey %}
+ {% include 'Object/_attribute/_scheduledForDeletionAttribute.php.twig' with {'fkName': fkName} %}
+ {% endif %}
+{% endfor %}
+
+{% if builder.hasDefaultValues %}
+ {% include 'Object/_applyDefaultValues.php.twig' with {'builder': builder} %}
+{% endif %}
View
4 src/Propel/Generator/Builder/Om/templates/Object/_constants.php.twig
@@ -0,0 +1,4 @@
+/**
+ * TableMap class name
+ */
+const TABLE_MAP = '{{ builder.tableMapBuilder.fullyQualifiedClassName|addSlashes }}';
Please sign in to comment.
Something went wrong with that request. Please try again.