From 357312c5c9d6e5362074b7ff062311eba961967f Mon Sep 17 00:00:00 2001 From: Sleon4 Date: Mon, 23 Dec 2024 18:10:37 -0500 Subject: [PATCH 1/4] ci: updated Docker Configuration --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 2a572cc4..289d19c9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -50,7 +50,7 @@ SHELL ["/bin/bash", "--login", "-i", "-c"] RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash \ && source /home/lion/.bashrc \ && nvm install 20 \ - && npm install -g npm@10 + && npm install -g npm@11 # Install OhMyZsh RUN sh -c "$(wget https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh -O -)" From 466cbdaf43230a35003827db8657fe6d90f6a2ae Mon Sep 17 00:00:00 2001 From: Sleon4 Date: Mon, 23 Dec 2024 18:36:16 -0500 Subject: [PATCH 2/4] feat: FetchConfiguration added --- .../Helpers/Http/FetchConfiguration.php | 36 ++++++++++++++++ tests/Helpers/Http/FetchConfigurationTest.php | 42 +++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 src/LionBundle/Helpers/Http/FetchConfiguration.php create mode 100644 tests/Helpers/Http/FetchConfigurationTest.php diff --git a/src/LionBundle/Helpers/Http/FetchConfiguration.php b/src/LionBundle/Helpers/Http/FetchConfiguration.php new file mode 100644 index 00000000..65d5ea43 --- /dev/null +++ b/src/LionBundle/Helpers/Http/FetchConfiguration.php @@ -0,0 +1,36 @@ + $configuration [Configuration data] + * + * @package Lion\Bundle\Helpers\Http + */ +class FetchConfiguration +{ + /** + * Class Constructor + * + * @param array $configuration [Configuration data] + */ + public function __construct( + private readonly array $configuration = [] + ) {} + + /** + * Returns configuration data + * + * @return array + * + * @internal + */ + public function getConfiguration(): array + { + return $this->configuration; + } +} diff --git a/tests/Helpers/Http/FetchConfigurationTest.php b/tests/Helpers/Http/FetchConfigurationTest.php new file mode 100644 index 00000000..6a3d072d --- /dev/null +++ b/tests/Helpers/Http/FetchConfigurationTest.php @@ -0,0 +1,42 @@ + true, + ]; + + $fetchConfiguration = new FetchConfiguration($configuration); + + $this->initReflection($fetchConfiguration); + + $this->assertSame($configuration, $this->getPrivateProperty('configuration')); + } + + #[Testing] + public function getConfiguration(): void + { + $configuration = [ + 'verify' => true, + ]; + + $fetchConfiguration = new FetchConfiguration($configuration); + + $this->assertSame($configuration, $fetchConfiguration->getConfiguration()); + } +} From 4caeccf8e183f45f461a052afafdb18dd4d88c97 Mon Sep 17 00:00:00 2001 From: Sleon4 Date: Mon, 23 Dec 2024 19:01:58 -0500 Subject: [PATCH 3/4] feat: Fetch added --- src/LionBundle/Helpers/Http/Fetch.php | 95 +++++++++++++++++++ tests/Helpers/Http/FetchConfigurationTest.php | 4 +- tests/Helpers/Http/FetchTest.php | 91 ++++++++++++++++++ 3 files changed, 187 insertions(+), 3 deletions(-) create mode 100644 src/LionBundle/Helpers/Http/Fetch.php create mode 100644 tests/Helpers/Http/FetchTest.php diff --git a/src/LionBundle/Helpers/Http/Fetch.php b/src/LionBundle/Helpers/Http/Fetch.php new file mode 100644 index 00000000..07778302 --- /dev/null +++ b/src/LionBundle/Helpers/Http/Fetch.php @@ -0,0 +1,95 @@ + $options [Options to send through the request, + * such as headers or parameters] + * @property FetchConfiguration|null $fetchConfiguration [Defines the + * configuration data for making an HTTP request] + * + * @package Lion\Bundle\Helpers\Http + */ +class Fetch +{ + /** + * @var FetchConfiguration|null $fetchConfiguration [Defines the + * configuration data for making an HTTP request] + */ + private ?FetchConfiguration $fetchConfiguration = null; + + /** + * Class Constructor + * + * @param string $httpMethod [HTTP protocol] + * @param string $uri [URL to make the request] + * @param array $options [Options to send through the request, such as + * headers or parameters] + */ + public function __construct( + private readonly string $httpMethod, + private readonly string $uri, + private readonly array $options = [] + ) {} + + /** + * Returns an HTTP configuration object + * + * @return FetchConfiguration|null + */ + public function getFetchConfiguration(): ?FetchConfiguration + { + return $this->fetchConfiguration; + } + + /** + * Adds an HTTP configuration object + * + * @param FetchConfiguration $fetchConfiguration [Defines the configuration + * data for making an HTTP request] + * + * @return Fetch + */ + public function setFetchConfiguration(FetchConfiguration $fetchConfiguration): Fetch + { + $this->fetchConfiguration = $fetchConfiguration; + + return $this; + } + + /** + * Returns the HTTP method of the HTTP request + * + * @return string + */ + public function getHttpMethod(): string + { + return $this->httpMethod; + } + + /** + * Returns the URI of the HTTP request + * + * @return string + */ + public function getUri(): string + { + return $this->uri; + } + + /** + * Returns the data from the HTTP request + * + * @return array + */ + public function getOptions(): array + { + return $this->options; + } +} diff --git a/tests/Helpers/Http/FetchConfigurationTest.php b/tests/Helpers/Http/FetchConfigurationTest.php index 6a3d072d..c26e0ed5 100644 --- a/tests/Helpers/Http/FetchConfigurationTest.php +++ b/tests/Helpers/Http/FetchConfigurationTest.php @@ -21,9 +21,7 @@ public function constructor(): void 'verify' => true, ]; - $fetchConfiguration = new FetchConfiguration($configuration); - - $this->initReflection($fetchConfiguration); + $this->initReflection(new FetchConfiguration($configuration)); $this->assertSame($configuration, $this->getPrivateProperty('configuration')); } diff --git a/tests/Helpers/Http/FetchTest.php b/tests/Helpers/Http/FetchTest.php new file mode 100644 index 00000000..d2fe12e6 --- /dev/null +++ b/tests/Helpers/Http/FetchTest.php @@ -0,0 +1,91 @@ + [ + 'Content-Type' => 'application/json', + ], + ]; + private const array CONFIGURATION = [ + 'verify' => false, + ]; + + /** + * @throws ReflectionException + */ + #[Testing] + public function constructor(): void + { + $fetchConfiguration = new FetchConfiguration(self::CONFIGURATION); + + $this->initReflection( + (new Fetch(self::HTTP_METHOD, self::URI, self::OPTIONS)) + ->setFetchConfiguration($fetchConfiguration) + ); + + $this->assertSame(self::HTTP_METHOD, $this->getPrivateProperty('httpMethod')); + $this->assertSame(self::URI, $this->getPrivateProperty('uri')); + $this->assertSame(self::OPTIONS, $this->getPrivateProperty('options')); + $this->assertSame($fetchConfiguration, $this->getPrivateProperty('fetchConfiguration')); + } + + #[Testing] + public function getFetchConfiguration(): void + { + $fetchConfiguration = new FetchConfiguration(self::CONFIGURATION); + + $fetch = (new Fetch(self::HTTP_METHOD, self::URI, self::OPTIONS)) + ->setFetchConfiguration($fetchConfiguration); + + $this->assertSame($fetchConfiguration, $fetch->getFetchConfiguration()); + } + + #[Testing] + public function setFetchConfiguration(): void + { + $fetchConfiguration = new FetchConfiguration(self::CONFIGURATION); + + $fetch = new Fetch(self::HTTP_METHOD, self::URI, self::OPTIONS); + + $this->assertInstanceOf(Fetch::class, $fetch->setFetchConfiguration($fetchConfiguration)); + $this->assertSame($fetchConfiguration, $fetch->getFetchConfiguration()); + } + + #[Testing] + public function getHttpMethod(): void + { + $fetch = new Fetch(self::HTTP_METHOD, self::URI, self::OPTIONS); + + $this->assertSame(self::HTTP_METHOD, $fetch->getHttpMethod()); + } + + #[Testing] + public function getUri(): void + { + $fetch = new Fetch(self::HTTP_METHOD, self::URI, self::OPTIONS); + + $this->assertSame(self::URI, $fetch->getUri()); + } + + #[Testing] + public function getOptions(): void + { + $fetch = new Fetch(self::HTTP_METHOD, self::URI, self::OPTIONS); + + $this->assertSame(self::OPTIONS, $fetch->getOptions()); + } +} From 1b97a8aee00b1167ee4997e7cee40324adfbabf5 Mon Sep 17 00:00:00 2001 From: Sleon4 Date: Mon, 23 Dec 2024 19:18:37 -0500 Subject: [PATCH 4/4] refactor: fetch helper parameters are modified --- .env.example | 1 + lion | 6 ++- npm | 6 ++- routes/web.php | 4 +- .../Lion/Route/PostmanCollectionCommand.php | 15 ++++-- .../Commands/Lion/Route/RouteListCommand.php | 15 ++++-- src/LionBundle/Helpers/Bundle/constants.php | 9 ++-- src/LionBundle/Helpers/Bundle/helpers.php | 18 ++++--- src/LionBundle/Helpers/Http/Fetch.php | 8 +++ tests/Helpers/Bundle/ConstantsTest.php | 8 --- tests/Helpers/Bundle/HelpersTest.php | 5 +- .../Commands/PostmanCollectionTest.php | 49 +++++++++++-------- tests/bootstrap.php | 6 ++- 13 files changed, 91 insertions(+), 59 deletions(-) diff --git a/.env.example b/.env.example index 75521268..07a4c407 100644 --- a/.env.example +++ b/.env.example @@ -1,3 +1,4 @@ +DEVELOPMENT_ENVIRONMENT="dev" ################################################################################ ################# SERVER INFORMATION ------------------------- ################# ################################################################################ diff --git a/lion b/lion index dc7fcc68..e43034c4 100644 --- a/lion +++ b/lion @@ -4,8 +4,6 @@ declare(strict_types=1); define('LION_START', microtime(true)); -define('IS_INDEX', false); - /** * ----------------------------------------------------------------------------- * Register The Auto Loader @@ -22,6 +20,10 @@ use Lion\Bundle\Commands\CommandHandler; use Lion\Database\Driver; use Lion\Files\Store; +define('IS_INDEX', false); + +define('DEVELOPMENT_ENVIRONMENT', 'dev' === env('DEVELOPMENT_ENVIRONMENT')); + /** * ----------------------------------------------------------------------------- * Register environment variable loader automatically diff --git a/npm b/npm index b5eeeefb..8676c4d3 100644 --- a/npm +++ b/npm @@ -4,8 +4,6 @@ declare(strict_types=1); define('LION_START', microtime(true)); -define('IS_INDEX', false); - /** * ----------------------------------------------------------------------------- * Register The Auto Loader @@ -22,6 +20,10 @@ use Lion\Bundle\Commands\CommandHandler; use Lion\Database\Driver; use Lion\Files\Store; +define('IS_INDEX', false); + +define('DEVELOPMENT_ENVIRONMENT', 'dev' === env('DEVELOPMENT_ENVIRONMENT')); + /** * ----------------------------------------------------------------------------- * Register environment variable loader automatically diff --git a/routes/web.php b/routes/web.php index 8a0ffce6..8e323d98 100644 --- a/routes/web.php +++ b/routes/web.php @@ -4,7 +4,9 @@ define('LION_START', microtime(true)); -define('IS_INDEX', false); +define('IS_INDEX', true); + +define('DEVELOPMENT_ENVIRONMENT', 'dev' === env('DEVELOPMENT_ENVIRONMENT')); /** * ----------------------------------------------------------------------------- diff --git a/src/LionBundle/Commands/Lion/Route/PostmanCollectionCommand.php b/src/LionBundle/Commands/Lion/Route/PostmanCollectionCommand.php index 01def8fc..ad63b154 100644 --- a/src/LionBundle/Commands/Lion/Route/PostmanCollectionCommand.php +++ b/src/LionBundle/Commands/Lion/Route/PostmanCollectionCommand.php @@ -9,6 +9,7 @@ use GuzzleHttp\Exception\GuzzleException; use Lion\Bundle\Helpers\Commands\ClassFactory; use Lion\Bundle\Helpers\Commands\PostmanCollection; +use Lion\Bundle\Helpers\Http\Fetch; use Lion\Bundle\Helpers\Http\Routes; use Lion\Command\Command; use Lion\Files\Store; @@ -187,11 +188,15 @@ private function fetchRoutes(): void $this->jsonName = $this->str->of(Carbon::now()->format('Y_m_d'))->concat('_lion_collection')->lower()->get(); $this->routes = json_decode( - fetch(Route::GET, ($_ENV['SERVER_URL'] . '/route-list'), [ - 'headers' => [ - 'Lion-Auth' => $_ENV['SERVER_HASH'] - ] - ])->getBody()->getContents(), + fetch( + new Fetch(Route::GET, ($_ENV['SERVER_URL'] . '/route-list'), [ + 'headers' => [ + 'Lion-Auth' => $_ENV['SERVER_HASH'], + ], + ]) + ) + ->getBody() + ->getContents(), true ); diff --git a/src/LionBundle/Commands/Lion/Route/RouteListCommand.php b/src/LionBundle/Commands/Lion/Route/RouteListCommand.php index 07331a2e..4c67d418 100644 --- a/src/LionBundle/Commands/Lion/Route/RouteListCommand.php +++ b/src/LionBundle/Commands/Lion/Route/RouteListCommand.php @@ -6,6 +6,7 @@ use DI\Attribute\Inject; use GuzzleHttp\Exception\GuzzleException; +use Lion\Bundle\Helpers\Http\Fetch; use Lion\Bundle\Helpers\Http\Routes; use Lion\Command\Command; use Lion\Helpers\Arr; @@ -211,11 +212,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int private function fetchRoutes(): void { $this->routes = json_decode( - fetch(Route::GET, ($_ENV['SERVER_URL'] . '/route-list'), [ - 'headers' => [ - 'Lion-Auth' => $_ENV['SERVER_HASH'] - ] - ])->getBody()->getContents(), + fetch( + new Fetch(Route::GET, ($_ENV['SERVER_URL'] . '/route-list'), [ + 'headers' => [ + 'Lion-Auth' => $_ENV['SERVER_HASH'], + ], + ]) + ) + ->getBody() + ->getContents(), true ); diff --git a/src/LionBundle/Helpers/Bundle/constants.php b/src/LionBundle/Helpers/Bundle/constants.php index 2ff826fa..6c3efa45 100644 --- a/src/LionBundle/Helpers/Bundle/constants.php +++ b/src/LionBundle/Helpers/Bundle/constants.php @@ -2,23 +2,20 @@ declare(strict_types=1); -use GuzzleHttp\Client; use Lion\Helpers\Arr; use Lion\Helpers\Str; use Lion\Request\Request; use Lion\Request\Response; /** - * [Object of the GuzzleHttp Client class] - * - * @var Client + * [Object with properties captured in an HTTP request] */ -const client = new Client(); +define('request', (new Request())->capture()); /** * [Object with properties captured in an HTTP request] */ -define('request', (new Request())->capture()); +define('ssl', (new Request())->capture()); /** * [Object of Response class to generate response objects] diff --git a/src/LionBundle/Helpers/Bundle/helpers.php b/src/LionBundle/Helpers/Bundle/helpers.php index ddfcff17..bc92c68a 100644 --- a/src/LionBundle/Helpers/Bundle/helpers.php +++ b/src/LionBundle/Helpers/Bundle/helpers.php @@ -5,11 +5,13 @@ use Carbon\Carbon; use Faker\Factory; use Faker\Generator; +use GuzzleHttp\Client; use GuzzleHttp\Exception\GuzzleException; use GuzzleHttp\Psr7\Response; use Lion\Bundle\Enums\LogTypeEnum; use Lion\Bundle\Helpers\Env; use Lion\Bundle\Helpers\Fake; +use Lion\Bundle\Helpers\Http\Fetch; use Lion\Files\Store; use Lion\Request\Http; use Lion\Request\Request; @@ -56,20 +58,22 @@ function now(null|DateTimeZone|string $tz = null): Carbon if (!function_exists('fetch')) { /** - * Function to make HTTP requests with guzzlehttp + * Function to make HTTP requests with GuzzleHttp * - * @param string $method [HTTP protocol] - * @param string $uri [URL to make the request] - * @param array $options [Options to send through the request, such as - * headers or parameters] + * @param Fetch $fetch [Defines parameters for consuming HTTP requests with + * GuzzleHttp] * * @return Response * * @throws GuzzleException */ - function fetch(string $method, string $uri, array $options = []): Response + function fetch(Fetch $fetch): Response { - return client->request($method, $uri, $options); + $client = new Client( + null === $fetch->getFetchConfiguration() ? [] : $fetch->getFetchConfiguration()->getConfiguration() + ); + + return $client->request($fetch->getHttpMethod(), $fetch->getUri(), $fetch->getOptions()); } } diff --git a/src/LionBundle/Helpers/Http/Fetch.php b/src/LionBundle/Helpers/Http/Fetch.php index 07778302..2317fe47 100644 --- a/src/LionBundle/Helpers/Http/Fetch.php +++ b/src/LionBundle/Helpers/Http/Fetch.php @@ -42,6 +42,8 @@ public function __construct( * Returns an HTTP configuration object * * @return FetchConfiguration|null + * + * @internal */ public function getFetchConfiguration(): ?FetchConfiguration { @@ -67,6 +69,8 @@ public function setFetchConfiguration(FetchConfiguration $fetchConfiguration): F * Returns the HTTP method of the HTTP request * * @return string + * + * @internal */ public function getHttpMethod(): string { @@ -77,6 +81,8 @@ public function getHttpMethod(): string * Returns the URI of the HTTP request * * @return string + * + * @internal */ public function getUri(): string { @@ -87,6 +93,8 @@ public function getUri(): string * Returns the data from the HTTP request * * @return array + * + * @internal */ public function getOptions(): array { diff --git a/tests/Helpers/Bundle/ConstantsTest.php b/tests/Helpers/Bundle/ConstantsTest.php index b32b948f..7e8ff8ad 100644 --- a/tests/Helpers/Bundle/ConstantsTest.php +++ b/tests/Helpers/Bundle/ConstantsTest.php @@ -4,7 +4,6 @@ namespace Tests\Helpers\Bundle; -use GuzzleHttp\Client; use Lion\Helpers\Arr; use Lion\Helpers\Str; use Lion\Request\Response; @@ -13,13 +12,6 @@ class ConstantsTest extends Test { - #[Testing] - public function clientConstant(): void - { - $this->assertTrue(defined('client')); - $this->assertInstanceOf(Client::class, client); - } - #[Testing] public function requestConstant(): void { diff --git a/tests/Helpers/Bundle/HelpersTest.php b/tests/Helpers/Bundle/HelpersTest.php index aa011eb9..55dd1fad 100644 --- a/tests/Helpers/Bundle/HelpersTest.php +++ b/tests/Helpers/Bundle/HelpersTest.php @@ -9,6 +9,7 @@ use Faker\Generator; use GuzzleHttp\Exception\GuzzleException; use Lion\Bundle\Enums\LogTypeEnum; +use Lion\Bundle\Helpers\Http\Fetch; use Lion\Request\Http; use Lion\Request\Status; use Lion\Security\AES; @@ -86,7 +87,9 @@ public function testNow(): void */ public function testFetch(): void { - $fetchResponse = fetch(Http::GET, env('SERVER_URL'))->getBody()->getContents(); + $fetchResponse = fetch(new Fetch(Http::GET, env('SERVER_URL'))) + ->getBody() + ->getContents(); $response = json_decode($fetchResponse, true); diff --git a/tests/Helpers/Commands/PostmanCollectionTest.php b/tests/Helpers/Commands/PostmanCollectionTest.php index c70a383f..32e95763 100644 --- a/tests/Helpers/Commands/PostmanCollectionTest.php +++ b/tests/Helpers/Commands/PostmanCollectionTest.php @@ -6,6 +6,7 @@ use GuzzleHttp\Exception\GuzzleException; use Lion\Bundle\Helpers\Commands\PostmanCollection; +use Lion\Bundle\Helpers\Http\Fetch; use Lion\Dependency\Injection\Container; use Lion\Files\Store; use Lion\Request\Http; @@ -196,11 +197,13 @@ public function addRequest(string $name, string $route, string $method, array $p public function addRoutes(): void { $routes = json_decode( - fetch(Http::GET, (self::HOST . '/route-list'), [ - 'headers' => [ - 'Lion-Auth' => $_ENV['SERVER_HASH'] - ] - ]) + fetch( + new Fetch(Http::GET, (self::HOST . '/route-list'), [ + 'headers' => [ + 'Lion-Auth' => $_ENV['SERVER_HASH'], + ], + ]) + ) ->getBody() ->getContents(), true @@ -227,11 +230,13 @@ public function addRoutes(): void public function createCollection(): void { $routes = json_decode( - fetch(Http::GET, (self::HOST . '/route-list'), [ - 'headers' => [ - 'Lion-Auth' => $_ENV['SERVER_HASH'] - ] - ]) + fetch( + new Fetch(Http::GET, (self::HOST . '/route-list'), [ + 'headers' => [ + 'Lion-Auth' => $_ENV['SERVER_HASH'], + ], + ]) + ) ->getBody() ->getContents(), true @@ -267,11 +272,13 @@ public function createCollection(): void public function getRoutes(): void { $routes = json_decode( - fetch(Http::GET, (self::HOST . '/route-list'), [ - 'headers' => [ - 'Lion-Auth' => $_ENV['SERVER_HASH'] - ] - ]) + fetch( + new Fetch(Http::GET, (self::HOST . '/route-list'), [ + 'headers' => [ + 'Lion-Auth' => $_ENV['SERVER_HASH'], + ], + ]) + ) ->getBody() ->getContents(), true @@ -302,11 +309,13 @@ public function getRoutes(): void public function getItems(): void { $routes = json_decode( - fetch(Http::GET, (self::HOST . '/route-list'), [ - 'headers' => [ - 'Lion-Auth' => $_ENV['SERVER_HASH'] - ] - ]) + fetch( + new Fetch(Http::GET, (self::HOST . '/route-list'), [ + 'headers' => [ + 'Lion-Auth' => $_ENV['SERVER_HASH'], + ], + ]) + ) ->getBody() ->getContents(), true diff --git a/tests/bootstrap.php b/tests/bootstrap.php index ebedcf60..dc029abd 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -4,8 +4,6 @@ define('LION_START', microtime(true)); -define('IS_INDEX', false); - /** * ----------------------------------------------------------------------------- * Register The Auto Loader @@ -20,6 +18,10 @@ use Dotenv\Dotenv; use Lion\Files\Store; +define('IS_INDEX', false); + +define('DEVELOPMENT_ENVIRONMENT', 'dev' === env('DEVELOPMENT_ENVIRONMENT')); + /** * ----------------------------------------------------------------------------- * Register environment variable loader automatically