-
-
Notifications
You must be signed in to change notification settings - Fork 6
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 #664 from hydephp/refactor-view-publishers
Refactor publishable templates
- Loading branch information
Showing
9 changed files
with
282 additions
and
134 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
25 changes: 25 additions & 0 deletions
25
packages/framework/src/Console/Concerns/AsksToRebuildSite.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,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Hyde\Console\Concerns; | ||
|
||
use Illuminate\Support\Facades\Artisan; | ||
|
||
trait AsksToRebuildSite | ||
{ | ||
protected function askToRebuildSite(): void | ||
{ | ||
if ($this->option('no-interaction')) { | ||
return; | ||
} | ||
|
||
if ($this->confirm('Would you like to rebuild the site?', 'Yes')) { | ||
$this->line('Okay, building site!'); | ||
Artisan::call('build'); | ||
$this->info('Site is built!'); | ||
} else { | ||
$this->line('Okay, you can always run the build later!'); | ||
} | ||
} | ||
} |
52 changes: 0 additions & 52 deletions
52
packages/framework/src/Framework/Actions/PublishesHomepageView.php
This file was deleted.
Oops, something went wrong.
63 changes: 63 additions & 0 deletions
63
packages/framework/src/Framework/Features/Templates/Homepages.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,63 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Hyde\Framework\Features\Templates; | ||
|
||
use Illuminate\Support\Collection; | ||
|
||
/** @internal */ | ||
final class Homepages | ||
{ | ||
public static function options(): Collection | ||
{ | ||
return new Collection([ | ||
'welcome' => self::welcome(), | ||
'posts' => self::posts(), | ||
'blank' => self::blank(), | ||
]); | ||
} | ||
|
||
public static function exists(string $page): bool | ||
{ | ||
return self::options()->has($page); | ||
} | ||
|
||
public static function get(string $page): ?PublishableContract | ||
{ | ||
return self::options()->get($page); | ||
} | ||
|
||
public static function welcome(): PublishableContract | ||
{ | ||
return new class extends PublishableView | ||
{ | ||
protected static string $title = 'Welcome'; | ||
protected static string $desc = 'The default welcome page.'; | ||
protected static string $path = 'resources/views/homepages/welcome.blade.php'; | ||
protected static ?string $outputPath = 'index.blade.php'; | ||
}; | ||
} | ||
|
||
public static function posts(): PublishableContract | ||
{ | ||
return new class extends PublishableView | ||
{ | ||
protected static string $title = 'Posts Feed'; | ||
protected static string $desc = 'A feed of your latest posts. Perfect for a blog site!'; | ||
protected static string $path = 'resources/views/homepages/post-feed.blade.php'; | ||
protected static ?string $outputPath = 'index.blade.php'; | ||
}; | ||
} | ||
|
||
public static function blank(): PublishableContract | ||
{ | ||
return new class extends PublishableView | ||
{ | ||
protected static string $title = 'Blank Starter'; | ||
protected static string $desc = 'A blank Blade template with just the base layout.'; | ||
protected static string $path = 'resources/views/homepages/blank.blade.php'; | ||
protected static ?string $outputPath = 'index.blade.php'; | ||
}; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
packages/framework/src/Framework/Features/Templates/PublishableContract.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,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Hyde\Framework\Features\Templates; | ||
|
||
interface PublishableContract | ||
{ | ||
public static function publish(bool $force = false): bool; | ||
|
||
public static function getTitle(): string; | ||
|
||
public static function getDescription(): string; | ||
|
||
public static function getOutputPath(): string; | ||
|
||
public static function toArray(): array; | ||
} |
57 changes: 57 additions & 0 deletions
57
packages/framework/src/Framework/Features/Templates/PublishableView.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,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Hyde\Framework\Features\Templates; | ||
|
||
use Hyde\Hyde; | ||
|
||
abstract class PublishableView implements PublishableContract | ||
{ | ||
protected static string $title; | ||
protected static string $desc; | ||
protected static string $path; | ||
protected static ?string $outputPath; | ||
|
||
public static function publish(bool $force = false): bool | ||
{ | ||
$path = static::getOutputPath(); | ||
|
||
if (file_exists($path) && ! $force) { | ||
return false; | ||
} | ||
|
||
return copy(static::getSourcePath(), $path); | ||
} | ||
|
||
public static function getTitle(): string | ||
{ | ||
return static::$title; | ||
} | ||
|
||
public static function getDescription(): string | ||
{ | ||
return static::$desc; | ||
} | ||
|
||
public static function getOutputPath(): string | ||
{ | ||
// All publishable views at this time are Blade templates so to | ||
// reduce premature complexity we just use the Blade paths here. | ||
|
||
return Hyde::getBladePagePath(static::$outputPath ?? static::$path); | ||
} | ||
|
||
protected static function getSourcePath(): string | ||
{ | ||
return Hyde::vendorPath(static::$path); | ||
} | ||
|
||
public static function toArray(): array | ||
{ | ||
return [ | ||
'name' => static::getTitle(), | ||
'description' => static::getDescription(), | ||
]; | ||
} | ||
} |
Oops, something went wrong.