Skip to content

Commit

Permalink
Merge pull request #1652 from hydephp/improve-codebase-by-using-the-n…
Browse files Browse the repository at this point in the history
…ew-feature-enum

Improve the codebase using the new Feature enum hydephp/develop@940d9ae
  • Loading branch information
github-actions committed Apr 11, 2024
1 parent a5b3a9f commit 323eb96
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 42 deletions.
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 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 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 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 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
24 changes: 5 additions & 19 deletions src/Framework/Concerns/Internal/MockableFeatures.php
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 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 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 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)
);
}
}

0 comments on commit 323eb96

Please sign in to comment.