Skip to content
This repository has been archived by the owner on Nov 30, 2022. It is now read-only.

Using config_api_callback with a cached config or inside Laravel Vapor #995

Open
ClaraLeigh opened this issue Oct 14, 2021 · 0 comments
Open

Comments

@ClaraLeigh
Copy link
Contributor

Unfortunately, Laravel doesn't allow you to cache closures in the config files.
So if you ever ran artisan config:cache it would fail when using config_api_callback as a closure.

Now the easy solution is to just not cache your config but in Laravel Vapor, we are kinda forced to.
Therefore I needed to make a workaround, I have included it below.

Also, this might be good to talk about in the docs:
https://github.com/osiset/laravel-shopify/wiki/Multiple-Custom-Apps-on-Same-Codebase

Workaround

In the shopify-app.php config file

update:
'config_api_callback' => '\App\Providers\AppServiceProvider::shopifyConfigClosure',

add:

    'domain_api_keys' => collect($_ENV)->filter(
        fn($value, $key) => \Illuminate\Support\Str::startsWith($key, ['API_KEY_', 'API_SECRET_'])
    )->toArray(),

In the AppServiceProvider.php

add:

    public static function shopifyConfigClosure(string $key, $shop)
    {
        $fullKey = "shopify-app.{$key}";
        if (! $shop) {
            // No shop passed, return default
            return \Config::get($fullKey);
        }

        // Clean the shop domain
        $shopDomain = $shop instanceof \Osiset\ShopifyApp\Contracts\Objects\Values\ShopDomain ? $shop->toNative() : $shop;
        $shopDomain = preg_replace('/[^A-Z0-9]/', '', strtoupper(explode('.', $shopDomain)[0]));

        // Try to get env defined for shop, fallback to config value
        return \Config::get(
            "shopify-app.domain_api_keys." . strtoupper($key)."_".$shopDomain,
            \Config::get($fullKey)
        );
    }
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant