Skip to content
This repository was archived by the owner on May 20, 2019. It is now read-only.

Commit 56f3580

Browse files
committed
Add Plugin
1 parent dad84f4 commit 56f3580

File tree

1 file changed

+98
-0
lines changed
  • app/code/Magento/WebapiAsync/Plugin/Cache

1 file changed

+98
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\WebapiAsync\Plugin\Cache;
10+
11+
use Magento\WebapiAsync\Controller\Rest\AsynchronousSchemaRequestProcessor;
12+
use Magento\Framework\Webapi\Rest\Request;
13+
14+
/**
15+
* Class Webapi
16+
*/
17+
class Webapi
18+
{
19+
/**
20+
* Cache key for Async Routes
21+
*/
22+
const ASYNC_ROUTES_CONFIG_CACHE_ID = 'async-routes-services-config';
23+
24+
/**
25+
* @var AsynchronousSchemaRequestProcessor
26+
*/
27+
private $asynchronousSchemaRequestProcessor;
28+
29+
/**
30+
* @var \Magento\Framework\Webapi\Rest\Request
31+
*/
32+
private $request;
33+
34+
/**
35+
* ServiceMetadata constructor.
36+
*
37+
* @param Request $request
38+
* @param AsynchronousSchemaRequestProcessor $asynchronousSchemaRequestProcessor
39+
*/
40+
public function __construct(
41+
\Magento\Framework\Webapi\Rest\Request $request,
42+
AsynchronousSchemaRequestProcessor $asynchronousSchemaRequestProcessor
43+
) {
44+
$this->request = $request;
45+
$this->asynchronousSchemaRequestProcessor = $asynchronousSchemaRequestProcessor;
46+
}
47+
48+
/**
49+
* Change identifier in case if Async request before cache load
50+
*
51+
* @param \Magento\Webapi\Model\Cache\Type\Webapi $subject
52+
* @param string $identifier
53+
* @return null|string
54+
*/
55+
public function beforeLoad(\Magento\Webapi\Model\Cache\Type\Webapi $subject, $identifier)
56+
{
57+
if ($this->asynchronousSchemaRequestProcessor->canProcess($this->request)
58+
&& $identifier === \Magento\Webapi\Model\ServiceMetadata::ROUTES_CONFIG_CACHE_ID) {
59+
return self::ASYNC_ROUTES_CONFIG_CACHE_ID;
60+
}
61+
return null;
62+
}
63+
64+
/**
65+
* Change identifier in case if Async request before cache save
66+
*
67+
* @param \Magento\Webapi\Model\Cache\Type\Webapi $subject
68+
* @param $data
69+
* @param string $identifier
70+
* @param array $tags
71+
* @param null $lifeTime
72+
* @return array|null
73+
*/
74+
public function beforeSave(\Magento\Webapi\Model\Cache\Type\Webapi $subject, $data, $identifier, array $tags = [], $lifeTime = null)
75+
{
76+
if ($this->asynchronousSchemaRequestProcessor->canProcess($this->request)
77+
&& $identifier === \Magento\Webapi\Model\ServiceMetadata::ROUTES_CONFIG_CACHE_ID) {
78+
return [$data, self::ASYNC_ROUTES_CONFIG_CACHE_ID, $tags, $lifeTime];
79+
}
80+
return null;
81+
}
82+
83+
/**
84+
* Change identifier in case if Async request before remove cache
85+
*
86+
* @param \Magento\Webapi\Model\Cache\Type\Webapi $subject
87+
* @param string $identifier
88+
* @return null|string
89+
*/
90+
public function beforeRemove(\Magento\Webapi\Model\Cache\Type\Webapi $subject, $identifier)
91+
{
92+
if ($this->asynchronousSchemaRequestProcessor->canProcess($this->request)
93+
&& $identifier === \Magento\Webapi\Model\ServiceMetadata::ROUTES_CONFIG_CACHE_ID) {
94+
return self::ASYNC_ROUTES_CONFIG_CACHE_ID;
95+
}
96+
return null;
97+
}
98+
}

0 commit comments

Comments
 (0)