diff --git a/composer.json b/composer.json index 30fda17..36c8636 100644 --- a/composer.json +++ b/composer.json @@ -51,7 +51,10 @@ "laravel": { "providers": [ "Suarez\\UtmParameter\\Providers\\UtmParameterServiceProvider" - ] + ], + "aliases": { + "UtmParameter": "Suarez\\UtmParameter\\Facades\\UtmParameter" + } } }, "minimum-stability": "stable", diff --git a/src/Facades/UtmParameter.php b/src/Facades/UtmParameter.php new file mode 100644 index 0000000..ef7f39c --- /dev/null +++ b/src/Facades/UtmParameter.php @@ -0,0 +1,26 @@ +shouldAcceptUtmParameter($request)) { - app(UtmParameter::class)->boot($request); + UtmParameter::boot($request); } return $next($request); diff --git a/src/Providers/UtmParameterServiceProvider.php b/src/Providers/UtmParameterServiceProvider.php index 35c7d75..37fccbf 100644 --- a/src/Providers/UtmParameterServiceProvider.php +++ b/src/Providers/UtmParameterServiceProvider.php @@ -4,6 +4,7 @@ use Illuminate\Support\Facades\Blade; use Suarez\UtmParameter\UtmParameter; +use Illuminate\Foundation\AliasLoader; use Illuminate\Support\ServiceProvider; class UtmParameterServiceProvider extends ServiceProvider @@ -43,5 +44,7 @@ public function boot() Blade::if('containsNotUtm', function (string $key, string|null $value = null) { return contains_not_utm($key, $value); }); + + AliasLoader::getInstance()->alias('UtmParameter', \Suarez\UtmParameter\Facades\UtmParameter::class); } } diff --git a/src/UtmParameter.php b/src/UtmParameter.php index c99a327..7ca3879 100644 --- a/src/UtmParameter.php +++ b/src/UtmParameter.php @@ -48,7 +48,7 @@ public function boot(Request $request) */ public function useRequestOrSession(Request $request) { - $currentRequestParameter = self::getParameter($request); + $currentRequestParameter = $this->getParameter($request); $sessionParameter = session($this->sessionKey); if (!empty($currentRequestParameter) && empty($sessionParameter)) { @@ -70,9 +70,9 @@ public function useRequestOrSession(Request $request) * * @return array */ - public static function all() + public function all() { - return app(UtmParameter::class)->parameters ?? []; + return $this->parameters ?? []; } /** @@ -82,10 +82,10 @@ public static function all() * * @return string|null */ - public static function get(string $key) + public function get(string $key) { - $parameters = self::all(); - $key = self::ensureUtmPrefix($key); + $parameters = $this->all(); + $key = $this->ensureUtmPrefix($key); if (!array_key_exists($key, $parameters)) { return null; @@ -102,17 +102,17 @@ public static function get(string $key) * * @return bool */ - public static function has(string $key, $value = null) + public function has(string $key, $value = null) { - $parameters = self::all(); - $key = self::ensureUtmPrefix($key); + $parameters = $this->all(); + $key = $this->ensureUtmPrefix($key); if (!array_key_exists($key, $parameters)) { return false; } if (array_key_exists($key, $parameters) && $value !== null) { - return self::get($key) === $value; + return $this->get($key) === $value; } return true; @@ -125,16 +125,16 @@ public static function has(string $key, $value = null) * @param string $value * @return bool */ - public static function contains(string $key, string $value) + public function contains(string $key, string $value) { - $parameters = self::all(); - $key = self::ensureUtmPrefix($key); + $parameters = $this->all(); + $key = $this->ensureUtmPrefix($key); if (!array_key_exists($key, $parameters) || !is_string($value)) { return false; } - return str_contains(self::get($key), $value); + return str_contains($this->get($key), $value); } /** @@ -142,10 +142,10 @@ public static function contains(string $key, string $value) * * @return bool */ - public static function clear() + public function clear() { - session()->forget(app(UtmParameter::class)->sessionKey); - app(UtmParameter::class)->parameters = null; + session()->forget($this->sessionKey); + $this->parameters = null; return true; } @@ -175,7 +175,7 @@ protected static function getParameter(Request $request) * @param string $key * @return string */ - protected static function ensureUtmPrefix(string $key): string + protected function ensureUtmPrefix(string $key): string { return str_starts_with($key, 'utm_') ? $key : 'utm_' . $key; } diff --git a/src/helper.php b/src/helper.php index abbccef..2bbf8c7 100644 --- a/src/helper.php +++ b/src/helper.php @@ -1,6 +1,6 @@ singleton(UtmParameter::class, fn () => new UtmParameter()); - app(UtmParameter::class)->boot($request); - session([$this->sessionKey => $parameters]); + UtmParameter::boot($request); } public function tearDown() : void @@ -273,7 +271,7 @@ public function test_it_should_overwrite_new_utm_parameter() ]; $request = Request::create('/', 'GET', $parameters); - app(UtmParameter::class)->boot($request); + UtmParameter::boot($request); $source = UtmParameter::get('source'); $this->assertEquals('newsletter', $source); @@ -298,7 +296,7 @@ public function test_it_should_keep_existing_parameters() ]; $request = Request::create('/test', 'GET', $parameters); - app(UtmParameter::class)->boot($request); + UtmParameter::boot($request); $id = UtmParameter::get('id'); $this->assertEmpty($id); @@ -325,7 +323,7 @@ public function test_it_should_keep_existing_parameters_while_browsing() $parameters = ['id' => '0123456789', 'sorting' => 'relevance']; $request = Request::create('/new-page', 'GET', $parameters); - app(UtmParameter::class)->boot($request); + UtmParameter::boot($request); $id = UtmParameter::get('id'); $this->assertEmpty($id); @@ -340,7 +338,7 @@ public function test_it_should_keep_existing_parameters_while_browsing() $parameters = []; $request = Request::create('/second-page', 'GET', $parameters); - app(UtmParameter::class)->boot($request); + UtmParameter::boot($request); $id = UtmParameter::get('id'); $this->assertEmpty($id); @@ -367,7 +365,7 @@ public function test_it_should_only_use_utm_parameters_in_the_allowed_list() ]; $request = Request::create('/test', 'GET', $parameters); - app(UtmParameter::class)->boot($request); + UtmParameter::boot($request); $source = UtmParameter::get('source'); $this->assertEquals('newsletter', $source); @@ -409,7 +407,7 @@ public function test_it_should_sanitize_utm_parameter() ]; $request = Request::create('/test', 'GET', $parameters); - app(UtmParameter::class)->boot($request); + UtmParameter::boot($request); $source = UtmParameter::get('source'); $this->assertEquals('<span onclick="alert('alert')">google</span>', $source);