From af3edb76b0d4305da6255e1dd5b357d770617dde Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 24 Jun 2022 22:19:01 +0200 Subject: [PATCH 01/35] Create DataCollection.php --- .../src/Modules/DataCollections/DataCollection.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 packages/framework/src/Modules/DataCollections/DataCollection.php diff --git a/packages/framework/src/Modules/DataCollections/DataCollection.php b/packages/framework/src/Modules/DataCollections/DataCollection.php new file mode 100644 index 00000000000..6b1100cd761 --- /dev/null +++ b/packages/framework/src/Modules/DataCollections/DataCollection.php @@ -0,0 +1,12 @@ + Date: Fri, 24 Jun 2022 22:19:04 +0200 Subject: [PATCH 02/35] Create DataCollectionServiceProvider.php --- .../DataCollectionServiceProvider.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 packages/framework/src/Modules/DataCollections/DataCollectionServiceProvider.php diff --git a/packages/framework/src/Modules/DataCollections/DataCollectionServiceProvider.php b/packages/framework/src/Modules/DataCollections/DataCollectionServiceProvider.php new file mode 100644 index 00000000000..07b511293cf --- /dev/null +++ b/packages/framework/src/Modules/DataCollections/DataCollectionServiceProvider.php @@ -0,0 +1,18 @@ + Date: Fri, 24 Jun 2022 22:23:54 +0200 Subject: [PATCH 03/35] Register the class alias --- .../DataCollections/DataCollectionServiceProvider.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Modules/DataCollections/DataCollectionServiceProvider.php b/packages/framework/src/Modules/DataCollections/DataCollectionServiceProvider.php index 07b511293cf..e4276ecbabf 100644 --- a/packages/framework/src/Modules/DataCollections/DataCollectionServiceProvider.php +++ b/packages/framework/src/Modules/DataCollections/DataCollectionServiceProvider.php @@ -2,13 +2,17 @@ namespace Hyde\Framework\Modules\DataCollections; +use Illuminate\Foundation\AliasLoader; use Illuminate\Support\ServiceProvider; class DataCollectionServiceProvider extends ServiceProvider { public function register() { - // + // Register the class alias + AliasLoader::getInstance()->alias( + 'Collection', DataCollection::class + ); } public function boot() From 7f826f517fb14413807ccb4e5daca785f486083e Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 24 Jun 2022 22:25:53 +0200 Subject: [PATCH 04/35] Create the _data directory if it doesn't exist --- .../DataCollections/DataCollectionServiceProvider.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Modules/DataCollections/DataCollectionServiceProvider.php b/packages/framework/src/Modules/DataCollections/DataCollectionServiceProvider.php index e4276ecbabf..e6a7e92804d 100644 --- a/packages/framework/src/Modules/DataCollections/DataCollectionServiceProvider.php +++ b/packages/framework/src/Modules/DataCollections/DataCollectionServiceProvider.php @@ -17,6 +17,9 @@ public function register() public function boot() { - // + // Create the _data directory if it doesn't exist + if (!is_dir(Hyde::path('_data'))) { + mkdir(Hyde::path('_data')); + } } } From 14650e44d844849fbfee098a3e9407fc99ef06bf Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 24 Jun 2022 22:26:36 +0200 Subject: [PATCH 05/35] Register module service providers --- packages/framework/src/HydeServiceProvider.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/framework/src/HydeServiceProvider.php b/packages/framework/src/HydeServiceProvider.php index 0a4c751588b..f176638e664 100644 --- a/packages/framework/src/HydeServiceProvider.php +++ b/packages/framework/src/HydeServiceProvider.php @@ -88,6 +88,8 @@ function () { Commands\HydePackageDiscoverCommand::class, ]); + + $this->registerModuleServiceProviders(); } /** @@ -136,4 +138,12 @@ protected function storeCompiledSiteIn(string $directory): void { StaticPageBuilder::$outputPath = $directory; } + + /** + * Register module service providers. + */ + protected function registerModuleServiceProviders(): void + { + $this->app->register(Modules\DataCollections\DataCollectionServiceProvider::class); + } } From c2fdf4bec49b6358e57d84b1f1f02ef1d081fb1b Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 24 Jun 2022 22:26:53 +0200 Subject: [PATCH 06/35] Add todo: Make modules configurable --- packages/framework/src/HydeServiceProvider.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/framework/src/HydeServiceProvider.php b/packages/framework/src/HydeServiceProvider.php index f176638e664..c190b79816b 100644 --- a/packages/framework/src/HydeServiceProvider.php +++ b/packages/framework/src/HydeServiceProvider.php @@ -141,6 +141,8 @@ protected function storeCompiledSiteIn(string $directory): void /** * Register module service providers. + * + * @todo Make modules configurable. */ protected function registerModuleServiceProviders(): void { From b79c96c5628d37987431347be808cabea3a51a02 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 24 Jun 2022 22:28:17 +0200 Subject: [PATCH 07/35] Import the Hyde class --- .../Modules/DataCollections/DataCollectionServiceProvider.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Modules/DataCollections/DataCollectionServiceProvider.php b/packages/framework/src/Modules/DataCollections/DataCollectionServiceProvider.php index e6a7e92804d..85a0ff23695 100644 --- a/packages/framework/src/Modules/DataCollections/DataCollectionServiceProvider.php +++ b/packages/framework/src/Modules/DataCollections/DataCollectionServiceProvider.php @@ -2,8 +2,9 @@ namespace Hyde\Framework\Modules\DataCollections; -use Illuminate\Foundation\AliasLoader; use Illuminate\Support\ServiceProvider; +use Illuminate\Foundation\AliasLoader; +use Hyde\Framework\Hyde; class DataCollectionServiceProvider extends ServiceProvider { From f56bb0a9097b14662c77abd141638c7042192506 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 24 Jun 2022 22:33:32 +0200 Subject: [PATCH 08/35] Add base Markdown collection facade --- .../DataCollections/DataCollection.php | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Modules/DataCollections/DataCollection.php b/packages/framework/src/Modules/DataCollections/DataCollection.php index 6b1100cd761..a9897ccb015 100644 --- a/packages/framework/src/Modules/DataCollections/DataCollection.php +++ b/packages/framework/src/Modules/DataCollections/DataCollection.php @@ -2,11 +2,29 @@ namespace Hyde\Framework\Modules\DataCollections; +use Hyde\Framework\Hyde; +use Hyde\Framework\Services\MarkdownFileService; +use Illuminate\Support\Collection as BaseCollection; + /** * Provides a facade for generating on-the-fly Laravel Collections * from static data files such as Markdown components and YAML files. */ class DataCollection { - // + /** + * Get a collection of Markdown documents in the _data/<$key> directory. + * Each Markdown file will be parsed into a MarkdownDocument with front matter. + */ + public static function markdown(string $key): BaseCollection + { + $files = glob(Hyde::path('_data/' . $key . '/*.md')); + $collection = new BaseCollection(); + foreach ($files as $file) { + $collection->push( + (new MarkdownFileService($file))->get() + ); + } + return $collection; + } } From bb79192bd10e8583b2a67aadd11fccfa97c12c2f Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 24 Jun 2022 22:33:52 +0200 Subject: [PATCH 09/35] Track execution time --- .../src/Modules/DataCollections/DataCollection.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/framework/src/Modules/DataCollections/DataCollection.php b/packages/framework/src/Modules/DataCollections/DataCollection.php index a9897ccb015..6630646cfd3 100644 --- a/packages/framework/src/Modules/DataCollections/DataCollection.php +++ b/packages/framework/src/Modules/DataCollections/DataCollection.php @@ -18,6 +18,7 @@ class DataCollection */ public static function markdown(string $key): BaseCollection { + $time_start = microtime(true); $files = glob(Hyde::path('_data/' . $key . '/*.md')); $collection = new BaseCollection(); foreach ($files as $file) { @@ -25,6 +26,12 @@ public static function markdown(string $key): BaseCollection (new MarkdownFileService($file))->get() ); } + $collection->parseTimeInMs = static::getExecutionTime($time_start); return $collection; } + + protected static function getExecutionTime(float $time_start): float + { + return round((microtime(true) - $time_start) * 1000, 2); + } } From 361ad367a9f96a567b27d9bfba71406c538e7eb2 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 24 Jun 2022 22:35:54 +0200 Subject: [PATCH 10/35] Add collection key and inferred name to class --- .../framework/src/Modules/DataCollections/DataCollection.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/framework/src/Modules/DataCollections/DataCollection.php b/packages/framework/src/Modules/DataCollections/DataCollection.php index 6630646cfd3..ea0ce3045cb 100644 --- a/packages/framework/src/Modules/DataCollections/DataCollection.php +++ b/packages/framework/src/Modules/DataCollections/DataCollection.php @@ -21,6 +21,8 @@ public static function markdown(string $key): BaseCollection $time_start = microtime(true); $files = glob(Hyde::path('_data/' . $key . '/*.md')); $collection = new BaseCollection(); + $collection->key = $key; + $collection->name = Hyde::titleFromSlug($key); foreach ($files as $file) { $collection->push( (new MarkdownFileService($file))->get() From 713b411786e9fde5e2e9204307cd1495253fabd9 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 24 Jun 2022 22:36:37 +0200 Subject: [PATCH 11/35] Inline unnecessary local variable --- .../framework/src/Modules/DataCollections/DataCollection.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/framework/src/Modules/DataCollections/DataCollection.php b/packages/framework/src/Modules/DataCollections/DataCollection.php index ea0ce3045cb..572f79a8f6b 100644 --- a/packages/framework/src/Modules/DataCollections/DataCollection.php +++ b/packages/framework/src/Modules/DataCollections/DataCollection.php @@ -19,11 +19,10 @@ class DataCollection public static function markdown(string $key): BaseCollection { $time_start = microtime(true); - $files = glob(Hyde::path('_data/' . $key . '/*.md')); $collection = new BaseCollection(); $collection->key = $key; $collection->name = Hyde::titleFromSlug($key); - foreach ($files as $file) { + foreach (glob(Hyde::path('_data/' . $key . '/*.md')) as $file) { $collection->push( (new MarkdownFileService($file))->get() ); From e19f8230395d1276a4546c261c44869de4cb1d92 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 24 Jun 2022 22:38:56 +0200 Subject: [PATCH 12/35] Bind the data path to class property to make it configurable --- .../framework/src/Modules/DataCollections/DataCollection.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Modules/DataCollections/DataCollection.php b/packages/framework/src/Modules/DataCollections/DataCollection.php index 572f79a8f6b..a9a70f92562 100644 --- a/packages/framework/src/Modules/DataCollections/DataCollection.php +++ b/packages/framework/src/Modules/DataCollections/DataCollection.php @@ -12,6 +12,8 @@ */ class DataCollection { + public static string $dataPath = '_data'; + /** * Get a collection of Markdown documents in the _data/<$key> directory. * Each Markdown file will be parsed into a MarkdownDocument with front matter. @@ -22,7 +24,7 @@ public static function markdown(string $key): BaseCollection $collection = new BaseCollection(); $collection->key = $key; $collection->name = Hyde::titleFromSlug($key); - foreach (glob(Hyde::path('_data/' . $key . '/*.md')) as $file) { + foreach (glob(Hyde::path(static::$dataPath . '/' . $key . '/*.md')) as $file) { $collection->push( (new MarkdownFileService($file))->get() ); From 4769cfb1c2e3c2b05b305e6c43e037f2f0e70cfe Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 24 Jun 2022 22:40:00 +0200 Subject: [PATCH 13/35] Get the preferred Markdown file extension from base model class --- .../framework/src/Modules/DataCollections/DataCollection.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Modules/DataCollections/DataCollection.php b/packages/framework/src/Modules/DataCollections/DataCollection.php index a9a70f92562..8d3d4b24dcd 100644 --- a/packages/framework/src/Modules/DataCollections/DataCollection.php +++ b/packages/framework/src/Modules/DataCollections/DataCollection.php @@ -3,6 +3,7 @@ namespace Hyde\Framework\Modules\DataCollections; use Hyde\Framework\Hyde; +use Hyde\Framework\Models\MarkdownDocument; use Hyde\Framework\Services\MarkdownFileService; use Illuminate\Support\Collection as BaseCollection; @@ -24,7 +25,7 @@ public static function markdown(string $key): BaseCollection $collection = new BaseCollection(); $collection->key = $key; $collection->name = Hyde::titleFromSlug($key); - foreach (glob(Hyde::path(static::$dataPath . '/' . $key . '/*.md')) as $file) { + foreach (glob(Hyde::path(static::$dataPath . '/' . $key . '/*' . MarkdownDocument::$fileExtension)) as $file) { $collection->push( (new MarkdownFileService($file))->get() ); From 21acf6fcc259b4f1c3edf197dd9075225a0d31fb Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 24 Jun 2022 22:42:23 +0200 Subject: [PATCH 14/35] Move the facade to dedicated namespaced class --- .../DataCollections/DataCollection.php | 35 +--------------- .../DataCollectionServiceProvider.php | 2 +- .../DataCollections/Facades/Collection.php | 41 +++++++++++++++++++ 3 files changed, 43 insertions(+), 35 deletions(-) create mode 100644 packages/framework/src/Modules/DataCollections/Facades/Collection.php diff --git a/packages/framework/src/Modules/DataCollections/DataCollection.php b/packages/framework/src/Modules/DataCollections/DataCollection.php index 8d3d4b24dcd..8121076f974 100644 --- a/packages/framework/src/Modules/DataCollections/DataCollection.php +++ b/packages/framework/src/Modules/DataCollections/DataCollection.php @@ -2,40 +2,7 @@ namespace Hyde\Framework\Modules\DataCollections; -use Hyde\Framework\Hyde; -use Hyde\Framework\Models\MarkdownDocument; -use Hyde\Framework\Services\MarkdownFileService; -use Illuminate\Support\Collection as BaseCollection; - -/** - * Provides a facade for generating on-the-fly Laravel Collections - * from static data files such as Markdown components and YAML files. - */ class DataCollection { - public static string $dataPath = '_data'; - - /** - * Get a collection of Markdown documents in the _data/<$key> directory. - * Each Markdown file will be parsed into a MarkdownDocument with front matter. - */ - public static function markdown(string $key): BaseCollection - { - $time_start = microtime(true); - $collection = new BaseCollection(); - $collection->key = $key; - $collection->name = Hyde::titleFromSlug($key); - foreach (glob(Hyde::path(static::$dataPath . '/' . $key . '/*' . MarkdownDocument::$fileExtension)) as $file) { - $collection->push( - (new MarkdownFileService($file))->get() - ); - } - $collection->parseTimeInMs = static::getExecutionTime($time_start); - return $collection; - } - - protected static function getExecutionTime(float $time_start): float - { - return round((microtime(true) - $time_start) * 1000, 2); - } + // } diff --git a/packages/framework/src/Modules/DataCollections/DataCollectionServiceProvider.php b/packages/framework/src/Modules/DataCollections/DataCollectionServiceProvider.php index 85a0ff23695..004cdb52be5 100644 --- a/packages/framework/src/Modules/DataCollections/DataCollectionServiceProvider.php +++ b/packages/framework/src/Modules/DataCollections/DataCollectionServiceProvider.php @@ -12,7 +12,7 @@ public function register() { // Register the class alias AliasLoader::getInstance()->alias( - 'Collection', DataCollection::class + 'Collection', Facades\Collection::class ); } diff --git a/packages/framework/src/Modules/DataCollections/Facades/Collection.php b/packages/framework/src/Modules/DataCollections/Facades/Collection.php new file mode 100644 index 00000000000..7e3c55993cf --- /dev/null +++ b/packages/framework/src/Modules/DataCollections/Facades/Collection.php @@ -0,0 +1,41 @@ + directory. + * Each Markdown file will be parsed into a MarkdownDocument with front matter. + */ + public static function markdown(string $key): BaseCollection + { + $time_start = microtime(true); + $collection = new BaseCollection(); + $collection->key = $key; + $collection->name = Hyde::titleFromSlug($key); + foreach (glob(Hyde::path(static::$dataPath . '/' . $key . '/*' . MarkdownDocument::$fileExtension)) as $file) { + $collection->push( + (new MarkdownFileService($file))->get() + ); + } + $collection->parseTimeInMs = static::getExecutionTime($time_start); + return $collection; + } + + protected static function getExecutionTime(float $time_start): float + { + return round((microtime(true) - $time_start) * 1000, 2); + } +} \ No newline at end of file From c644fd7fcd3bd7a21451fb6556f9c118cbce5201 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 24 Jun 2022 22:50:49 +0200 Subject: [PATCH 15/35] Create base collection class to hold data --- .../DataCollections/DataCollection.php | 29 +++++++++++++++++-- .../DataCollections/Facades/Collection.php | 21 ++++---------- 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/packages/framework/src/Modules/DataCollections/DataCollection.php b/packages/framework/src/Modules/DataCollections/DataCollection.php index 8121076f974..6eff2fab6fa 100644 --- a/packages/framework/src/Modules/DataCollections/DataCollection.php +++ b/packages/framework/src/Modules/DataCollections/DataCollection.php @@ -2,7 +2,32 @@ namespace Hyde\Framework\Modules\DataCollections; -class DataCollection +use Hyde\Framework\Hyde; +use Illuminate\Support\Collection; + +class DataCollection extends Collection { - // + public string $key; + public string $name; + + protected float $timeStart; + public float $parseTimeInMs; + + public static string $dataPath = '_data'; + + public function __construct(string $key, ?string $name = null) + { + $this->timeStart = microtime(true); + $this->key = $key; + $this->name = $name ?? Hyde::titleFromSlug($key); + + parent::__construct(); + } + + public function push(...$values) + { + $this->parseTimeInMs = round((microtime(true) - $this->timeStart) * 1000, 2); + + return parent::push($values); + } } diff --git a/packages/framework/src/Modules/DataCollections/Facades/Collection.php b/packages/framework/src/Modules/DataCollections/Facades/Collection.php index 7e3c55993cf..8e9a7bea4af 100644 --- a/packages/framework/src/Modules/DataCollections/Facades/Collection.php +++ b/packages/framework/src/Modules/DataCollections/Facades/Collection.php @@ -4,8 +4,8 @@ use Hyde\Framework\Hyde; use Hyde\Framework\Models\MarkdownDocument; +use Hyde\Framework\Modules\DataCollections\DataCollection; use Hyde\Framework\Services\MarkdownFileService; -use Illuminate\Support\Collection as BaseCollection; /** * Provides a facade for generating on-the-fly Laravel Collections @@ -13,29 +13,18 @@ */ class Collection { - public static string $dataPath = '_data'; - /** * Get a collection of Markdown documents in the _data/<$key> directory. * Each Markdown file will be parsed into a MarkdownDocument with front matter. */ - public static function markdown(string $key): BaseCollection + public static function markdown(string $key): DataCollection { - $time_start = microtime(true); - $collection = new BaseCollection(); - $collection->key = $key; - $collection->name = Hyde::titleFromSlug($key); - foreach (glob(Hyde::path(static::$dataPath . '/' . $key . '/*' . MarkdownDocument::$fileExtension)) as $file) { + $collection = new DataCollection($key); + foreach (glob(Hyde::path(DataCollection::$dataPath . '/' . $key . '/*' . MarkdownDocument::$fileExtension)) as $file) { $collection->push( (new MarkdownFileService($file))->get() ); } - $collection->parseTimeInMs = static::getExecutionTime($time_start); return $collection; } - - protected static function getExecutionTime(float $time_start): float - { - return round((microtime(true) - $time_start) * 1000, 2); - } -} \ No newline at end of file +} From 389f610ea2f8a0c98b1530be81a22a63904b6d95 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 24 Jun 2022 22:51:43 +0200 Subject: [PATCH 16/35] Change static property dataPath to sourceDirectory to match convention --- .../framework/src/Modules/DataCollections/DataCollection.php | 2 +- .../src/Modules/DataCollections/Facades/Collection.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Modules/DataCollections/DataCollection.php b/packages/framework/src/Modules/DataCollections/DataCollection.php index 6eff2fab6fa..56b18459d6f 100644 --- a/packages/framework/src/Modules/DataCollections/DataCollection.php +++ b/packages/framework/src/Modules/DataCollections/DataCollection.php @@ -13,7 +13,7 @@ class DataCollection extends Collection protected float $timeStart; public float $parseTimeInMs; - public static string $dataPath = '_data'; + public static string $sourceDirectory = '_data'; public function __construct(string $key, ?string $name = null) { diff --git a/packages/framework/src/Modules/DataCollections/Facades/Collection.php b/packages/framework/src/Modules/DataCollections/Facades/Collection.php index 8e9a7bea4af..746a7e2a778 100644 --- a/packages/framework/src/Modules/DataCollections/Facades/Collection.php +++ b/packages/framework/src/Modules/DataCollections/Facades/Collection.php @@ -20,7 +20,7 @@ class Collection public static function markdown(string $key): DataCollection { $collection = new DataCollection($key); - foreach (glob(Hyde::path(DataCollection::$dataPath . '/' . $key . '/*' . MarkdownDocument::$fileExtension)) as $file) { + foreach (glob(Hyde::path(DataCollection::$sourceDirectory . '/' . $key . '/*' . MarkdownDocument::$fileExtension)) as $file) { $collection->push( (new MarkdownFileService($file))->get() ); From fc471b6092fdb783b4f7e890e9723338e675b4af Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 24 Jun 2022 22:54:00 +0200 Subject: [PATCH 17/35] Replace dynamic execution time tracking method --- .../framework/src/Modules/DataCollections/DataCollection.php | 5 +++-- .../src/Modules/DataCollections/Facades/Collection.php | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/framework/src/Modules/DataCollections/DataCollection.php b/packages/framework/src/Modules/DataCollections/DataCollection.php index 56b18459d6f..29422d0c968 100644 --- a/packages/framework/src/Modules/DataCollections/DataCollection.php +++ b/packages/framework/src/Modules/DataCollections/DataCollection.php @@ -24,10 +24,11 @@ public function __construct(string $key, ?string $name = null) parent::__construct(); } - public function push(...$values) + public function getCollection(): DataCollection { $this->parseTimeInMs = round((microtime(true) - $this->timeStart) * 1000, 2); + unset($this->timeStart); - return parent::push($values); + return $this; } } diff --git a/packages/framework/src/Modules/DataCollections/Facades/Collection.php b/packages/framework/src/Modules/DataCollections/Facades/Collection.php index 746a7e2a778..48b58a8c436 100644 --- a/packages/framework/src/Modules/DataCollections/Facades/Collection.php +++ b/packages/framework/src/Modules/DataCollections/Facades/Collection.php @@ -25,6 +25,6 @@ public static function markdown(string $key): DataCollection (new MarkdownFileService($file))->get() ); } - return $collection; + return $collection->getCollection(); } } From 147e0bc6d3ea7d1e2dc97b89f4d6496fda58cbc9 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 24 Jun 2022 22:56:03 +0200 Subject: [PATCH 18/35] Extract long method call to helper method --- .../src/Modules/DataCollections/Facades/Collection.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Modules/DataCollections/Facades/Collection.php b/packages/framework/src/Modules/DataCollections/Facades/Collection.php index 48b58a8c436..f29965bf081 100644 --- a/packages/framework/src/Modules/DataCollections/Facades/Collection.php +++ b/packages/framework/src/Modules/DataCollections/Facades/Collection.php @@ -20,11 +20,18 @@ class Collection public static function markdown(string $key): DataCollection { $collection = new DataCollection($key); - foreach (glob(Hyde::path(DataCollection::$sourceDirectory . '/' . $key . '/*' . MarkdownDocument::$fileExtension)) as $file) { + foreach (static::getMarkdownFiles($key) as $file) { $collection->push( (new MarkdownFileService($file))->get() ); } return $collection->getCollection(); } + + protected static function getMarkdownFiles(string $key): array + { + return glob(Hyde::path( + DataCollection::$sourceDirectory . '/' . $key . '/*' . MarkdownDocument::$fileExtension + )); + } } From 220896f61ac1856676543007eaa413ab48bd44b7 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 24 Jun 2022 22:59:38 +0200 Subject: [PATCH 19/35] Move getMarkdownFiles helper to collection class --- .../src/Modules/DataCollections/DataCollection.php | 8 ++++++++ .../src/Modules/DataCollections/Facades/Collection.php | 9 +-------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/packages/framework/src/Modules/DataCollections/DataCollection.php b/packages/framework/src/Modules/DataCollections/DataCollection.php index 29422d0c968..e20c0b1100e 100644 --- a/packages/framework/src/Modules/DataCollections/DataCollection.php +++ b/packages/framework/src/Modules/DataCollections/DataCollection.php @@ -3,6 +3,7 @@ namespace Hyde\Framework\Modules\DataCollections; use Hyde\Framework\Hyde; +use Hyde\Framework\Models\MarkdownDocument; use Illuminate\Support\Collection; class DataCollection extends Collection @@ -31,4 +32,11 @@ public function getCollection(): DataCollection return $this; } + + public function getMarkdownFiles(): array + { + return glob(Hyde::path( + static::$sourceDirectory . '/' . $this->key . '/*' . MarkdownDocument::$fileExtension + )); + } } diff --git a/packages/framework/src/Modules/DataCollections/Facades/Collection.php b/packages/framework/src/Modules/DataCollections/Facades/Collection.php index f29965bf081..53b219addb0 100644 --- a/packages/framework/src/Modules/DataCollections/Facades/Collection.php +++ b/packages/framework/src/Modules/DataCollections/Facades/Collection.php @@ -20,18 +20,11 @@ class Collection public static function markdown(string $key): DataCollection { $collection = new DataCollection($key); - foreach (static::getMarkdownFiles($key) as $file) { + foreach ($collection->getMarkdownFiles() as $file) { $collection->push( (new MarkdownFileService($file))->get() ); } return $collection->getCollection(); } - - protected static function getMarkdownFiles(string $key): array - { - return glob(Hyde::path( - DataCollection::$sourceDirectory . '/' . $key . '/*' . MarkdownDocument::$fileExtension - )); - } } From 20cf7b35bc0f2c9732c1ba04feff1b7d331c6467 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 24 Jun 2022 23:05:38 +0200 Subject: [PATCH 20/35] Remove unused use statements --- .../src/Modules/DataCollections/Facades/Collection.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/framework/src/Modules/DataCollections/Facades/Collection.php b/packages/framework/src/Modules/DataCollections/Facades/Collection.php index 53b219addb0..3026dbe1fe5 100644 --- a/packages/framework/src/Modules/DataCollections/Facades/Collection.php +++ b/packages/framework/src/Modules/DataCollections/Facades/Collection.php @@ -2,8 +2,6 @@ namespace Hyde\Framework\Modules\DataCollections\Facades; -use Hyde\Framework\Hyde; -use Hyde\Framework\Models\MarkdownDocument; use Hyde\Framework\Modules\DataCollections\DataCollection; use Hyde\Framework\Services\MarkdownFileService; From 339bd62621ac7660a094e464f207073aa1d0e8aa Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Fri, 24 Jun 2022 23:12:23 +0200 Subject: [PATCH 21/35] Add type annotations --- .../src/Modules/DataCollections/Facades/Collection.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/framework/src/Modules/DataCollections/Facades/Collection.php b/packages/framework/src/Modules/DataCollections/Facades/Collection.php index 3026dbe1fe5..30f0bb1e8cd 100644 --- a/packages/framework/src/Modules/DataCollections/Facades/Collection.php +++ b/packages/framework/src/Modules/DataCollections/Facades/Collection.php @@ -14,6 +14,9 @@ class Collection /** * Get a collection of Markdown documents in the _data/<$key> directory. * Each Markdown file will be parsed into a MarkdownDocument with front matter. + * + * @param string $key for a subdirectory of the _data directory + * @return DataCollection<\Hyde\Framework\Models\MarkdownDocument> */ public static function markdown(string $key): DataCollection { From 93af26b61ae425298b36cf56a57c858d4c0009df Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 25 Jun 2022 08:51:58 +0200 Subject: [PATCH 22/35] Update README.md --- packages/framework/src/Modules/README.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Modules/README.md b/packages/framework/src/Modules/README.md index 4bc1fd40d3d..57fff10f6f5 100644 --- a/packages/framework/src/Modules/README.md +++ b/packages/framework/src/Modules/README.md @@ -1,4 +1,10 @@ # Hyde Modules This directory contains self-contained code modules, -that may eventually be extracted into packages. \ No newline at end of file +that may eventually be extracted into packages. + +They may also be merged into the Hyde core. + +As these modules may be experimental, +the namespaces used may be changed without notice +as per the current 0.x semantic versioning range. From 1ffdf46cf89b18e0c312649f700f530056ef44c8 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 25 Jun 2022 09:55:14 +0200 Subject: [PATCH 23/35] Move facade method to base class --- .../DataCollections/DataCollection.php | 23 +++++++++++++++++++ .../DataCollections/Facades/Collection.php | 23 ++----------------- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/packages/framework/src/Modules/DataCollections/DataCollection.php b/packages/framework/src/Modules/DataCollections/DataCollection.php index e20c0b1100e..56063378dcd 100644 --- a/packages/framework/src/Modules/DataCollections/DataCollection.php +++ b/packages/framework/src/Modules/DataCollections/DataCollection.php @@ -4,8 +4,13 @@ use Hyde\Framework\Hyde; use Hyde\Framework\Models\MarkdownDocument; +use Hyde\Framework\Services\MarkdownFileService; use Illuminate\Support\Collection; +/** + * Generates Laravel Collections from static data files, + * such as Markdown components and YAML files. + */ class DataCollection extends Collection { public string $key; @@ -39,4 +44,22 @@ public function getMarkdownFiles(): array static::$sourceDirectory . '/' . $this->key . '/*' . MarkdownDocument::$fileExtension )); } + + /** + * Get a collection of Markdown documents in the _data/<$key> directory. + * Each Markdown file will be parsed into a MarkdownDocument with front matter. + * + * @param string $key for a subdirectory of the _data directory + * @return DataCollection<\Hyde\Framework\Models\MarkdownDocument> + */ + public static function markdown(string $key): DataCollection + { + $collection = new DataCollection($key); + foreach ($collection->getMarkdownFiles() as $file) { + $collection->push( + (new MarkdownFileService($file))->get() + ); + } + return $collection->getCollection(); + } } diff --git a/packages/framework/src/Modules/DataCollections/Facades/Collection.php b/packages/framework/src/Modules/DataCollections/Facades/Collection.php index 30f0bb1e8cd..1b875bd7a39 100644 --- a/packages/framework/src/Modules/DataCollections/Facades/Collection.php +++ b/packages/framework/src/Modules/DataCollections/Facades/Collection.php @@ -5,27 +5,8 @@ use Hyde\Framework\Modules\DataCollections\DataCollection; use Hyde\Framework\Services\MarkdownFileService; -/** - * Provides a facade for generating on-the-fly Laravel Collections - * from static data files such as Markdown components and YAML files. - */ + class Collection { - /** - * Get a collection of Markdown documents in the _data/<$key> directory. - * Each Markdown file will be parsed into a MarkdownDocument with front matter. - * - * @param string $key for a subdirectory of the _data directory - * @return DataCollection<\Hyde\Framework\Models\MarkdownDocument> - */ - public static function markdown(string $key): DataCollection - { - $collection = new DataCollection($key); - foreach ($collection->getMarkdownFiles() as $file) { - $collection->push( - (new MarkdownFileService($file))->get() - ); - } - return $collection->getCollection(); - } + // } From 4e11f02bdb1267d69b877bcd06f31037d5467642 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 25 Jun 2022 09:59:46 +0200 Subject: [PATCH 24/35] Remove name property Reduce complexity, it can easily be handled in the view --- .../framework/src/Modules/DataCollections/DataCollection.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/framework/src/Modules/DataCollections/DataCollection.php b/packages/framework/src/Modules/DataCollections/DataCollection.php index 56063378dcd..a9c02e6696c 100644 --- a/packages/framework/src/Modules/DataCollections/DataCollection.php +++ b/packages/framework/src/Modules/DataCollections/DataCollection.php @@ -14,18 +14,16 @@ class DataCollection extends Collection { public string $key; - public string $name; protected float $timeStart; public float $parseTimeInMs; public static string $sourceDirectory = '_data'; - public function __construct(string $key, ?string $name = null) + public function __construct(string $key) { $this->timeStart = microtime(true); $this->key = $key; - $this->name = $name ?? Hyde::titleFromSlug($key); parent::__construct(); } From 1abcb7b38ba94154d24cd7f5db081a7ae27751c5 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 25 Jun 2022 10:00:34 +0200 Subject: [PATCH 25/35] Rename Collection facade to MarkdownCollection --- .../DataCollections/DataCollectionServiceProvider.php | 2 +- .../Facades/{Collection.php => MarkdownCollection.php} | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) rename packages/framework/src/Modules/DataCollections/Facades/{Collection.php => MarkdownCollection.php} (64%) diff --git a/packages/framework/src/Modules/DataCollections/DataCollectionServiceProvider.php b/packages/framework/src/Modules/DataCollections/DataCollectionServiceProvider.php index 004cdb52be5..8699bfcc78a 100644 --- a/packages/framework/src/Modules/DataCollections/DataCollectionServiceProvider.php +++ b/packages/framework/src/Modules/DataCollections/DataCollectionServiceProvider.php @@ -12,7 +12,7 @@ public function register() { // Register the class alias AliasLoader::getInstance()->alias( - 'Collection', Facades\Collection::class + 'Collection', Facades\MarkdownCollection::class ); } diff --git a/packages/framework/src/Modules/DataCollections/Facades/Collection.php b/packages/framework/src/Modules/DataCollections/Facades/MarkdownCollection.php similarity index 64% rename from packages/framework/src/Modules/DataCollections/Facades/Collection.php rename to packages/framework/src/Modules/DataCollections/Facades/MarkdownCollection.php index 1b875bd7a39..768ce09a3cf 100644 --- a/packages/framework/src/Modules/DataCollections/Facades/Collection.php +++ b/packages/framework/src/Modules/DataCollections/Facades/MarkdownCollection.php @@ -3,10 +3,8 @@ namespace Hyde\Framework\Modules\DataCollections\Facades; use Hyde\Framework\Modules\DataCollections\DataCollection; -use Hyde\Framework\Services\MarkdownFileService; - -class Collection +class MarkdownCollection { - // + // } From cc8b02f8c847ac5dd74dbc5bb65b60b34f2e3fdb Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 25 Jun 2022 10:01:58 +0200 Subject: [PATCH 26/35] Create the MarkdownCollection facade --- .../DataCollections/DataCollectionServiceProvider.php | 2 +- .../Modules/DataCollections/Facades/MarkdownCollection.php | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Modules/DataCollections/DataCollectionServiceProvider.php b/packages/framework/src/Modules/DataCollections/DataCollectionServiceProvider.php index 8699bfcc78a..3e5b53168ee 100644 --- a/packages/framework/src/Modules/DataCollections/DataCollectionServiceProvider.php +++ b/packages/framework/src/Modules/DataCollections/DataCollectionServiceProvider.php @@ -12,7 +12,7 @@ public function register() { // Register the class alias AliasLoader::getInstance()->alias( - 'Collection', Facades\MarkdownCollection::class + 'MarkdownCollection', Facades\MarkdownCollection::class ); } diff --git a/packages/framework/src/Modules/DataCollections/Facades/MarkdownCollection.php b/packages/framework/src/Modules/DataCollections/Facades/MarkdownCollection.php index 768ce09a3cf..f7829aaad21 100644 --- a/packages/framework/src/Modules/DataCollections/Facades/MarkdownCollection.php +++ b/packages/framework/src/Modules/DataCollections/Facades/MarkdownCollection.php @@ -6,5 +6,8 @@ class MarkdownCollection { - // + public static function get(string $collectionKey): DataCollection + { + return new DataCollection($collectionKey); + } } From 5cc3a36d76f35fad4f2ed10ef140133668e27aa0 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Sat, 25 Jun 2022 08:03:12 +0000 Subject: [PATCH 27/35] Apply fixes from StyleCI --- .../src/Modules/DataCollections/DataCollection.php | 5 +++-- .../DataCollections/DataCollectionServiceProvider.php | 6 +++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/framework/src/Modules/DataCollections/DataCollection.php b/packages/framework/src/Modules/DataCollections/DataCollection.php index a9c02e6696c..e2725d4514c 100644 --- a/packages/framework/src/Modules/DataCollections/DataCollection.php +++ b/packages/framework/src/Modules/DataCollections/DataCollection.php @@ -39,7 +39,7 @@ public function getCollection(): DataCollection public function getMarkdownFiles(): array { return glob(Hyde::path( - static::$sourceDirectory . '/' . $this->key . '/*' . MarkdownDocument::$fileExtension + static::$sourceDirectory.'/'.$this->key.'/*'.MarkdownDocument::$fileExtension )); } @@ -47,7 +47,7 @@ public function getMarkdownFiles(): array * Get a collection of Markdown documents in the _data/<$key> directory. * Each Markdown file will be parsed into a MarkdownDocument with front matter. * - * @param string $key for a subdirectory of the _data directory + * @param string $key for a subdirectory of the _data directory * @return DataCollection<\Hyde\Framework\Models\MarkdownDocument> */ public static function markdown(string $key): DataCollection @@ -58,6 +58,7 @@ public static function markdown(string $key): DataCollection (new MarkdownFileService($file))->get() ); } + return $collection->getCollection(); } } diff --git a/packages/framework/src/Modules/DataCollections/DataCollectionServiceProvider.php b/packages/framework/src/Modules/DataCollections/DataCollectionServiceProvider.php index 3e5b53168ee..2f3b28ea4f6 100644 --- a/packages/framework/src/Modules/DataCollections/DataCollectionServiceProvider.php +++ b/packages/framework/src/Modules/DataCollections/DataCollectionServiceProvider.php @@ -2,9 +2,9 @@ namespace Hyde\Framework\Modules\DataCollections; -use Illuminate\Support\ServiceProvider; -use Illuminate\Foundation\AliasLoader; use Hyde\Framework\Hyde; +use Illuminate\Foundation\AliasLoader; +use Illuminate\Support\ServiceProvider; class DataCollectionServiceProvider extends ServiceProvider { @@ -19,7 +19,7 @@ public function register() public function boot() { // Create the _data directory if it doesn't exist - if (!is_dir(Hyde::path('_data'))) { + if (! is_dir(Hyde::path('_data'))) { mkdir(Hyde::path('_data')); } } From 2320232e7c6689168dc1637ceb6926f71bb80ec0 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 25 Jun 2022 10:10:18 +0200 Subject: [PATCH 28/35] Use the internal helper to get the actual Markdown collection --- .../src/Modules/DataCollections/Facades/MarkdownCollection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Modules/DataCollections/Facades/MarkdownCollection.php b/packages/framework/src/Modules/DataCollections/Facades/MarkdownCollection.php index f7829aaad21..1a5734cab1a 100644 --- a/packages/framework/src/Modules/DataCollections/Facades/MarkdownCollection.php +++ b/packages/framework/src/Modules/DataCollections/Facades/MarkdownCollection.php @@ -8,6 +8,6 @@ class MarkdownCollection { public static function get(string $collectionKey): DataCollection { - return new DataCollection($collectionKey); + return DataCollection::markdown($collectionKey); } } From 60ed906eadde85916e1f2d58ea44988994bbf572 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 25 Jun 2022 12:23:12 +0200 Subject: [PATCH 29/35] Exclude files starting with underscores from discovery --- .../src/Modules/DataCollections/DataCollection.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/framework/src/Modules/DataCollections/DataCollection.php b/packages/framework/src/Modules/DataCollections/DataCollection.php index e2725d4514c..37284e38a64 100644 --- a/packages/framework/src/Modules/DataCollections/DataCollection.php +++ b/packages/framework/src/Modules/DataCollections/DataCollection.php @@ -54,9 +54,11 @@ public static function markdown(string $key): DataCollection { $collection = new DataCollection($key); foreach ($collection->getMarkdownFiles() as $file) { - $collection->push( - (new MarkdownFileService($file))->get() - ); + if (! str_starts_with(basename($file), '_')) { + $collection->push( + (new MarkdownFileService($file))->get() + ); + } } return $collection->getCollection(); From d420630c8ae74771243f43609a49391b7a5226b1 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 25 Jun 2022 12:50:29 +0200 Subject: [PATCH 30/35] Create tests for the DataCollection class --- .../DataCollections/DataCollection.php | 2 + .../DataCollectionTest/DataCollectionTest.php | 149 ++++++++++++++++++ 2 files changed, 151 insertions(+) create mode 100644 tests/Hyde/Feature/Hyde/Framework/Testing/Modules/DataCollections/DataCollectionTest/DataCollectionTest.php diff --git a/packages/framework/src/Modules/DataCollections/DataCollection.php b/packages/framework/src/Modules/DataCollections/DataCollection.php index 37284e38a64..0a9e96604b0 100644 --- a/packages/framework/src/Modules/DataCollections/DataCollection.php +++ b/packages/framework/src/Modules/DataCollections/DataCollection.php @@ -10,6 +10,8 @@ /** * Generates Laravel Collections from static data files, * such as Markdown components and YAML files. + * + * @see \Hyde\Framework\Testing\Modules\DataCollections\DataCollectionTest\DataCollectionTest */ class DataCollection extends Collection { diff --git a/tests/Hyde/Feature/Hyde/Framework/Testing/Modules/DataCollections/DataCollectionTest/DataCollectionTest.php b/tests/Hyde/Feature/Hyde/Framework/Testing/Modules/DataCollections/DataCollectionTest/DataCollectionTest.php new file mode 100644 index 00000000000..6c16ad9f632 --- /dev/null +++ b/tests/Hyde/Feature/Hyde/Framework/Testing/Modules/DataCollections/DataCollectionTest/DataCollectionTest.php @@ -0,0 +1,149 @@ +assertEquals('_data', DataCollection::$sourceDirectory); + } + + // constructor creates new data collection instance + public function test_constructor_creates_new_data_collection_instance() + { + $class = new DataCollection('foo'); + $this->assertInstanceOf(DataCollection::class, $class); + $this->assertInstanceOf(Collection::class, $class); + } + + // test constructor sets key + public function test_constructor_sets_key() + { + $class = new DataCollection('foo'); + $this->assertEquals('foo', $class->key); + } + + // test key is required + public function test_key_is_required() + { + $this->expectException(\ArgumentCountError::class); + new DataCollection(); + } + + // test Get Collection method returns the collection instance + public function test_get_collection_method_returns_the_collection_instance() + { + $class = new DataCollection('foo'); + $this->assertSame($class, $class->getCollection()); + } + + // test get collection method sets parse time in ms + public function test_get_collection_method_sets_parse_time_in_ms() + { + $class = new DataCollection('foo'); + $class->getCollection(); + $this->assertIsFloat($class->parseTimeInMs); + } + + // test get markdown files method returns empty array if the specified directory does not exist + public function test_get_markdown_files_method_returns_empty_array_if_the_specified_directory_does_not_exist() + { + $class = new DataCollection('foo'); + $this->assertIsArray($class->getMarkdownFiles()); + $this->assertEmpty($class->getMarkdownFiles()); + } + + // test get markdown files method returns empty array if no files are found in specified directory + public function test_get_markdown_files_method_returns_empty_array_if_no_files_are_found_in_specified_directory() + { + mkdir(Hyde::path('_data/foo')); + $class = new DataCollection('foo'); + $this->assertIsArray($class->getMarkdownFiles()); + $this->assertEmpty($class->getMarkdownFiles()); + rmdir(Hyde::path('_data/foo')); + } + + // test get markdown files method returns an array of markdown files in the specified directory + public function test_get_markdown_files_method_returns_an_array_of_markdown_files_in_the_specified_directory() + { + mkdir(Hyde::path('_data/foo')); + touch(Hyde::path('_data/foo/foo.md')); + touch(Hyde::path('_data/foo/bar.md')); + + $this->assertEquals([ + Hyde::path('_data/foo/bar.md'), + Hyde::path('_data/foo/foo.md'), + ], (new DataCollection('foo'))->getMarkdownFiles()); + + File::deleteDirectory(Hyde::path('_data/foo')); + } + + // test get markdown files method does not include files in subdirectories + public function test_get_markdown_files_method_does_not_include_files_in_subdirectories() + { + mkdir(Hyde::path('_data/foo')); + mkdir(Hyde::path('_data/foo/bar')); + touch(Hyde::path('_data/foo/foo.md')); + touch(Hyde::path('_data/foo/bar/bar.md')); + $this->assertEquals([ + Hyde::path('_data/foo/foo.md'), + ], (new DataCollection('foo'))->getMarkdownFiles()); + File::deleteDirectory(Hyde::path('_data/foo')); + } + + // test get markdown files method does not include files with extensions other than .md + public function test_get_markdown_files_method_does_not_include_files_with_extensions_other_than_md() + { + mkdir(Hyde::path('_data/foo')); + touch(Hyde::path('_data/foo/foo.md')); + touch(Hyde::path('_data/foo/bar.txt')); + $this->assertEquals([ + Hyde::path('_data/foo/foo.md'), + ], (new DataCollection('foo'))->getMarkdownFiles()); + File::deleteDirectory(Hyde::path('_data/foo')); + } + + // test static markdown helper returns new data collection instance + public function test_static_markdown_helper_returns_new_data_collection_instance() + { + $this->assertInstanceOf(DataCollection::class, DataCollection::markdown('foo')); + } + + // test static markdown helper discovers and parses markdown files in the specified directory + public function test_static_markdown_helper_discovers_and_parses_markdown_files_in_the_specified_directory() + { + mkdir(Hyde::path('_data/foo')); + touch(Hyde::path('_data/foo/foo.md')); + touch(Hyde::path('_data/foo/bar.md')); + + $collection = DataCollection::markdown('foo'); + + $this->assertContainsOnlyInstancesOf(MarkdownDocument::class, $collection); + + File::deleteDirectory(Hyde::path('_data/foo')); + } + + // test static markdown helper ignores files starting with an underscore + public function test_static_markdown_helper_ignores_files_starting_with_an_underscore() + { + mkdir(Hyde::path('_data/foo')); + touch(Hyde::path('_data/foo/foo.md')); + touch(Hyde::path('_data/foo/_bar.md')); + $this->assertCount(1, DataCollection::markdown('foo')); + File::deleteDirectory(Hyde::path('_data/foo')); + } +} From e8a20723f0a9246bb1844d7663a2a7afd68e078f Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 25 Jun 2022 12:55:44 +0200 Subject: [PATCH 31/35] Add test for the MarkdownCollection facade --- .../DataCollections/Facades/MarkdownCollection.php | 3 +++ .../DataCollectionTest/DataCollectionTest.php | 11 +++++++++++ 2 files changed, 14 insertions(+) diff --git a/packages/framework/src/Modules/DataCollections/Facades/MarkdownCollection.php b/packages/framework/src/Modules/DataCollections/Facades/MarkdownCollection.php index 1a5734cab1a..dd7478a5e5a 100644 --- a/packages/framework/src/Modules/DataCollections/Facades/MarkdownCollection.php +++ b/packages/framework/src/Modules/DataCollections/Facades/MarkdownCollection.php @@ -4,6 +4,9 @@ use Hyde\Framework\Modules\DataCollections\DataCollection; +/** + * @see \Hyde\Framework\Testing\Modules\DataCollections\DataCollectionTest\DataCollectionTest + */ class MarkdownCollection { public static function get(string $collectionKey): DataCollection diff --git a/tests/Hyde/Feature/Hyde/Framework/Testing/Modules/DataCollections/DataCollectionTest/DataCollectionTest.php b/tests/Hyde/Feature/Hyde/Framework/Testing/Modules/DataCollections/DataCollectionTest/DataCollectionTest.php index 6c16ad9f632..b304cbc3fce 100644 --- a/tests/Hyde/Feature/Hyde/Framework/Testing/Modules/DataCollections/DataCollectionTest/DataCollectionTest.php +++ b/tests/Hyde/Feature/Hyde/Framework/Testing/Modules/DataCollections/DataCollectionTest/DataCollectionTest.php @@ -5,6 +5,7 @@ use Hyde\Framework\Hyde; use Hyde\Framework\Models\MarkdownDocument; use Hyde\Framework\Modules\DataCollections\DataCollection; +use Hyde\Framework\Modules\DataCollections\Facades\MarkdownCollection; use Hyde\Testing\TestCase; use Illuminate\Support\Collection; use Illuminate\Support\Facades\File; @@ -146,4 +147,14 @@ public function test_static_markdown_helper_ignores_files_starting_with_an_under $this->assertCount(1, DataCollection::markdown('foo')); File::deleteDirectory(Hyde::path('_data/foo')); } + + // test markdown facade returns same result as static markdown helper + public function test_markdown_facade_returns_same_result_as_static_markdown_helper() + { + $expected = DataCollection::markdown('foo'); + $actual = MarkdownCollection::get('foo'); + unset($expected->parseTimeInMs); + unset($actual->parseTimeInMs); + $this->assertEquals($expected, $actual); + } } From 08b6da99536f596e3b601b02c4da372b97f12096 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 25 Jun 2022 13:04:19 +0200 Subject: [PATCH 32/35] Add test for the DataCollectionServiceProvider --- .../DataCollectionServiceProvider.php | 3 +++ .../DataCollectionTest/DataCollectionTest.php | 20 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/packages/framework/src/Modules/DataCollections/DataCollectionServiceProvider.php b/packages/framework/src/Modules/DataCollections/DataCollectionServiceProvider.php index 2f3b28ea4f6..513ebeac396 100644 --- a/packages/framework/src/Modules/DataCollections/DataCollectionServiceProvider.php +++ b/packages/framework/src/Modules/DataCollections/DataCollectionServiceProvider.php @@ -6,6 +6,9 @@ use Illuminate\Foundation\AliasLoader; use Illuminate\Support\ServiceProvider; +/** + * @see \Hyde\Framework\Testing\Modules\DataCollections\DataCollectionTest\DataCollectionTest + */ class DataCollectionServiceProvider extends ServiceProvider { public function register() diff --git a/tests/Hyde/Feature/Hyde/Framework/Testing/Modules/DataCollections/DataCollectionTest/DataCollectionTest.php b/tests/Hyde/Feature/Hyde/Framework/Testing/Modules/DataCollections/DataCollectionTest/DataCollectionTest.php index b304cbc3fce..c436ceba073 100644 --- a/tests/Hyde/Feature/Hyde/Framework/Testing/Modules/DataCollections/DataCollectionTest/DataCollectionTest.php +++ b/tests/Hyde/Feature/Hyde/Framework/Testing/Modules/DataCollections/DataCollectionTest/DataCollectionTest.php @@ -5,8 +5,10 @@ use Hyde\Framework\Hyde; use Hyde\Framework\Models\MarkdownDocument; use Hyde\Framework\Modules\DataCollections\DataCollection; +use Hyde\Framework\Modules\DataCollections\DataCollectionServiceProvider; use Hyde\Framework\Modules\DataCollections\Facades\MarkdownCollection; use Hyde\Testing\TestCase; +use Illuminate\Foundation\AliasLoader; use Illuminate\Support\Collection; use Illuminate\Support\Facades\File; @@ -157,4 +159,22 @@ public function test_markdown_facade_returns_same_result_as_static_markdown_help unset($actual->parseTimeInMs); $this->assertEquals($expected, $actual); } + + // Test DataCollectionServiceProvider registers the facade as an alias + public function test_DataCollectionServiceProvider_registers_the_facade_as_an_alias() + { + $this->assertArrayHasKey('MarkdownCollection', AliasLoader::getInstance()->getAliases()); + $this->assertContains(MarkdownCollection::class, AliasLoader::getInstance()->getAliases()); + } + + // test DataCollectionServiceProvider creates the _data directory if it does not exist + public function test_DataCollectionServiceProvider_creates_the__data_directory_if_it_does_not_exist() + { + File::deleteDirectory(Hyde::path('_data')); + $this->assertFileDoesNotExist(Hyde::path('_data')); + + (new DataCollectionServiceProvider($this->app))->boot(); + + $this->assertFileExists(Hyde::path('_data')); + } } From 3122d24a4c0e538f7765d315a5bcc6ecfe4cca65 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 25 Jun 2022 13:07:03 +0200 Subject: [PATCH 33/35] Test source directory can be changed --- .../DataCollectionTest/DataCollectionTest.php | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/tests/Hyde/Feature/Hyde/Framework/Testing/Modules/DataCollections/DataCollectionTest/DataCollectionTest.php b/tests/Hyde/Feature/Hyde/Framework/Testing/Modules/DataCollections/DataCollectionTest/DataCollectionTest.php index c436ceba073..b5667b5b2ef 100644 --- a/tests/Hyde/Feature/Hyde/Framework/Testing/Modules/DataCollections/DataCollectionTest/DataCollectionTest.php +++ b/tests/Hyde/Feature/Hyde/Framework/Testing/Modules/DataCollections/DataCollectionTest/DataCollectionTest.php @@ -19,12 +19,6 @@ */ class DataCollectionTest extends TestCase { - // test class has static source directory property - public function testClassHasStaticSourceDirectoryProperty() - { - $this->assertEquals('_data', DataCollection::$sourceDirectory); - } - // constructor creates new data collection instance public function test_constructor_creates_new_data_collection_instance() { @@ -177,4 +171,22 @@ public function test_DataCollectionServiceProvider_creates_the__data_directory_i $this->assertFileExists(Hyde::path('_data')); } + + // test class has static source directory property + public function testClassHasStaticSourceDirectoryProperty() + { + $this->assertEquals('_data', DataCollection::$sourceDirectory); + } + + // test source directory can be changed + public function testSourceDirectoryCanBeChanged() + { + DataCollection::$sourceDirectory = 'foo'; + mkdir(Hyde::path('foo/bar'), recursive: true); + touch(Hyde::path('foo/bar/foo.md')); + $this->assertEquals([ + Hyde::path('foo/bar/foo.md'), + ], (new DataCollection('bar'))->getMarkdownFiles()); + File::deleteDirectory(Hyde::path('foo')); + } } From 4e260853d047908535d088716376a4eb93cd1259 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 25 Jun 2022 13:07:43 +0200 Subject: [PATCH 34/35] Remove Copilot comments and clean up test names --- .../DataCollectionTest/DataCollectionTest.php | 24 ++++--------------- 1 file changed, 4 insertions(+), 20 deletions(-) diff --git a/tests/Hyde/Feature/Hyde/Framework/Testing/Modules/DataCollections/DataCollectionTest/DataCollectionTest.php b/tests/Hyde/Feature/Hyde/Framework/Testing/Modules/DataCollections/DataCollectionTest/DataCollectionTest.php index b5667b5b2ef..1c42ddc7f51 100644 --- a/tests/Hyde/Feature/Hyde/Framework/Testing/Modules/DataCollections/DataCollectionTest/DataCollectionTest.php +++ b/tests/Hyde/Feature/Hyde/Framework/Testing/Modules/DataCollections/DataCollectionTest/DataCollectionTest.php @@ -27,28 +27,24 @@ public function test_constructor_creates_new_data_collection_instance() $this->assertInstanceOf(Collection::class, $class); } - // test constructor sets key public function test_constructor_sets_key() { $class = new DataCollection('foo'); $this->assertEquals('foo', $class->key); } - // test key is required public function test_key_is_required() { $this->expectException(\ArgumentCountError::class); new DataCollection(); } - // test Get Collection method returns the collection instance public function test_get_collection_method_returns_the_collection_instance() { $class = new DataCollection('foo'); $this->assertSame($class, $class->getCollection()); } - // test get collection method sets parse time in ms public function test_get_collection_method_sets_parse_time_in_ms() { $class = new DataCollection('foo'); @@ -56,7 +52,6 @@ public function test_get_collection_method_sets_parse_time_in_ms() $this->assertIsFloat($class->parseTimeInMs); } - // test get markdown files method returns empty array if the specified directory does not exist public function test_get_markdown_files_method_returns_empty_array_if_the_specified_directory_does_not_exist() { $class = new DataCollection('foo'); @@ -64,7 +59,6 @@ public function test_get_markdown_files_method_returns_empty_array_if_the_specif $this->assertEmpty($class->getMarkdownFiles()); } - // test get markdown files method returns empty array if no files are found in specified directory public function test_get_markdown_files_method_returns_empty_array_if_no_files_are_found_in_specified_directory() { mkdir(Hyde::path('_data/foo')); @@ -74,7 +68,6 @@ public function test_get_markdown_files_method_returns_empty_array_if_no_files_a rmdir(Hyde::path('_data/foo')); } - // test get markdown files method returns an array of markdown files in the specified directory public function test_get_markdown_files_method_returns_an_array_of_markdown_files_in_the_specified_directory() { mkdir(Hyde::path('_data/foo')); @@ -89,7 +82,6 @@ public function test_get_markdown_files_method_returns_an_array_of_markdown_file File::deleteDirectory(Hyde::path('_data/foo')); } - // test get markdown files method does not include files in subdirectories public function test_get_markdown_files_method_does_not_include_files_in_subdirectories() { mkdir(Hyde::path('_data/foo')); @@ -102,7 +94,6 @@ public function test_get_markdown_files_method_does_not_include_files_in_subdire File::deleteDirectory(Hyde::path('_data/foo')); } - // test get markdown files method does not include files with extensions other than .md public function test_get_markdown_files_method_does_not_include_files_with_extensions_other_than_md() { mkdir(Hyde::path('_data/foo')); @@ -114,13 +105,11 @@ public function test_get_markdown_files_method_does_not_include_files_with_exten File::deleteDirectory(Hyde::path('_data/foo')); } - // test static markdown helper returns new data collection instance public function test_static_markdown_helper_returns_new_data_collection_instance() { $this->assertInstanceOf(DataCollection::class, DataCollection::markdown('foo')); } - // test static markdown helper discovers and parses markdown files in the specified directory public function test_static_markdown_helper_discovers_and_parses_markdown_files_in_the_specified_directory() { mkdir(Hyde::path('_data/foo')); @@ -134,7 +123,6 @@ public function test_static_markdown_helper_discovers_and_parses_markdown_files_ File::deleteDirectory(Hyde::path('_data/foo')); } - // test static markdown helper ignores files starting with an underscore public function test_static_markdown_helper_ignores_files_starting_with_an_underscore() { mkdir(Hyde::path('_data/foo')); @@ -144,7 +132,6 @@ public function test_static_markdown_helper_ignores_files_starting_with_an_under File::deleteDirectory(Hyde::path('_data/foo')); } - // test markdown facade returns same result as static markdown helper public function test_markdown_facade_returns_same_result_as_static_markdown_helper() { $expected = DataCollection::markdown('foo'); @@ -155,14 +142,13 @@ public function test_markdown_facade_returns_same_result_as_static_markdown_help } // Test DataCollectionServiceProvider registers the facade as an alias - public function test_DataCollectionServiceProvider_registers_the_facade_as_an_alias() + public function test_data_collection_service_provider_registers_the_facade_as_an_alias() { $this->assertArrayHasKey('MarkdownCollection', AliasLoader::getInstance()->getAliases()); $this->assertContains(MarkdownCollection::class, AliasLoader::getInstance()->getAliases()); } - // test DataCollectionServiceProvider creates the _data directory if it does not exist - public function test_DataCollectionServiceProvider_creates_the__data_directory_if_it_does_not_exist() + public function test_data_collection_service_provider_creates_the__data_directory_if_it_does_not_exist() { File::deleteDirectory(Hyde::path('_data')); $this->assertFileDoesNotExist(Hyde::path('_data')); @@ -172,14 +158,12 @@ public function test_DataCollectionServiceProvider_creates_the__data_directory_i $this->assertFileExists(Hyde::path('_data')); } - // test class has static source directory property - public function testClassHasStaticSourceDirectoryProperty() + public function test_class_has_static_source_directory_property() { $this->assertEquals('_data', DataCollection::$sourceDirectory); } - // test source directory can be changed - public function testSourceDirectoryCanBeChanged() + public function test_source_directory_can_be_changed() { DataCollection::$sourceDirectory = 'foo'; mkdir(Hyde::path('foo/bar'), recursive: true); From a84edbeb6bcdebe3fc455a15c05dbb9da5e094f1 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Sat, 25 Jun 2022 11:07:54 +0000 Subject: [PATCH 35/35] Apply fixes from StyleCI --- .../DataCollections/DataCollectionTest/DataCollectionTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Hyde/Feature/Hyde/Framework/Testing/Modules/DataCollections/DataCollectionTest/DataCollectionTest.php b/tests/Hyde/Feature/Hyde/Framework/Testing/Modules/DataCollections/DataCollectionTest/DataCollectionTest.php index 1c42ddc7f51..50aa0f28da2 100644 --- a/tests/Hyde/Feature/Hyde/Framework/Testing/Modules/DataCollections/DataCollectionTest/DataCollectionTest.php +++ b/tests/Hyde/Feature/Hyde/Framework/Testing/Modules/DataCollections/DataCollectionTest/DataCollectionTest.php @@ -131,7 +131,7 @@ public function test_static_markdown_helper_ignores_files_starting_with_an_under $this->assertCount(1, DataCollection::markdown('foo')); File::deleteDirectory(Hyde::path('_data/foo')); } - + public function test_markdown_facade_returns_same_result_as_static_markdown_helper() { $expected = DataCollection::markdown('foo');