Skip to content

Commit

Permalink
Merge pull request #581 from hydephp/develop
Browse files Browse the repository at this point in the history
HydePHP v1.3.0 - 2023-10-30
  • Loading branch information
caendesilva authored Oct 30, 2023
2 parents 969ecc1 + e37fef9 commit 2b6df1e
Show file tree
Hide file tree
Showing 48 changed files with 542 additions and 141 deletions.
21 changes: 20 additions & 1 deletion config/hyde.php
Original file line number Diff line number Diff line change
Expand Up @@ -399,14 +399,33 @@
|--------------------------------------------------------------------------
|
| Here you can configure settings for the built-in realtime compiler server.
| The server also includes a magic dashboard feature that supercharges
| your local development! This feature can alo be customised here.
|
*/

'server' => [
// The default port the preview is served on
'port' => env('SERVER_PORT', 8080),

// The default host the preview is served on
'host' => env('SERVER_HOST', 'localhost'),
'dashboard' => env('SERVER_DASHBOARD', true),

// Should preview pages be saved to the output directory?
'save_preview' => true,

// Configure the realtime compiler dashboard
'dashboard' => [
// Should the realtime compiler dashboard be enabled?
'enabled' => env('SERVER_DASHBOARD', true),

// Can the dashboard make edits to the project file system?
'interactive' => true,

// Should the dashboard show tips?
'tips' => true,
],

],

/*
Expand Down
17 changes: 13 additions & 4 deletions src/Console/Commands/BuildSiteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ public function handle(): int

$this->service = new BuildService($this->output);

$this->taskService = app(BuildTaskService::class);
$this->taskService->setOutput($this->output);
$this->configureBuildTaskService();

$this->runPreBuildActions();

Expand All @@ -62,11 +61,21 @@ public function handle(): int
return $this->getExitCode();
}

protected function configureBuildTaskService(): void
{
/** @var BuildTaskService $taskService */
$taskService = app(BuildTaskService::class);

$this->taskService = $taskService;
$this->taskService->setOutput($this->output);
}

