Installation:
composer require yansongda/supports
Ensure your project uses PHP 8.0+ and Guzzle 7.x (Laravel 9+ compatible).
First Use Case: HTTP Request Handling
Use the HasHttpRequest trait to simplify Guzzle HTTP calls in a service or controller:
use Yansongda\Supports\Traits\HasHttpRequest;
class MyService {
use HasHttpRequest;
public function fetchData() {
return $this->get('https://api.example.com/data', [
'timeout' => 10,
'connectTimeout' => 5,
]);
}
}
Note: The trait uses Guzzle under the hood. Ensure your Laravel app’s config/http.php aligns with these settings.
First Use Case: Array/Config Utilities
Leverage array manipulation methods (e.g., array_get, array_set):
use Yansongda\Supports\Helpers\Arr;
$config = [
'settings' => [
'theme' => 'dark',
],
];
$theme = Arr::get($config, 'settings.theme'); // Returns 'dark'
Arr::set($config, 'settings.theme', 'light'); // Modifies array
Pipeline Integration (Advanced) Use the package’s pipeline support for workflows (e.g., request processing):
use Yansongda\Supports\Pipeline;
$result = Pipeline::send($request)
->through([
function ($request) { return $request->validate(); },
function ($request) { return $this->logRequest($request); },
])
->then(function ($request) { return $this->process($request); });
HasHttpRequest trait to enforce consistent timeouts, retries, and headers.class ApiClient {
use HasHttpRequest;
public function __construct() {
$this->client = $this->client()->withOptions([
'timeout' => 30,
'retries' => 3,
'headers' => ['Accept' => 'application/json'],
]);
}
public function getUser($id) {
return $this->get("/users/{$id}");
}
}
ApiClient to Laravel’s container in AppServiceProvider for dependency injection.Arr helper for deep array operations (e.g., nested config validation, dynamic defaults).use Yansongda\Supports\Helpers\Arr;
// Merge configs with defaults
$merged = Arr::merge([
'defaults' => ['timeout' => 30],
], $userConfig);
// Get nested value with fallback
$timeout = Arr::get($merged, 'settings.timeout', 10);
Arr facade to include these methods if needed:
Facade::register('Arr', \Yansongda\Supports\Helpers\Arr::class);
Pipeline class.use Yansongda\Supports\Pipeline;
$result = Pipeline::send($data)
->through([
function ($data) { return $this->validateData($data); },
function ($data) { return $this->sanitizeData($data); },
])
->then(function ($data) { return $this->storeData($data); });
Pipeline facade to include package-specific pipes if needed.class DataProcessor {
use HasHttpRequest, \Yansongda\Supports\Traits\Arrayable;
public function process() {
$rawData = $this->get('https://api.example.com/data');
return $this->arrayFlatten($rawData['nested']['structure']);
}
}
// app/Providers/AppServiceProvider.php
public function boot() {
// Bind HasHttpRequest to a container alias
$this->app->bind('apiClient', function () {
$client = new class { use HasHttpRequest; };
return $client;
});
}
HasHttpRequest to fetch external data during validation:
use Yansongda\Supports\Traits\HasHttpRequest;
class StoreUserRequest extends FormRequest {
use HasHttpRequest;
public function rules() {
$userExists = $this->get("https://api.example.com/users/{$this->user_id}");
return [
'email' => ['required', 'unique:users,email,' . ($userExists ? $this->user_id : null)],
];
}
}
class ProcessDataMiddleware {
public function handle($request, Closure $next) {
return Pipeline::send($request)
->through([
[$this, 'validateRequest'],
[$this, 'logRequest'],
])
->then($next);
}
}
Yansongda\Supports\Logger.Log facade:
// Before (broken)
Logger::info('Message');
// After
\Log::info('Message');
// config/app.php
'aliases' => [
'GuzzleHttp' => \GuzzleHttp\Client::class,
],
get, set) may collide with Laravel classes (e.g., Illuminate\Database\Eloquent\Model).// Avoid
$this->get('key');
// Use
\Yansongda\Supports\Helpers\Arr::get($array, 'key');
Pipeline may not fully align with Laravel’s Illuminate\Pipeline\Pipeline.use Illuminate\Support\Facades\Pipeline as LaravelPipeline;
$result = LaravelPipeline::send($data)
->through([...])
->via(YansongdaPipelineAdapter::class)
->then(...);
spatie/array as an alternative).class MyService {
use HasHttpRequest;
public function get($uri, $options = []) {
\Log::debug('HTTP GET called with URI: ' . $uri);
return parent::get($uri, $options);
}
}
$client = (new class { use HasHttpRequest; })->client();
\Log::debug('Guzzle config:', $client->getConfig());
dd() to inspect array structures:
$value = Arr::get($config, 'nested.key');
dd($value, Arr::all($config)); // Debug full array
connectTimeout NamingconnectTimeout (camelCase), while Guzzle may expect connect_timeoutHow can I help you explore Laravel packages today?