Skip to content

Commit 873650e

Browse files
authored
Merge master to 1.x (#49)
1 parent 60f99ee commit 873650e

11 files changed

+1210
-130
lines changed

.travis.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ env:
1111

1212
matrix:
1313
include:
14-
- php: '7.2'
15-
env: PREFER_LOWEST='--prefer-lowest'
1614
- php: '7.3'
15+
env: PREFER_LOWEST='--prefer-lowest'
1716
- php: '7.4'
1817
env: SYMFONY_VERSION='~4.4.0'
1918
- php: '7.4'
2019
env: SYMFONY_VERSION='~5.0.0'
20+
- php: '8.0'
21+
env: SYMFONY_VERSION='~5.0.0'
2122
fast_finish: true
2223

2324
before_install:

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
],
1616

1717
"require": {
18-
"php": "^7.2",
18+
"php": "^7.3 || ^8.0",
1919
"symfony/console": "^3.0 || ^4.0 || ^5.0",
2020
"symfony/dependency-injection": "^3.0 || ^4.1.12 || ^5.0",
2121
"symfony/process": "^3.0 || ^4.0 || ^5.0",

src/Configuration.php

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Webmozarts Console Parallelization package.
5+
*
6+
* (c) Webmozarts GmbH <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Webmozarts\Console\Parallelization;
15+
16+
use Webmozart\Assert\Assert;
17+
use function ceil;
18+
use function sprintf;
19+
20+
final class Configuration
21+
{
22+
private $segmentSize;
23+
private $rounds;
24+
private $batches;
25+
26+
public function __construct(
27+
bool $numberOfProcessesDefined,
28+
int $numberOfProcesses,
29+
int $numberOfItems,
30+
int $segmentSize,
31+
int $batchSize
32+
) {
33+
Assert::greaterThan(
34+
$numberOfProcesses,
35+
0,
36+
sprintf(
37+
'Expected the number of processes to be 1 or greater. Got "%s"',
38+
$numberOfProcesses
39+
)
40+
);
41+
Assert::natural(
42+
$numberOfItems,
43+
sprintf(
44+
'Expected the number of items to be 0 or greater. Got "%s"',
45+
$numberOfItems
46+
)
47+
);
48+
Assert::greaterThan(
49+
$segmentSize,
50+
0,
51+
sprintf(
52+
'Expected the segment size to be 1 or greater. Got "%s"',
53+
$segmentSize
54+
)
55+
);
56+
Assert::greaterThan(
57+
$batchSize,
58+
0,
59+
sprintf(
60+
'Expected the batch size to be 1 or greater. Got "%s"',
61+
$batchSize
62+
)
63+
);
64+
65+
// We always check those (and not the calculated ones) since they come from the command
66+
// configuration so an issue there hints on a misconfiguration which should be fixed.
67+
Assert::greaterThanEq(
68+
$segmentSize,
69+
$batchSize,
70+
sprintf(
71+
'Expected the segment size ("%s") to be greater or equal to the batch size ("%s")',
72+
$segmentSize,
73+
$batchSize
74+
)
75+
);
76+
77+
$this->segmentSize = 1 === $numberOfProcesses && !$numberOfProcessesDefined
78+
? $numberOfItems
79+
: $segmentSize
80+
;
81+
$this->rounds = (int) (1 === $numberOfProcesses ? 1 : ceil($numberOfItems / $segmentSize));
82+
$this->batches = (int) (ceil($segmentSize / $batchSize) * $this->rounds);
83+
}
84+
85+
public function getSegmentSize(): int
86+
{
87+
return $this->segmentSize;
88+
}
89+
90+
public function getNumberOfSegments(): int
91+
{
92+
return $this->rounds;
93+
}
94+
95+
public function getNumberOfBatches(): int
96+
{
97+
return $this->batches;
98+
}
99+
}

src/ItemBatchIterator.php

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Webmozarts Console Parallelization package.
5+
*
6+
* (c) Webmozarts GmbH <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Webmozarts\Console\Parallelization;
15+
16+
use Closure;
17+
use Webmozart\Assert\Assert;
18+
use function array_chunk;
19+
use function array_values;
20+
use function count;
21+
use function get_class;
22+
use function gettype;
23+
use function is_numeric;
24+
use function is_object;
25+
use function sprintf;
26+
27+
final class ItemBatchIterator
28+
{
29+
private $items;
30+
private $numberOfItems;
31+
private $batchSize;
32+
private $itemsChunks;
33+
34+
/**
35+
* @param Closure(): list<string> $fetchItems
36+
*/
37+
public static function create(?string $item, Closure $fetchItems, int $batchSize): self
38+
{
39+
if (null !== $item) {
40+
$items = [$item];
41+
} else {
42+
$items = $fetchItems();
43+
44+
Assert::isArray(
45+
$items,
46+
sprintf(
47+
'Expected the fetched items to be a list of strings. Got "%s"',
48+
gettype($items)
49+
)
50+
);
51+
}
52+
53+
return new self($items, $batchSize);
54+
}
55+
56+
/**
57+
* @return list<string>
58+
*/
59+
private static function normalizeItems($items): array
60+
{
61+
foreach ($items as $index => $item) {
62+
if (is_numeric($item)) {
63+
$items[$index] = (string) $item;
64+
65+
continue;
66+
}
67+
68+
Assert::string(
69+
$item,
70+
sprintf(
71+
'The items are potentially passed to the child processes via the STDIN. For this reason they are expected to be string values. Got "%s" for the item "%s"',
72+
is_object($item) ? get_class($item) : gettype($item),
73+
$index
74+
)
75+
);
76+
}
77+
78+
return array_values($items);
79+
}
80+
81+
/**
82+
* @param list<string> $items
83+
* @param int $batchSize
84+
*/
85+
public function __construct(array $items, int $batchSize)
86+
{
87+
Assert::greaterThan(
88+
$batchSize,
89+
0,
90+
sprintf(
91+
'Expected the batch size to be 1 or greater. Got "%s"',
92+
$batchSize
93+
)
94+
);
95+
96+
$this->items = self::normalizeItems($items);
97+
$this->itemsChunks = array_chunk(
98+
$this->items,
99+
$batchSize,
100+
false
101+
);
102+
$this->numberOfItems = count($this->items);
103+
$this->batchSize = $batchSize;
104+
}
105+
106+
/**
107+
* @return list<string>
108+
*/
109+
public function getItems(): array
110+
{
111+
return $this->items;
112+
}
113+
114+
public function getNumberOfItems(): int
115+
{
116+
return $this->numberOfItems;
117+
}
118+
119+
public function getBatchSize(): int
120+
{
121+
return $this->batchSize;
122+
}
123+
124+
/**
125+
* @return array<list<string>>
126+
*/
127+
public function getItemBatches(): array
128+
{
129+
return $this->itemsChunks;
130+
}
131+
}

0 commit comments

Comments
 (0)