Skip to content

Commit 3c41f48

Browse files
committed
🚀 Initial commit
0 parents  commit 3c41f48

26 files changed

+1061
-0
lines changed

‎.editorconfig

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
insert_final_newline = true
7+
indent_style = space
8+
indent_size = 4
9+
trim_trailing_whitespace = true
10+
11+
[*.md]
12+
trim_trailing_whitespace = false
13+
14+
[*.yml]
15+
indent_size = 2

‎.gitattributes

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
* text=auto
2+
3+
tests/ export-ignore
4+
.editorconfig export-ignore
5+
.gitattributes export-ignore
6+
.gitignore export-ignore
7+
.scrutinizer.yml export-ignore
8+
.styleci.yml export-ignore
9+
.travis.yml export-ignore
10+
phpunit.xml.dist export-ignore

‎.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
vendor/
2+
.idea/
3+
composer.lock
4+
phpunit.xml
5+
.phpunit.result.cache

‎.scrutinizer.yml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
filter:
2+
paths:
3+
- 'src/*'
4+
excluded_paths:
5+
- 'tests/*'
6+
- 'vendor/*'
7+
8+
checks:
9+
php: true
10+
11+
tools:
12+
external_code_coverage:
13+
runs: 29
14+
timeout: 900

‎.styleci.yml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
preset: psr2
2+
3+
enabled:
4+
- alpha_ordered_imports
5+
- concat_with_spaces
6+
- no_trailing_comma_in_list_call
7+
- no_trailing_comma_in_singleline_array
8+
- no_unused_imports
9+
- no_useless_else
10+
- no_useless_return
11+
- no_whitespace_before_comma_in_array
12+
- short_array_syntax
13+
- trailing_comma_in_multiline_array
14+
15+
risky: true
16+
17+
finder:
18+
exclude:
19+
- vendor
20+
name: "*.php"

‎.travis.yml

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
language: php
2+
3+
cache:
4+
directories:
5+
- $HOME/.composer/cache
6+
7+
php:
8+
- 7.1
9+
- 7.2
10+
- 7.3
11+
12+
env:
13+
- ILLUMINATE_VERSION=5.1.* TESTBENCH_VERSION=3.1.* PHPUNIT_VERSION=^5.0
14+
- ILLUMINATE_VERSION=5.2.* TESTBENCH_VERSION=3.2.* PHPUNIT_VERSION=^5.0
15+
- ILLUMINATE_VERSION=5.3.* TESTBENCH_VERSION=3.3.* PHPUNIT_VERSION=^5.0
16+
- ILLUMINATE_VERSION=5.4.* TESTBENCH_VERSION=3.4.* PHPUNIT_VERSION=^5.0
17+
- ILLUMINATE_VERSION=5.5.* TESTBENCH_VERSION=3.5.* PHPUNIT_VERSION=^6.0
18+
- ILLUMINATE_VERSION=5.6.* TESTBENCH_VERSION=3.6.* PHPUNIT_VERSION=^7.0
19+
- ILLUMINATE_VERSION=5.7.* TESTBENCH_VERSION=3.7.* PHPUNIT_VERSION=^7.0
20+
- ILLUMINATE_VERSION=5.8.* TESTBENCH_VERSION=3.8.* PHPUNIT_VERSION=^7.0 || ^8.0
21+
22+
sudo: false
23+
24+
before_install:
25+
- composer require illuminate/support:"${ILLUMINATE_VERSION}" --no-update
26+
- composer require orchestra/testbench:"${TESTBENCH_VERSION}" --no-update
27+
- composer require phpunit/phpunit:"${PHPUNIT_VERSION}" --no-update
28+
29+
install:
30+
- composer install --no-interaction --no-progress --no-suggest --prefer-dist
31+
32+
script:
33+
- ./vendor/bin/phpunit --coverage-clover=coverage.clover --verbose
34+
35+
after_script:
36+
- wget https://scrutinizer-ci.com/ocular.phar
37+
- php ocular.phar code-coverage:upload --format=php-clover coverage.clover

‎LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 LaraCrafts
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

‎README.md

