From fffee0a58dac00f3807c7d51d431fd8b7ca72ce4 Mon Sep 17 00:00:00 2001
From: Caen De Silva <caen@desilva.se>
Date: Sat, 28 Oct 2023 20:20:00 +0200
Subject: [PATCH] Add runtime configuration option for `--no-api` build flag

Resolves https://github.com/hydephp/develop/pull/1408#pullrequestreview-1702819229
---
 .../framework/src/Foundation/Internal/LoadConfiguration.php  | 5 +++++
 .../src/Framework/Features/Blogging/Models/FeaturedImage.php | 3 +--
 packages/framework/tests/Unit/LoadConfigurationTest.php      | 4 +++-
 3 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/packages/framework/src/Foundation/Internal/LoadConfiguration.php b/packages/framework/src/Foundation/Internal/LoadConfiguration.php
index 3bf2a20486c..b4b51bf41ca 100644
--- a/packages/framework/src/Foundation/Internal/LoadConfiguration.php
+++ b/packages/framework/src/Foundation/Internal/LoadConfiguration.php
@@ -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);
+            }
         }
     }
 }
diff --git a/packages/framework/src/Framework/Features/Blogging/Models/FeaturedImage.php b/packages/framework/src/Framework/Features/Blogging/Models/FeaturedImage.php
index 71bf71ee4ea..ac016f42b79 100644
--- a/packages/framework/src/Framework/Features/Blogging/Models/FeaturedImage.php
+++ b/packages/framework/src/Framework/Features/Blogging/Models/FeaturedImage.php
@@ -13,7 +13,6 @@
 use Hyde\Framework\Exceptions\FileNotFoundException;
 use Hyde\Markdown\Contracts\FrontMatter\SubSchemas\FeaturedImageSchema;
 
-use function in_array;
 use function array_key_exists;
 use function array_flip;
 use function file_exists;
@@ -226,7 +225,7 @@ protected function getContentLengthForLocalImage(): int
     protected function getContentLengthForRemoteImage(): int
     {
         // Check if the --no-api flag is set when running the build command, and if so, skip the API call.
-        if (! (isset($_SERVER['argv']) && in_array('--no-api', $_SERVER['argv'], true))) {
+        if (Config::getBool('hyde.api_calls', true)) {
             $headers = Http::withHeaders([
                 'User-Agent' => Config::getString('hyde.http_user_agent', 'RSS Request Client'),
             ])->head($this->getSource())->headers();
diff --git a/packages/framework/tests/Unit/LoadConfigurationTest.php b/packages/framework/tests/Unit/LoadConfigurationTest.php
index 15bcbf7a881..d1125608255 100644
--- a/packages/framework/tests/Unit/LoadConfigurationTest.php
+++ b/packages/framework/tests/Unit/LoadConfigurationTest.php
@@ -17,7 +17,7 @@ public function testItLoadsRuntimeConfiguration()
     {
         $serverBackup = $_SERVER;
 
-        $_SERVER['argv'] = ['--pretty-urls'];
+        $_SERVER['argv'] = ['--pretty-urls', '--no-api'];
 
         $app = new Application(getcwd());
 
@@ -25,10 +25,12 @@ public function testItLoadsRuntimeConfiguration()
         $loader->bootstrap($app);
 
         $this->assertTrue(config('hyde.pretty_urls'));
+        $this->assertFalse(config('hyde.api_calls'));
 
         $_SERVER = $serverBackup;
 
         $loader->bootstrap($app);
         $this->assertFalse(config('hyde.pretty_urls'));
+        $this->assertNull(config('hyde.api_calls'));
     }
 }