-
-
Notifications
You must be signed in to change notification settings - Fork 178
Replace rest with graphql for theme level support #360
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
enmaboya
wants to merge
21
commits into
Kyon147:master
Choose a base branch
from
enmaboya:replace_rest_with_graphql_for_theme_level_support
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
97db04b
add theme actions
enmaboya f5afea8
remove theme helper
enmaboya bc93798
update tests
enmaboya 3bb2e3f
clean asset data json
enmaboya 917cb23
set default asset content
enmaboya 2aab761
upgrade PHP requirement: 8.0 → 8.1, code adjustments
enmaboya f35990d
normalize composer json
enmaboya fb92729
update phpstan
enmaboya 1f0a5b6
add optional baseline
enmaboya 1a1bbb5
Merge branch 'chore/bump-php-min-8.1' of github.com:enmaboya/laravel-…
enmaboya f145bc2
Merge branch 'master' into replace_rest_with_graphql_for_theme_level_…
enmaboya 8c755c5
Merge branch 'master' into replace_rest_with_graphql_for_theme_level_…
enmaboya 6ec7f20
Merge remote-tracking branch 'upstream/master' into replace_rest_with…
enmaboya 112041f
Merge remote-tracking branch 'upstream/master'
enmaboya a05fdb0
Merge branch 'master' into replace_rest_with_graphql_for_theme_level_…
enmaboya b5f3e41
Merge remote-tracking branch 'upstream/master'
enmaboya 8353fa0
Merge branch 'master' into replace_rest_with_graphql_for_theme_level_…
enmaboya a258f72
simplification of the ThemeSupportLevel configuration
enmaboya 9ec56c5
add test for store with only product template
enmaboya ede1a66
add store theme level support tests
enmaboya a8f550d
update tests, add app extensions test with two assets
enmaboya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Osiset\ShopifyApp\Actions; | ||
|
|
||
| use Illuminate\Support\Facades\Log; | ||
| use Osiset\ShopifyApp\Contracts\ShopModel; | ||
|
|
||
| final class FetchMainTheme | ||
| { | ||
| /** | ||
| * @return array{id: ?string, name: ?string} | ||
| */ | ||
| public function handle(ShopModel $shop): array | ||
| { | ||
| $response = $shop->api()->graph('{ | ||
| themes(first: 1, roles: MAIN) { | ||
| nodes { | ||
| id | ||
| name | ||
| } | ||
| } | ||
| }'); | ||
|
|
||
| if ($response['errors']) { | ||
| Log::error('Fetching main theme error: '.json_encode($response['errors'])); | ||
|
|
||
| return []; | ||
| } | ||
|
|
||
| return data_get($response['body']->toArray(), 'data.themes.nodes.0', []); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Osiset\ShopifyApp\Actions; | ||
|
|
||
| use Illuminate\Support\Facades\Log; | ||
| use Osiset\ShopifyApp\Contracts\ShopModel; | ||
|
|
||
| final class FetchThemeAssets | ||
| { | ||
| /** | ||
| * @param string[] $filenames | ||
| */ | ||
| public function handle(ShopModel $shop, string $mainThemeId, array $filenames): array | ||
| { | ||
| $response = $shop->api()->graph('query ($id: ID!, $filenames: [String!]) { | ||
| theme(id: $id) { | ||
| id | ||
| name | ||
| role | ||
| files(filenames: $filenames) { | ||
| nodes { | ||
| filename | ||
| body { | ||
| ... on OnlineStoreThemeFileBodyText { | ||
| content | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }', [ | ||
| 'id' => $mainThemeId, | ||
| 'filenames' => array_values($filenames), | ||
| ]); | ||
|
|
||
| if ($response['errors']) { | ||
| Log::error('Fetching settings data error: '.json_encode($response['errors'])); | ||
|
|
||
| return []; | ||
| } | ||
|
|
||
| $nodes = data_get($response['body']->toArray(), 'data.theme.files.nodes', []); | ||
|
|
||
| return array_values(array_filter(array_map(function (array $data) { | ||
| $content = data_get($data, 'body.content'); | ||
|
|
||
| if ($content === null) { | ||
| Log::warning('Theme file body is not text, skipping: '.data_get($data, 'filename', '?')); | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| return [ | ||
| 'filename' => $data['filename'], | ||
| 'content' => $content, | ||
| ]; | ||
| }, $nodes))); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,45 +1,119 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Osiset\ShopifyApp\Actions; | ||
|
|
||
| use Osiset\ShopifyApp\Contracts\Queries\Shop as IShopQuery; | ||
| use Osiset\ShopifyApp\Contracts\ShopModel; | ||
| use Osiset\ShopifyApp\Objects\Enums\ThemeSupportLevel; | ||
| use Osiset\ShopifyApp\Objects\Values\ShopId; | ||
| use Osiset\ShopifyApp\Services\ThemeHelper; | ||
|
|
||
| class VerifyThemeSupport | ||
| final class VerifyThemeSupport | ||
| { | ||
| private const ASSET_FILE_NAMES = ['templates/product.json', 'templates/collection.json', 'templates/index.json']; | ||
|
enmaboya marked this conversation as resolved.
|
||
|
|
||
| private const MAIN_ROLE = 'main'; | ||
|
|
||
| public function __construct( | ||
| protected IShopQuery $shopQuery, | ||
| protected ThemeHelper $themeHelper | ||
| private IShopQuery $shopQuery, | ||
| private FetchMainTheme $fetchMainTheme, | ||
| private FetchThemeAssets $fetchThemeAssets, | ||
| ) { | ||
| } | ||
|
|
||
| public function __invoke(ShopId $shopId): int | ||
| { | ||
| $this->themeHelper->extractStoreMainTheme($shopId); | ||
|
|
||
| if ($this->themeHelper->themeIsReady()) { | ||
| $templateJSONFiles = $this->themeHelper->templateJSONFiles(); | ||
| $templateMainSections = $this->themeHelper->mainSections($templateJSONFiles); | ||
| $sectionsWithAppBlock = $this->themeHelper->sectionsWithAppBlock($templateMainSections); | ||
| $shop = $this->shopQuery->getById($shopId); | ||
|
|
||
| $hasTemplates = count($templateJSONFiles) > 0; | ||
| $allTemplatesHasRightType = count($templateJSONFiles) === count($sectionsWithAppBlock); | ||
| $templatesСountWithRightType = count($sectionsWithAppBlock); | ||
| /** @var array{id: string, name: string} */ | ||
| $mainTheme = $this->fetchMainTheme->handle($shop); | ||
|
|
||
| switch (true) { | ||
| case $hasTemplates && $allTemplatesHasRightType: | ||
| return ThemeSupportLevel::FULL; | ||
| if (isset($mainTheme['id'])) { | ||
| /** @var array<int, array{filename: string, content: string}> */ | ||
| $assets = $this->fetchThemeAssets->handle( | ||
| shop: $shop, | ||
| mainThemeId: $mainTheme['id'], | ||
| filenames: self::ASSET_FILE_NAMES | ||
| ); | ||
| $templateMainSections = $this->mainSections( | ||
| shop: $shop, | ||
|
enmaboya marked this conversation as resolved.
|
||
| mainTheme: $mainTheme, | ||
| assets: $assets | ||
| ); | ||
| $sectionsWithAppBlock = $this->sectionsWithAppBlock($templateMainSections); | ||
|
|
||
| case $templatesСountWithRightType: | ||
| return ThemeSupportLevel::PARTIAL; | ||
| $hasTemplates = count($assets) > 0; | ||
| $allTemplatesHasRightType = count($assets) === count($sectionsWithAppBlock); | ||
| $hasTemplatesCountWithRightType = count($sectionsWithAppBlock) > 0; | ||
|
|
||
| default: | ||
| return ThemeSupportLevel::UNSUPPORTED; | ||
| } | ||
| return match (true) { | ||
| $hasTemplates && $allTemplatesHasRightType => ThemeSupportLevel::FULL, | ||
| $hasTemplatesCountWithRightType => ThemeSupportLevel::PARTIAL, | ||
| default => ThemeSupportLevel::UNSUPPORTED | ||
| }; | ||
| } | ||
|
|
||
| return ThemeSupportLevel::UNSUPPORTED; | ||
| } | ||
|
|
||
| /** | ||
| * @param array{id: string} $mainTheme | ||
| * @param array{content: string} $assets | ||
| * | ||
| * @return array<int, array{filename: string, content: string}> | ||
| */ | ||
| private function mainSections(ShopModel $shop, array $mainTheme, array $assets): array | ||
| { | ||
| $filenamesForMainSections = array_filter( | ||
| array_map(function ($asset) { | ||
| $content = $asset['content']; | ||
|
|
||
| if (! $this->json_validate($content)) { | ||
| $content = preg_replace("#(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|([\s\t]//.*)|(^//.*)#", '', $content); | ||
| } | ||
|
|
||
| $assetContent = json_decode($content, true); | ||
|
|
||
|
|
||
| $mainAsset = array_filter($assetContent['sections'], function ($value, $key) { | ||
| return $key == self::MAIN_ROLE || str_starts_with($value['type'], self::MAIN_ROLE); | ||
| }, ARRAY_FILTER_USE_BOTH); | ||
|
|
||
| if ($mainAsset) { | ||
| return 'sections/'.end($mainAsset)['type'].'.liquid'; | ||
| } | ||
| }, $assets) | ||
| ); | ||
|
|
||
| return $this->fetchThemeAssets->handle( | ||
| shop: $shop, | ||
| mainThemeId: $mainTheme['id'], | ||
| filenames: $filenamesForMainSections | ||
| ); | ||
| } | ||
|
|
||
| private function sectionsWithAppBlock(array $templateMainSections): array | ||
| { | ||
| return array_filter(array_map(function ($file) { | ||
| $acceptsAppBlock = false; | ||
|
|
||
| preg_match('/\{\%-?\s+schema\s+-?\%\}([\s\S]*?)\{\%-?\s+endschema\s+-?\%\}/m', $file['content'], $matches); | ||
| $schema = json_decode($matches[1] ?? '{}', true); | ||
|
|
||
| if ($schema && isset($schema['blocks'])) { | ||
| $acceptsAppBlock = in_array('@app', array_column($schema['blocks'], 'type')); | ||
| } | ||
|
|
||
| return $acceptsAppBlock ? $file : null; | ||
| }, $templateMainSections)); | ||
| } | ||
|
|
||
|
|
||
| private function json_validate(string $string): bool | ||
| { | ||
| json_decode($string); | ||
|
|
||
| return json_last_error() === JSON_ERROR_NONE; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.