Permalink
Browse files

[Generator] [I18N] Fix behavior to work with recent changes

  • Loading branch information...
1 parent 5066ac3 commit 6abb019d51121e9b727e99a3f57c268bcbdc9380 @willdurand willdurand committed Jan 12, 2013
View
221 src/Propel/Generator/Behavior/I18n/I18nBehavior.php
@@ -17,44 +17,131 @@
use Propel\Generator\Behavior\Validate\ValidateBehavior;
/**
- * Allows translation of text columns through transparent one-to-many relationship
+ * Allows translation of text columns through transparent one-to-many
+ * relationship.
*
* @author Francois Zaninotto
- * @version $Revision$
*/
class I18nBehavior extends Behavior
{
const DEFAULT_LOCALE = 'en_EN';
// default parameters value
protected $parameters = array(
- 'i18n_table' => '%TABLE%_i18n',
- 'i18n_phpname' => '%PHPNAME%I18n',
- 'i18n_columns' => '',
- 'locale_column' => 'locale',
- 'default_locale' => null,
- 'locale_alias' => '',
+ 'i18n_table' => '%TABLE%_i18n',
+ 'i18n_phpname' => '%PHPNAME%I18n',
+ 'i18n_columns' => '',
+ 'locale_column' => 'locale',
+ 'default_locale' => null,
+ 'locale_alias' => '',
);
protected $tableModificationOrder = 70;
protected $objectBuilderModifier;
+
protected $queryBuilderModifier;
- protected $peerBuilderModifier;
+
protected $i18nTable;
public function modifyDatabase()
{
foreach ($this->getDatabase()->getTables() as $table) {
if ($table->hasBehavior('i18n') && !$table->getBehavior('i18n')->getParameter('default_locale')) {
$table->getBehavior('i18n')->addParameter(array(
- 'name' => 'default_locale',
- 'value' => $this->getParameter('default_locale')
+ 'name' => 'default_locale',
+ 'value' => $this->getParameter('default_locale'),
));
}
}
}
+ public function getDefaultLocale()
+ {
+ if (!$defaultLocale = $this->getParameter('default_locale')) {
+ $defaultLocale = self::DEFAULT_LOCALE;
+ }
+
+ return $defaultLocale;
+ }
+
+ public function getI18nTable()
+ {
+ return $this->i18nTable;
+ }
+
+ public function getI18nForeignKey()
+ {
+ foreach ($this->i18nTable->getForeignKeys() as $fk) {
+ if ($fk->getForeignTableName() == $this->table->getName()) {
+ return $fk;
+ }
+ }
+ }
+
+ public function getLocaleColumn()
+ {
+ return $this->getI18nTable()->getColumn($this->getLocaleColumnName());
+ }
+
+ public function getI18nColumns()
+ {
+ $columns = array();
+ $i18nTable = $this->getI18nTable();
+ if ($columnNames = $this->getI18nColumnNamesFromConfig()) {
+ // Strategy 1: use the i18n_columns parameter
+ foreach ($columnNames as $columnName) {
+ $columns []= $i18nTable->getColumn($columnName);
+ }
+ } else {
+ // strategy 2: use the columns of the i18n table
+ // warning: does not work when database behaviors add columns to all tables
+ // (such as timestampable behavior)
+ foreach ($i18nTable->getColumns() as $column) {
+ if (!$column->isPrimaryKey()) {
+ $columns []= $column;
+ }
+ }
+ }
+
+ return $columns;
+ }
+
+ public function replaceTokens($string)
+ {
+ $table = $this->getTable();
+
+ return strtr($string, array(
+ '%TABLE%' => $table->getName(),
+ '%PHPNAME%' => $table->getPhpName(),
+ ));
+ }
+
+ public function getObjectBuilderModifier()
+ {
+ if (null === $this->objectBuilderModifier) {
+ $this->objectBuilderModifier = new I18nBehaviorObjectBuilderModifier($this);
+ }
+
+ return $this->objectBuilderModifier;
+ }
+
+ public function getQueryBuilderModifier()
+ {
+ if (null === $this->queryBuilderModifier) {
+ $this->queryBuilderModifier = new I18nBehaviorQueryBuilderModifier($this);
+ }
+
+ return $this->queryBuilderModifier;
+ }
+
+ public function staticAttributes($builder)
+ {
+ return $this->renderTemplate('staticAttributes', array(
+ 'defaultLocale' => $this->getDefaultLocale(),
+ ));
+ }
+
public function modifyTable()
{
$this->addI18nTable();
@@ -65,9 +152,10 @@ public function modifyTable()
protected function addI18nTable()
{
- $table = $this->getTable();
- $database = $table->getDatabase();
+ $table = $this->getTable();
+ $database = $table->getDatabase();
$i18nTableName = $this->getI18nTableName();
+
if ($database->hasTable($i18nTableName)) {
$this->i18nTable = $database->getTable($i18nTableName);
} else {
@@ -78,6 +166,7 @@ protected function addI18nTable()
'schema' => $table->getSchema(),
'namespace' => $table->getNamespace() ? '\\' . $table->getNamespace() : null,
));
+
// every behavior adding a table should re-execute database behaviors
foreach ($database->getBehaviors() as $behavior) {
$behavior->modifyDatabase();
@@ -87,38 +176,45 @@ protected function addI18nTable()
protected function relateI18nTableToMainTable()
{
- $table = $this->getTable();
+ $table = $this->getTable();
$i18nTable = $this->i18nTable;
- $pks = $this->getTable()->getPrimaryKey();
+ $pks = $this->getTable()->getPrimaryKey();
+
if (count($pks) > 1) {
throw new EngineException('The i18n behavior does not support tables with composite primary keys');
}
+
foreach ($pks as $column) {
if (!$i18nTable->hasColumn($column->getName())) {
$column = clone $column;
$column->setAutoIncrement(false);
$i18nTable->addColumn($column);
}
}
+
if (in_array($table->getName(), $i18nTable->getForeignTableNames())) {
return;
}
+
$fk = new ForeignKey();
$fk->setForeignTableCommonName($table->getCommonName());
$fk->setForeignSchemaName($table->getSchema());
$fk->setDefaultJoin('LEFT JOIN');
$fk->setOnDelete(ForeignKey::CASCADE);
$fk->setOnUpdate(ForeignKey::NONE);
+
foreach ($pks as $column) {
$fk->addReference($column->getName(), $column->getName());
}
+
$i18nTable->addForeignKey($fk);
}
protected function addLocaleColumnToI18n()
{
$localeColumnName = $this->getLocaleColumnName();
- if (!$this->i18nTable->hasColumn($localeColumnName)) {
+
+ if (! $this->i18nTable->hasColumn($localeColumnName)) {
$this->i18nTable->addColumn(array(
'name' => $localeColumnName,
'type' => PropelTypes::VARCHAR,
@@ -134,14 +230,16 @@ protected function addLocaleColumnToI18n()
*/
protected function moveI18nColumns()
{
- $table = $this->getTable();
+ $table = $this->getTable();
$i18nTable = $this->i18nTable;
+
$i18nValidateParams = array();
foreach ($this->getI18nColumnNamesFromConfig() as $columnName) {
if (!$i18nTable->hasColumn($columnName)) {
if (!$table->hasColumn($columnName)) {
throw new EngineException(sprintf('No column named %s found in table %s', $columnName, $table->getName()));
}
+
$column = $table->getColumn($columnName);
// add the column
$i18nColumn = $i18nTable->addColumn(clone $column);
@@ -155,6 +253,7 @@ protected function moveI18nColumns()
}
// FIXME: also move FKs, and indices on this column
}
+
if ($table->hasColumn($columnName)) {
$table->removeColumn($columnName);
}
@@ -201,92 +300,4 @@ protected function getI18nColumnNamesFromConfig()
return $columnNames;
}
-
- public function getDefaultLocale()
- {
- if (!$defaultLocale = $this->getParameter('default_locale')) {
- $defaultLocale = self::DEFAULT_LOCALE;
- }
-
- return $defaultLocale;
- }
-
- public function getI18nTable()
- {
- return $this->i18nTable;
- }
-
- public function getI18nForeignKey()
- {
- foreach ($this->i18nTable->getForeignKeys() as $fk) {
- if ($fk->getForeignTableName() == $this->table->getName()) {
- return $fk;
- }
- }
- }
-
- public function getLocaleColumn()
- {
- return $this->getI18nTable()->getColumn($this->getLocaleColumnName());
- }
-
- public function getI18nColumns()
- {
- $columns = array();
- $i18nTable = $this->getI18nTable();
- if ($columnNames = $this->getI18nColumnNamesFromConfig()) {
- // Strategy 1: use the i18n_columns parameter
- foreach ($columnNames as $columnName) {
- $columns []= $i18nTable->getColumn($columnName);
- }
- } else {
- // strategy 2: use the columns of the i18n table
- // warning: does not work when database behaviors add columns to all tables
- // (such as timestampable behavior)
- foreach ($i18nTable->getColumns() as $column) {
- if (!$column->isPrimaryKey()) {
- $columns []= $column;
- }
- }
- }
-
- return $columns;
- }
-
- public function replaceTokens($string)
- {
- $table = $this->getTable();
-
- return strtr($string, array(
- '%TABLE%' => $table->getName(),
- '%PHPNAME%' => $table->getPhpName(),
- ));
- }
-
- public function getObjectBuilderModifier()
- {
- if (null === $this->objectBuilderModifier) {
- $this->objectBuilderModifier = new I18nBehaviorObjectBuilderModifier($this);
- }
-
- return $this->objectBuilderModifier;
- }
-
- public function getQueryBuilderModifier()
- {
- if (null === $this->queryBuilderModifier) {
- $this->queryBuilderModifier = new I18nBehaviorQueryBuilderModifier($this);
- }
-
- return $this->queryBuilderModifier;
- }
-
- public function getPeerBuilderModifier()
- {
- if (null === $this->peerBuilderModifier) {
- $this->peerBuilderModifier = new I18nBehaviorPeerBuilderModifier($this);
- }
-
- return $this->peerBuilderModifier;
- }
}
View
37 src/Propel/Generator/Behavior/I18n/I18nBehaviorPeerBuilderModifier.php
@@ -1,37 +0,0 @@
-<?php
-
-/**
- * This file is part of the Propel package.
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- *
- * @license MIT License
- */
-
-namespace Propel\Generator\Behavior\I18n;
-
-/**
- * Allows translation of text columns through transparent one-to-many relationship.
- * Modifier for the peer builder.
- *
- * @author François Zaninotto
- */
-class I18nBehaviorPeerBuilderModifier
-{
- protected $behavior;
-
- public function __construct($behavior)
- {
- $this->behavior = $behavior;
- }
-
- public function staticConstants()
- {
- return "
-/**
- * The default locale to use for translations
- * @var string
- */
-const DEFAULT_LOCALE = '{$this->behavior->getDefaultLocale()}';";
- }
-}
View
4 src/Propel/Generator/Behavior/I18n/I18nBehaviorQueryBuilderModifier.php
@@ -19,13 +19,15 @@
class I18nBehaviorQueryBuilderModifier
{
protected $behavior;
+
protected $table;
+
protected $builder;
public function __construct($behavior)
{
$this->behavior = $behavior;
- $this->table = $behavior->getTable();
+ $this->table = $behavior->getTable();
}
public function queryMethods($builder)
View
4 src/Propel/Generator/Behavior/I18n/templates/objectAttributes.php
@@ -3,10 +3,10 @@
* Current locale
* @var string
*/
-protected $currentLocale = '<?php echo $defaultLocale ?>';
+protected $currentLocale = '<?= $defaultLocale ?>';
/**
* Current translation objects
- * @var array[<?php echo $objectClassName ?>]
+ * @var array[<?= $objectClassName ?>]
*/
protected $currentTranslations;
View
2 src/Propel/Generator/Behavior/I18n/templates/objectClearReferences.php
@@ -1,2 +1,2 @@
-$this->currentLocale = '<?php echo $defaultLocale ?>';
+$this->currentLocale = '<?= $defaultLocale ?>';
@hhamon
hhamon Jan 12, 2013

why are we using short tags?

@willdurand
willdurand Jan 12, 2013

it's more readable. And, as Propel2 requires PHP 5.4, there is no problem :)

$this->currentTranslations = null;
View
2 src/Propel/Generator/Behavior/I18n/templates/objectGetCurrentTranslation.php
@@ -4,7 +4,7 @@
*
* @param ConnectionInterface $con an optional connection object
*
- * @return <?php echo $i18nTablePhpName ?>
+ * @return <?= $i18nTablePhpName ?>
*/
public function getCurrentTranslation(ConnectionInterface $con = null)
{
View
2 src/Propel/Generator/Behavior/I18n/templates/objectGetLocale.php
@@ -4,7 +4,7 @@
*
* @return string $locale Locale to use for the translation, e.g. 'fr_FR'
*/
-public function get<?php echo $localeColumnName ?>()
+public function get<?= $localeColumnName ?>()
{
return $this->currentLocale;
}
View
7 src/Propel/Generator/Behavior/I18n/templates/staticAttributes.php
@@ -0,0 +1,7 @@
+
+/**
+ * The default locale to use for translations.
+ *
+ * @var string
+ */
+const DEFAULT_LOCALE = '<?= $defaultLocale ?>';
View
3 src/Propel/Generator/Builder/Om/PeerBuilder.php
@@ -135,6 +135,7 @@ protected function addClassOpen(&$script)
protected function addClassBody(&$script)
{
$this->declareClassFromBuilder($this->getStubObjectBuilder());
+ $this->declareClassFromBuilder($this->getTableMapBuilder());
parent::addClassBody($script);
@@ -1716,6 +1717,8 @@ protected static function doOnDeleteCascade(Criteria \$criteria, ConnectionInter
$columnNamesF = $fk->getLocalColumns();
$columnNamesL = $fk->getForeignColumns();
+ $this->declareClassFromBuilder($joinedTablePeerBuilder->getTableMapBuilder());
+
$script .= "
// delete related $fkClassName objects
View
10 tests/Propel/Tests/Generator/Behavior/I18n/I18nBehaviorQueryBuilderModifierTest.php
@@ -12,7 +12,6 @@
use Propel\Generator\Util\QuickBuilder;
use Propel\Generator\Behavior\I18n\I18nBehavior;
-
use Propel\Runtime\Propel;
use Propel\Runtime\ActiveQuery\Criteria;
use Propel\Runtime\Util\BasePeer;
@@ -52,7 +51,6 @@ public function setUp()
</table>
</database>
EOF;
- //QuickBuilder::debugClassesForTable($schema, 'i18n_behavior_test_11');
QuickBuilder::buildSchema($schema);
}
}
@@ -103,7 +101,7 @@ public function testJoinI18nAcceptsAJoinType()
public function testJoinI18nCreatesACorrectQuery()
{
- $con = Propel::getServiceContainer()->getConnection(\I18nBehaviorTest11Peer::DATABASE_NAME);
+ $con = Propel::getServiceContainer()->getConnection(\Map\I18nBehaviorTest11TableMap::DATABASE_NAME);
$con->useDebug(true);
\I18nBehaviorTest11Query::create()
->joinI18n('fr_FR')
@@ -143,7 +141,7 @@ public function testUseI18nQueryAcceptsARelationAlias()
public function testUseI18nQueryCreatesACorrectQuery()
{
- $con = Propel::getServiceContainer()->getConnection(\I18nBehaviorTest11Peer::DATABASE_NAME);
+ $con = Propel::getServiceContainer()->getConnection(\Map\I18nBehaviorTest11TableMap::DATABASE_NAME);
$con->useDebug(true);
\I18nBehaviorTest11Query::create()
->useI18nQuery('fr_FR')
@@ -194,7 +192,7 @@ public function testJoinWithI18nPrunesResultsWithoutTranslationWhenUsingInnerJoi
public function testJoinWithI18nHydratesRelatedObject()
{
- $con = Propel::getServiceContainer()->getConnection(\I18nBehaviorTest11Peer::DATABASE_NAME);
+ $con = Propel::getServiceContainer()->getConnection(\Map\I18nBehaviorTest11TableMap::DATABASE_NAME);
$con->useDebug(true);
\I18nBehaviorTest11Query::create()->deleteAll();
\I18nBehaviorTest11I18nQuery::create()->deleteAll();
@@ -259,7 +257,7 @@ public function testJoinWithI18nDoesNotExecuteAdditionalQueryWhenNoTranslationIs
{
$this->markTestSkipped();
- $con = Propel::getServiceContainer()->getConnection(\I18nBehaviorTest11Peer::DATABASE_NAME);
+ $con = Propel::getServiceContainer()->getConnection(\Map\I18nBehaviorTest11TableMap::DATABASE_NAME);
$con->useDebug(true);
\I18nBehaviorTest11Query::create()->deleteAll();
\I18nBehaviorTest11I18nQuery::create()->deleteAll();
View
65 tests/Propel/Tests/Generator/Behavior/I18n/I18nBehaviorTableMapBuilderModifierTest.php
@@ -1,65 +0,0 @@
-<?php
-
-/**
- * This file is part of the Propel package.
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- *
- * @license MIT License
- */
-
-namespace Propel\Tests\Generator\Behavior\I18n;
-
-use Propel\Generator\Util\QuickBuilder;
-use Propel\Generator\Behavior\I18n\I18nBehavior;
-use Propel\Runtime\Propel;
-use Propel\Runtime\Map\TableMap;
-
-/**
- * Tests for I18nBehavior class peer modifier
- *
- * @author François Zaninotto
- */
-class I18nBehaviorTableMapBuilderModifierTest extends \PHPUnit_Framework_TestCase
-{
- public function testDefaultLocaleConstant()
- {
- $schema = <<<EOF
-<database name="i18n_behavior_test_0">
- <table name="i18n_behavior_test_01">
- <column name="id" primaryKey="true" type="INTEGER" autoIncrement="true" />
- <behavior name="i18n" />
- </table>
- <table name="i18n_behavior_test_02">
- <column name="id" primaryKey="true" type="INTEGER" autoIncrement="true" />
- <behavior name="i18n">
- <parameter name="default_locale" value="fr_FR" />
- </behavior>
- </table>
-</database>
-EOF;
- QuickBuilder::buildSchema($schema);
- $this->assertEquals('en_EN', \I18nBehaviorTest01TableMap::DEFAULT_LOCALE);
- $this->assertEquals('fr_FR', \I18nBehaviorTest02TableMap::DEFAULT_LOCALE);
- }
-
- public function testFieldKeys()
- {
- $schema = <<<EOF
-<database name="i18n_behavior_test_0">
- <table name="i18n_behavior_test_03">
- <column name="id" primaryKey="true" type="INTEGER" autoIncrement="true" />
- <column name="foo1" type="VARCHAR" />
- <column name="foo2" type="VARCHAR" />
- <column name="foo3" type="VARCHAR" />
- <behavior name="i18n">
- <parameter name="i18n_columns" value="foo2" />
- </behavior>
- </table>
-</database>
-EOF;
- QuickBuilder::buildSchema($schema);
- $this->assertEquals(array(0, 1, 2), \I18nBehaviorTest03TableMap::getFieldNames(TableMap::TYPE_NUM));
- }
-
-}

0 comments on commit 6abb019

Please sign in to comment.