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
Multiple channels with php-cli is not possible #9987
Comments
When executing commands you don't have any frame of reference which channel to use. I would suggest that your command should have an argument that is called channel and then you just load the channel from the repository. |
That's why I propose to add a special I could propose 2 solutions : |
I would suggest a different approach. Write a |
This issue has been automatically marked as stale because it has not had any recent activity. It will be closed in a week if no further activity occurs. Thank you for your contributions. |
Any recommended solution? |
The solution @Prometee suggested should work. It has the following elements:
|
Hey folks! I would like to add my 2 cents here. Contexts by design should be considered as stateful services. They reference to session, current request etc. As a result they should be used mostly in HTTP Controllers or as close as possible to them. As a result all CLI and or MessengerHandlers should be context free. Still, this is a recommendation and not whole code is supporting this case, like related #13545. From the ideological point of view, we even shouldn't be able to determine channel, locale, cart or customer of CLI operation. Perhaps, we should do some high-level early return for all these contexts to not execute services below, as they may be heavy. This doesn't mean that a context where you may define default context may be useful. |
@lchrusciel for that to work, Sylius would need to separate all of it's business logic from the HTTP layer and get the channel as an argument instead of relying on channel context everywhere. Respectively, the channel context should only be used in the HTTP layer to get the current channel and pass it downstream to the application's business layer. |
Yes, you are correct - but I wanted to share generic perspective for them. I didn't say, that we didn't make any mistake or misuse with them. But the truth is, that they are stateful and therefore can make a lot of problems. Perhaps, we should just provide good default values for CLI and other not request based executions. |
This issue becomes more painful with Sylius Plus, when the website has multiple channels and you have a cli command that needs to check inventory of products for whatever reason. With Sylius Plus, one of the default filters when resolving inventory sources is |
Hi @vladfilimon, indeed, it is as you said. We'll propose a solution in the future :) |
I strongly concur with stateless approach, channel, currency, locale, user shouldn't be available in CLI as context. Even if we're to skip messenger point and stateless as default approach, It's not uncommon to have background cli commands, which are specific to certain channels and which cannot accept any other channel as interchangeable. i.e. different channels/currencies/locales/users to different services, instead of one global channel/currency/locale/user to all services. But if there is a big demand for this feature and not just erroneous usage of existing contexts in CLI, then maybe new set of Channel/Currency/Locale/User aware interfaces and provider interface could be done as separate system to context. |
@diimpp I also agree that it should be stateless, but right now it's not possible as the stateful contexts are used all other the place in Sylius. |
@vvasiloi While Sylius "sync" and stateful nature is undeniable, I was working with shop-api based ecommerce for 3 years now with extensive modifications and I can't say I ever encountered context related issues in such stateless and async environment. It would be good to document at least several such context-bottleneck places and devise solution based on practical needs. If refactor is too much and project was at acceptable level for all those years, then maybe this a issue for Sylius V2 to solve. |
…onsole commands (jakubtobiasz) This PR was merged into the 1.13 branch. Discussion ---------- | Q | A | |-----------------|--------------------------------------------------------------| | Branch? | 1.13 <!-- see the comment below --> | | Bug fix? | no | | New feature? | no | | BC breaks? | no | | Deprecations? | no <!-- don't forget to update the UPGRADE-*.md file --> | | Related tickets | related to #9987 and #14549 | | License | MIT | If you need a context, read Łukasz's comment in #14549. Preview: ![CleanShot 2022-12-16 at 17 42 13@2x](https://user-images.githubusercontent.com/80641364/208146910-89e99e04-d33a-488c-8725-69cc74353f3b.png) Commits ------- eccb7dd Add a cookbook about dealing with multiple channels in console commands
Summary: The cookbook: |
Sylius versions affected: > 1.2 I suppose
Description
When having two channels and try to use a command witch use a
ChannelContext
, it throw aChannelNotFoundException
Steps to reproduce
Two channels defined and trying to use
ChannelContext
while executing commands.Possible Solution
Create a
PhpCliChannelContext
(priority: -256
) that check if we are in a command line env and load aChannel
witch can configured by service configuration (calls: [['setDefaultChannelCode', ['MY_DEFAULT_CHANNEL_CODE']]]
) or by a global SyliusChannel
configuration.If the
defaultChannelCode
is not provided, we could fallback to the first channel we found.The text was updated successfully, but these errors were encountered: