Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bpolaszek committed Nov 10, 2017
0 parents commit 38938d8
Show file tree
Hide file tree
Showing 18 changed files with 565 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
vendor
composer.lock
32 changes: 32 additions & 0 deletions .scrutinizer.yml
@@ -0,0 +1,32 @@
filter:
excluded_paths: [tests/*]
checks:
php:
code_rating: true
remove_extra_empty_lines: true
remove_php_closing_tag: true
remove_trailing_whitespace: true
fix_use_statements:
remove_unused: true
preserve_multiple: false
preserve_blanklines: true
order_alphabetically: true
fix_php_opening_tag: true
fix_linefeed: true
fix_line_ending: true
fix_identation_4spaces: true
fix_doc_comments: true
tools:
php_analyzer: true
php_code_coverage: false
php_code_sniffer:
config:
standard: PSR2
filter:
paths: ['src']
php_loc:
enabled: true
excluded_dirs: [vendor, tests]
php_cpd:
enabled: true
excluded_dirs: [vendor, tests]
19 changes: 19 additions & 0 deletions .travis.yml
@@ -0,0 +1,19 @@
dist: xenial

language: php

php:
- 7.1
- 7.2

before_script:
- travis_retry composer self-update
- travis_retry composer install --no-interaction --prefer-source --dev

script:
- ./vendor/bin/phpcs --standard=psr2 -n src/
- mkdir -p build/logs
- ./vendor/bin/phpunit --coverage-clover build/logs/clover.xml

after_script:
- php vendor/bin/coveralls -v
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2017 Benoit POLASZEK

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
42 changes: 42 additions & 0 deletions README.md
@@ -0,0 +1,42 @@
[![Latest Stable Version](https://poser.pugx.org/bentools/uri-factory/v/stable)](https://packagist.org/packages/bentools/uri-factory)
[![License](https://poser.pugx.org/bentools/uri-factory/license)](https://packagist.org/packages/bentools/uri-factory)
[![Build Status](https://img.shields.io/travis/bpolaszek/uri-factory/master.svg?style=flat-square)](https://travis-ci.org/bpolaszek/uri-factory)
[![Coverage Status](https://coveralls.io/repos/github/bpolaszek/uri-factory/badge.svg?branch=master)](https://coveralls.io/github/bpolaszek/uri-factory?branch=master)
[![Quality Score](https://img.shields.io/scrutinizer/g/bpolaszek/uri-factory.svg?style=flat-square)](https://scrutinizer-ci.com/g/bpolaszek/uri-factory)
[![Total Downloads](https://poser.pugx.org/bentools/uri-factory/downloads)](https://packagist.org/packages/bentools/uri-factory)

# Uri Factory

PSR-7 `UriInterface` factory bridge, with automatic discovery of existing libraries in your project:

* `guzzlehttp/psr7`
* `zendframework/zend-diactoros`
* `league/uri`

## Usage

On any URL string:
```php
use function BenTools\UriFactory\Helper\uri;
$uri = uri('http://www.example.net'); // Will convert to a Psr\Http\Message\UriInterface instance
```

On current location:
```php
use function BenTools\UriFactory\Helper\request_uri;
$uri = request_uri(); // Will convert the current location to a Psr\Http\Message\UriInterface instance
```

## Installation

PHP 7.1+ is required.

> composer require bentools/uri-factory 1.0.x-dev
## Tests

> ./vendor/bin/phpunit
## License

MIT.
52 changes: 52 additions & 0 deletions composer.json
@@ -0,0 +1,52 @@
{
"name": "bentools/uri-factory",
"description": "PSR-7 URI factory",
"keywords": ["url", "uri", "psr-7", "psr7", "UriInterface", "factory"],
"authors": [
{
"name": "Beno!t POLASZEK",
"email": "bpolaszek@gmail.com",
"homepage": "http://www.polaszek.fr",
"role": "Developer"
}
],
"type": "library",
"license": "MIT",
"require": {
"php": ">=7.1",
"psr/http-message": "^1.0"
},
"require-dev": {
"guzzlehttp/psr7": "^1.4",
"league/uri": "^5.0",
"phpunit/phpunit": "@stable",
"satooshi/php-coveralls": "@stable",
"squizlabs/php_codesniffer": "@stable",
"symfony/var-dumper": "^3.3",
"zendframework/zend-diactoros": "^1.6"
},
"autoload": {
"psr-4": {
"BenTools\\UriFactory\\": "src"
},
"files": [
"src/Helper/functions.php"
]
},
"autoload-dev": {
"psr-4": {
"BenTools\\UriFactory\\Tests\\": "tests"
},
"files": [
"vendor/symfony/var-dumper/Resources/functions/dump.php"
]
},
"config": {
"sort-packages": true
},
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
}
}
27 changes: 27 additions & 0 deletions phpunit.xml.dist
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
beStrictAboutTestsThatDoNotTestAnything="true"
beStrictAboutOutputDuringTests="true"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
failOnRisky="true"
failOnWarning="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
verbose="true"
>
<testsuite>
<directory suffix="Test.php">tests</directory>
</testsuite>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
</phpunit>
16 changes: 16 additions & 0 deletions src/Adapter/AdapterInterface.php
@@ -0,0 +1,16 @@
<?php

namespace BenTools\UriFactory\Adapter;

use BenTools\UriFactory\UriFactoryInterface;

interface AdapterInterface extends UriFactoryInterface
{

/**
* Check if the dependency is installed or not.
*
* @return bool
*/
public static function isInstalled(): bool;
}
41 changes: 41 additions & 0 deletions src/Adapter/GuzzleAdapter.php
@@ -0,0 +1,41 @@
<?php

