diff --git a/src/Modules/Generators/TaxonomyArchiveGenerator.php b/src/Modules/Generators/TaxonomyArchiveGenerator.php index c758548..72a205a 100644 --- a/src/Modules/Generators/TaxonomyArchiveGenerator.php +++ b/src/Modules/Generators/TaxonomyArchiveGenerator.php @@ -3,6 +3,7 @@ namespace Tapestry\Modules\Generators; use Tapestry\Entities\Project; +use Tapestry\Modules\Source\ClonedSource; use Tapestry\Modules\Source\SourceInterface; /** @@ -48,12 +49,42 @@ class TaxonomyArchiveGenerator extends AbstractGenerator implements GeneratorInt * * @param Project $project * @return array|SourceInterface[] + * @throws \Exception */ public function generate(Project $project): array { + $generated = []; + if (! $uses = $this->source->getData('use')){ + return [$this->source]; + } + foreach ($uses as $use){ - // TODO: Implement generate() method. + if (! $data = $this->source->getData($use.'_items')) { + continue; + } + + $classifications = array_keys($data); + + foreach ($data as $taxonomyName => $files) { + $newFile = new ClonedSource($this->source); + + // @todo refactor so that a TaxonomyViewHelper is injected? + $newFile->setData([ + 'generator' => array_filter($this->source->getData('generator'), function ($value) { + return $value !== 'TaxonomyArchiveGenerator'; + }), + 'taxonomyName' => $taxonomyName, + $use.'_items' => $files, + $use => $classifications, + ]); + $newFile->setUid($newFile->getUid().'_'.$taxonomyName); + $newFile->setOverloaded('filename', $taxonomyName); + array_push($generated, $newFile); + } + } + + return $generated; } } \ No newline at end of file diff --git a/tests/Unit/ContentGeneratorsNTest.php b/tests/Unit/ContentGeneratorsNTest.php index d37d8a5..5bcda3e 100644 --- a/tests/Unit/ContentGeneratorsNTest.php +++ b/tests/Unit/ContentGeneratorsNTest.php @@ -16,9 +16,11 @@ use Tapestry\Modules\Generators\ContentGeneratorFactory; use Tapestry\Modules\Generators\Generator; use Tapestry\Modules\Generators\PaginationGenerator; +use Tapestry\Modules\Generators\TaxonomyArchiveGenerator; use Tapestry\Modules\Source\ClonedSource; use Tapestry\Modules\Source\MemorySource; use Tapestry\Steps\LoadContentTypes; +use Tapestry\Steps\ParseContentTypes; use Tapestry\Tests\TestCase; class ContentGeneratorsNTest extends TestCase @@ -251,9 +253,17 @@ public function testTaxonomyArchiveGenerator() } unset($x,$y); - $n = 1; + $t = new MemorySource('template', '', 'template.phtml', 'phtml', '/', '/template.phtml', ['date' => time(), 'generator' => ['TaxonomyArchiveGenerator'], 'use' => ['blog_tags']]); + $contentTypes->bucketSource($t); - $this->markTestIncomplete('This test has not been implemented yet'); + (new ParseContentTypes())($project, new NullOutput()); + + $generator = new TaxonomyArchiveGenerator(); + $generator->setSource($t); + $result = $generator->generate($project); + + $this->assertTrue(is_array($result)); + $this->assertCount(2, $result); } // @todo add test to check this modifies the graph