Skip to content

Commit

Permalink
Merge pull request #1217 from hydephp/general-code-polishing
Browse files Browse the repository at this point in the history
Finalize and polish the all around codebase
  • Loading branch information
caendesilva authored Mar 6, 2023
2 parents fd4a885 + 408e247 commit 7449e0a
Show file tree
Hide file tree
Showing 132 changed files with 832 additions and 742 deletions.
13 changes: 8 additions & 5 deletions _ide_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,15 @@ class Author extends \Hyde\Facades\Author {}
class Includes extends \Hyde\Facades\Includes {}
class Features extends \Hyde\Facades\Features {}
class Config extends \Hyde\Facades\Config {}
class BladePage extends \Hyde\Pages\BladePage {}
class MarkdownPage extends \Hyde\Pages\MarkdownPage {}
class MarkdownPost extends \Hyde\Pages\MarkdownPost {}
class DocumentationPage extends \Hyde\Pages\DocumentationPage {}
/** @mixin \Illuminate\Filesystem\Filesystem */
class Filesystem extends \Hyde\Facades\Filesystem {}
class DataCollection extends \Hyde\Support\DataCollection {}
class DataCollections extends \Hyde\Support\DataCollections {}
/** @mixin \Hyde\Foundation\Kernel\RouteCollection */
class Routes extends \Hyde\Foundation\Facades\Routes {}

// Page classes (aliased in app/config.php)
class HtmlPage extends \Hyde\Pages\HtmlPage {}
class BladePage extends \Hyde\Pages\BladePage {}
class MarkdownPage extends \Hyde\Pages\MarkdownPage {}
class MarkdownPost extends \Hyde\Pages\MarkdownPost {}
class DocumentationPage extends \Hyde\Pages\DocumentationPage {}
7 changes: 4 additions & 3 deletions app/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,14 @@
'Includes' => \Hyde\Facades\Includes::class,
'Features' => \Hyde\Facades\Features::class,
'Config' => \Hyde\Facades\Config::class,
'Filesystem' => \Hyde\Facades\Filesystem::class,
'Routes' => \Hyde\Foundation\Facades\Routes::class,
'HtmlPage' => \Hyde\Pages\HtmlPage::class,
'BladePage' => \Hyde\Pages\BladePage::class,
'MarkdownPage' => \Hyde\Pages\MarkdownPage::class,
'MarkdownPost' => \Hyde\Pages\MarkdownPost::class,
'DocumentationPage' => \Hyde\Pages\DocumentationPage::class,
'Filesystem' => \Hyde\Facades\Filesystem::class,
'DataCollection' => \Hyde\Support\DataCollection::class,
'Routes' => \Hyde\Foundation\Facades\Routes::class,
'DataCollections' => \Hyde\Support\DataCollections::class,
],

];
30 changes: 15 additions & 15 deletions docs/digging-deeper/collections.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ This article covers advanced usage intended for those who are writing their own
To make collections easy to use and understand, Hyde makes a few assumptions about the structure of your collections.
Follow these conventions and creating dynamic static sites will be a breeze.

1. Collections are accessed through static methods in the `DataCollection` class.
1. Collections are accessed through static methods in the `DataCollections` class.
2. Collections are stored as files in subdirectories of the `resources/collections` directory.
3. To get a collection, specify name of the subdirectory the files are stored in.
4. Data will be parsed into differing objects depending on which facade method you use. See the table below.
Expand All @@ -41,9 +41,9 @@ Follow these conventions and creating dynamic static sites will be a breeze.
The following facade methods for creating data collections are available:

```php
\Hyde\Support\DataCollection::markdown(string $name);
\Hyde\Support\DataCollection::yaml(string $name);
\Hyde\Support\DataCollection::json(string $name, bool $asArray = false);
\Hyde\Support\DataCollections::markdown(string $name);
\Hyde\Support\DataCollections::yaml(string $name);
\Hyde\Support\DataCollections::json(string $name, bool $asArray = false);
```

### Quick Reference Table
Expand All @@ -59,15 +59,15 @@ The following facade methods for creating data collections are available:
### Usage

```php
$collection = \Hyde\Support\DataCollection::markdown('name');
$collection = \Hyde\Support\DataCollections::markdown('name');
```

### Example returns

Here is an approximation of the data types contained by the variable created above:

