Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the codebase using the new Feature enum #1652

Merged
merged 24 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
be6d082
Check feature states using the feature enum
caendesilva Apr 10, 2024
3ad20db
Register the feature enum as a class alias
caendesilva Apr 10, 2024
59907ad
Use aliased enum class
caendesilva Apr 10, 2024
5a747ad
Use facade method shorthand
caendesilva Apr 10, 2024
085cc1d
Use proper helper to mock feature state instead of mocking kernel
caendesilva Apr 10, 2024
419b41d
Merge branch 'master' into improve-codebase-by-using-the-new-feature-…
caendesilva Apr 10, 2024
a930cfa
Trigger a deprecation when passing a string to feature state helper
caendesilva Apr 10, 2024
990fb86
Report a build warning when triggering the deprecation
caendesilva Apr 10, 2024
21822b2
Codecoverage ignore deprecated block
caendesilva Apr 10, 2024
3e7e127
Link to associated pull request
caendesilva Apr 10, 2024
e6ece92
Cleanup string assembly
caendesilva Apr 10, 2024
321f8cc
Remove deprecated string type from method annotation
caendesilva Apr 10, 2024
f1c79e0
Extract ternary path into if block
caendesilva Apr 10, 2024
b7ae503
Create deprecated method to ease refactors
caendesilva Apr 10, 2024
4dfba16
Use the proper pascal case helper
caendesilva Apr 10, 2024
56c0b94
Mock features using the enum names
caendesilva Apr 10, 2024
3ae0e24
Simplify internal feature mocking to remove array support
caendesilva Apr 11, 2024
8329400
Simplify method call
caendesilva Apr 11, 2024
e30c337
Revert "Simplify method call"
caendesilva Apr 11, 2024
88e9866
Simplify method logic
caendesilva Apr 11, 2024
df37cce
Use camel case instead of kebab case to match current conventions
caendesilva Apr 11, 2024
b1b7934
Revert "Use camel case instead of kebab case to match current convent…
caendesilva Apr 11, 2024
2a57408
Remove string backing from feature enum
caendesilva Apr 11, 2024
d17713a
Inline temporary deprecated method
caendesilva Apr 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
'DocumentationPage' => \Hyde\Pages\DocumentationPage::class,
'DataCollections' => \Hyde\Support\DataCollections::class,
'Includes' => \Hyde\Support\Includes::class,
'Feature' => \Hyde\Enums\Feature::class,
],

];
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@if(Hyde::hasFeature('darkmode'))
@if(Features::hasDarkmode()))
<button @click="toggleTheme" {{ $attributes->merge(['class' => 'theme-toggle-button flex items-center px-2 py-1 hover:text-gray-700 dark:text-gray-200']) }} title="Toggle theme">
<span class="sr-only">Toggle dark theme</span>
<svg width="1.25rem" height="1.25rem" class="w-5 h-5 hidden dark:block" fill="#FFFFFF" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path d="M10 2a1 1 0 011 1v1a1 1 0 11-2 0V3a1 1 0 011-1zm4 8a4 4 0 11-8 0 4 4 0 018 0zm-.464 4.95l.707.707a1 1 0 001.414-1.414l-.707-.707a1 1 0 00-1.414 1.414zm2.12-10.607a1 1 0 010 1.414l-.706.707a1 1 0 11-1.414-1.414l.707-.707a1 1 0 011.414 0zM17 11a1 1 0 100-2h-1a1 1 0 100 2h1zm-7 4a1 1 0 011 1v1a1 1 0 11-2 0v-1a1 1 0 011-1zM5.05 6.464A1 1 0 106.465 5.05l-.708-.707a1 1 0 00-1.414 1.414l.707.707zm1.414 8.486l-.707.707a1 1 0 01-1.414-1.414l.707-.707a1 1 0 011.414 1.414zM4 11a1 1 0 100-2H3a1 1 0 000 2h1z" fill-rule="evenodd" clip-rule="evenodd"></path></svg>
Expand Down
2 changes: 1 addition & 1 deletion packages/framework/resources/views/layouts/head.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
{{-- App Stylesheets --}}
@include('hyde::layouts.styles')

