Skip to content

Commit

Permalink
Stop plugins from executing more than once by making them unique per …
Browse files Browse the repository at this point in the history
…item.
  • Loading branch information
ben-xo committed Aug 24, 2022
1 parent 572148f commit 69fa47a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
24 changes: 20 additions & 4 deletions dir2cast.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public function __call($method, $params)
}

interface Podcast_Helper {
public function id();
public function appendToChannel(DOMElement $d, DOMDocument $doc);
public function appendToItem(DOMElement $d, DOMDocument $doc, RSS_Item $item);
public function addNamespaceTo(DOMElement $d, DOMDocument $doc);
Expand All @@ -135,7 +136,10 @@ public function addNamespaceTo(DOMElement $d, DOMDocument $doc);
*
*/
class getID3_Podcast_Helper implements Podcast_Helper {

public function id()
{
return get_class($this);
}
static $AUTO_SAVE_COVER_ART = false;

public function appendToChannel(DOMElement $d, DOMDocument $doc) { /* nothing */ }
Expand Down Expand Up @@ -206,6 +210,10 @@ public function appendToItem(DOMElement $d, DOMDocument $doc, RSS_Item $item)
*
*/
class Caching_getID3_Podcast_Helper implements Podcast_Helper {
public function id()
{
return get_class($this);
}

protected $wrapped_helper;
protected $cache_dir;
Expand Down Expand Up @@ -267,6 +275,10 @@ protected function saveToCache($filename, Serializable $item) {
}

class Atom_Podcast_Helper extends GetterSetter implements Podcast_Helper {
public function id()
{
return get_class($this);
}

protected $self_link;

Expand Down Expand Up @@ -312,6 +324,10 @@ public function setSelfLink($link)
}

class iTunes_Podcast_Helper extends GetterSetter implements Podcast_Helper {
public function id()
{
return get_class($this);
}

static $ITUNES_SUBTITLE_SUFFIX = '';

Expand Down Expand Up @@ -554,7 +570,7 @@ public function appendToChannel(DOMElement $channel, DOMDocument $doc)

public function addHelper(Podcast_Helper $helper)
{
$this->helpers[] = $helper;
$this->helpers[$helper->id()] = $helper;
return $helper;
}
}
Expand Down Expand Up @@ -978,7 +994,7 @@ abstract class Podcast extends GetterSetter

public function addHelper(Podcast_Helper $helper)
{
$this->helpers[] = $helper;
$this->helpers[$helper->id()] = $helper;

// attach helper to items already added.
// new items will have the helper attached when they are added.
Expand Down Expand Up @@ -1415,7 +1431,7 @@ protected function cache_is_stale($cache_date, $most_recent_modification)
*/
public function renew()
{
touch($this->temp_file); // renew cache file life expectancy
touch($this->temp_file); // renew cache file life expectancy
}

public function uncache()
Expand Down
4 changes: 4 additions & 0 deletions test/Dir_PodcastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,11 @@ public function test_helpers_added_to_found_items()
$mp = $this->newPodcast();

$helper = $this->createMock(Podcast_Helper::class);
$helper->expects($this->atLeastOnce())->method('id')->willReturn('Mock1');
$helper->expects($this->exactly(4))->method('appendToItem');

$helper2 = $this->createMock(Podcast_Helper::class);
$helper2->expects($this->atLeastOnce())->method('id')->willReturn('Mock2');
$helper2->expects($this->exactly(4))->method('appendToItem');

$mp->addHelper($helper);
Expand All @@ -185,9 +187,11 @@ public function test_files_added_to_podcast_obeys_ITEM_COUNT()
$mp = $this->newPodcast();

$helper = $this->createMock(Podcast_Helper::class);
$helper->expects($this->atLeastOnce())->method('id')->willReturn('Mock1');
$helper->expects($this->exactly(2))->method('appendToItem');

$helper2 = $this->createMock(Podcast_Helper::class);
$helper2->expects($this->atLeastOnce())->method('id')->willReturn('Mock2');
$helper2->expects($this->exactly(2))->method('appendToItem');

$mp->addHelper($helper);
Expand Down
6 changes: 6 additions & 0 deletions test/PodcastHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ public static function setUpBeforeClass(): void
public function test_helpers_applied_to_newly_added_items()
{
$helper = $this->createMock(Podcast_Helper::class);
$helper->expects($this->atLeastOnce())->method('id')->willReturn('Mock1');
$helper->expects($this->exactly(2))->method('appendToItem');

$helper2 = $this->createMock(Podcast_Helper::class);
$helper2->expects($this->atLeastOnce())->method('id')->willReturn('Mock2');
$helper2->expects($this->exactly(2))->method('appendToItem');

$mp = new MyPodcast();
Expand All @@ -34,9 +36,11 @@ public function test_helpers_applied_to_newly_added_items()
public function test_helpers_applied_to_already_added_items()
{
$helper = $this->createMock(Podcast_Helper::class);
$helper->expects($this->atLeastOnce())->method('id')->willReturn('Mock1');
$helper->expects($this->exactly(2))->method('appendToItem');

$helper2 = $this->createMock(Podcast_Helper::class);
$helper2->expects($this->atLeastOnce())->method('id')->willReturn('Mock2');
$helper2->expects($this->exactly(2))->method('appendToItem');

$mp = new MyPodcast();
Expand All @@ -57,9 +61,11 @@ public function test_helpers_applied_to_already_added_items()
public function test_helpers_given_opportunity_to_add_namespace()
{
$helper = $this->createMock(Podcast_Helper::class);
$helper->expects($this->atLeastOnce())->method('id')->willReturn('Mock1');
$helper->expects($this->once())->method('addNamespaceTo');

$helper2 = $this->createMock(Podcast_Helper::class);
$helper2->expects($this->atLeastOnce())->method('id')->willReturn('Mock2');
$helper2->expects($this->once())->method('addNamespaceTo');

$mp = new MyPodcast();
Expand Down
4 changes: 2 additions & 2 deletions test/RSS_Item_getID3_Podcast_Helper_AUTO_SAVE_COVERTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function test_auto_save_doesnt_create_spurious_helper_duplication()
mkdir('temp');
$mp = new Cached_Dir_Podcast('.', 'temp');
$mp->init();
$getid3 = $mp->addHelper(new Caching_getID3_Podcast_Helper('tmp', new getID3_Podcast_Helper()));
$getid3 = $mp->addHelper(new Caching_getID3_Podcast_Helper('temp', new getID3_Podcast_Helper()));
$atom = $mp->addHelper(new Atom_Podcast_Helper());
$itunes = $mp->addHelper(new iTunes_Podcast_Helper());
$content = $mp->generate();
Expand All @@ -84,7 +84,7 @@ public function test_auto_save_doesnt_create_spurious_helper_duplication()

$mp = new Cached_Dir_Podcast('.', 'temp');
$mp->init();
$getid3 = $mp->addHelper(new Caching_getID3_Podcast_Helper('tmp', new getID3_Podcast_Helper()));
$getid3 = $mp->addHelper(new Caching_getID3_Podcast_Helper('temp', new getID3_Podcast_Helper()));
$atom = $mp->addHelper(new Atom_Podcast_Helper());
$itunes = $mp->addHelper(new iTunes_Podcast_Helper());
$content = $mp->generate();
Expand Down

0 comments on commit 69fa47a

Please sign in to comment.