Skip to content

Commit a4b7e6d

Browse files
authored
Merge pull request #1 from comwrap/initial
Initial commit
2 parents 3922ecf + 1c89e29 commit a4b7e6d

File tree

8 files changed

+193
-1
lines changed

8 files changed

+193
-1
lines changed

Model/Config.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Comwrap\WebapiAsyncObjectValidation\Model;
6+
7+
use Magento\Framework\App\Request\Http as HttpRequest;
8+
9+
class Config
10+
{
11+
public const BULK_PROCESSOR_PATH_REGEXP = "/\/async\/bulk(\\/V.+)/";
12+
public const ARRAY_MODE_HEADER_KEY = 'disable-object-validation';
13+
14+
/** @var HttpRequest */
15+
private HttpRequest $request;
16+
17+
/** @var null|bool */
18+
private ?bool $isArrayMode = null;
19+
20+
/**
21+
* @param HttpRequest $request
22+
*/
23+
public function __construct(HttpRequest $request)
24+
{
25+
$this->request = $request;
26+
}
27+
28+
/**
29+
* Check if is bulk api request and header is set
30+
*
31+
* @return bool
32+
*/
33+
public function isPublishingArrayMode(): bool
34+
{
35+
if ($this->isArrayMode === null) {
36+
$this->isArrayMode =
37+
$this->isBulkApiRequest() && $this->request->getHeader(self::ARRAY_MODE_HEADER_KEY);
38+
}
39+
return $this->isArrayMode;
40+
}
41+
42+
/**
43+
* Check if current request is bulk
44+
*
45+
* @return bool
46+
*/
47+
public function isBulkApiRequest(): bool
48+
{
49+
return (bool)preg_match(self::BULK_PROCESSOR_PATH_REGEXP, $this->request->getPathInfo());
50+
}
51+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Comwrap\WebapiAsyncObjectValidation\Plugin\Magento\Framework\MessageQueue;
6+
7+
use Comwrap\WebapiAsyncObjectValidation\Model\Config;
8+
use Magento\Framework\MessageQueue\MessageValidator as OriginMessageValidator;
9+
10+
class MessageValidator
11+
{
12+
/** @var Config */
13+
private Config $config;
14+
15+
/**
16+
* @param Config $config
17+
*/
18+
public function __construct(Config $config)
19+
{
20+
$this->config = $config;
21+
}
22+
23+
/**
24+
* Skip validation if array mode is set
25+
*
26+
* @param OriginMessageValidator $subject
27+
* @param \Closure $proceed
28+
* @param mixed ...$args
29+
* @return void
30+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
31+
*/
32+
public function aroundValidate(OriginMessageValidator $subject, \Closure $proceed, ...$args): void
33+
{
34+
if ($this->config->isPublishingArrayMode()) {
35+
return;
36+
}
37+
$proceed(...$args);
38+
}
39+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Comwrap\WebapiAsyncObjectValidation\Plugin\Magento\Framework\Webapi;
6+
7+
use Comwrap\WebapiAsyncObjectValidation\Model\Config;
8+
use Magento\Framework\Webapi\ServiceInputProcessor as OriginServiceInputProcessor;
9+
10+
class ServiceInputProcessor
11+
{
12+
/** @var Config */
13+
private Config $config;
14+
15+
/**
16+
* @param Config $config
17+
*/
18+
public function __construct(Config $config)
19+
{
20+
$this->config = $config;
21+
}
22+
23+
/**
24+
* Return array if array mode is set
25+
*
26+
* @param OriginServiceInputProcessor $subject
27+
* @param \Closure $proceed
28+
* @param string $serviceClassName
29+
* @param string $serviceMethodName
30+
* @param array $inputArray
31+
* @return array
32+
*/
33+
public function aroundProcess(
34+
OriginServiceInputProcessor $subject,
35+
\Closure $proceed,
36+
string $serviceClassName,
37+
string $serviceMethodName,
38+
array $inputArray
39+
): array
40+
{
41+
if ($this->config->isPublishingArrayMode()) {
42+
return [];
43+
}
44+
return $proceed($serviceClassName, $serviceMethodName, $inputArray);
45+
}
46+
}

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
1-
# Comwrap_WebapiAsyncObjectValidation
1+
# Comwrap_WebapiAsyncObjectValidation
2+
3+
Increase Bulk API performance, using direct entity publishing as array without creating objects for validation.
4+
5+
## How to use
6+
7+
Extension works for API request to the Bulk API endpoints. Set the custom request header with name 'disable-object-validation'.
8+
Value for a header could be any, even empty. If header is set, the extension skips objects creation and validation.
9+
These operations take quite long time among the request duration.

composer.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "comwrap/module-webapi-async-object-validation",
3+
"version": "0.1.0",
4+
"description": "Increase Bulk API performance, using direct entity publishing as array without creating objects for validation",
5+
"type": "magento2-module",
6+
"require": {
7+
"magento/framework": "^103"
8+
},
9+
"license": [
10+
"Proprietary"
11+
],
12+
"autoload": {
13+
"files": [
14+
"registration.php"
15+
],
16+
"psr-4": {
17+
"Comwrap\\WebapiAsyncObjectValidation\\": ""
18+
}
19+
}
20+
}

etc/module.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" ?>
2+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
4+
<module name="Comwrap_WebapiAsyncObjectValidation">
5+
<sequence>
6+
<module name="Magento_WebapiAsync"/>
7+
</sequence>
8+
</module>
9+
</config>

etc/webapi_rest/di.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0"?>
2+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
4+
<type name="Magento\Framework\MessageQueue\MessageValidator">
5+
<plugin name="comwrapWebapiAsyncObjectValidationPluginMagentoFrameworkMessageQueueMessageValidator"
6+
type="Comwrap\WebapiAsyncObjectValidation\Plugin\Magento\Framework\MessageQueue\MessageValidator"/>
7+
</type>
8+
<type name="Magento\Framework\Webapi\ServiceInputProcessor">
9+
<plugin name="comwrapWebapiAsyncObjectValidationPluginMagentoFrameworkWebapiServiceInputProcessor"
10+
type="Comwrap\WebapiAsyncObjectValidation\Plugin\Magento\Framework\Webapi\ServiceInputProcessor"/>
11+
</type>
12+
</config>

registration.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Magento\Framework\Component\ComponentRegistrar;
6+
7+
ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Comwrap_WebapiAsyncObjectValidation', __DIR__);

0 commit comments

Comments
 (0)