From f19afc41f0555cdfc244e9b178b5866f12441af3 Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Sun, 16 Nov 2025 22:52:07 -0600 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9C=A8=20Implement=20Trakt=20config=20lo?= =?UTF-8?q?ading=20from=20JSON=20file=20with=20ENV=20fallback?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 3 +- composer.lock | 160 ++++++++++++++++++++++++++++++++++++++++++++++- config/trakt.php | 28 ++++++--- 3 files changed, 180 insertions(+), 11 deletions(-) diff --git a/composer.json b/composer.json index c130c1b..728138d 100644 --- a/composer.json +++ b/composer.json @@ -37,7 +37,8 @@ "laravel-zero/framework": "^10.0", "nunomaduro/termwind": "^1.3", "php-ffmpeg/php-ffmpeg": "^1.0", - "spatie/laravel-data": "^2.0" + "spatie/laravel-data": "^2.0", + "xdg/base-directory": "^1.2" }, "require-dev": { "fakerphp/faker": "^1.9.1", diff --git a/composer.lock b/composer.lock index f09a1ad..94880d3 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "9ebd8521e376754effa9bc94f7afbc9e", + "content-hash": "dbc82eba2362bad2cc7266710afb0827", "packages": [ { "name": "brick/math", @@ -5166,6 +5166,76 @@ ], "time": "2024-09-25T14:21:43+00:00" }, + { + "name": "symfony/filesystem", + "version": "v6.4.24", + "source": { + "type": "git", + "url": "https://github.com/symfony/filesystem.git", + "reference": "75ae2edb7cdcc0c53766c30b0a2512b8df574bd8" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/filesystem/zipball/75ae2edb7cdcc0c53766c30b0a2512b8df574bd8", + "reference": "75ae2edb7cdcc0c53766c30b0a2512b8df574bd8", + "shasum": "" + }, + "require": { + "php": ">=8.1", + "symfony/polyfill-ctype": "~1.8", + "symfony/polyfill-mbstring": "~1.8" + }, + "require-dev": { + "symfony/process": "^5.4|^6.4|^7.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Symfony\\Component\\Filesystem\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Provides basic utilities for the filesystem", + "homepage": "https://symfony.com", + "support": { + "source": "https://github.com/symfony/filesystem/tree/v6.4.24" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://github.com/nicolas-grekas", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2025-07-10T08:14:14+00:00" + }, { "name": "symfony/finder", "version": "v6.4.24", @@ -6907,6 +6977,94 @@ "source": "https://github.com/webmozarts/assert/tree/1.12.0" }, "time": "2025-10-20T12:43:39+00:00" + }, + { + "name": "xdg/base-directory", + "version": "1.2", + "source": { + "type": "git", + "url": "https://github.com/php-xdg/base-directory.git", + "reference": "7a91de4c387e9e0d31b8ab6aa564dc85f546b381" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-xdg/base-directory/zipball/7a91de4c387e9e0d31b8ab6aa564dc85f546b381", + "reference": "7a91de4c387e9e0d31b8ab6aa564dc85f546b381", + "shasum": "" + }, + "require": { + "php": ">=8.1", + "symfony/filesystem": "^5.4||^6.0", + "xdg/environment": "^2.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Xdg\\BaseDirectory\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "ju1ius", + "email": "jules.bernable@gmail.com" + } + ], + "description": "Implementation of the XDG Base Directory Specification", + "support": { + "issues": "https://github.com/php-xdg/base-directory/issues", + "source": "https://github.com/php-xdg/base-directory/tree/1.2" + }, + "time": "2023-03-02T08:43:12+00:00" + }, + { + "name": "xdg/environment", + "version": "2.1", + "source": { + "type": "git", + "url": "https://github.com/php-xdg/environment.git", + "reference": "d626d63e902150bbf326756e4b78eea461fd7c12" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-xdg/environment/zipball/d626d63e902150bbf326756e4b78eea461fd7c12", + "reference": "d626d63e902150bbf326756e4b78eea461fd7c12", + "shasum": "" + }, + "require": { + "php": ">=8.1" + }, + "type": "library", + "autoload": { + "psr-4": { + "Xdg\\Environment\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "ju1ius", + "email": "jules.bernable@gmail.com" + } + ], + "description": "An abstraction layer over environment variables.", + "keywords": [ + "environment", + "environment-variables", + "freedesktop", + "xdg" + ], + "support": { + "issues": "https://github.com/php-xdg/environment/issues", + "source": "https://github.com/php-xdg/environment/tree/2.1" + }, + "time": "2023-03-13T19:24:46+00:00" } ], "packages-dev": [ diff --git a/config/trakt.php b/config/trakt.php index 5b91722..90b13f3 100644 --- a/config/trakt.php +++ b/config/trakt.php @@ -1,11 +1,21 @@ env('TRAKT_APP_ID'), - 'app_secret' => env('TRAKT_APP_secret'), - 'redirect_uri' => env('TRAKT_REDIRECT_URI', 'urn:ietf:wg:oauth:2.0:oob'), - 'login' => [ - 'device' => json_decode(env('TRAKT_DEVICE_RESP', '{}}'), true), - 'oauth' => json_decode(env('TRAKT_OAUTH_RESP', '{}}'), true), - ], -]; +use Xdg\BaseDirectory\XdgBaseDirectory as Xdg; +use Illuminate\Support\Arr; + +$data = []; + +$xdg = Xdg::fromEnvironment(); +$path = $xdg->findDataPath("trakt/data.json", file_exists(...)); +if (file_exists($path)) { + $data = json_decode(file_get_contents($path), true); +} + +$data = Arr::add($data, 'app_id', env('TRAKT_APP_ID')); +$data = Arr::add($data, 'app_secret', env('TRAKT_APP_SECRET')); +$data = Arr::add($data, 'redirect_uri', env('TRAKT_REDIRECT_URI', 'urn:ietf:wg:oauth:2.0:oob')); +$data['login'] = $data['login'] ?? []; +$data['login'] = Arr::add($data['login'], 'device', json_decode(env('TRAKT_DEVICE_RESP', '{}'), true)); +$data['login'] = Arr::add($data['login'], 'oauth', json_decode(env('TRAKT_OAUTH_RESP', '{}'), true)); + +return $data; From dd7196572c3d430e9af66ee1af353cab0a7a004e Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Mon, 17 Nov 2025 00:03:08 -0600 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=94=92=20Persist=20Trakt=20authentica?= =?UTF-8?q?tion=20tokens=20to=20config=20JSON=20file?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Commands/TraktDebug.php | 5 ++++- app/Commands/TraktLogin.php | 7 ++++++- app/Commands/TraktRefreshLogin.php | 7 ++++++- config/trakt.php | 1 + 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/app/Commands/TraktDebug.php b/app/Commands/TraktDebug.php index f9a783e..99aa120 100644 --- a/app/Commands/TraktDebug.php +++ b/app/Commands/TraktDebug.php @@ -6,13 +6,14 @@ namespace App\Commands; use App\Services\Trakt; use Illuminate\Support\Arr; +use Illuminate\Contracts\Config\Repository; class TraktDebug extends Command { protected $signature = 'trakt:debug {endpoint : Trakt API endpoint to call} {method=GET : HTTP method to use} {--d|data=* : data}'; protected $description = 'Make arbitrary requests to Trakt API.' . PHP_EOL . '-d should be formatted with key=value or key:=value for non-strings'; - public function handle(Trakt $trakt): int + public function handle(Trakt $trakt, Repository $config): int { $body = $this->getBody(); $method = $this->argument('method'); @@ -28,6 +29,8 @@ class TraktDebug extends Command $resp = $resp->send($method, $url); $this->line('Response (' . $resp->status() . '):' . PHP_EOL . json_encode($resp->json(), JSON_PRETTY_PRINT)); + $this->line('Config: ' . json_encode($config->get('trakt'))); + return static::SUCCESS; } diff --git a/app/Commands/TraktLogin.php b/app/Commands/TraktLogin.php index 1be6a85..1380ef8 100644 --- a/app/Commands/TraktLogin.php +++ b/app/Commands/TraktLogin.php @@ -6,12 +6,14 @@ namespace App\Commands; use App\Services\Trakt; use Illuminate\Contracts\Config\Repository; +use Illuminate\Filesystem\Filesystem; class TraktLogin extends Command { protected $signature = 'trakt:login'; protected $description = 'Login and get new token.'; - public function handle(Trakt $trakt, Repository $config): int + + public function handle(Trakt $trakt, Repository $config, Filesystem $files): int { $data = [ 'client_id' => $config->get('trakt.app_id'), @@ -31,6 +33,7 @@ class TraktLogin extends Command } $this->line("TRAKT_DEVICE_RESP='" . json_encode($body) . "'"); + $config->set('trakt.login.device', $body); $data = [ 'client_id' => $config->get('trakt.app_id'), @@ -48,6 +51,8 @@ class TraktLogin extends Command $body = $resp->json(); $this->line("TRAKT_OAUTH_RESP='" . json_encode($body) . "'"); $this->line('TRAKT_OAUTH_TOKEN=' . $body['access_token']); + $config->set('trakt.login.oauth', $body); + $files->put($config->get('trakt.path'), json_encode($config->get('trakt'), JSON_PRETTY_PRINT)); return static::SUCCESS; diff --git a/app/Commands/TraktRefreshLogin.php b/app/Commands/TraktRefreshLogin.php index 3baf499..4e5acea 100644 --- a/app/Commands/TraktRefreshLogin.php +++ b/app/Commands/TraktRefreshLogin.php @@ -6,12 +6,14 @@ namespace App\Commands; use App\Services\Trakt; use Illuminate\Contracts\Config\Repository; +use Illuminate\Filesystem\Filesystem; class TraktRefreshLogin extends Command { protected $signature = 'trakt:refresh'; protected $description = 'Refresh access token.'; - public function handle(Trakt $trakt, Repository $config): int + + public function handle(Trakt $trakt, Repository $config, Filesystem $files): int { $refresh_token = $config->get('trakt.login.oauth.refresh_token'); if (empty($refresh_token)) { @@ -40,6 +42,9 @@ class TraktRefreshLogin extends Command $this->line("TRAKT_OAUTH_RESP='" . json_encode($body) . "'"); $this->line('TRAKT_OAUTH_TOKEN=' . $body['access_token']); + $config->set('trakt.login.oauth', $body); + $files->put($config->get('trakt.path'), json_encode($config->get('trakt'), JSON_PRETTY_PRINT)); + return static::SUCCESS; } } diff --git a/config/trakt.php b/config/trakt.php index 90b13f3..5da2121 100644 --- a/config/trakt.php +++ b/config/trakt.php @@ -11,6 +11,7 @@ if (file_exists($path)) { $data = json_decode(file_get_contents($path), true); } +$data['path'] = $path; $data = Arr::add($data, 'app_id', env('TRAKT_APP_ID')); $data = Arr::add($data, 'app_secret', env('TRAKT_APP_SECRET')); $data = Arr::add($data, 'redirect_uri', env('TRAKT_REDIRECT_URI', 'urn:ietf:wg:oauth:2.0:oob'));