-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #556 from hydephp/develop
v0.43.0-beta - 2022-06-25 - File-based Collections
- Loading branch information
Showing
12 changed files
with
191 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
namespace Hyde\Framework\Modules\DataCollections; | ||
|
||
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. | ||
* | ||
* @see \Hyde\Framework\Testing\Modules\DataCollections\DataCollectionTest\DataCollectionTest | ||
*/ | ||
class DataCollection extends Collection | ||
{ | ||
public string $key; | ||
|
||
protected float $timeStart; | ||
public float $parseTimeInMs; | ||
|
||
public static string $sourceDirectory = '_data'; | ||
|
||
public function __construct(string $key) | ||
{ | ||
$this->timeStart = microtime(true); | ||
$this->key = $key; | ||
|
||
parent::__construct(); | ||
} | ||
|
||
public function getCollection(): DataCollection | ||
{ | ||
$this->parseTimeInMs = round((microtime(true) - $this->timeStart) * 1000, 2); | ||
unset($this->timeStart); | ||
|
||
return $this; | ||
} | ||
|
||
public function getMarkdownFiles(): array | ||
{ | ||
return glob(Hyde::path( | ||
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) { | ||
if (! str_starts_with(basename($file), '_')) { | ||
$collection->push( | ||
(new MarkdownFileService($file))->get() | ||
); | ||
} | ||
} | ||
|
||
return $collection->getCollection(); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/Modules/DataCollections/DataCollectionServiceProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace Hyde\Framework\Modules\DataCollections; | ||
|
||
use Hyde\Framework\Hyde; | ||
use Illuminate\Foundation\AliasLoader; | ||
use Illuminate\Support\ServiceProvider; | ||
|
||
/** | ||
* @see \Hyde\Framework\Testing\Modules\DataCollections\DataCollectionTest\DataCollectionTest | ||
*/ | ||
class DataCollectionServiceProvider extends ServiceProvider | ||
{ | ||
public function register() | ||
{ | ||
// Register the class alias | ||
AliasLoader::getInstance()->alias( | ||
'MarkdownCollection', Facades\MarkdownCollection::class | ||
); | ||
} | ||
|
||
public function boot() | ||
{ | ||
// Create the _data directory if it doesn't exist | ||
if (! is_dir(Hyde::path('_data'))) { | ||
mkdir(Hyde::path('_data')); | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/Modules/DataCollections/Facades/MarkdownCollection.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace Hyde\Framework\Modules\DataCollections\Facades; | ||
|
||
use Hyde\Framework\Modules\DataCollections\DataCollection; | ||
|
||
/** | ||
* @see \Hyde\Framework\Testing\Modules\DataCollections\DataCollectionTest\DataCollectionTest | ||
*/ | ||
class MarkdownCollection | ||
{ | ||
public static function get(string $collectionKey): DataCollection | ||
{ | ||
return DataCollection::markdown($collectionKey); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Hyde Modules | ||
|
||
This directory contains self-contained code modules, | ||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters