Skip to content
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

Optimize download memory consumption #62

Merged
merged 3 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Builder/Capacity/Lookup/All.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@
return match ($type) {
'referenceEntityRecord' => new Node\Identifier('referenceEntityCode'),
'assetManager' => new Node\Identifier('assetFamilyCode'),
default => new Node\Identifier('attributeCode')
default => new Node\Identifier('attributeCode'),
};
}

private function compileSearch(): array

Check failure on line 95 in src/Builder/Capacity/Lookup/All.php

View workflow job for this annotation

GitHub Actions / phpstan8

Method Kiboko\Plugin\Akeneo\Builder\Capacity\Lookup\All::compileSearch() return type has no value type specified in iterable type array.

Check failure on line 95 in src/Builder/Capacity/Lookup/All.php

View workflow job for this annotation

GitHub Actions / phpstan6

Method Kiboko\Plugin\Akeneo\Builder\Capacity\Lookup\All::compileSearch() return type has no value type specified in iterable type array.

Check failure on line 95 in src/Builder/Capacity/Lookup/All.php

View workflow job for this annotation

GitHub Actions / phpstan7

Method Kiboko\Plugin\Akeneo\Builder\Capacity\Lookup\All::compileSearch() return type has no value type specified in iterable type array.
{
if (null === $this->search) {
return [];
Expand Down
30 changes: 21 additions & 9 deletions src/Builder/Capacity/Lookup/Download.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,23 @@
),
),
),
new Node\Stmt\Expression(
expr: new Node\Expr\Assign(
var: new Node\Expr\Variable('imageFromAkeneo'),
expr: new Node\Expr\MethodCall(
var: new Node\Expr\MethodCall(
var: new Node\Expr\Variable('data'),
name: new Node\Identifier('getBody')
),
name: new Node\Identifier('detach')
)
),
),
new Node\Stmt\Expression(
expr: new Node\Expr\FuncCall(
name: new Node\Name('stream_copy_to_stream'),
args: [
new Node\Arg(
new Node\Expr\MethodCall(
var: new Node\Expr\MethodCall(
var: new Node\Expr\Variable('data'),
name: new Node\Identifier('getBody')
),
name: new Node\Identifier('detach')
)
),
new Node\Arg(new Node\Expr\Variable('imageFromAkeneo')),
new Node\Arg(new Node\Expr\Variable('image')),
]
),
Expand All @@ -125,7 +129,7 @@
new Node\Stmt\If_(
cond: new Node\Expr\FuncCall(
name: new Node\Name('preg_match'),
args: [

Check failure on line 132 in src/Builder/Capacity/Lookup/Download.php

View workflow job for this annotation

GitHub Actions / phpstan8

Parameter $args of class PhpParser\Node\Expr\FuncCall constructor expects array<PhpParser\Node\Arg|PhpParser\Node\VariadicPlaceholder>, array<int, PhpParser\Node\Arg|PhpParser\Node\Expr\Variable> given.

Check failure on line 132 in src/Builder/Capacity/Lookup/Download.php

View workflow job for this annotation

GitHub Actions / phpstan5

Parameter $args of class PhpParser\Node\Expr\FuncCall constructor expects array<PhpParser\Node\Arg|PhpParser\Node\VariadicPlaceholder>, array<int, PhpParser\Node\Arg|PhpParser\Node\Expr\Variable> given.

Check failure on line 132 in src/Builder/Capacity/Lookup/Download.php

View workflow job for this annotation

GitHub Actions / phpstan6

Parameter $args of class PhpParser\Node\Expr\FuncCall constructor expects array<PhpParser\Node\Arg|PhpParser\Node\VariadicPlaceholder>, array<int, PhpParser\Node\Arg|PhpParser\Node\Expr\Variable> given.

Check failure on line 132 in src/Builder/Capacity/Lookup/Download.php

View workflow job for this annotation

GitHub Actions / phpstan7

Parameter $args of class PhpParser\Node\Expr\FuncCall constructor expects array<PhpParser\Node\Arg|PhpParser\Node\VariadicPlaceholder>, array<int, PhpParser\Node\Arg|PhpParser\Node\Expr\Variable> given.
new Node\Arg(new Node\Scalar\String_('/filename="([^"]+)"/')),
new Node\Arg(
new Node\Expr\ArrayDimFetch(
Expand Down Expand Up @@ -163,6 +167,14 @@
],
]
),
new Node\Stmt\Expression(
expr: new Node\Expr\FuncCall(
name: new Node\Name('fclose'),
args: [
new Node\Arg(new Node\Expr\Variable('imageFromAkeneo')),
]
),
),
],
catches: [
new Node\Stmt\Catch_(
Expand Down
4 changes: 2 additions & 2 deletions src/Builder/Capacity/Lookup/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
private Node\Expr|Node\Identifier|null $endpoint = null;
private ?Node\Expr $identifier = null;
private ?Node\Expr $code = null;
private ?string $type = '';

Check failure on line 16 in src/Builder/Capacity/Lookup/Get.php

View workflow job for this annotation

GitHub Actions / phpstan

Property Kiboko\Plugin\Akeneo\Builder\Capacity\Lookup\Get::$type (string|null) is never assigned null so it can be removed from the property type.

Check failure on line 16 in src/Builder/Capacity/Lookup/Get.php

View workflow job for this annotation

GitHub Actions / phpstan8

Property Kiboko\Plugin\Akeneo\Builder\Capacity\Lookup\Get::$type (string|null) is never assigned null so it can be removed from the property type.

Check failure on line 16 in src/Builder/Capacity/Lookup/Get.php

View workflow job for this annotation

GitHub Actions / phpstan5

Property Kiboko\Plugin\Akeneo\Builder\Capacity\Lookup\Get::$type (string|null) is never assigned null so it can be removed from the property type.

Check failure on line 16 in src/Builder/Capacity/Lookup/Get.php

View workflow job for this annotation

GitHub Actions / phpstan6

Property Kiboko\Plugin\Akeneo\Builder\Capacity\Lookup\Get::$type (string|null) is never assigned null so it can be removed from the property type.

Check failure on line 16 in src/Builder/Capacity/Lookup/Get.php

View workflow job for this annotation

GitHub Actions / phpstan7

Property Kiboko\Plugin\Akeneo\Builder\Capacity\Lookup\Get::$type (string|null) is never assigned null so it can be removed from the property type.

public function withEndpoint(Node\Expr|Node\Identifier $endpoint): self
{
Expand Down Expand Up @@ -66,7 +66,7 @@
args: array_filter(
[
new Node\Arg(
value: $this->identifier,

Check failure on line 69 in src/Builder/Capacity/Lookup/Get.php

View workflow job for this annotation

GitHub Actions / phpstan8

Parameter $value of class PhpParser\Node\Arg constructor expects PhpParser\Node\Expr, PhpParser\Node\Expr|null given.
name: $this->compileIdentifierNamedArgument($this->type),
),
null !== $this->code ? new Node\Arg(
Expand Down Expand Up @@ -125,7 +125,7 @@
new Node\Stmt\Expression(
expr: new Node\Expr\MethodCall(
var: new Node\Expr\Variable('bucket'),
name: new Node\Name('reject'),

Check failure on line 128 in src/Builder/Capacity/Lookup/Get.php

View workflow job for this annotation

GitHub Actions / phpstan5

Parameter $name of class PhpParser\Node\Expr\MethodCall constructor expects PhpParser\Node\Expr|PhpParser\Node\Identifier|string, PhpParser\Node\Name given.

Check failure on line 128 in src/Builder/Capacity/Lookup/Get.php

View workflow job for this annotation

GitHub Actions / phpstan6

Parameter $name of class PhpParser\Node\Expr\MethodCall constructor expects PhpParser\Node\Expr|PhpParser\Node\Identifier|string, PhpParser\Node\Name given.

Check failure on line 128 in src/Builder/Capacity/Lookup/Get.php

View workflow job for this annotation

GitHub Actions / phpstan7

Parameter $name of class PhpParser\Node\Expr\MethodCall constructor expects PhpParser\Node\Expr|PhpParser\Node\Identifier|string, PhpParser\Node\Name given.
args: [
new Node\Arg(
value: new Node\Expr\Variable('input')
Expand All @@ -145,7 +145,7 @@
return match ($type) {
'referenceEntityRecord' => new Node\Identifier('referenceEntityCode'),
'assetManager' => new Node\Identifier('assetFamilyCode'),
default => new Node\Identifier('attributeCode')
default => new Node\Identifier('attributeCode'),
};
}

Expand All @@ -154,7 +154,7 @@
return match ($type) {
'referenceEntityRecord' => new Node\Identifier('recordCode'),
'assetManager' => new Node\Identifier('assetCode'),
default => new Node\Identifier('code')
default => new Node\Identifier('code'),
};
}
}
4 changes: 2 additions & 2 deletions src/Builder/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
final class Search implements Builder
{
public function __construct(
private array $filters = []
private array $filters = [],
) {
}

Expand All @@ -19,7 +19,7 @@ public function addFilter(
Node\Expr $operator,
?Node\Expr $value = null,
?Node\Expr $scope = null,
?Node\Expr $locale = null
?Node\Expr $locale = null,
): self {
$arguments = [
new Node\Arg(
Expand Down
6 changes: 3 additions & 3 deletions src/Capacity/Extractor/All.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ private function compileFilters(array ...$filters): Node\Expr
$builder = new Akeneo\Builder\Search();
foreach ($filters as $filter) {
if (\in_array($filter['operator'], self::$unaryOperators, true) && \array_key_exists('value', $filter)) {
throw new InvalidConfigurationException(sprintf('You should not provide a value for the %s operator', $filter['operator']));
throw new InvalidConfigurationException(\sprintf('You should not provide a value for the %s operator', $filter['operator']));
}
if (!\in_array($filter['operator'], self::$unaryOperators, true) && !\array_key_exists('value', $filter)) {
throw new InvalidConfigurationException(sprintf('You should provide a value for the %s operator', $filter['operator']));
throw new InvalidConfigurationException(\sprintf('You should provide a value for the %s operator', $filter['operator']));
}

$builder->addFilter(
Expand All @@ -93,7 +93,7 @@ private function compileFilters(array ...$filters): Node\Expr
public function getBuilder(array $config): Builder
{
$builder = (new Akeneo\Builder\Capacity\Extractor\All())
->withEndpoint(new Node\Identifier(sprintf('get%sApi', ucfirst((string) $config['type']))))
->withEndpoint(new Node\Identifier(\sprintf('get%sApi', ucfirst((string) $config['type']))))
;

if (isset($config['search']) && \is_array($config['search']) && \count($config['search']) > 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/Capacity/Extractor/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function applies(array $config): bool
public function getBuilder(array $config): Builder
{
$builder = (new Akeneo\Builder\Capacity\Extractor\Get())
->withEndpoint(new Node\Identifier(sprintf('get%sApi', ucfirst((string) $config['type']))))
->withEndpoint(new Node\Identifier(\sprintf('get%sApi', ucfirst((string) $config['type']))))
;

$builder->withIdentifier(compileValueWhenExpression($this->interpreter, $config['identifier']));
Expand Down
6 changes: 3 additions & 3 deletions src/Capacity/Extractor/ListPerPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ private function compileFilters(array ...$filters): Node\Expr
$builder = new Akeneo\Builder\Search();
foreach ($filters as $filter) {
if (\in_array($filter['operator'], self::$unaryOperators, true) && \array_key_exists('value', $filter)) {
throw new InvalidConfigurationException(sprintf('You should not provide a value for the %s operator', $filter['operator']));
throw new InvalidConfigurationException(\sprintf('You should not provide a value for the %s operator', $filter['operator']));
}
if (!\in_array($filter['operator'], self::$unaryOperators, true) && !\array_key_exists('value', $filter)) {
throw new InvalidConfigurationException(sprintf('You should provide a value for the %s operator', $filter['operator']));
throw new InvalidConfigurationException(\sprintf('You should provide a value for the %s operator', $filter['operator']));
}

$builder->addFilter(
Expand All @@ -91,7 +91,7 @@ private function compileFilters(array ...$filters): Node\Expr
public function getBuilder(array $config): Builder
{
$builder = (new Akeneo\Builder\Capacity\Extractor\ListPerPage())
->withEndpoint(new Node\Identifier(sprintf('get%sApi', ucfirst((string) $config['type']))))
->withEndpoint(new Node\Identifier(\sprintf('get%sApi', ucfirst((string) $config['type']))))
;

if (isset($config['search']) && \is_array($config['search'])) {
Expand Down
2 changes: 1 addition & 1 deletion src/Capacity/Loader/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function applies(array $config): bool
public function getBuilder(array $config): Builder
{
return (new Akeneo\Builder\Capacity\Loader\Create())
->withEndpoint(endpoint: new Node\Identifier(sprintf('get%sApi', ucfirst((string) $config['type']))))
->withEndpoint(endpoint: new Node\Identifier(\sprintf('get%sApi', ucfirst((string) $config['type']))))
->withCode(code: compileValueWhenExpression($this->interpreter, $config['code'], 'line'))
->withData(line: new Node\Expr\Variable('line["product"]'))
;
Expand Down
2 changes: 1 addition & 1 deletion src/Capacity/Loader/Upsert.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function applies(array $config): bool
public function getBuilder(array $config): Builder
{
$builder = (new Akeneo\Builder\Capacity\Loader\Upsert())
->withEndpoint(endpoint: new Node\Identifier(sprintf('get%sApi', ucfirst((string) $config['type']))))
->withEndpoint(endpoint: new Node\Identifier(\sprintf('get%sApi', ucfirst((string) $config['type']))))
->withCode(code: compileValueWhenExpression($this->interpreter, $config['code'], 'line'))
->withData(line: new Node\Expr\Variable('line'))
;
Expand Down
2 changes: 1 addition & 1 deletion src/Capacity/Loader/UpsertList.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function applies(array $config): bool
public function getBuilder(array $config): Builder
{
$builder = (new Akeneo\Builder\Capacity\Loader\UpsertList())
->withEndpoint(endpoint: new Node\Identifier(sprintf('get%sApi', ucfirst((string) $config['type']))))
->withEndpoint(endpoint: new Node\Identifier(\sprintf('get%sApi', ucfirst((string) $config['type']))))
->withData(data: new Node\Expr\Variable('line'))
;

Expand Down
6 changes: 3 additions & 3 deletions src/Capacity/Lookup/All.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ private function compileFilters(array ...$filters): Node\Expr
$builder = new Akeneo\Builder\Search();
foreach ($filters as $filter) {
if (\in_array($filter['operator'], self::$unaryOperators, true) && \array_key_exists('value', $filter)) {
throw new InvalidConfigurationException(sprintf('You should not provide a value for the %s operator', $filter['operator']));
throw new InvalidConfigurationException(\sprintf('You should not provide a value for the %s operator', $filter['operator']));
}
if (!\in_array($filter['operator'], self::$unaryOperators, true) && !\array_key_exists('value', $filter)) {
throw new InvalidConfigurationException(sprintf('You should provide a value for the %s operator', $filter['operator']));
throw new InvalidConfigurationException(\sprintf('You should provide a value for the %s operator', $filter['operator']));
}

$builder->addFilter(
Expand All @@ -93,7 +93,7 @@ public function getBuilder(array $config): Builder
{
$builder = (new Akeneo\Builder\Capacity\Lookup\All())
->withType((string) $config['type'])
->withEndpoint(new Node\Identifier(sprintf('get%sApi', ucfirst((string) $config['type']))))
->withEndpoint(new Node\Identifier(\sprintf('get%sApi', ucfirst((string) $config['type']))))
;

if (isset($config['search']) && \is_array($config['search'])) {
Expand Down
2 changes: 1 addition & 1 deletion src/Capacity/Lookup/Download.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function applies(array $config): bool
public function getBuilder(array $config): Builder
{
$builder = (new Akeneo\Builder\Capacity\Lookup\Download())
->withEndpoint(new Node\Identifier(sprintf('get%sApi', ucfirst((string) $config['type']))))
->withEndpoint(new Node\Identifier(\sprintf('get%sApi', ucfirst((string) $config['type']))))
;

if (!\array_key_exists('file', $config)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Capacity/Lookup/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function applies(array $config): bool
public function getBuilder(array $config): Builder
{
$builder = (new Akeneo\Builder\Capacity\Lookup\Get())
->withEndpoint(new Node\Identifier(sprintf('get%sApi', ucfirst((string) $config['type']))))
->withEndpoint(new Node\Identifier(\sprintf('get%sApi', ucfirst((string) $config['type']))))
->withType($config['type'])
;

Expand Down
6 changes: 3 additions & 3 deletions src/Capacity/Lookup/ListPerPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ private function compileFilters(array ...$filters): Node\Expr
$builder = new Akeneo\Builder\Search();
foreach ($filters as $filter) {
if (\in_array($filter['operator'], self::$unaryOperators, true) && \array_key_exists('value', $filter)) {
throw new InvalidConfigurationException(sprintf('You should not provide a value for the %s operator', $filter['operator']));
throw new InvalidConfigurationException(\sprintf('You should not provide a value for the %s operator', $filter['operator']));
}
if (!\in_array($filter['operator'], self::$unaryOperators, true) && !\array_key_exists('value', $filter)) {
throw new InvalidConfigurationException(sprintf('You should provide a value for the %s operator', $filter['operator']));
throw new InvalidConfigurationException(\sprintf('You should provide a value for the %s operator', $filter['operator']));
}

$builder->addFilter(
Expand All @@ -91,7 +91,7 @@ private function compileFilters(array ...$filters): Node\Expr
public function getBuilder(array $config): Builder
{
$builder = (new Akeneo\Builder\Capacity\Lookup\ListPerPage())
->withEndpoint(new Node\Identifier(sprintf('get%sApi', ucfirst((string) $config['type']))))
->withEndpoint(new Node\Identifier(\sprintf('get%sApi', ucfirst((string) $config['type']))))
;

if (isset($config['search']) && \is_array($config['search'])) {
Expand Down
4 changes: 2 additions & 2 deletions src/Configuration/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public function getConfigTreeBuilder(): Config\Definition\Builder\TreeBuilder
->ifArray()
->then(function (array $item) {
if (!\in_array($item['method'], self::$endpoints[$item['type']])) {
throw new \InvalidArgumentException(sprintf('the value should be one of [%s], got %s', implode(', ', self::$endpoints[$item['type']]), json_encode($item['method'], \JSON_THROW_ON_ERROR)));
throw new \InvalidArgumentException(\sprintf('the value should be one of [%s], got %s', implode(', ', self::$endpoints[$item['type']]), json_encode($item['method'], \JSON_THROW_ON_ERROR)));
}

return $item;
Expand Down Expand Up @@ -211,7 +211,7 @@ public function getConfigTreeBuilder(): Config\Definition\Builder\TreeBuilder
->validate()
->ifNotInArray(array_keys(self::$endpoints))
->thenInvalid(
sprintf('the value should be one of [%s], got %%s', implode(', ', array_keys(self::$endpoints)))
\sprintf('the value should be one of [%s], got %%s', implode(', ', array_keys(self::$endpoints)))
)
->end()
->end()
Expand Down
4 changes: 2 additions & 2 deletions src/Configuration/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public function getConfigTreeBuilder(): Config\Definition\Builder\TreeBuilder
->ifArray()
->then(function (array $item) {
if (!\in_array($item['method'], self::$endpoints[$item['type']])) {
throw new \InvalidArgumentException(sprintf('the value should be one of [%s], got %s', implode(', ', self::$endpoints[$item['type']]), json_encode($item['method'], \JSON_THROW_ON_ERROR)));
throw new \InvalidArgumentException(\sprintf('the value should be one of [%s], got %s', implode(', ', self::$endpoints[$item['type']]), json_encode($item['method'], \JSON_THROW_ON_ERROR)));
}

return $item;
Expand Down Expand Up @@ -230,7 +230,7 @@ public function getConfigTreeBuilder(): Config\Definition\Builder\TreeBuilder
->validate()
->ifNotInArray(array_keys(self::$endpoints))
->thenInvalid(
sprintf('the value should be one of [%s]', implode(', ', array_keys(self::$endpoints)))
\sprintf('the value should be one of [%s]', implode(', ', array_keys(self::$endpoints)))
)
->end()
->end()
Expand Down
8 changes: 4 additions & 4 deletions src/Configuration/Lookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public function getConfigTreeBuilder(): Config\Definition\Builder\TreeBuilder
->ifTrue(fn ($data) => !\array_key_exists('conditional', $data) && \is_array($data))
->then(function (array $item) {
if (!\in_array($item['method'], self::$endpoints[$item['type']])) {
throw new \InvalidArgumentException(sprintf('The value should be one of [%s], got %s', implode(', ', self::$endpoints[$item['type']]), json_encode($item['method'], \JSON_THROW_ON_ERROR)));
throw new \InvalidArgumentException(\sprintf('The value should be one of [%s], got %s', implode(', ', self::$endpoints[$item['type']]), json_encode($item['method'], \JSON_THROW_ON_ERROR)));
}

return $item;
Expand All @@ -183,7 +183,7 @@ public function getConfigTreeBuilder(): Config\Definition\Builder\TreeBuilder
->validate()
->ifNotInArray(array_keys(self::$endpoints))
->thenInvalid(
sprintf('The value should be one of [%s], got %%s', implode(', ', array_keys(self::$endpoints)))
\sprintf('The value should be one of [%s], got %%s', implode(', ', array_keys(self::$endpoints)))
)
->end()
->end()
Expand Down Expand Up @@ -228,7 +228,7 @@ private function getConditionalTreeBuilder(): Config\Definition\Builder\TreeBuil
->ifArray()
->then(function (array $item) {
if (!\in_array($item['method'], self::$endpoints[$item['type']])) {
throw new \InvalidArgumentException(sprintf('the value should be one of [%s], got %s', implode(', ', self::$endpoints[$item['type']]), json_encode($item['method'], \JSON_THROW_ON_ERROR)));
throw new \InvalidArgumentException(\sprintf('the value should be one of [%s], got %s', implode(', ', self::$endpoints[$item['type']]), json_encode($item['method'], \JSON_THROW_ON_ERROR)));
}

return $item;
Expand Down Expand Up @@ -262,7 +262,7 @@ private function getConditionalTreeBuilder(): Config\Definition\Builder\TreeBuil
->validate()
->ifNotInArray(array_keys(self::$endpoints))
->thenInvalid(
sprintf('The value should be one of [%s], got %%s', implode(', ', array_keys(self::$endpoints)))
\sprintf('The value should be one of [%s], got %%s', implode(', ', array_keys(self::$endpoints)))
)
->end()
->end()
Expand Down
Loading