protected function runPreBuildActions(): void
{
if ($this->option('no-api')) {
$this->info('Disabling external API calls');
$this->newLine();
/** @var array<string, string> $config */
$config = Config::getArray('hyde.features', []);
unset($config[array_search('torchlight', $config)]);
Config::set(['hyde.features' => $config]);
Expand Down Expand Up @@ -132,7 +141,7 @@ protected function runNodeCommand(string $command, string $message, ?string $act

$output = shell_exec(sprintf(
'%s%s',
app()->environment() === 'testing' ? 'echo ' : '',
(string) app()->environment() === 'testing' ? 'echo ' : '',
$command
));

Expand All @@ -150,7 +159,7 @@ protected function hasWarnings(): bool
protected function getExitCode(): int
{
if ($this->hasWarnings() && BuildWarnings::reportsWarningsAsExceptions()) {
return self::INVALID;
return Command::INVALID;
}

return Command::SUCCESS;
Expand Down
5 changes: 4 additions & 1 deletion src/Console/Commands/DebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ protected function printVerbosePathInformation(): void

protected function printEnabledFeatures(): void
{
foreach (Config::getArray('hyde.features') as $feature) {
/** @var array<string, string> $features */
$features = Config::getArray('hyde.features', []);

foreach ($features as $feature) {
$this->line(" - $feature");
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Console/Commands/MakePageCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Hyde\Pages\BladePage;
use Hyde\Pages\DocumentationPage;
use Hyde\Pages\MarkdownPage;
use LaravelZero\Framework\Commands\Command;
use Hyde\Console\Concerns\Command;

use function strtolower;
use function ucfirst;
Expand Down Expand Up @@ -80,7 +80,7 @@ protected function validateOptions(): void
protected function getTitle(): string
{
return $this->argument('title')
?? $this->ask('What is the title of the page?')
?? $this->askForString('What is the title of the page?')
?? 'My New Page';
}

Expand Down
6 changes: 0 additions & 6 deletions src/Console/Commands/MakePostCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Hyde\Console\Concerns\Command;
use Hyde\Framework\Actions\CreatesNewMarkdownPostFile;

use function is_string;
use function sprintf;
use function ucwords;

Expand Down Expand Up @@ -99,9 +98,4 @@ protected function createPostFile(CreatesNewMarkdownPostFile $creator): int
return (int) $exception->getCode();
}
}

protected function askForString(string $question, ?string $default = null): ?string
{
return is_string($answer = $this->output->ask($question, $default)) ? $answer : null;
}
}
2 changes: 2 additions & 0 deletions src/Console/Commands/PublishHomepageCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class PublishHomepageCommand extends Command
/** @var string */
protected $description = 'Publish one of the default homepages to index.blade.php.';

/** @var array<string, array{name: string, description: string, group: string}> */
protected array $options = [
'welcome'=> [
'name' => 'Welcome',
Expand Down Expand Up @@ -96,6 +97,7 @@ protected function formatPublishableChoices(): array
})->values()->toArray();
}

/** @return Collection<array{name: string, description: string, group: string}> */
protected function getTemplateOptions(): Collection
{
return new Collection($this->options);
Expand Down
9 changes: 9 additions & 0 deletions src/Console/Concerns/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Hyde\Facades\Config;
use LaravelZero\Framework\Commands\Command as BaseCommand;

use function is_string;
use function array_keys;
use function array_values;
use function realpath;
Expand Down Expand Up @@ -128,4 +129,12 @@ public function indentedLine(int $spaces, string $string): void
{
$this->line(str_repeat(' ', $spaces).$string);
}

public function askForString(string $question, ?string $default = null): ?string
{
/** @var string|null $answer */
$answer = $this->output->ask($question, $default);

return is_string($answer) ? $answer : $default;
}
}
4 changes: 3 additions & 1 deletion src/Facades/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use TypeError;

use function sprintf;
use function call_user_func;

/**
* An extension of the Laravel Config facade with extra
Expand Down Expand Up @@ -45,6 +46,7 @@ public static function getFloat(string $key, float $default = null): float
/** @experimental Could possibly be merged by allowing null returns if default is null? Preferably with generics so the type is matched by IDE support. */
public static function getNullableString(string $key, string $default = null): ?string
{
/** @var array|string|int|bool|float|null $value */
$value = static::get($key, $default);

if ($value === null) {
Expand All @@ -56,7 +58,7 @@ public static function getNullableString(string $key, string $default = null): ?

protected static function validated(mixed $value, string $type, string $key): mixed
{
if (! ("is_$type")($value)) {
if (! call_user_func("is_$type", $value)) {
throw new TypeError(sprintf('%s(): Config value %s must be of type %s, %s given', __METHOD__, $key, $type, gettype($value)));
}

Expand Down
3 changes: 1 addition & 2 deletions src/Facades/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Hyde\Foundation\HydeKernel;
use Hyde\Framework\Concerns\Internal\ForwardsIlluminateFilesystem;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\File;

use function app;

Expand Down Expand Up @@ -63,7 +62,7 @@ public static function relativePath(string $path): string
*
* @param string $pattern
* @param int $flags
* @return \Illuminate\Support\Collection<string>
* @return \Illuminate\Support\Collection<int, string>
*/
public static function smartGlob(string $pattern, int $flags = 0): Collection
{
Expand Down
2 changes: 1 addition & 1 deletion src/Foundation/HydeKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class HydeKernel implements SerializableContract
use Serializable;
use Macroable;

final public const VERSION = '1.2.1';
final public const VERSION = '1.3.0';

protected static self $instance;

Expand Down
5 changes: 5 additions & 0 deletions src/Foundation/Internal/LoadConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ private function loadRuntimeConfiguration(Application $app, RepositoryContract $
if (in_array('--pretty-urls', $_SERVER['argv'], true)) {
$repository->set('hyde.pretty_urls', true);
}

// Check if the `--no-api` CLI argument is set, and if so, set the config value accordingly.
if (in_array('--no-api', $_SERVER['argv'], true)) {
$repository->set('hyde.api_calls', false);
}
}
}
}
2 changes: 2 additions & 0 deletions src/Foundation/Internal/LoadYamlConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ protected function hasYamlConfigFile(): bool
|| file_exists(Hyde::path('hyde.yaml'));
}

/** @return array<string, mixed> */
protected function getYaml(): array
{
return (array) Yaml::parse(file_get_contents($this->getFile()));
Expand All @@ -64,6 +65,7 @@ protected function mergeParsedConfiguration(): void
// If the Yaml file contains namespaces, we merge those using more granular logic
// that only applies the namespace data to each configuration namespace.
if ($this->configurationContainsNamespaces($yaml)) {
/** @var array<string, array<string, scalar>> $yaml */
foreach ($yaml as $namespace => $data) {
$this->mergeConfiguration($namespace, (array) $data);
}
Expand Down
10 changes: 9 additions & 1 deletion src/Foundation/Kernel/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public function path(string $path = ''): string
* Get an absolute file path from a supplied relative path.
*
* Input types are matched, meaning that if the input is a string so will the output be.
*
* @param string|array<string> $path
*/
public function pathToAbsolute(string|array $path): string|array
{
Expand Down Expand Up @@ -143,6 +145,8 @@ public function vendorPath(string $path = '', string $package = 'framework'): st

/**
* Touch one or more files in the project's directory.
*
* @param string|array<string> $path
*/
public function touch(string|array $path): bool
{
Expand All @@ -159,6 +163,8 @@ public function touch(string|array $path): bool

/**
* Unlink one or more files in the project's directory.
*
* @param string|array<string> $path
*/
public function unlink(string|array $path): bool
{
Expand All @@ -185,9 +191,11 @@ public function unlinkIfExists(string $path): bool
return false;
}

/** @return \Illuminate\Support\Collection<int, string> */
public function smartGlob(string $pattern, int $flags = 0): Collection
{
return collect(\Hyde\Facades\Filesystem::glob($pattern, $flags))
->map(fn (string $path): string => $this->pathToRelative($path));
->map(fn (string $path): string => $this->pathToRelative($path))
->values();
}
}
6 changes: 5 additions & 1 deletion src/Framework/Actions/BladeMatterParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ protected static function extractValue(string $line): string
return trim($key);
}

/** @return scalar|array<string, scalar> */
protected static function getValueWithType(string $value): mixed
{
$value = trim($value);
Expand All @@ -138,6 +139,7 @@ protected static function getValueWithType(string $value): mixed
return json_decode($value) ?? $value;
}

/** @return array<string, scalar> */
protected static function parseArrayString(string $string): array
{
$array = [];
Expand Down Expand Up @@ -167,7 +169,9 @@ protected static function parseArrayString(string $string): array
$pair = explode('=>', $token);

// Add key/value pair to array
$array[static::getValueWithType(trim(trim($pair[0]), "'"))] = static::getValueWithType(trim(trim($pair[1]), "'"));
$key = (string) static::getValueWithType(trim(trim($pair[0]), "'"));
$value = static::getValueWithType(trim(trim($pair[1]), "'"));
$array[$key] = $value;
}

return $array;
Expand Down
13 changes: 8 additions & 5 deletions src/Framework/Actions/CreatesNewMarkdownPostFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
use Hyde\Framework\Exceptions\FileConflictException;
use Hyde\Facades\Filesystem;
use Hyde\Pages\MarkdownPost;
use Illuminate\Support\Carbon;
use Illuminate\Support\Str;

use function date;

/**
* Offloads logic for the make:post command.
*
Expand All @@ -28,6 +27,7 @@ class CreatesNewMarkdownPostFile
protected string $author;
protected string $date;
protected string $identifier;
protected ?string $customContent;

/**
* Construct the class.
Expand All @@ -36,15 +36,18 @@ class CreatesNewMarkdownPostFile
* @param string|null $description The Post Meta Description.
* @param string|null $category The Primary Post Category.
* @param string|null $author The Username of the Author.
* @param string|null $date Optionally specify a custom date.
* @param string|null $customContent Optionally specify custom post content.
*/
public function __construct(string $title, ?string $description, ?string $category, ?string $author)
public function __construct(string $title, ?string $description, ?string $category, ?string $author, ?string $date = null, ?string $customContent = null)
{
$this->title = $title;
$this->description = $description ?? 'A short description used in previews and SEO';
$this->category = $category ?? 'blog';
$this->author = $author ?? 'default';
$this->customContent = $customContent;

$this->date = date('Y-m-d H:i');
$this->date = Carbon::make($date ?? Carbon::now())->format('Y-m-d H:i');
$this->identifier = Str::slug($title);
}

Expand All @@ -58,7 +61,7 @@ public function __construct(string $title, ?string $description, ?string $catego
*/
public function save(bool $force = false): string
{
$page = new MarkdownPost($this->identifier, $this->toArray(), '## Write something awesome.');
$page = new MarkdownPost($this->identifier, $this->toArray(), $this->customContent ?? '## Write something awesome.');

if ($force !== true && Filesystem::exists($page->getSourcePath())) {
throw new FileConflictException($page->getSourcePath());
Expand Down
Loading

0 comments on commit 2b6df1e

Please sign in to comment.