stepanets/kww-domstorlib-c
C library for a DOM-style storage layer (domstorlib-c), packaged for PHP/Laravel workflows. Provides low-level primitives for storing and retrieving structured data in a DOM-like format, aimed at embedding in other tools rather than end-user apps.
Installation Add the package via Composer:
composer require stepanets/kww-domstorlib-c
Ensure your Laravel project targets PHP 5.6+ (last release compatibility).
First Use Case Basic initialization (adjust for your use case):
use Kww\DomStorLib\DomStorLib;
$domStorLib = new DomStorLib();
$domStorLib->setBaseUrl('https://example.com');
$domStorLib->setUserAgent('MyLaravelApp/1.0');
Where to Look
DomStorLib class methods (e.g., setBaseUrl, getPage, parseResponse).HTTP Requests Use for scraping or interacting with legacy systems:
$response = $domStorLib->getPage('/api/data');
$data = $domStorLib->parseResponse($response);
Data Parsing Chain parsing methods (if supported):
$domStorLib->parseHtml($response)
->extractNodes('//div[@class="target"]')
->getText();
Integration with Laravel
$this->app->singleton(DomStorLib::class, function ($app) {
$lib = new DomStorLib();
$lib->setBaseUrl(config('services.legacy_api.url'));
return $lib;
});
// app/Facades/LegacyApi.php
public static function getData() {
return app(DomStorLib::class)->getPage('/data');
}
Caching Responses Cache responses to avoid repeated requests:
$cacheKey = 'legacy_data_' . md5('/api/data');
$data = Cache::remember($cacheKey, now()->addHours(1), function () use ($domStorLib) {
return $domStorLib->getPage('/api/data');
});
Deprecated Dependencies
dom, curl, or custom C bindings).php -m | grep -E 'dom|curl'
No Laravel-Specific Features
dispatch()).Error Handling
throw_if).try {
$response = $domStorLib->getPage('/data');
} catch (\Exception $e) {
Log::error("Legacy API failed: " . $e->getMessage());
throw new \RuntimeException("API unavailable", 503);
}
Legacy PHP-C API Quirks
// Example: Check if response is a DOMDocument
if ($response instanceof DOMDocument) {
// Parse with DOM methods
}
Enable Verbose Logging Override methods to log requests/responses:
$domStorLib->getPage = function ($url) {
Log::debug("Requesting: " . $url);
return parent::getPage($url);
};
Test with curl First
Verify endpoints manually before integrating:
curl -v -A "MyLaravelApp/1.0" https://example.com/api/data
Fallback for Missing Extensions Use Laravel’s HTTP client as a backup:
$client = new \GuzzleHttp\Client();
$response = $client->get('https://example.com/api/data');
Custom Parsers Extend the library by adding methods:
class ExtendedDomStorLib extends DomStorLib {
public function extractJsonLd() {
$response = $this->getPage('/data');
return json_decode($response, true);
}
}
Event Listeners Dispatch events for critical actions (e.g., API failures):
event(new LegacyApiRequested($url, $response));
Configuration
Centralize settings in config/services.php:
'legacy_api' => [
'base_url' => env('LEGACY_API_URL'),
'timeout' => 30,
],
Then inject into the library:
$domStorLib->setBaseUrl(config('services.legacy_api.base_url'));
How can I help you explore Laravel packages today?