namespace BenTools\UriFactory\Adapter;

use BenTools\UriFactory\UriFactoryInterface;
use GuzzleHttp\Psr7\Uri;
use Psr\Http\Message\UriInterface;

final class GuzzleAdapter implements AdapterInterface
{
/**
* GuzzleAdapter constructor.
*/
protected function __construct()
{
}

/**
* @inheritDoc
*/
public function createUri(string $uri): UriInterface
{
return new Uri($uri);
}

/**
* @inheritDoc
*/
public static function factory(): UriFactoryInterface
{
return new self;
}

/**
* @inheritDoc
*/
public static function isInstalled(): bool
{
return class_exists('GuzzleHttp\Psr7\Uri');
}
}
48 changes: 48 additions & 0 deletions src/Adapter/LeagueUriAdapter.php
@@ -0,0 +1,48 @@
<?php

namespace BenTools\UriFactory\Adapter;

use BenTools\UriFactory\UriFactoryInterface;
use League\Uri\Factory;
use Psr\Http\Message\UriInterface;

final class LeagueUriAdapter implements AdapterInterface
{
/**
* @var Factory
*/
private $factory;

/**
* LeagueUriAdapter constructor.
* @param Factory $factory
*/
protected function __construct(Factory $factory)
{
$this->factory = $factory;
}

/**
* @inheritDoc
*/
public function createUri(string $uri): UriInterface
{
return $this->factory->create($uri);
}

/**
* @inheritDoc
*/
public static function factory(Factory $factory = null): UriFactoryInterface
{
return new self($factory ?? new Factory());
}

/**
* @inheritDoc
*/
public static function isInstalled(): bool
{
return class_exists('League\Uri\Factory');
}
}
42 changes: 42 additions & 0 deletions src/Adapter/ZendDiactorosAdapter.php
@@ -0,0 +1,42 @@
<?php

namespace BenTools\UriFactory\Adapter;

use BenTools\UriFactory\UriFactoryInterface;
use Psr\Http\Message\UriInterface;
use Zend\Diactoros\Uri;

final class ZendDiactorosAdapter implements AdapterInterface
{

/**
* ZendDiactorosAdapter constructor.
*/
protected function __construct()
{
}

/**
* @inheritDoc
*/
public function createUri(string $uri): UriInterface
{
return new Uri($uri);
}

/**
* @inheritDoc
*/
public static function factory(): UriFactoryInterface
{
return new self;
}

/**
* @inheritDoc
*/
public static function isInstalled(): bool
{
return class_exists('Zend\Diactoros\Uri');
}
}
24 changes: 24 additions & 0 deletions src/Helper/functions.php
@@ -0,0 +1,24 @@
<?php

namespace BenTools\UriFactory\Helper;

use BenTools\UriFactory\UriFactory;
use Psr\Http\Message\UriInterface;

/**
* @param string $uri
* @return UriInterface
*/
function uri(string $uri): UriInterface
{
return UriFactory::factory()->createUri($uri);
}

/**
* @return UriInterface
* @throws \RuntimeException
*/
function request_uri(): UriInterface
{
return UriFactory::factory()->createFromRequestUri();
}

0 comments on commit 38938d8

Please sign in to comment.