From c04324ddfa5c2fd3e5bb7e1f6c21f1f1ab967918 Mon Sep 17 00:00:00 2001 From: Ilya Sabelnikov Date: Wed, 19 Oct 2011 21:55:17 +0300 Subject: [PATCH] Solve probles with installing/uninstalling inherited and plugin models, fix #2, #3 --- .gitignore | 1 - README.markdown | 2 +- .../sfDoctrineTableGeneratedTemplate.php | 4 +- .../sfDoctrineTableGenerator.class.php | 71 ++++++++---- lib/vendor/Doctrine/Table/Scoped.php | 7 ++ package.xml | 35 ++++-- .../sfDoctrineTableGeneratedTemplate.php | 2 +- .../functional/backend/BuildTableTaskTest.php | 106 +++++++++++++++--- 8 files changed, 173 insertions(+), 55 deletions(-) diff --git a/.gitignore b/.gitignore index 540abf1..6983bd5 100644 --- a/.gitignore +++ b/.gitignore @@ -9,5 +9,4 @@ test/fixtures/project/data/db/* test/fixtures/project/lib/model/doctrine/ test/fixtures/project/plugins/.channels/ test/fixtures/project/plugins/.registry/ -test/fixtures/project/plugins/sfDoctrineGuardPlugin/lib/model/doctrine/ !.gitignore diff --git a/README.markdown b/README.markdown index c163ac5..c00d078 100644 --- a/README.markdown +++ b/README.markdown @@ -408,4 +408,4 @@ before existing one. [sfDoctrineTable] functional/backend/MethodExistanceTest.............ok [sfDoctrineTable] functional/backend/MethodWhereTest.................ok All tests successful. - Files=3, Tests=113 \ No newline at end of file + Files=3, Tests=138 \ No newline at end of file diff --git a/data/generator/sfDoctrineTable/default/template/sfDoctrineTableGeneratedTemplate.php b/data/generator/sfDoctrineTable/default/template/sfDoctrineTableGeneratedTemplate.php index 6c6fb05..8259c1e 100644 --- a/data/generator/sfDoctrineTable/default/template/sfDoctrineTableGeneratedTemplate.php +++ b/data/generator/sfDoctrineTable/default/template/sfDoctrineTableGeneratedTemplate.php @@ -11,7 +11,7 @@ * @package ##PROJECT_NAME## * @subpackage table * @author ##AUTHOR_NAME## - * @version v1.0 + * @version v1.1 * getPHPDocByPattern('findBy%s') as $column => $method): ?> * @method getCollectionClass() ?>|array () (scalar $value, int $hydrationMode = null) Finds records by field "" @@ -49,7 +49,7 @@ * */ - abstract class BasemodelName ?>Table extends getFormClassToExtend() . PHP_EOL ?> + abstract class BasemodelName ?>Table extends getTableToExtendFrom() . PHP_EOL ?> { /** * @return string Base table class name used for late-static-bindings purposes diff --git a/lib/generator/sfDoctrineTableGenerator.class.php b/lib/generator/sfDoctrineTableGenerator.class.php index 6ee150f..e3b1fb3 100644 --- a/lib/generator/sfDoctrineTableGenerator.class.php +++ b/lib/generator/sfDoctrineTableGenerator.class.php @@ -375,8 +375,18 @@ public function getParentModel() * * @return string */ - public function getFormClassToExtend() + public function getTableToExtendFrom() { + $pluginName = $this->getPluginNameForModel($this->modelName); + + /** + * Plugin model base tables should be extended by it's own base table + */ + if ($pluginName) + { + return "{$this->builderOptions['packagesPrefix']}{$this->modelName}Table"; + } + $baseClasses = array( 'Doctrine_Record', 'sfDoctrineRecord', @@ -786,6 +796,7 @@ public function isMinified () protected function uninstallTable () { $baseDir = sfConfig::get('sf_lib_dir') . '/model/doctrine'; + $customTableClass = sfConfig::get('app_sf_doctrine_table_plugin_custom_table_class'); if (! $this->isPluginModel($this->modelName)) { @@ -802,15 +813,6 @@ protected function uninstallTable () return; } - if (null === ($parentInheritedModelName = $this->getParentModel())) - { - $inheritanceClass = 'Doctrine_Table'; - } - else - { - $inheritanceClass = "{$parentInheritedModelName}Table"; - } - $tableContent = file_get_contents($tableLocation); $count = null; @@ -822,7 +824,7 @@ protected function uninstallTable () */ $tableContent = preg_replace( "/class(\s+){$this->modelName}Table(\s+)extends(\s+)Base{$this->modelName}Table/ms", - "class\\1{$this->modelName}Table\\2extends\\3{$inheritanceClass}", + "class\\1{$this->modelName}Table\\2extends\\3{$this->getClassNameToExtendFromAfterUninstalling()}", $tableContent, 1, $count ); @@ -849,25 +851,13 @@ protected function uninstallTable () if (is_file($pluginTableLocation)) { - if (null === ($parentInheritedModelName = $this->getParentModel())) - { - $inheritanceClass = 'Doctrine_Table'; - } - else - { - $inheritanceClass = "{$this->builderOptions['packagesPrefix']}{$parentInheritedModelName}Table"; - } - - - $customTableClass = sfConfig::get('app_sf_doctrine_table_plugin_custom_table_class'); - $pluginTableContent = file_get_contents($pluginTableLocation); $count = null; $pluginTableContent = preg_replace( "/class(\s+){$this->builderOptions['packagesPrefix']}{$this->modelName}Table(\s+)extends(\s+){$customTableClass}/ms", - "class\\1{$this->builderOptions['packagesPrefix']}{$this->modelName}Table\\2extends\\3{$inheritanceClass}", + "class\\1{$this->builderOptions['packagesPrefix']}{$this->modelName}Table\\2extends\\3{$this->getClassNameToExtendFromAfterUninstalling()}", $pluginTableContent, 1, $count ); @@ -1069,4 +1059,37 @@ protected function installTable () } } } + + /** + * Return right class name to extend when uninstalling base tables + * + * Uninstalling table class could be other than default class. + * It happens when model has custom inheritance or project uses own + * Doctrine_Table class. + * + * @return string + */ + protected function getClassNameToExtendFromAfterUninstalling () + { + if (false !== ($pluginName = $this->getPluginNameForModel ($this->modelName))) + { + return 'Doctrine_Table'; + } + + $customTableClass + = sfConfig::get('app_sf_doctrine_table_plugin_custom_table_class'); + + if (null === ($parentInheritedModelName = $this->getParentModel())) + { + $inheritanceClass = $customTableClass == 'Doctrine_Table_Scoped' + ? 'Doctrine_Table' + : $customTableClass; + } + else + { + $inheritanceClass = "{$parentInheritedModelName}Table"; + } + + return $inheritanceClass; + } } diff --git a/lib/vendor/Doctrine/Table/Scoped.php b/lib/vendor/Doctrine/Table/Scoped.php index a08b258..1d12882 100644 --- a/lib/vendor/Doctrine/Table/Scoped.php +++ b/lib/vendor/Doctrine/Table/Scoped.php @@ -29,6 +29,13 @@ class Doctrine_Table_Scoped extends Doctrine_Table */ public function __call($method, $arguments) { + if ('getGenericTableName' == $method) + { + throw new LogicException( + 'Inheritence order is invalid. Please install base tables first.' + ); + } + # Late static bindings in action $generatedBaseTableClass = new ReflectionClass(static::getGenericTableName()); diff --git a/package.xml b/package.xml index ef2faea..5c4661d 100644 --- a/package.xml +++ b/package.xml @@ -22,11 +22,11 @@ fruit.dev@gmail.com yes - 2011-10-18 - + 2011-10-19 + - 1.0.2 - 1.0.0 + 1.0.3 + 1.0.1 stable @@ -34,7 +34,8 @@ MIT - * [Fixed] Invalid extends class when uninstalling model with "concrete" inheritance (GH-1) + * [Fixed] Uninstalling tables with a custom Doctrine_Table contains invalid class name (GH-2) + * [Fixed] Uninstalling/installing plugin base tables with broken class inheritance (GH-3) @@ -43,15 +44,15 @@ - + - + - + @@ -64,7 +65,7 @@ - + @@ -91,6 +92,22 @@ + + + 1.0.3 + 1.0.1 + + + stable + stable + + 2011-10-19 + MIT + + * [Fixed] Uninstalling tables with a custom Doctrine_Table contains invalid class name (GH-2) + * [Fixed] Uninstalling/installing plugin base tables with broken class inheritance (GH-3) + + 1.0.2 diff --git a/test/fixtures/project/data/generator/TestTable/default/template/sfDoctrineTableGeneratedTemplate.php b/test/fixtures/project/data/generator/TestTable/default/template/sfDoctrineTableGeneratedTemplate.php index bbe0118..0c430b4 100644 --- a/test/fixtures/project/data/generator/TestTable/default/template/sfDoctrineTableGeneratedTemplate.php +++ b/test/fixtures/project/data/generator/TestTable/default/template/sfDoctrineTableGeneratedTemplate.php @@ -12,7 +12,7 @@ * */ - abstract class BasemodelName ?>Table extends getFormClassToExtend() . PHP_EOL ?> + abstract class BasemodelName ?>Table extends getTableToExtendFrom() . PHP_EOL ?> { /** * @return string diff --git a/test/functional/backend/BuildTableTaskTest.php b/test/functional/backend/BuildTableTaskTest.php index 9641ef1..00468f5 100644 --- a/test/functional/backend/BuildTableTaskTest.php +++ b/test/functional/backend/BuildTableTaskTest.php @@ -25,38 +25,67 @@ $task->run(array(), array('uninstall' => true, 'env' => 'test', 'no-confirmation' => true)); - /** - * Uninstalled model table should be instanceof Doctrine_Table - */ + $t->diag('Checking models'); + $tests = array( "{$libDir}/sfDoctrineGuardPlugin" => array( - 'sfGuardGroup', 'sfGuardGroupPermission', 'sfGuardUserGroup', 'sfGuardUser', + 'sfGuardGroup' => 'PluginsfGuardGroupTable', + 'sfGuardGroupPermission' => 'PluginsfGuardGroupPermissionTable', + 'sfGuardUserGroup' => 'PluginsfGuardUserGroupTable', + 'sfGuardUser' => 'PluginsfGuardUserTable', + ), + $libDir => array( + 'Post' => 'Doctrine_Table_Example', + 'Section' => 'Doctrine_Table_Example', + 'Culture' => 'Doctrine_Table_Example', + 'PostMedia' => 'Doctrine_Table_Example', + 'PostMediaImage' => 'PostMediaTable', ), - $libDir => array('Post', 'Section', 'Culture'), ); foreach ($tests as $path => $models) { - foreach ($models as $className) + $t->diag(sprintf('Entering %s', $path)); + + foreach ($models as $modelName => $extendsFrom) { $t->ok( - ! preg_match( - "/class\s{$className}Table\sextends\sBase{$className}Table/", - file_get_contents("{$path}/{$className}Table.class.php") + preg_match( + "/class\s{$modelName}Table\sextends\s{$extendsFrom}/", + file_get_contents("{$path}/{$modelName}Table.class.php") ), - sprintf('Class "%sTable" is not instance of "Base%sTable"', $className, $className) + sprintf('Class "%sTable" extends from "%s"', $modelName, $extendsFrom) ); } } - $t->ok( - preg_match( - "/class\sPostMediaImageTable\sextends\sPostMediaTable/", - file_get_contents("{$libDir}/PostMediaImageTable.class.php") + $t->diag('Checking plugin tables'); + + $tests = array( + sfConfig::get('sf_plugins_dir') . '/sfDoctrineGuardPlugin/lib/model/doctrine' => array( + 'sfGuardGroup' => 'Doctrine_Table', + 'sfGuardGroupPermission' => 'Doctrine_Table', + 'sfGuardUserGroup' => 'Doctrine_Table', + 'sfGuardUser' => 'Doctrine_Table', ), - 'Class "PostMediaImageTable" has correct extend class "PostMediaTable"' ); + foreach ($tests as $path => $models) + { + $t->diag(sprintf('Entering %s', $path)); + + foreach ($models as $modelName => $extendsFrom) + { + $t->ok( + preg_match( + "/class\sPlugin{$modelName}Table\sextends\s{$extendsFrom}/", + file_get_contents("{$path}/Plugin{$modelName}Table.class.php") + ), + sprintf('Class "%sTable" extends from "%s"', $modelName, $extendsFrom) + ); + } + } + /** * application, env, depth, minified, uninstall, generator-class, no-confirmation */ @@ -79,6 +108,8 @@ ), ); + $t->diag('Checking models'); + foreach ($tests as $path => $models) { foreach ($models as $className => $isEnabledTables) @@ -124,9 +155,50 @@ } } + $t->diag('Checking tables'); + + /** + * Base table "extends X" checks (previously task params) + */ + $tests = array( + "{$libDir}/sfDoctrineGuardPlugin" => array( + # plugin model checks + 'sfGuardPermission' => 'PluginsfGuardPermissionTable', + 'sfGuardUserPermission' => 'PluginsfGuardUserPermissionTable', + 'sfGuardUser' => 'PluginsfGuardUserTable', + ), + $libDir => array( + # default model checks + 'Post' => 'Doctrine_Table_Example', + 'Section' => 'Doctrine_Table_Example', + 'Culture' => 'Doctrine_Table_Example', + 'PostMedia' => 'Doctrine_Table_Example', + # inheritance check + 'PostMediaImage' => 'PostMediaTable', + ), + ); + + foreach ($tests as $path => $models) + { + $t->diag(sprintf('Entering %s', $path)); + + foreach ($models as $model => $extendsFrom) + { + $t->ok( + preg_match( + "/class\sBase{$model}Table\sextends\s{$extendsFrom}/", + file_get_contents("{$path}/base/Base{$model}Table.class.php") + ), + "Class \"Base{$model}Table\" has extend class \"{$extendsFrom}\"" + ); + } + } + + $t->diag('Executing: ./symfony doctrine:build-table --depth=3 --env=test --minified --no-confirmation'); $task->run(array(), array('depth' => 3, 'env' => 'test', 'minified' => true, 'no-confirmation' => true)); + $t->diag('Checking tables'); foreach ($tests as $path => $models) { @@ -156,11 +228,11 @@ } } - - $t->diag('Executing: ./symfony doctrine:build-table --depth=1 --env=test --generator-class=TestTableGenerator --no-confirmation'); $task->run(array(), array('depth' => 1, 'env' => 'test', 'generator-class' => 'TestTableGenerator', 'no-confirmation' => true)); + $t->diag('Checking tables'); + foreach ($tests as $path => $models) { foreach ($models as $className => $isEnabledTables)