diff --git a/Autodocs.php b/Autodocs.php deleted file mode 100644 index dad86db..0000000 --- a/Autodocs.php +++ /dev/null @@ -1,10 +0,0 @@ -=8" + }, + "require-dev": { + "friendsofphp/php-cs-fixer": "^3.5", + "pestphp/pest": "^1.0", + "phpunit/phpunit": "^9.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Minicli\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Dummy template generator", + "homepage": "https://github.com/minicli/stencil", + "keywords": [ + "minicli", + "template" + ], + "support": { + "issues": "https://github.com/minicli/stencil/issues", + "source": "https://github.com/minicli/stencil/tree/0.2.1" + }, + "time": "2023-07-13T13:44:49+00:00" } ], "packages-dev": [ diff --git a/src/DataFeed/DataFeedInterface.php b/src/DataFeed/DataFeedInterface.php new file mode 100644 index 0000000..f20f579 --- /dev/null +++ b/src/DataFeed/DataFeedInterface.php @@ -0,0 +1,9 @@ +identifier = $identifier; - } - /** * @throws NotFoundException */ @@ -44,8 +36,8 @@ public function loadFromArray(array $data): void $this->data = json_encode($data); } - public function save(string $filePath): void + public function save(string $path): void { - file_put_contents($filePath, $this->data); + file_put_contents($path, $this->data); } } diff --git a/src/Mark/Mark.php b/src/Mark.php similarity index 98% rename from src/Mark/Mark.php rename to src/Mark.php index 2d85853..a73caf2 100644 --- a/src/Mark/Mark.php +++ b/src/Mark.php @@ -1,6 +1,6 @@ loadFromArray([ + $this->dataFeed = new JsonDataFeed(); + $this->dataFeed->loadFromArray([ 'title' => 'example', 'description' => 'description' ]); - $this->registerDataFeed($datafeed); } public function getName(): string @@ -23,12 +23,7 @@ public function getName(): string public function getContent(): string { - $data = $this->getDataFeed('example'); - if ($data == null) { - return ""; - } - - return $data->json['title'] . ' - ' . $data->json['description']; + return $this->dataFeed->json['title'] . ' - ' . $this->dataFeed->json['description']; } public function getSavePath(): string diff --git a/src/Page/ReferencePage.php b/src/Page/ReferencePage.php index e14657f..3846475 100644 --- a/src/Page/ReferencePage.php +++ b/src/Page/ReferencePage.php @@ -2,31 +2,13 @@ namespace Autodocs\Page; -use Autodocs\DataFeed; +use Autodocs\Service\AutodocsService; -abstract class ReferencePage +abstract class ReferencePage implements ReferencePageInterface { - public array $dataFeeds = []; - - public function registerDataFeed(DataFeed $dataFeed): void - { - $this->dataFeeds[] = $dataFeed; - } - - public function getDataFeed(string $identifier): ?DataFeed + public AutodocsService $autodocs; + public function __construct(AutodocsService $autodocs) { - foreach ($this->dataFeeds as $dataFeed) { - if ($dataFeed->identifier === $identifier) { - return $dataFeed; - } - } + $this->autodocs = $autodocs; } - - abstract public function loadData(): void; - - abstract public function getName(): string; - - abstract public function getSavePath(): string; - - abstract public function getContent(): string; -} \ No newline at end of file +} diff --git a/src/Page/ReferencePageInterface.php b/src/Page/ReferencePageInterface.php new file mode 100644 index 0000000..7a09b65 --- /dev/null +++ b/src/Page/ReferencePageInterface.php @@ -0,0 +1,17 @@ +loadFile(__DIR__ . '/../../tests/Resources/images-tags.json'); - $this->registerDataFeed($datafeed); + // } public function getName(): string @@ -21,12 +19,8 @@ public function getName(): string public function getContent(): string { $content = ""; - $data = $this->getDataFeed('test'); - if ($data == null) { - return ""; - } - - foreach ($data->json as $item) { + $dataFeed = $this->autodocs->getDataFeed('images-tags.json'); + foreach ($dataFeed->json as $item) { $content .= "Image Name: " . $item['repo']['name'] . "\n"; } diff --git a/src/PageBuilder.php b/src/PageBuilder.php index c8281de..721a887 100644 --- a/src/PageBuilder.php +++ b/src/PageBuilder.php @@ -2,43 +2,12 @@ namespace Autodocs; -use Autodocs\Page\ReferencePage; +use Autodocs\Page\ReferencePageInterface; +use Autodocs\Storage\FileStorage; class PageBuilder { - public Storage $storage; - public array $referencePages = []; - - public function __construct(Storage $storage) - { - $this->storage = $storage; - } - - public function registerPages(array $referencePages): void - { - foreach ($referencePages as $referencePage) { - $this->registerPage($referencePage); - } - } - - public function registerPage(ReferencePage $page): void - { - $page->loadData(); - $this->referencePages[] = $page; - } - - public function buildPages(string $pages="all"): void - { - $buildPages = explode(",", $pages); - /** @var ReferencePage $referencePage */ - foreach ($this->referencePages as $referencePage) { - if ($pages === "all" || in_array($referencePage->getName(), $buildPages)) { - $this->storage->saveFile($referencePage->getSavePath(), $this->buildPage($referencePage)); - } - } - } - - public function buildPage(ReferencePage $page): string + public function buildPage(ReferencePageInterface $page): string { return $page->getContent(); } diff --git a/src/Service/AutodocsService.php b/src/Service/AutodocsService.php new file mode 100644 index 0000000..18cf62b --- /dev/null +++ b/src/Service/AutodocsService.php @@ -0,0 +1,89 @@ +config = $app->config->autodocs; + $this->storage = new FileStorage(); + $this->stencil = new Stencil($this->config['templates_dir']); + if (isset($this->config['storage'])) { + $this->storage = new $this->config['storage']; + } + + $this->builder = new PageBuilder(); + foreach ($this->config['pages'] as $page) { + $page = new $page($this); + $this->registerPage($page); + } + + if (isset($this->config['cache_dir'])) { + foreach (glob($this->config['cache_dir'] . '/*.json') as $jsonCache) { + $this->registerDataFeed(basename($jsonCache), new JsonDataFeed()); + } + } + } + protected function registerDataFeed(string $identifier, JsonDataFeed $dataFeed): void + { + $this->dataFeeds[$identifier] = $dataFeed; + } + + public function getDataFeed(string $identifier): ?JsonDataFeed + { + if (isset($this->dataFeeds[$identifier])) { + /** @var JsonDataFeed $dataFeed */ + $dataFeed = $this->dataFeeds[$identifier]; + $dataFeed->loadFile($this->config['cache_dir'] . '/' . $identifier); + return $dataFeed; + } + return null; + } + + public function getDataFeedsList(string $filter = ""): array + { + $cachedFiles = array_keys($this->dataFeeds); + return array_filter($cachedFiles, function($var) use ($filter) { + return str_starts_with($var, $filter); + }); + } + + public function registerPage(ReferencePageInterface $page): void + { + $this->referencePages[] = $page; + } + + public function buildPages(string $pages="all", array $parameters = []): void + { + $buildPages = explode(",", $pages); + /** @var ReferencePageInterface $referencePage */ + foreach ($this->referencePages as $referencePage) { + if ($pages === "all" || in_array($referencePage->getName(), $buildPages)) { + $referencePage->loadData($parameters); + $savePath = $this->config['output'] . '/' . $referencePage->getSavePath(); + if (!is_dir(dirname($savePath))) { + mkdir(dirname($savePath), 0755, true); + } + + $this->storage->saveFile($savePath, $this->builder->buildPage($referencePage)); + } + } + } +} diff --git a/src/Storage.php b/src/Storage/FileStorage.php similarity index 64% rename from src/Storage.php rename to src/Storage/FileStorage.php index 87160c5..f63170b 100644 --- a/src/Storage.php +++ b/src/Storage/FileStorage.php @@ -1,8 +1,8 @@ extend('toBeOne', function () { return $this->toBe(1); }); @@ -41,5 +44,21 @@ function getAutodocs() { + $config = [ + 'autodocs' => [ + 'images_sources' => __DIR__ . '/../workdir/sources', + 'pages' => [ + \Autodocs\Page\ExamplePage::class, + \Autodocs\Page\TestPage::class + ], + 'output' => __DIR__ . '/../var/output', + 'cache_dir' => __DIR__ . '/Resources', + 'templates_dir' => __DIR__ . '/Resources' + ] + ]; + + $autodocs = new AutodocsService(); + $autodocs->load(new App($config)); + return $autodocs; } \ No newline at end of file diff --git a/tests/TestCase.php b/tests/TestCase.php deleted file mode 100644 index cfb05b6..0000000 --- a/tests/TestCase.php +++ /dev/null @@ -1,10 +0,0 @@ -assertCount(2, $autodocs->referencePages); + $this->assertNotEmpty($autodocs->dataFeeds); + $this->assertInstanceOf(\Autodocs\DataFeed\JsonDataFeed::class, $autodocs->dataFeeds['images-tags.json']); + $this->assertEmpty($autodocs->dataFeeds['images-tags.json']->json); + $data = $autodocs->getDataFeed('images-tags.json'); + + $this->assertNotEmpty($data->json); +}); + +it('builds reference pages', function () { + $autodocs = getAutodocs(); + $storage = Mockery::mock(FileStorage::class); + $storage->shouldReceive('saveFile'); + $autodocs->storage = $storage; + + $this->assertCount(2, $autodocs->referencePages); + $autodocs->buildPages(); +}); + +it('skips building when page is not listed', function () { + $autodocs = getAutodocs(); + $storage = Mockery::mock(FileStorage::class); + $storage->shouldNotReceive('saveFile'); + $autodocs->storage = $storage; + + $autodocs->buildPages('page2,page3'); +}); + +it('builds only designated page', function () { + $autodocs = getAutodocs(); + $storage = Mockery::mock(FileStorage::class); + $storage->shouldReceive('saveFile')->once(); + $autodocs->storage = $storage; + + $autodocs->buildPages('example'); +}); diff --git a/tests/Unit/DataFeedTest.php b/tests/Unit/DataFeedTest.php index eb3b6ab..e483b01 100644 --- a/tests/Unit/DataFeedTest.php +++ b/tests/Unit/DataFeedTest.php @@ -1,10 +1,10 @@ loadFile(__DIR__ . '/../Resources/images-tags.json'); $this->assertNotEmpty($datafeed->json); diff --git a/tests/Unit/PageBuilderTest.php b/tests/Unit/PageBuilderTest.php deleted file mode 100644 index 79f72f2..0000000 --- a/tests/Unit/PageBuilderTest.php +++ /dev/null @@ -1,54 +0,0 @@ -loadData(); - - $storage = Mockery::mock(Storage::class); - $storage->shouldReceive('saveFile'); - - $builder = new PageBuilder($storage); - $builder->registerPage($page); - - $this->assertCount(1, $builder->referencePages); - - $builder->buildPages(); -}); - -it('skips building when page is not listed', function () { - $page = new ExamplePage(); - $page->loadData(); - - $storage = Mockery::mock(Storage::class); - $storage->shouldNotReceive('saveFile'); - - $builder = new PageBuilder($storage); - $builder->registerPage($page); - - $this->assertCount(1, $builder->referencePages); - - $builder->buildPages('page2,page3'); -}); - -it('builds only designated page', function () { - $page1 = new ExamplePage(); - $page1->loadData(); - - $page2 = new TestPage(); - $page2->loadData(); - - $storage = Mockery::mock(Storage::class); - $storage->shouldReceive('saveFile')->once(); - - $builder = new PageBuilder($storage); - $builder->registerPages([$page1, $page2]); - - $this->assertCount(2, $builder->referencePages); - - $builder->buildPages('example'); -}); diff --git a/tests/Unit/ReferencePageTest.php b/tests/Unit/ReferencePageTest.php index 35b2e12..f380cc6 100644 --- a/tests/Unit/ReferencePageTest.php +++ b/tests/Unit/ReferencePageTest.php @@ -1,9 +1,12 @@ loadData(); $content = $page->getContent(); diff --git a/var/.gitignore b/var/.gitignore new file mode 100644 index 0000000..c96a04f --- /dev/null +++ b/var/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file