@if(Hyde::hasFeature('darkmode'))
@if(Features::hasDarkmode()))
{{-- Check the local storage for theme preference to avoid FOUC --}}
<meta id="meta-color-scheme" name="color-scheme" content="{{ config('hyde.default_color_scheme', 'light') }}">
<script>if (localStorage.getItem('color-theme') === 'dark' || (!('color-theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches)) { document.documentElement.classList.add('dark'); document.getElementById('meta-color-scheme').setAttribute('content', 'dark');} else { document.documentElement.classList.remove('dark') } </script>
Expand Down
18 changes: 9 additions & 9 deletions packages/framework/src/Enums/Feature.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@
*
* @see \Hyde\Facades\Features
*/
enum Feature: string
enum Feature
{
// Page Modules
case HtmlPages = 'html-pages';
case MarkdownPosts = 'markdown-posts';
case BladePages = 'blade-pages';
case MarkdownPages = 'markdown-pages';
case DocumentationPages = 'documentation-pages';
case HtmlPages;
case MarkdownPosts;
case BladePages;
case MarkdownPages;
case DocumentationPages;

// Frontend Features
case Darkmode = 'darkmode';
case DocumentationSearch = 'documentation-search';
case Darkmode;
case DocumentationSearch;

// Integrations
case Torchlight = 'torchlight';
case Torchlight;
}
2 changes: 1 addition & 1 deletion packages/framework/src/Facades/Features.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Features implements SerializableContract
*/
public static function enabled(Feature $feature): bool
{
return static::resolveMockedInstance($feature->value) ?? in_array(
return static::resolveMockedInstance($feature->name) ?? in_array(
$feature, Config::getArray('hyde.features', static::getDefaultOptions())
);
}
Expand Down
40 changes: 39 additions & 1 deletion packages/framework/src/Foundation/HydeKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Hyde\Enums\Feature;
use Hyde\Facades\Features;
use Hyde\Support\BuildWarnings;
use Hyde\Foundation\Kernel\Filesystem;
use Hyde\Foundation\Kernel\Hyperlinks;
use Hyde\Foundation\Kernel\FileCollection;
Expand All @@ -14,6 +15,14 @@
use Hyde\Support\Contracts\SerializableContract;
use Hyde\Support\Concerns\Serializable;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Support\Str;

use function getcwd;
use function sprintf;
use function is_string;
use function var_export;
use function debug_backtrace;
use function trigger_deprecation;

/**
* Encapsulates a HydePHP project, providing helpful methods for interacting with it.
Expand Down Expand Up @@ -92,7 +101,36 @@ public function features(): Features

public function hasFeature(Feature|string $feature): bool
{
return Features::enabled(is_string($feature) ? Feature::from($feature) : $feature);
if (is_string($feature)) {
/** @see https://github.com/hydephp/develop/pull/1650 */

// @codeCoverageIgnoreStart

$message = 'Passing a string to HydeKernel::hasFeature() is deprecated. Use a Feature enum case instead.';
trigger_deprecation('hydephp/hyde', '1.5.0', $message);

BuildWarnings::report(sprintf("$message\n <fg=gray>Replace </><fg=default>`%s`</><fg=gray> with </><fg=default>`%s`</><fg=gray> \n in file %s:%s</>",
sprintf('HydeKernel::hasFeature(%s)', var_export($feature, true)),
sprintf('HydeKernel::hasFeature(Feature::%s)', Str::studly($feature)),
debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['file'],
debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['line']
));

$feature = match ($feature) {
'html-pages' => Feature::HtmlPages,
'markdown-posts' => Feature::MarkdownPosts,
'blade-pages' => Feature::BladePages,
'markdown-pages' => Feature::MarkdownPages,
'documentation-pages' => Feature::DocumentationPages,
'darkmode' => Feature::Darkmode,
'documentation-search' => Feature::DocumentationSearch,
'torchlight' => Feature::Torchlight,
};

// @codeCoverageIgnoreEnd
}

return Features::enabled($feature);
}

/** @inheritDoc */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

namespace Hyde\Framework\Concerns\Internal;

use Hyde\Enums\Feature;

use function is_array;
use Illuminate\Support\Str;

/**
* Allows the Features class to be mocked.
Expand All @@ -19,26 +17,14 @@ trait MockableFeatures
{
protected static array $mockedInstances = [];

public static function mock(string|array $feature, ?bool $enabled = null): void
public static function mock(string $feature, bool $enabled): void
{
if (is_array($feature)) {
foreach ($feature as $key => $value) {
static::mock($key, $value);
}

return;
}

static::$mockedInstances[$feature] = $enabled;
static::$mockedInstances[Str::studly($feature)] = $enabled;
}

public static function resolveMockedInstance(Feature|string $feature): ?bool
public static function resolveMockedInstance(string $feature): ?bool
{
if ($feature instanceof Feature) {
$feature = $feature->value;
}

return static::$mockedInstances[$feature] ?? null;
return static::$mockedInstances[Str::studly($feature)] ?? null;
}

public static function clearMockedInstances(): void
Expand Down
2 changes: 1 addition & 1 deletion packages/framework/src/Hyde.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
* @method static HydeKernel getInstance()
* @method static Filesystem filesystem()
* @method static array getRegisteredExtensions()
* @method static bool hasFeature(Feature|string $feature)
* @method static bool hasFeature(Feature $feature)
* @method static bool hasSiteUrl()
* @method static void setInstance(HydeKernel $instance)
* @method static void setBasePath(string $basePath)
Expand Down
12 changes: 4 additions & 8 deletions packages/framework/tests/Feature/ConfigurableFeaturesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,14 @@ public function testDynamicFeaturesCanBeMocked()

public function testMultipleFeaturesCanBeMocked()
{
Features::mock([
'rss' => true,
'darkmode' => true,
]);
Features::mock('rss', true);
Features::mock('darkmode', true);

$this->assertTrue(Features::rss());
$this->assertTrue(Features::hasDarkmode());

Features::mock([
'rss' => false,
'darkmode' => false,
]);
Features::mock('rss', false);
Features::mock('darkmode', false);

$this->assertFalse(Features::rss());
$this->assertFalse(Features::hasDarkmode());
Expand Down
3 changes: 2 additions & 1 deletion packages/framework/tests/Unit/HydeHelperFacadeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Hyde\Framework\Testing\Unit;

use Hyde\Enums\Feature;
use Hyde\Facades\Features;
use Hyde\Hyde;
use Hyde\Testing\TestCase;
Expand Down Expand Up @@ -31,7 +32,7 @@ public function testFeaturesFacadeCanBeUsedToCallStaticMethodsOnFeaturesClass()
public function testHydeHasFeatureShorthandCallsStaticMethodOnFeaturesClass()
{
$this->assertTrue(
Hyde::hasFeature('markdown-posts')
Hyde::hasFeature(Feature::MarkdownPosts)
);
}
}
Loading