```php
\Hyde\Support\DataCollection {
\Hyde\Support\DataCollections {
"testimonials/1.md" => Hyde\Markdown\Models\MarkdownDocument
"testimonials/2.md" => Hyde\Markdown\Models\MarkdownDocument
"testimonials/3.md" => Hyde\Markdown\Models\MarkdownDocument
Expand Down Expand Up @@ -105,15 +105,15 @@ Lorem ipsum dolor sit amet, consectetur adipiscing elit...
### Usage

```php
$collection = \Hyde\Support\DataCollection::yaml('name');
$collection = \Hyde\Support\DataCollections::yaml('name');
```

### Example returns

Here is an approximation of the data types contained by the variable created above:

```php
\Hyde\Support\DataCollection {
\Hyde\Support\DataCollections {
"authors/1.yaml" => Hyde\Markdown\Models\FrontMatter {
+data: array:1 [
"name" => "John Doe",
Expand All @@ -138,13 +138,13 @@ email: "john@example.org"
### Usage

```php
$collection = \Hyde\Support\DataCollection::json('name');
$collection = \Hyde\Support\DataCollections::json('name');
```

By default, the entries will be returned as `stdClass` objects. If you want to return an associative array instead, pass `true` as the second parameter:

```php
$collection = \Hyde\Support\DataCollection::json('name', true);
$collection = \Hyde\Support\DataCollections::json('name', true);
```

Since both return values use native PHP types, there are no example returns added here, as I'm sure you can imagine what they look like.
Expand Down Expand Up @@ -187,18 +187,18 @@ resources/collections

#### Using the Facade to Access the Collections

Now for the fun part! We will use the `DataCollection::markdown()` to access all our files into a convenient object.
Now for the fun part! We will use the `DataCollections::markdown()` to access all our files into a convenient object.
The class is registered with an alias, so you don't need to include any namespaces when in a Blade file.

The general syntax to use the facade is as follows:

```blade
DataCollection::markdown('subdirectory_name')
DataCollections::markdown('subdirectory_name')
```

This will return a Hyde DataCollection object, containing our Markdown files as MarkdownDocument objects. Here is a quick look at the object the facade returns:
This will return a Hyde DataCollections object, containing our Markdown files as MarkdownDocument objects. Here is a quick look at the object the facade returns:

<pre style="display: block; white-space: pre-wrap; padding: 1rem 1.5rem; overflow: initial !important; background-color: rgb(24, 23, 27); color: rgb(255, 132, 0); font: 400 12px Menlo, Monaco, Consolas, monospace; overflow-wrap: break-word; position: relative; z-index: 99999; word-break: break-all; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><span class="sf-dump-default" style="display: inline; background-color: rgb(24, 23, 27); color: rgb(255, 132, 0); font: 12px Menlo, Monaco, Consolas, monospace; overflow-wrap: break-word; white-space: pre-wrap; position: relative; z-index: 99999; word-break: break-all;">^</span><span class="sf-dump-default" style="display: inline; background-color: rgb(24, 23, 27); color: rgb(255, 132, 0); font: 12px Menlo, Monaco, Consolas, monospace; overflow-wrap: break-word; white-space: pre-wrap; position: relative; z-index: 99999; word-break: break-all;"> </span><span style="display: inline; color: rgb(18, 153, 218);">Hyde\Support\DataCollection</span> {<span style="text-decoration: none; border: 0px; outline: none; color: rgb(160, 160, 160);">#651 <span style="display: inline;">▼</span></span><samp>
<pre style="display: block; white-space: pre-wrap; padding: 1rem 1.5rem; overflow: initial !important; background-color: rgb(24, 23, 27); color: rgb(255, 132, 0); font: 400 12px Menlo, Monaco, Consolas, monospace; overflow-wrap: break-word; position: relative; z-index: 99999; word-break: break-all; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><span class="sf-dump-default" style="display: inline; background-color: rgb(24, 23, 27); color: rgb(255, 132, 0); font: 12px Menlo, Monaco, Consolas, monospace; overflow-wrap: break-word; white-space: pre-wrap; position: relative; z-index: 99999; word-break: break-all;">^</span><span class="sf-dump-default" style="display: inline; background-color: rgb(24, 23, 27); color: rgb(255, 132, 0); font: 12px Menlo, Monaco, Consolas, monospace; overflow-wrap: break-word; white-space: pre-wrap; position: relative; z-index: 99999; word-break: break-all;"> </span><span style="display: inline; color: rgb(18, 153, 218);">Hyde\Support\DataCollections</span> {<span style="text-decoration: none; border: 0px; outline: none; color: rgb(160, 160, 160);">#651 <span style="display: inline;">▼</span></span><samp>
#<span style="display: inline; color: rgb(255, 255, 255);">items</span>: <span style="display: inline; color: rgb(18, 153, 218);">array:3</span> [<span style="text-decoration: none; border: 0px; outline: none; color: rgb(160, 160, 160);"><span style="display: inline;">▼</span></span><samp>
"<span style="display: inline; color: rgb(86, 219, 58);">testimonials/1.md</span>" =&gt; <span style="display: inline; color: rgb(18, 153, 218);"><span>Hyde\Markdown\Models</span><span style="display: inline-block; text-overflow: ellipsis; max-width: none; white-space: nowrap; overflow: hidden; vertical-align: top; color: rgb(18, 153, 218);">\</span>MarkdownDocument</span> {<span style="text-decoration: none; border: 0px; outline: none; color: rgb(160, 160, 160);">#653 <span style="display: inline;">▼</span></span><samp>
+<span style="display: inline; color: rgb(255, 255, 255);">matter</span>: <span style="display: inline; color: rgb(18, 153, 218);"><span>Hyde\Markdown\Models</span><span style="display: inline-block; text-overflow: ellipsis; max-width: none; white-space: nowrap; overflow: hidden; vertical-align: top; color: rgb(18, 153, 218);">\</span>FrontMatter</span> {<span style="text-decoration: none; border: 0px; outline: none; color: rgb(160, 160, 160);">#652 <span style="display: inline;">▶</span></span>}
Expand All @@ -223,7 +223,7 @@ we are able to get the author from the front matter, and the content from the bo

```blade
// filepath _pages/testimonials.blade.php
@foreach(DataCollection::markdown('testimonials') as $testimonial)
@foreach(DataCollections::markdown('testimonials') as $testimonial)
<blockquote>
<p>{{ $testimonial->body }}</p>
<small>{{ $testimonial->matter['author'] }}</small>
Expand Down
2 changes: 1 addition & 1 deletion docs/digging-deeper/customization.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ and the rest of the data will be pulled from a matching entry.
```php
// torchlight! {"lineNumbers": false}
'authors' => [
PostAuthor::create(
Author::create(
username: 'mr_hyde', // Required username
name: 'Mr. Hyde', // Optional display name
website: 'https://hydephp.com' // Optional website URL
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@php /** @var \Hyde\Framework\Features\Paginator $paginator */ @endphp
@php /** @var \Hyde\Support\Paginator $paginator */ @endphp
<nav class="flex justify-center mt-4">
@if($paginator->previous())
<x-link :href="$paginator->previous()">&#8249;</x-link>
Expand Down
15 changes: 11 additions & 4 deletions packages/framework/src/Console/Commands/BuildSiteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@

namespace Hyde\Console\Commands;

use Hyde\Hyde;
use Hyde\Facades\Config;
use Hyde\Support\BuildWarnings;
use Hyde\Console\Concerns\Command;
use Hyde\Framework\Services\BuildService;
use Hyde\Framework\Services\BuildTaskService;
use Hyde\Hyde;
use Hyde\Support\BuildWarnings;
use Illuminate\Support\Facades\Config;
use function memory_get_peak_usage;
use function number_format;
use function array_search;
use function shell_exec;
use function microtime;
use function sprintf;
use function app;

/**
* Hyde Command to run the Build Process.
Expand Down Expand Up @@ -61,7 +68,7 @@ protected function runPreBuildActions(): void
if ($this->option('no-api')) {
$this->info('Disabling external API calls');
$this->newLine();
$config = (array) config('hyde.features');
$config = Config::getArray('hyde.features', []);
unset($config[array_search('torchlight', $config)]);
Config::set(['hyde.features' => $config]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@

namespace Hyde\Console\Commands;

use function array_unique;
use function basename;
use function config;
use Hyde\Console\Concerns\Command;
use Hyde\Facades\Filesystem;
use Hyde\Hyde;
use Hyde\Pages\BladePage;
use Hyde\Pages\DocumentationPage;
use Hyde\Facades\Config;
use Hyde\Pages\HtmlPage;
use Hyde\Pages\BladePage;
use Hyde\Facades\Filesystem;
use Hyde\Pages\MarkdownPage;
use Hyde\Pages\MarkdownPost;
use Hyde\Pages\DocumentationPage;
use Hyde\Console\Concerns\Command;
use InvalidArgumentException;
use function realpath;
use function array_unique;
use function str_replace;
use function basename;
use function realpath;

/**
* @see \Hyde\Framework\Testing\Feature\Commands\ChangeSourceDirectoryCommandTest
Expand Down Expand Up @@ -51,7 +51,7 @@ public function handle(): int
}

$this->gray(' > Updating configuration file');
$this->updateConfigurationFile($newDirectoryName, (string) config('hyde.source_root', ''));
$this->updateConfigurationFile($newDirectoryName, Config::getString('hyde.source_root', ''));

// We could also check if there are any more page classes (from packages) and add a note that they may need manual attention

Expand Down Expand Up @@ -83,7 +83,7 @@ protected function getValidatedName(string $name): string

protected function validateName(string $name): void
{
if (realpath(Hyde::path($name)) === realpath(Hyde::path(config('hyde.source_root', '')))) {
if (realpath(Hyde::path($name)) === realpath(Hyde::path(Config::getString('hyde.source_root', '')))) {
throw new InvalidArgumentException("The directory '$name' is already set as the project source root!");
}
}
Expand Down
9 changes: 6 additions & 3 deletions packages/framework/src/Console/Commands/DebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

namespace Hyde\Console\Commands;

use Composer\InstalledVersions;
use Hyde\Facades\Config;
use Hyde\Hyde;
use Hyde\Facades\Config;
use Composer\InstalledVersions;
use LaravelZero\Framework\Commands\Command;
use function str_replace;
use function realpath;
use function app;

/**
* Hyde Command to print debug information.
Expand All @@ -24,7 +27,7 @@ public function __construct()
{
parent::__construct();

if (config('app.env', 'production') !== 'development') {
if (Config::getString('app.env', 'production') !== 'development') {
$this->setHidden();
}
}
Expand Down
10 changes: 6 additions & 4 deletions packages/framework/src/Console/Commands/MakePostCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
namespace Hyde\Console\Commands;

use Exception;
use Hyde\Console\Concerns\Command;
use Hyde\Framework\Actions\CreatesNewMarkdownPostFile;
use LaravelZero\Framework\Commands\Command;
use function is_string;
use function sprintf;
use function ucwords;

/**
Expand Down Expand Up @@ -37,7 +39,7 @@ public function handle(): int
if (! $this->confirm('Do you wish to continue?', true)) {
$this->info('Aborting.');

return 130;
return Command::USER_EXIT;
}

return $this->createPostFile($creator);
Expand All @@ -46,8 +48,8 @@ public function handle(): int
protected function getTitle(): string
{
$this->line($this->argument('title')
? '<info>Selected title: '.$this->argument('title')."</info>\n"
: 'Please enter the title of the post, it will be used to generate the filename.'
? '<info>Selected title: '.$this->argument('title')."</info>\n"
: 'Please enter the title of the post, it will be used to generate the filename.'
);

return $this->argument('title')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

namespace Hyde\Console\Commands;

use Hyde\Console\Concerns\Command;
use Hyde\Hyde;
use Hyde\Console\Concerns\Command;
use Illuminate\Support\Facades\Artisan;
use function array_search;
use function sprintf;

/**
* Publish the Hyde Config Files.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use Hyde\Console\Concerns\Command;
use Illuminate\Support\Facades\Artisan;
use function str_replace;
use function sprintf;
use function strstr;

/**
* Publish the Hyde Blade views.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
use Exception;
use Hyde\Console\Concerns\Command;
use Hyde\Foundation\Facades\Pages;
use Hyde\Framework\Actions\StaticPageBuilder;
use Hyde\Framework\Features\BuildTasks\BuildTask;
use Hyde\Framework\Services\BuildService;
use Hyde\Framework\Services\RebuildService;
use Hyde\Hyde;
use Illuminate\Console\OutputStyle;
use function dirname;
Expand Down Expand Up @@ -67,7 +67,7 @@ public function handle(): void
{
$this->validate();

(new RebuildService($this->path))->execute();
(new StaticPageBuilder(Pages::getPage($this->path)))->__invoke();
}

public function printFinishMessage(): void
Expand Down
3 changes: 3 additions & 0 deletions packages/framework/src/Console/Commands/ValidateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use Hyde\Framework\Concerns\TracksExecutionTime;
use Hyde\Framework\Services\ValidationService;
use LaravelZero\Framework\Commands\Command;
use function number_format;
use function microtime;
use function sprintf;

/**
* @see \Hyde\Framework\Testing\Feature\Commands\ValidateCommandTest
Expand Down
2 changes: 1 addition & 1 deletion packages/framework/src/Console/Concerns/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/
abstract class Command extends BaseCommand
{
public final const USER_EXIT = 130;
final public const USER_EXIT = 130;

/**
* The base handle method that can be overridden by child classes.
Expand Down
1 change: 0 additions & 1 deletion packages/framework/src/Facades/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
* @see \Hyde\Framework\Services\AssetService
*
* @method static string version()
* @method static string stylePath()
* @method static string cdnLink(string $file)
* @method static string mediaLink(string $file)
* @method static bool hasMediaFile(string $file)
Expand Down
12 changes: 12 additions & 0 deletions packages/framework/src/Facades/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ public static function getFloat(string $key, float $default = null): float
return (float) self::validated(static::get($key, $default), 'float', $key);
}

/** @experimental */
public static function getNullableString(string $key, string $default = null): ?string
{
$value = static::get($key, $default);

if ($value === null) {
return null;
}

return (string) self::validated($value, 'string', $key);
}

protected static function validated(mixed $value, string $type, string $key): mixed
{
if (! ("is_$type")($value)) {
Expand Down
Loading

0 comments on commit 7449e0a

Please sign in to comment.