Skip to content

Commit 4f7809a

Browse files
committed
- init commit
0 parents  commit 4f7809a

13 files changed

+649
-0
lines changed

.github/workflows/tests-php8.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: Test PHP 8.x
2+
3+
on:
4+
push:
5+
paths-ignore:
6+
- 'readme.md'
7+
pull_request:
8+
paths-ignore:
9+
- 'readme.md'
10+
release:
11+
12+
jobs:
13+
tests:
14+
runs-on: ubuntu-latest
15+
16+
strategy:
17+
matrix:
18+
laravel: [6.*, 7.*, 8.*]
19+
php: [8.0, 8.1]
20+
fail-fast: false
21+
22+
name: Laravel ${{ matrix.laravel }}, PHP ${{ matrix.php }}
23+
24+
steps:
25+
- name: Check out code
26+
uses: actions/checkout@v2
27+
28+
- name: Set up PHP
29+
uses: shivammathur/setup-php@v2
30+
with:
31+
php-version: ${{ matrix.php }}
32+
extension: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick
33+
34+
- name: Install dependencies
35+
run: |
36+
composer require "illuminate/support:${{ matrix.laravel }}" "illuminate/contracts:${{ matrix.laravel }}" --no-interaction --no-update
37+
composer update --prefer-stable --prefer-dist --no-interaction --no-suggest
38+
- name: Run tests
39+
run: vendor/bin/phpunit

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor
2+
composer.lock
3+
*.cache

.php-cs-fixer.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
use PhpCsFixer\Config;
4+
use PhpCsFixer\Finder;
5+
6+
$rules = [
7+
'array_syntax' => ['syntax' => 'short'],
8+
'@PSR2' => true,
9+
'phpdoc_inline_tag_normalizer' => true,
10+
'phpdoc_tag_type' => true,
11+
'phpdoc_no_access' => true,
12+
'phpdoc_no_package' => true,
13+
'phpdoc_no_useless_inheritdoc' => true,
14+
'phpdoc_scalar' => true,
15+
'phpdoc_single_line_var_spacing' => true,
16+
'phpdoc_summary' => true,
17+
'phpdoc_to_comment' => true,
18+
'phpdoc_trim' => true,
19+
'phpdoc_types' => true,
20+
];
21+
22+
23+
$finder = Finder::create()
24+
->in([
25+
__DIR__ . '/src',
26+
__DIR__ . '/tests',
27+
])
28+
->name('*.php')
29+
->notName('*.blade.php')
30+
->ignoreDotFiles(true)
31+
->ignoreVCS(true);
32+
33+
$config = new Config();
34+
return $config->setFinder($finder)
35+
->setRules($rules)
36+
->setRiskyAllowed(true)
37+
->setUsingCache(true);

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Changelog
2+
3+
## 1.0.0
4+
5+
- Init Commit :)

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) <Toni Suarez>
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
13+
all 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
21+
THE SOFTWARE.

README.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# Laravel UTM-Parameters
2+
3+
- [Introduction](#introduction)
4+
- [Installation](#installation)
5+
- [Middleware](#middleware)
6+
- [Usage](#usage)
7+
- [get_all_utm](#all-utm-parameters)
8+
- [get_utm](#Certain-utm-parameter)
9+
- [has_utm|has_not_utm](#determine-utm-parameter)
10+
- [License](#license)
11+
12+
---
13+
14+
## Introduction
15+
16+
A lightweight way to handle UTM-Parameters session-based in your Laravel Application.
17+
18+
*Example*
19+
20+
```blade
21+
@if(has_utm('source', 'newsletter'))
22+
<p>Special Content for Newsletter-Subscriber.</p>
23+
@endif
24+
```
25+
26+
## Installation
27+
28+
Install the `utm-parameter` package with composer:
29+
30+
```
31+
$ composer require suarez/laravel-utm-parameter
32+
```
33+
34+
### Middleware
35+
36+
Open `app/Http/Kernel.php` and add a new item to the `web` middleware group:
37+
38+
```php
39+
protected $middlewareGroups = [
40+
'web' => [
41+
/* ... keep the existing middleware here */
42+
\Suarez\UtmParameter\Middleware\UtmParameters::class,
43+
],
44+
];
45+
```
46+
47+
If you want to selectively make UTM-Parameters available on specific requests to your site, you should instead add a new mapping to the `routeMiddleware` array:
48+
49+
```php
50+
protected $routeMiddleware = [
51+
/* ... keep the existing mappings here */
52+
'utm-parameters' => \Suarez\UtmParameter\Middleware\UtmParameters::class,
53+
];
54+
```
55+
56+
To make UTM-Parameters available for certain given requests, use the `utm-parameters` middleware:
57+
58+
```php
59+
Route::middleware('utm-parameters')->get('langing-page/{slug}', 'LandingPageController@show');
60+
```
61+
62+
## Usage
63+
64+
### All UTM-Parameters
65+
66+
To get all UTM-Parameters as an array, you can use `get_all_utm()`.
67+
68+
### Certain UTM-Parameter
69+
70+
If you wish to retrieve certain UTM-Parameter, you can use ``get_utm('source|medium|campaign|term|content')`.
71+
72+
**For example:**
73+
74+
```blade
75+
<p>You came from {{ get_utm('source') }}</p>
76+
```
77+
78+
### Determine UTM-Parameter
79+
80+
Sometimes you want to show or do something, if user might have some or specific utm-parameters. Simply use `has_utm('source|medium|campaign|term|content', 'optional-value')` or `has_not_utm('source|medium|campaign|term|content', 'optional-value')`
81+
82+
**For example:**
83+
84+
```blade
85+
@if(has_utm('term'))
86+
<p>You have any term.</p>
87+
@end
88+
```
89+
90+
or
91+
92+
```php
93+
if (has_utm('campaign', 'special-sale')) {
94+
redirect('to/special-sale/page');
95+
}
96+
```
97+
98+
---
99+
100+
## License
101+
102+
The Laravel UTM-Parameters package is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).

composer.json

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"name": "suarez/laravel-utm-parameter",
3+
"description": "A little helper to store and handle utm-parameter",
4+
"homepage": "https://github.com/toni-suarez/laravel-utm-parameter",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Toni Suarez",
9+
"email": "[email protected]",
10+
"role": "Developer"
11+
}
12+
],
13+
"require": {
14+
"php": "^8.0",
15+
"illuminate/support": "^8.0",
16+
"illuminate/contracts": "^8.0"
17+
},
18+
"require-dev": {
19+
"nunomaduro/collision": "^5.10",
20+
"orchestra/testbench": "^6.22",
21+
"phpunit/phpunit": "^9.5",
22+
"friendsofphp/php-cs-fixer": "^3.0"
23+
},
24+
"autoload": {
25+
"psr-4": {
26+
"Suarez\\UtmParameter\\": "src"
27+
},
28+
"files": [
29+
"src/helper.php"
30+
]
31+
},
32+
"autoload-dev": {
33+
"psr-4": {
34+
"Suarez\\UtmParameter\\Tests\\": "tests"
35+
}
36+
},
37+
"scripts": {
38+
"test": "vendor/bin/phpunit"
39+
},
40+
"config": {
41+
"sort-packages": true
42+
},
43+
"extra": {
44+
"laravel": {
45+
"providers": [
46+
"Suarez\\UtmParameter\\Providers\\UtmParameterServiceProvider"
47+
]
48+
}
49+
},
50+
"minimum-stability": "dev",
51+
"prefer-stable": true
52+
}

phpunit.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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="Unit">
13+
<directory suffix="Test.php">./tests/Unit</directory>
14+
</testsuite>
15+
</testsuites>
16+
<filter>
17+
<whitelist processUncoveredFilesFromWhitelist="true">
18+
<directory suffix=".php">./src</directory>
19+
</whitelist>
20+
</filter>
21+
</phpunit>

src/Middleware/UtmParameters.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Suarez\UtmParameter\Middleware;
4+
5+
use Closure;
6+
use Illuminate\Http\Request;
7+
use Illuminate\Http\Response;
8+
use Suarez\UtmParameter\UtmParameter;
9+
10+
class UtmParameters
11+
{
12+
/**
13+
* Handle an incoming request.
14+
*
15+
* @param \Illuminate\Http\Request $request
16+
* @param \Closure $next
17+
* @return mixed
18+
*/
19+
public function handle(Request $request, Closure $next)
20+
{
21+
$response = $next($request);
22+
23+
if ($this->shouldAcceptUtmParameter($request, $response)) {
24+
app(UtmParameter::class)->boot(session('utm'));
25+
}
26+
27+
return $response;
28+
}
29+
30+
/**
31+
* Determines whether the given request/response pair should accept UTM-Parameters.
32+
*
33+
* @param \Illuminate\Http\Request $request
34+
* @param \Illuminate\Http\Response $response
35+
* @return bool
36+
*/
37+
protected function shouldAcceptUtmParameter(Request $request, Response $response)
38+
{
39+
return $request->isMethod('GET') && $response->getStatusCode() == 200;
40+
}
41+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Suarez\UtmParameter\Providers;
4+
5+
use Suarez\UtmParameter\UtmParameter;
6+
use Illuminate\Support\ServiceProvider;
7+
8+
class UtmParameterServiceProvider extends ServiceProvider
9+
{
10+
/**
11+
* Register any application services.
12+
*
13+
* @return void
14+
*/
15+
public function register()
16+
{
17+
$this->app->singleton(UtmParameter::class, function () {
18+
return new UtmParameter();
19+
});
20+
}
21+
22+
/**
23+
* Bootstrap any application services.
24+
*
25+
* @return void
26+
*/
27+
public function boot()
28+
{
29+
//
30+
}
31+
}

0 commit comments

Comments
 (0)