+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# laravel-url-shortener
2+
Powerful URL shortening tools in Laravel
3+
4+
<p align="center">
5+
<a href="https://travis-ci.org/LaraCrafts/laravel-url-shortener"><img src="https://travis-ci.org/LaraCrafts/laravel-url-shortener.svg?branch=master"></a>
6+
<a href="https://packagist.org/packages/laracrafts/laravel-url-shortenern"><img src="https://poser.pugx.org/laracrafts/laravel-url-shortener/downloads"></a>
7+
<a href="https://packagist.org/packages/laracrafts/laravel-url-shortener"><img src="https://poser.pugx.org/laracrafts/laravel-url-shortener/version"></a>
8+
<a href="https://scrutinizer-ci.com/g/LaraCrafts/laravel-url-shortener/"><img src="https://scrutinizer-ci.com/g/LaraCrafts/laravel-url-shortener/badges/coverage.png?b=master"></a>
9+
<a href="https://packagist.org/packages/laracrafts/laravel-url-shortener"><img src="https://poser.pugx.org/laracrafts/laravel-url-shortener/license"></a>
10+
</p>
11+
12+
- [Installation](#installation)
13+
- [Requirements](#requirements)
14+
- [Laravel 5.5+](#laravel-55)
15+
- [Laravel 5.1-5.4](#laravel-51-54)
16+
- [Lumen](#lumen)
17+
- [Usage](#usage)
18+
- [Changing the default driver](#changing-the-default-driver)
19+
- [Available drivers](#available-drivers)
20+
- [Bit.ly](#bitly)
21+
- [Shorte.st](#shortest)
22+
- [TinyURL](#tinyurl)
23+
24+
## Installation
25+
You can easily install this package using Composer, by running the following command:
26+
27+
```bash
28+
composer require laracrafts/laravel-url-shortener
29+
```
30+
31+
### Requirements
32+
This package has the following requirements:
33+
34+
- PHP 7.1 or higher
35+
- Laravel (or Lumen) 5.1 or higher
36+
37+
### Laravel 5.5+
38+
If you use Laravel 5.5 or higher, that's it. You can now use the package, continue to the [usage](#usage) section.
39+
40+
### Laravel 5.1-5.4
41+
If you're using an older version of Laravel, register the package's service provider to your application. You can do
42+
this by adding the following line to your `config/app.php` file:
43+
44+
```php
45+
'providers' => [
46+
...
47+
LaraCrafts\UrlShortener\UrlShortenerServiceProvider::class,
48+
...
49+
],
50+
```
51+
52+
### Lumen
53+
If you're using Lumen, register the package's service provider by adding the following line to your `bootstrap/app.php`
54+
file:
55+
56+
```php
57+
$app->register(LaraCrafts\UrlShortener\UrlShortenerServiceProvider::class);
58+
```
59+
60+
## Usage
61+
The shortener can be retrieved from the container in two ways:
62+
63+
```php
64+
// This works in both Laravel and Lumen
65+
$shortener = app('url.shortener');
66+
// This only works in Laravel 5.2+
67+
$shortener = url()->shortener();
68+
```
69+
70+
Once you have an instance of the shortener, you can shorten your URLs:
71+
72+
```php
73+
// This will return your shortened URL as a string
74+
$shortener->shorten(...);
75+
76+
// This will return a promise which will resolve to your shortened URL
77+
$shortener->shortenAsync(...);
78+
79+
// You can also call shortening from Laravel's url component directly
80+
app('url')->shorten(...);
81+
82+
// or...
83+
url()->shorten(...);
84+
```
85+
86+
This package relies on Guzzle's promise library for its asynchronous shortening, read their
87+
[documentation](https://github.com/guzzle/promises) for more information.
88+
89+
You can also use dependency injection to inject the shortener into a method:
90+
91+
```php
92+
class MyController extends Controller
93+
{
94+
public function myFunction(ShortenerManager $shortener)
95+
{
96+
$shortener->shorten(...);
97+
}
98+
}
99+
```
100+
101+
The shortener exposes the following methods:
102+
103+
Method | Description
104+
---------------|-------------------------------------
105+
`shorten` | Shorten the given URL
106+
`shortenAsync` | Shorten the given URL asynchronously
107+
`driver` | Retrieve a driver (e.g. `tiny_url`)
108+
109+
### Changing the default driver
110+
You can change the default driver by setting `URL_SHORTENER_DRIVER={driver}` in your environment file or publishing the
111+
config file and changing it directly.
112+
113+
## Available drivers
114+
Below is a list of available drivers along with their individual specs:
115+
116+
Service | Driver name | Since version | Analytics | Monetization
117+
-----------------------|-------------|---------------|-----------|-----------------
118+
[Bit.ly](#bitly) | `bit_ly` | 1.0.0 | yes | no
119+
[Shorte.st](#shortest) | `shorte_st` | 1.0.0 | yes | yes
120+
[TinyURL](#tinyurl) | `tiny_url` | 1.0.0 | no | no
121+
122+
### Bit.ly
123+
[website](https://bit.ly)
124+
125+
This driver uses the Bit.ly API version 4, which requires an access token. Currently only _generic access tokens_ are
126+
supported, which you can set using the `BIT_LY_API_TOKEN` environment variable. This driver also supports custom domains
127+
(if you have a paid Bit.ly account) which can be set through the `BIT_LY_DOMAIN` environment variable.
128+
129+
### Shorte.st
130+
[website](https://shorte.st)
131+
132+
This driver uses the Shorte.st API, which requires an access token. You can set this token by adding the
133+
`SHORTE_ST_API_TOKEN` environment variable or publishing and editing the package config file.
134+
135+
### TinyURL
136+
[website](http://tinyurl.com)
137+
138+
This driver uses the TinyURL API and does not require any additional setup, this driver is also the package default.

‎composer.json

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{
2+
"name": "laracrafts/laravel-url-shortener",
3+
"description": "Powerful URL shortening tools in Laravel",
4+
"keywords": [
5+
"laravel",
6+
"lumen",
7+
"url",
8+
"shortening",
9+
"shortener",
10+
"bit.ly",
11+
"tinyURL",
12+
"shorte.st"
13+
],
14+
"type": "library",
15+
"license": "MIT",
16+
"authors": [
17+
{
18+
"name": "Choraimy Kroonstuiver",
19+
"email": "[email protected]"
20+
}
21+
],
22+
"require": {
23+
"php": "^7.1",
24+
"guzzlehttp/guzzle": "^6.2",
25+
"guzzlehttp/promises": "^1.0",
26+
"guzzlehttp/psr7": "^1.0",
27+
"illuminate/support": "^5.1,<5.9",
28+
"psr/http-message": "^1.0"
29+
},
30+
"require-dev": {
31+
"orchestra/testbench": "^3.1",
32+
"phpunit/phpunit": "^5.0 || ^6.0 || ^7.0 || ^8.0"
33+
},
34+
"autoload": {
35+
"psr-4": {
36+
"LaraCrafts\\UrlShortener\\": "src/"
37+
}
38+
},
39+
"autoload-dev": {
40+
"psr-4": {
41+
"LaraCrafts\\UrlShortener\\Tests\\": "tests/"
42+
}
43+
},
44+
"config": {
45+
"sort-packages": true
46+
},
47+
"extra": {
48+
"laravel": {
49+
"providers": [
50+
"\\LaraCrafts\\UrlShortener\\UrlShortenerServiceProvider"
51+
]
52+
}
53+
}
54+
}

‎config/url-shortener.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
return [
4+
5+
'default' => env('URL_SHORTENER_DRIVER', 'tiny_url'),
6+
7+
'drivers' => [
8+
9+
'bit_ly' => [
10+
'domain' => env('BIT_LY_DOMAIN', 'bit.ly'),
11+
'token' => env('BIT_LY_API_TOKEN'),
12+
],
13+
14+
'shorte_st' => [
15+
'token' => env('SHORTE_ST_API_TOKEN'),
16+
],
17+
18+
'tiny_url' => [
19+
//
20+
],
21+
],
22+
];

‎phpunit.xml.dist

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false">
11+
<testsuites>
12+
<testsuite name="Integration">
13+
<directory suffix="Test.php">./tests/Integration</directory>
14+
</testsuite>
15+
<testsuite name="Unit">
16+
<directory suffix="Test.php">./tests/Unit</directory>
17+
</testsuite>
18+
</testsuites>
19+
<filter>
20+
<whitelist processUncoveredFilesFromWhitelist="true">
21+
<directory suffix=".php">./src</directory>
22+
</whitelist>
23+
</filter>
24+
</phpunit>

‎src/Contracts/AsyncShortener.php

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace LaraCrafts\UrlShortener\Contracts;
4+
5+
interface AsyncShortener extends Shortener
6+
{
7+
/**
8+
* Shorten the given URL asynchronously.
9+
*
10+
* @param \Psr\Http\Message\UriInterface|string $url
11+
* @param array $options
12+
* @return \GuzzleHttp\Promise\PromiseInterface
13+
*/
14+
public function shortenAsync($url, array $options = []);
15+
}

0 commit comments

Comments
 (0)