Skip to content

Commit

Permalink
Major bug. By calling unexisting method from the action with disabled…
Browse files Browse the repository at this point in the history
… sf_cache you got no Exceptions thrown. Added tests for this bug.
  • Loading branch information
fruit committed Feb 12, 2011
1 parent b20d0db commit cedcd23
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 116 deletions.
47 changes: 16 additions & 31 deletions lib/util/sfCacheTaggingToolkit.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ class sfCacheTaggingToolkit
const TEMPLATE_NAME = 'Cachetaggable';

/**
* @throws sfCacheDisabledException when "sf_cache" is OFF
* @throws sfInitializationException if context is not initialized
* @throws sfConfigurationException on plugin configuration issues
* @throws sfCacheDisabledException when "sf_cache" is OFF
* @throws sfCacheMissingContextException if context is not initialized
* @throws sfConfigurationException on plugin configuration issues
*
* @return sfCacheTagging
*/
Expand Down Expand Up @@ -113,7 +113,8 @@ public static function getModelTagNameSeparator ()
* Format passed tags to the array
*
* @param mixed $tags array|Doctrine_Collection_Cachetaggable|
* Doctrine_Record|ArrayIterator|Iterator
* Doctrine_Record|ArrayIterator|
* IteratorAggregate|Iterator
* @throws InvalidArgumentException
* @return array
*/
Expand Down Expand Up @@ -189,42 +190,26 @@ public static function formatTags ($tags)
*/
public static function listenOnComponentMethodNotFoundEvent (sfEvent $event)
{
$event->setProcessed(true);
$callable = array(new sfViewCacheTagManagerBridge(), $event['method']);

try
{
$taggingCache = sfCacheTaggingToolkit::getTaggingCache();
$value = call_user_func_array($callable, $event['arguments']);
$event->setReturnValue($value);

$event->setProcessed(true);
}
catch (sfCacheDisabledException $e)
catch (BadMethodCallException $e)
{
sfCacheTaggingToolkit::notifyApplicationLog(
__CLASS__, $e->getMessage(), sfLogger::NOTICE
);

return;
$event->setProcessed(false);
}
catch (sfConfigurationException $e)
catch (sfException $e)
{
sfCacheTaggingToolkit::notifyApplicationLog(
__CLASS__, $e->getMessage(), sfLogger::WARNING
);

return;
}
$event->setProcessed(true);

try
{
$callable = array(
new sfViewCacheTagManagerBridge($taggingCache), $event['method']
sfCacheTaggingToolkit::notifyApplicationLog(
__CLASS__, $e->getMessage(), sfLogger::NOTICE
);

$event
->setReturnValue(call_user_func_array($callable, $event['arguments']))
;
}
catch (BadMethodCallException $e)
{
$event->setProcessed(false);
}
}

Expand Down
88 changes: 53 additions & 35 deletions lib/util/sfViewCacheTagManagerBridge.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,43 @@
*/
class sfViewCacheTagManagerBridge
{
/**
* @var sfTaggingCache
*/
protected $taggingCache = null;

/**
* @param sfTaggingCache $taggingCache
*/
public function __construct (sfTaggingCache $taggingCache)
{
$this->taggingCache = $taggingCache;
}
protected $allowedCallMethods = array(
sfViewCacheTagManager::NAMESPACE_ACTION => array(
'setActionTags',
'addActionTags',
'getActionTags',
'removeActionTags',
'setActionTag',
'hasActionTag',
'removeActionTag',
),
sfViewCacheTagManager::NAMESPACE_PARTIAL => array(
'setPartialTags',
'addPartialTags',
'getPartialTags',
'removePartialTags',
'setPartialTag',
'hasPartialTag',
'removePartialTag',
),
sfViewCacheTagManager::NAMESPACE_PAGE => array(
'setPageTags',
'addPageTags',
'getPageTags',
'removePageTags',
'setPageTag',
'hasPageTag',
'removePageTag',
),
);

/**
* @see sfViewCacheTagManager::getTaggingCache
* @return sfTaggingCache
*/
protected function getTaggingCache ()
{
return $this->taggingCache;
return sfCacheTaggingToolkit::getTaggingCache();
}

/**
Expand All @@ -67,20 +84,27 @@ protected function getTaggingCache ()
* @throws BadMethodCallException
* @return void|array|boolean
*/
public function __call ($method, $arguments)
public function __call ($method, $arguments)
{
$orBlock = implode('|', sfViewCacheTagManager::getNamespaces());
$pattern = sprintf('/\w+(%s)Tags?/', $orBlock);
$contentNamespace = null;

if (! preg_match($pattern, $method, $matches))
foreach ($this->allowedCallMethods as $namespace => $methods)
{
if (in_array($method, $methods))
{
$contentNamespace = $namespace;

break;
}
}

if (null === $contentNamespace)
{
throw new BadMethodCallException(sprintf(
'Method "%s" does not exists in %s', $method, get_class($this)
));
}

$contentNamespace = $matches[1];

array_push($arguments, $contentNamespace);

$nsLength = strlen($contentNamespace);
Expand All @@ -90,28 +114,22 @@ public function __call ($method, $arguments)
$method, 'Content', strpos($method, $contentNamespace), $nsLength
);

try
{
$callable = new sfCallableArray(array(
$this->getTaggingCache()->getContentTagHandler(),
$contentAbstractMethod
));
$callable = new sfCallableArray(array(
$this->getTaggingCache()->getContentTagHandler(),
$contentAbstractMethod
));

return $callable->callArray($arguments);
}
catch (sfException $e)
{
throw new BadMethodCallException($e->getMessage());
}
return $callable->callArray($arguments);
}

/**
* Appends tags to doctrine result cache
*
* @param mixed $tags
* @param Doctrine_Query|string $q Doctrine_Query or string
* @param array $params params from $q->getParams()
*
* @param array $params params from $q->getParams()
* @return sfViewCacheTagManagerBridge
*/
public function addDoctrineTags ($tags, $q, array $params = array())
{
Expand All @@ -130,9 +148,9 @@ public function addDoctrineTags ($tags, $q, array $params = array())
throw new InvalidArgumentException('Invalid arguments are passed');
}

$this->getTaggingCache()->addTagsToCache(
$key, sfCacheTaggingToolkit::formatTags($tags)
);
$tags = sfCacheTaggingToolkit::formatTags($tags);

$this->getTaggingCache()->addTagsToCache($key, $tags);

return $this;
}
Expand Down
10 changes: 0 additions & 10 deletions test/functional/frontend/DoctrineTemplateCachetaggableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,6 @@

$t->is(count($tags = $comments->getTags()), 9);

$tagsKeys = array_keys($tags);

$expected = array('BlogPostComment');
for ($i = 1; $i <= 8; $expected[] = "BlogPostComment{$separator}{$i}", $i++);

sort($expected);
sort($tagsKeys);

$t->is($tagsKeys, $expected);

$commentsCnt = BlogPostCommentTable::getInstance()->count();
$postsCnt = BlogPostTable::getInstance()->count();
$voteCnt = BlogPostVoteTable::getInstance()->count();
Expand Down
1 change: 0 additions & 1 deletion test/functional/frontend/sfCacheTaggingToolkitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public function getIterator()
try
{
$p = new BlogPost();
// $p->setTitle('Blog post title "AAAA"');
$p->save();

sfCacheTaggingToolkit::formatTags($p);
Expand Down
Loading

0 comments on commit cedcd23

Please sign in to comment.