33 * Copyright © Magento, Inc. All rights reserved.
44 * See COPYING.txt for license details.
55 */
6+
67namespace Magento \Framework \App \PageCache ;
78
9+ use Magento \Framework \App \Config \ScopeConfigInterface ;
10+ use Magento \Framework \Stdlib \CookieManagerInterface ;
11+ use Magento \Framework \Stdlib \Cookie \CookieMetadataFactory ;
12+ use Magento \Framework \App \Request \Http ;
13+
814/**
915 * PageCache Version
1016 *
@@ -15,53 +21,38 @@ class Version
1521 /**
1622 * Name of cookie that holds private content version
1723 */
18- const COOKIE_NAME = 'private_content_version ' ;
24+ public const COOKIE_NAME = 'private_content_version ' ;
1925
2026 /**
2127 * Ten years cookie period
2228 */
23- const COOKIE_PERIOD = 315360000 ;
24-
25- /**
26- * Cookie Manager
27- *
28- * @var \Magento\Framework\Stdlib\CookieManagerInterface
29- */
30- protected $ cookieManager ;
31-
32- /**
33- * Request
34- *
35- * @var \Magento\Framework\App\Request\Http
36- */
37- protected $ request ;
29+ public const COOKIE_PERIOD = 315360000 ;
3830
3931 /**
40- * @var \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory
32+ * Config setting for disabling session for GraphQl
4133 */
42- protected $ cookieMetadataFactory ;
34+ private const XML_PATH_GRAPHQL_DISABLE_SESSION = ' graphql/session/disable ' ;
4335
4436 /**
45- * @param \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager
46- * @param \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory
47- * @param \Magento\Framework\App\Request\Http $request
37+ * @param CookieManagerInterface $cookieManager
38+ * @param CookieMetadataFactory $cookieMetadataFactory
39+ * @param Http $request
40+ * @param ScopeConfigInterface $scopeConfig
4841 */
4942 public function __construct (
50- \Magento \Framework \Stdlib \CookieManagerInterface $ cookieManager ,
51- \Magento \Framework \Stdlib \Cookie \CookieMetadataFactory $ cookieMetadataFactory ,
52- \Magento \Framework \App \Request \Http $ request
43+ private readonly CookieManagerInterface $ cookieManager ,
44+ private readonly CookieMetadataFactory $ cookieMetadataFactory ,
45+ private readonly Http $ request ,
46+ private readonly ScopeConfigInterface $ scopeConfig
5347 ) {
54- $ this ->cookieManager = $ cookieManager ;
55- $ this ->request = $ request ;
56- $ this ->cookieMetadataFactory = $ cookieMetadataFactory ;
5748 }
5849
5950 /**
6051 * Generate unique version identifier
6152 *
6253 * @return string
6354 */
64- protected function generateValue ()
55+ protected function generateValue (): string
6556 {
6657 //phpcs:ignore
6758 return md5 (rand () . time ());
@@ -75,16 +66,35 @@ protected function generateValue()
7566 *
7667 * @return void
7768 */
78- public function process ()
69+ public function process (): void
7970 {
80- if ($ this ->request ->isPost ()) {
81- $ publicCookieMetadata = $ this ->cookieMetadataFactory ->createPublicCookieMetadata ()
82- ->setDuration (self ::COOKIE_PERIOD )
83- ->setPath ('/ ' )
84- ->setSecure ($ this ->request ->isSecure ())
85- ->setHttpOnly (false )
86- ->setSameSite ('Lax ' );
87- $ this ->cookieManager ->setPublicCookie (self ::COOKIE_NAME , $ this ->generateValue (), $ publicCookieMetadata );
71+ if (!$ this ->request ->isPost ()) {
72+ return ;
8873 }
74+
75+ if ($ this ->request ->getOriginalPathInfo () === '/graphql ' && $ this ->isSessionDisabled () === true ) {
76+ return ;
77+ }
78+
79+ $ publicCookieMetadata = $ this ->cookieMetadataFactory ->createPublicCookieMetadata ()
80+ ->setDuration (self ::COOKIE_PERIOD )
81+ ->setPath ('/ ' )
82+ ->setSecure ($ this ->request ->isSecure ())
83+ ->setHttpOnly (false )
84+ ->setSameSite ('Lax ' );
85+ $ this ->cookieManager ->setPublicCookie (self ::COOKIE_NAME , $ this ->generateValue (), $ publicCookieMetadata );
86+ }
87+
88+ /**
89+ * Returns configuration setting for disable session for GraphQl
90+ *
91+ * @return bool
92+ */
93+ private function isSessionDisabled (): bool
94+ {
95+ return (bool )$ this ->scopeConfig ->getValue (
96+ self ::XML_PATH_GRAPHQL_DISABLE_SESSION ,
97+ ScopeConfigInterface::SCOPE_TYPE_DEFAULT
98+ );
8999 }
90100}
0 commit comments