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

[Http] Fix Http Client #23

Merged
merged 7 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion .git-blame-ignore-revs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ eb2793b4c08a4aae2ff7cefc61c336778a33d6a8
0c213c750e950f6e9ee4e8f2632ec41ac29a70e0
f4e014138ae1c12a4f8a7f9778535ccb7bf1c440
8e8e706c5f9ccd0aff6641ccac231dc12088af02
d1a762ce41196836e002bf963cff67359cf7c4ea
7e9c40547e404268d95e36e6b2ba197ae86a60bd
c9a16382e4be9bf7adda885cabac1de7634b784c
c9a16382e4be9bf7adda885cabac1de7634b784cca7287695de423a8644c517a57567f33a6ad923e
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"encoredigitalgroup/tachyon": "^2.0",
"guzzlehttp/guzzle": "6.x|7.x",
"illuminate/console": "^10|^11",
"illuminate/container": "^10|^11",
"illuminate/database": "^10|^11",
"illuminate/filesystem": "^10|^11",
"illuminate/http": "^10|^11",
Expand Down
164 changes: 161 additions & 3 deletions src/Common/src/Container/PhpGenesisContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@

use Illuminate\Container\Container as IlluminateContainer;
use Illuminate\Foundation\Application;
use Illuminate\Support\ServiceProvider;

/** @experimental */
class PhpGenesisContainer extends IlluminateContainer
class PhpGenesisContainer extends IlluminateContainer implements \Illuminate\Contracts\Foundation\Application
{
public static function getInstance(): IlluminateContainer|PhpGenesisContainer
{
Expand All @@ -25,13 +26,170 @@ public static function getInstance(): IlluminateContainer|PhpGenesisContainer
return static::$instance;
}

public static function isLaravel(): bool
{
return static::isLaravelApplication();
}

protected static function isLaravelApplication(): bool
{
return class_exists(Application::class);
}

public function isLaravel(): bool
public function version(): string
{
return static::isLaravelApplication();
return '';
}

public function basePath($path = ''): string
{
return '';
}

public function bootstrapPath($path = ''): string
{
return '';
}

public function configPath($path = ''): string
{
return '';
}

public function databasePath($path = ''): string
{
return '';
}

public function langPath($path = ''): string
{
return '';
}

public function publicPath($path = '')
{
return '';
}

public function resourcePath($path = '')
{
return '';
}

public function storagePath($path = '')
{
return '';
}

public function environment(...$environments): string|bool
{
return '';
}

public function runningInConsole(): bool
{
return false;
}

public function runningUnitTests(): bool
{
return false;
}

public function hasDebugModeEnabled(): false
{
return false;
}

/** @phpstan-ignore-next-line */
public function maintenanceMode(): bool
{
return false;
}

public function isDownForMaintenance(): bool
{
return false;
}

public function registerConfiguredProviders(): bool
{
return false;
}

/** @phpstan-ignore-next-line */
public function register($provider, $force = false): void {}

public function registerDeferredProvider($provider, $service = null): string
{
return $provider;
}

public function resolveProvider($provider): string|ServiceProvider
{
return $provider;
}

public function boot()
{
// TODO: Implement boot() method.
}

public function booting($callback)
{
// TODO: Implement booting() method.
}

public function booted($callback)
{
// TODO: Implement booted() method.
}

public function bootstrapWith(array $bootstrappers)
{
// TODO: Implement bootstrapWith() method.
}

public function getLocale(): string
{
return '';
}

public function getNamespace(): string
{
return '';
}

public function getProviders($provider): array
{
return [];
}

public function hasBeenBootstrapped(): bool
{
return false;
}

public function loadDeferredProviders()
{
// TODO: Implement loadDeferredProviders() method.
}

public function setLocale($locale)
{
// TODO: Implement setLocale() method.
}

public function shouldSkipMiddleware(): bool
{
return false;
}

/** @phpstan-ignore-next-line */
public function terminating($callback): void {}

public function terminate()
{
// TODO: Implement terminate() method.
}
}
4 changes: 3 additions & 1 deletion src/Http/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"license": "proprietary",
"require": {
"php": "^8.3",
"illuminate/http": "^10|^11"
"illuminate/container": "^10|^11",
"illuminate/http": "^10|^11",
"illuminate/support": "^10|^11"
},
"autoload": {
"psr-4": {
Expand Down
41 changes: 11 additions & 30 deletions src/Http/src/HttpClientBuilder.php
Original file line number Diff line number Diff line change
@@ -1,47 +1,28 @@
<?php
/*
* Copyright (c) 2024. Encore Digital Group.
* All Right Reserved.
*/

namespace PHPGenesis\Http;

use Illuminate\Http\Client\Factory as HttpFactory;
use Illuminate\Support\Facades\Http as HttpFacade;
use Illuminate\Support\Facades\Facade;
use PHPGenesis\Common\Container\PhpGenesisContainer;

/** @experimental */
class HttpClientBuilder
{
protected PhpGenesisContainer $container;

public function __construct()
{
$this->container = PhpGenesisContainer::getInstance();
$this->setupHttpFacade();
}

protected function setupHttpFacade(): HttpFacade|HttpFactory
{
if ($this->container->isLaravel()) {
// Use Laravel's existing Http facade
return HttpFacade::getFacadeRoot();
}
if (!PhpGenesisContainer::isLaravel()) {
// Step 1: Initialize the service container
$this->container = new PhpGenesisContainer();

// Set up a new container and Http facade
$clientFactory = new HttpFactory();
// Step 2: Bind the HttpFactory to the container
$this->container->singleton('http', function () {
return new HttpFactory();
});

// Bind the HTTP Factory to the container
$this->container->getInstance()->singleton('http', function () use ($clientFactory) {
return $clientFactory;
});

// Alias the Http facade to the HTTP Factory
HttpFacade::setFacadeApplication(null);
HttpFacade::resolved(function ($httpFactory): void {
HttpFacade::swap($httpFactory);
});

return $clientFactory;
// Step 3: Set the container as the facade application
Facade::setFacadeApplication($this->container);
}
}
}