Skip to content

Commit

Permalink
Solve probles with installing/uninstalling inherited and plugin models,
Browse files Browse the repository at this point in the history
fix #2, #3
  • Loading branch information
fruit committed Oct 19, 2011
1 parent 99d7530 commit c04324d
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 55 deletions.
1 change: 0 additions & 1 deletion .gitignore
Expand Up @@ -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
2 changes: 1 addition & 1 deletion README.markdown
Expand Up @@ -408,4 +408,4 @@ before existing one.
[sfDoctrineTable] functional/backend/MethodExistanceTest.............ok
[sfDoctrineTable] functional/backend/MethodWhereTest.................ok
All tests successful.
Files=3, Tests=113
Files=3, Tests=138
Expand Up @@ -11,7 +11,7 @@
* @package ##PROJECT_NAME##
* @subpackage table
* @author ##AUTHOR_NAME##
* @version v1.0
* @version v1.1
*
<?php foreach ($this->getPHPDocByPattern('findBy%s') as $column => $method): ?>
* @method <?php print $this->getCollectionClass() ?>|array <?php print $method ?>() <?php print $method ?>(scalar $value, int $hydrationMode = null) Finds records by field "<?php print $column ?>"
Expand Down Expand Up @@ -49,7 +49,7 @@
<?php endforeach; ?>
*
*/
abstract class Base<?php print $this->modelName ?>Table extends <?php print $this->getFormClassToExtend() . PHP_EOL ?>
abstract class Base<?php print $this->modelName ?>Table extends <?php print $this->getTableToExtendFrom() . PHP_EOL ?>
{
/**
* @return string Base table class name used for late-static-bindings purposes
Expand Down
71 changes: 47 additions & 24 deletions lib/generator/sfDoctrineTableGenerator.class.php
Expand Up @@ -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',
Expand Down Expand Up @@ -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))
{
Expand All @@ -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;
Expand All @@ -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
);

Expand All @@ -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
);

Expand Down Expand Up @@ -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;
}
}
7 changes: 7 additions & 0 deletions lib/vendor/Doctrine/Table/Scoped.php
Expand Up @@ -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());

Expand Down
35 changes: 26 additions & 9 deletions package.xml
Expand Up @@ -22,19 +22,20 @@
<email>fruit.dev@gmail.com</email>
<active>yes</active>
</lead>
<date>2011-10-18</date>
<time>22:20:00</time>
<date>2011-10-19</date>
<time>21:50:00</time>
<version>
<release>1.0.2</release>
<api>1.0.0</api>
<release>1.0.3</release>
<api>1.0.1</api>
</version>
<stability>
<release>stable</release>
<api>stable</api>
</stability>
<license uri="http://www.symfony-project.com/license">MIT</license>
<notes>
* [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)
</notes>
<contents>
<dir name="/">
Expand All @@ -43,15 +44,15 @@
<file name="sfDoctrineTablePluginConfiguration.class.php" md5sum="5db32b5f5bd84cd6a7f4dff131e4384c" role="data" />
</dir>
<file name="LICENSE" md5sum="be8ae084e78f9340892a3acd5b41a249" role="data" />
<file name="README" md5sum="9718d1e1d288c871d2651de45d4730cc" role="data" />
<file name="README" md5sum="4c9a26438d27e1a784501590b8ea7053" role="data" />
<dir name="lib">
<dir name="generator">
<file name="sfDoctrineTableGenerator.class.php" md5sum="841dba3ba88a609c2febb91a979f0f93" role="data" />
<file name="sfDoctrineTableGenerator.class.php" md5sum="c9ef5db2510cd00cab6734aceefcfbd4" role="data" />
</dir>
<dir name="vendor">
<dir name="Doctrine">
<dir name="Table">
<file name="Scoped.php" md5sum="69b54a782449695669a5dbfb8d49afbc" role="data" />
<file name="Scoped.php" md5sum="44c027e842e711c91a8a968b4c0ce137" role="data" />
</dir>
</dir>
</dir>
Expand All @@ -64,7 +65,7 @@
<dir name="sfDoctrineTable">
<dir name="default">
<dir name="template">
<file name="sfDoctrineTableGeneratedTemplate.php" md5sum="a867edf111740862fac335017fa72e3d" role="data" />
<file name="sfDoctrineTableGeneratedTemplate.php" md5sum="07486f2ba053886d2670f19c30b7bd45" role="data" />
</dir>
</dir>
</dir>
Expand All @@ -91,6 +92,22 @@
</dependencies>
<phprelease />
<changelog>
<release>
<version>
<release>1.0.3</release>
<api>1.0.1</api>
</version>
<stability>
<release>stable</release>
<api>stable</api>
</stability>
<date>2011-10-19</date>
<license uri="http://www.symfony-project.com/license">MIT</license>
<notes>
* [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)
</notes>
</release>
<release>
<version>
<release>1.0.2</release>
Expand Down
Expand Up @@ -12,7 +12,7 @@
<?php endforeach; ?>
*
*/
abstract class Base<?php print $this->modelName ?>Table extends <?php print $this->getFormClassToExtend() . PHP_EOL ?>
abstract class Base<?php print $this->modelName ?>Table extends <?php print $this->getTableToExtendFrom() . PHP_EOL ?>
{
/**
* @return string
Expand Down

0 comments on commit c04324d

Please sign in to comment.