bentools / uri-factory
PSR-7 URI factory
Installs: 19 259
Dependents: 5
Suggesters: 0
Security: 0
Stars: 4
Watchers: 3
Forks: 0
Open Issues: 0
Requires
- php: >=8.0
- psr/http-factory: ^1.0
- psr/http-message: ^1.0 || ^2.0
Requires (Dev)
- guzzlehttp/psr7: ^2.5
- league/uri: ^5.3
- nyholm/psr7: ^1.6
- php-coveralls/php-coveralls: ^2.5
- phpunit/phpunit: ^9.6
- ringcentral/psr7: ^1.3
- squizlabs/php_codesniffer: @stable
- symfony/var-dumper: ^5.4 || ^6.2
Suggests
- guzzlehttp/psr7: PSR-7 HTTP message library
- league/uri: URI manipulation Library
- nyholm/psr7: PSR-7 HTTP message library
- ringcentral/psr7: PSR-7 HTTP message library
README
Uri Factory
A library / framework agnostic PSR-7 UriInterface
factory.
There are several PSR-7 libraries on packagist but each one has its own factory for creating an UriInterface
object from a string.
bentools/uri-factory has no explicit dependency and automatically picks up your favorite library for creating UriInterface
instances.
Supported libraries so far:
guzzlehttp/psr7
league/uri
nyholm/psr7
ringcentral/psr7
Usage
On any URL string:
use function BenTools\UriFactory\Helper\uri; $uri = uri('http://www.example.net'); // Will convert to a Psr\Http\Message\UriInterface instance
On current location:
use function BenTools\UriFactory\Helper\current_location; $uri = current_location(); // Will convert the current location to a Psr\Http\Message\UriInterface instance
You can specify which library to use, by using the corresponding adapter:
use BenTools\UriFactory\Adapter\GuzzleAdapter; use BenTools\UriFactory\Adapter\LeagueUriAdapter; use function BenTools\UriFactory\Helper\current_location; use function BenTools\UriFactory\Helper\uri; $uri = uri('http://www.example.net', GuzzleAdapter::factory()); $uri = uri('http://www.example.net', LeagueUriAdapter::factory()); $uri = current_location(GuzzleAdapter::factory()); $uri = current_location(LeagueUriAdapter::factory());
Canonicalizer
This library ships with an URL canonicalizer.
It is not a perfect one since your PSR-7 library may behave differently regarding special chars, but it should work most of the time.
The canonicalize()
function accepts any PSR-7 UriInterface
object and will return a canonicalized one.
require_once __DIR__ . '/vendor/autoload.php'; use function BenTools\UriFactory\Helper\canonicalize; use function BenTools\UriFactory\Helper\uri; $url = 'http://example.org../foo/../bar/?#baz'; echo canonicalize(uri($url)); // http://example.org/bar/
Installation
PHP 8.0+ is required.
composer require bentools/uri-factory
Tests
./vendor/bin/phpunit
Contribute
If you want bentools/uri-factory to support more PSR-7 libraries, feel free to submit a PR.
License
MIT.