php-JiraCloud-RESTAPI icon indicating copy to clipboard operation
php-JiraCloud-RESTAPI copied to clipboard

Issue of loading .env variables (Laravel 11.9)

Open t-poeschl opened this issue 1 year ago • 3 comments

Strangely enough, we occasionally have the problem that after a deploy to Laravel Forge, the variables from the .env can still be loaded at the beginning and suddenly 1-2 days later no longer or then the following error occurs:

TypeError: JiraCloud\Configuration\AbstractConfiguration::getJiraHost(): Return value must be of type string, null returned in /home/forge/------/vendor/lesstif/jira-cloud-restapi/src/Configuration/AbstractConfiguration.php:93
Stack trace:
#0 /home/forge/------/vendor/lesstif/jira-cloud-restapi/src/JiraClient.php(400): JiraCloud\Configuration\AbstractConfiguration->getJiraHost()
#1 /home/forge/------/vendor/lesstif/jira-cloud-restapi/src/JiraClient.php(191): JiraCloud\JiraClient->createUrlByContext()
#2 /home/forge/------/vendor/lesstif/jira-cloud-restapi/src/Issue/IssueService.php(506): JiraCloud\JiraClient->exec()

Is the problem known?

t-poeschl avatar Nov 21 '24 19:11 t-poeschl

relatively sure this is the issue (i'm having the same trouble)

https://laravel.com/docs/12.x/configuration#configuration-caching

Once the configuration has been cached, your application's .env file will not be loaded by the framework during requests or Artisan commands; therefore, the env function will only return external, system level environment variables.

vendor/lesstif/jira-cloud-restapi/src/Configuration/DotEnvConfiguration.php line 17 to 46 use the $_ENV

briavers avatar Mar 11 '25 14:03 briavers

Hi @t-poeschl @briavers sorry to reply late. Did you guys resolve it yourselves? do you need to change code?

lesstif avatar Apr 05 '25 12:04 lesstif

Hey @lesstif

I was able to solve it by creating my own configuration, extending yours, and implementing that every time I created a client instance. Since this issue is something specifically for Laravel, I feel the only solution would be to add it to the docs "For laravel, you need to do this extra thing if your caching your env varbiables" since other PHP frameworks don't cache the .env variables as persistently (as far as i'm aware).

<?php
namespace App\Services;

use JiraCloud\Configuration\DotEnvConfiguration;

class JiraServiceConfigration extends DotEnvConfiguration
{
    public function __construct()
    {
        parent::__construct();

        $this->jiraHost = config('services.jira.host');
        $this->jiraUser = config('services.jira.username');
        $this->jiraPassword = config('services.jira.access_token');
    }
}

use App\Services\JiraServiceConfigration;
use JiraCloud\Issue\IssueService
....
...
....
$client = new IssueService(new JiraServiceConfigration());

briavers avatar Apr 07 '25 08:04 briavers