Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

[WIP] Twig integration #465

Closed
wants to merge 72 commits into
from
Commits
Jump to file or symbol
Failed to load files and symbols.
+5,247 −8,035
Split
View
@@ -22,7 +22,8 @@
"symfony/finder": "~2.2",
"symfony/validator": "~2.2",
"symfony/filesystem": "~2.2",
- "psr/log": "~1.0"
+ "psr/log": "~1.0",
+ "twig/twig": "~1.14"
},
"require-dev": {
"behat/behat": "~2.4",
@@ -33,6 +33,11 @@ class AggregateColumnBehavior extends Behavior
*/
public function modifyTable()
{
+ // TODO: behavior should get a validate() method like the builder::validate()
+ if (!$this->getParameter('foreign_table')) {
+ throw new \InvalidArgumentException(sprintf('You must define a \'foreign_table\' parameter for the \'aggregate_column\' behavior in the \'%s\' table', $this->getTable()->getName()));
+ }
+
$table = $this->getTable();
if (!$columnName = $this->getParameter('name')) {
throw new \InvalidArgumentException(sprintf('You must define a \'name\' parameter for the \'aggregate_column\' behavior in the \'%s\' table', $table->getName()));
@@ -57,19 +62,7 @@ public function modifyTable()
}
}
- public function objectMethods($builder)
- {
- if (!$this->getParameter('foreign_table')) {
- throw new \InvalidArgumentException(sprintf('You must define a \'foreign_table\' parameter for the \'aggregate_column\' behavior in the \'%s\' table', $this->getTable()->getName()));
- }
- $script = '';
- $script .= $this->addObjectCompute();
- $script .= $this->addObjectUpdate();
-
- return $script;
- }
-
- protected function addObjectCompute()
+ public function buildQuery()
{
$conditions = array();
if ($this->getParameter('condition')) {
@@ -94,18 +87,7 @@ protected function addObjectCompute()
implode(' AND ', $conditions)
);
- return $this->renderTemplate('objectCompute', array(
- 'column' => $this->getColumn(),
- 'sql' => $sql,
- 'bindings' => $bindings,
- ));
- }
-
- protected function addObjectUpdate()
- {
- return $this->renderTemplate('objectUpdate', array(
- 'column' => $this->getColumn(),
- ));
+ return ['sql' => $sql, 'bindings' => $bindings];
}
protected function getForeignTable()
@@ -131,7 +113,7 @@ protected function getForeignKey()
return array_shift($fks);
}
- protected function getColumn()
+ public function getColumn()
{
return $this->getTable()->getColumn($this->getParameter('name'));
}
@@ -25,40 +25,6 @@ class AggregateColumnRelationBehavior extends Behavior
'update_method' => '',
);
- public function postSave($builder)
- {
- $relationName = $this->getRelationName($builder);
-
- return "\$this->updateRelated{$relationName}(\$con);";
- }
-
- // no need for a postDelete() hook, since delete() uses Query::delete(),
- // which already has a hook
-
- public function objectAttributes($builder)
- {
- $relationName = $this->getRelationName($builder);
-
- return "protected \$old{$relationName};
-";
- }
-
- public function objectMethods($builder)
- {
- return $this->addObjectUpdateRelated($builder);
- }
-
- protected function addObjectUpdateRelated($builder)
- {
- $relationName = $this->getRelationName($builder);
-
- return $this->renderTemplate('objectUpdateRelated', array(
- 'relationName' => $relationName,
- 'variableName' => lcfirst($relationName),
- 'updateMethodName' => $this->getParameter('update_method'),
- ));
- }
-
public function objectFilter(&$script, $builder)
{
$relationName = $this->getRelationName($builder);
@@ -161,8 +127,19 @@ protected function getForeignKey()
return array_shift($fks);
}
- protected function getRelationName($builder)
+ public function getRelationName($builder)
{
return $builder->getFKPhpNameAffix($this->getForeignKey());
}
+
+ public function getTemplateDirectory()
+ {
+ $path = parent::getTemplateDirectory();
+
+ if($path !== null) {
+ return $path . '_relation';
+ }
+
+ return $path;
+ }
}
@@ -0,0 +1,31 @@
+{% set query = behavior.buildQuery %}
+{% set column = behavior.column %}
+/**
+ * Computes the value of the aggregate column {{ column.name }}
+ *
+ * @param ConnectionInterface $con A connection object
+ *
+ * @return mixed The scalar result from the aggregate query
+ */
+public function compute{{ column.phpName }}(ConnectionInterface $con)
+{
+ $stmt = $con->prepare('{{ query.sql }}');
+ {% for key, binding in query.bindings %}
+ $stmt->bindValue(':p{{ key }}', $this->get{{ binding }}());
@jaugustin

jaugustin Oct 9, 2013

Member

to much tab

@mpscholten

mpscholten Oct 9, 2013

Member

You're correct :) My indentation is currently wrong in all these templates, I'll fix this at the end of this pr. But the good side of this is, that the code is very readable. Maybe instead of indent everything in the templates we could use a php-formatter? What's your opinion on this?

@marcj

marcj Nov 4, 2013

Owner

Mh good question. The good side of a php-formatter is we can write the twig files more readable with indentation. The bad is that when we're developing new stuff into and debug it the php-formatter could create something strange when we for example have missed a } or something. That could be hard to debug then. My personal feeling is we should stay without formatter.

+ {% endfor %}
+ $stmt->execute();
+
+ return $stmt->fetchColumn();
+}
+
+
+/**
+ * Updates the aggregate column {{ column.name }}
+ *
+ * @param ConnectionInterface $con A connection object
+ */
+public function update{{ column.phpName }}(ConnectionInterface $con)
+{
+ $this->set{{ column.phpName }}($this->compute{{ column.phpName }}($con));
+ $this->save($con);
+}
@@ -1,18 +0,0 @@
-
-/**
- * Computes the value of the aggregate column <?=$column->getName()?>
- *
- * @param ConnectionInterface $con A connection object
- *
- * @return mixed The scalar result from the aggregate query
- */
-public function compute<?=$column->getPhpName()?>(ConnectionInterface $con)
-{
- $stmt = $con->prepare('<?=$sql?>');
-<?php foreach ($bindings as $key => $binding):?>
- $stmt->bindValue(':p<?=$key?>', $this->get<?=$binding?>());
-<?php endforeach;?>
- $stmt->execute();
-
- return $stmt->fetchColumn();
-}
@@ -1,11 +0,0 @@
-
-/**
- * Updates the aggregate column <?=$column->getName()?>
- *
- * @param ConnectionInterface $con A connection object
- */
-public function update<?=$column->getPhpName()?>(ConnectionInterface $con)
-{
- $this->set<?=$column->getPhpName()?>($this->compute<?=$column->getPhpName()?>($con));
- $this->save($con);
-}
@@ -1,16 +0,0 @@
-
-/**
- * Update the aggregate column in the related <?=$relationName?> object
- *
- * @param ConnectionInterface $con A connection object
- */
-protected function updateRelated<?=$relationName?>(ConnectionInterface $con)
-{
- if ($<?=$variableName?> = $this->get<?=$relationName?>()) {
- $<?=$variableName?>-><?=$updateMethodName?>($con);
- }
- if ($this->old<?=$relationName?>) {
- $this->old<?=$relationName?>-><?=$updateMethodName?>($con);
- $this->old<?=$relationName?> = null;
- }
-}
@@ -0,0 +1 @@
+protected $old{{ behavior.relationName(builder) }};
@@ -0,0 +1 @@
+$this->updateRelated{{ behavior.relationName(builder) }}($con);
@@ -0,0 +1,16 @@
+{% set relationName = behavior.relationName(builder) %}
+/**
+ * Update the aggregate column in the related {{ relationName }} object
+ *
+ * @param ConnectionInterface $con A connection object
+ */
+protected function updateRelated{{ relationName }}(ConnectionInterface $con)
+{
+ if (${{ relationName|lcfirst }} = $this->get{{ relationName }}()) {
+ ${{ relationName|lcfirst }}->{{ behavior.parameter('update_method') }}($con);
+ }
+ if ($this->old{{ relationName }}) {
+ $this->old{{ relationName }}->{{ behavior.parameter('update_method') }}($con);
+ $this->old{{ relationName }} = null;
+ }
+}
@@ -182,15 +182,6 @@ public function isArchiveOnDelete()
return 'true' === $this->getParameter('archive_on_delete');
}
- public function getObjectBuilderModifier()
- {
- if (null === $this->objectBuilderModifier) {
- $this->objectBuilderModifier = new ArchivableBehaviorObjectBuilderModifier($this);
- }
-
- return $this->objectBuilderModifier;
- }
-
public function getQueryBuilderModifier()
{
if (null === $this->queryBuilderModifier) {
Oops, something went wrong.