|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sludio\HelperBundle\Script\Utils; |
|
4
|
|
|
|
|
5
|
|
|
use RandomLib\Generator as RandomGenerator; |
|
|
|
|
|
|
6
|
|
|
use RandomLib\Factory; |
|
|
|
|
|
|
7
|
|
|
use SecurityLib\Strength; |
|
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @link https://github.com/gajus/paggern for the canonical source repository |
|
11
|
|
|
* @license https://github.com/gajus/paggern/blob/master/LICENSE BSD 3-Clause |
|
12
|
|
|
*/ |
|
13
|
|
|
class Generator |
|
14
|
|
|
{ |
|
15
|
|
|
private $generator; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @param RandomLib\Generator $generator |
|
|
|
|
|
|
19
|
|
|
*/ |
|
20
|
|
|
public function __construct(RandomGenerator $generator = null) |
|
21
|
|
|
{ |
|
22
|
|
|
if ($generator === null) { |
|
23
|
|
|
$factory = new Factory; |
|
24
|
|
|
$this->generator = $factory->getGenerator(new Strength(Strength::MEDIUM)); |
|
25
|
|
|
} |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Generate a set of random codes based on Paggern pattern. |
|
30
|
|
|
* Codes are guaranteed to be unique within the set. |
|
31
|
|
|
* |
|
32
|
|
|
* @param string $pattern Paggern pattern. |
|
33
|
|
|
* @param int $amount Number of codes to generate. |
|
34
|
|
|
* @param int $safeguard Number of additional codes generated in case there are duplicates that need to be replaced. |
|
35
|
|
|
* |
|
36
|
|
|
* @return array |
|
37
|
|
|
*/ |
|
38
|
|
|
public function generateFromPattern($pattern, $amount = 1, $safeguard = 100) |
|
39
|
|
|
{ |
|
40
|
|
|
$lexer = new \Gajus\Paggern\Lexer(); |
|
|
|
|
|
|
41
|
|
|
$tokens = $lexer->tokenise($pattern, true); |
|
42
|
|
|
$codes = array_fill(0, $amount + $safeguard, ''); |
|
43
|
|
|
foreach ($tokens as &$token) { |
|
44
|
|
|
if ($token['type'] !== 'literal') { |
|
45
|
|
|
$token['pool'] = $this->generator->generateString($token['repetition'] * ($amount + $safeguard), $token['haystack']); |
|
46
|
|
|
} |
|
47
|
|
|
unset($token); |
|
48
|
|
|
} |
|
49
|
|
|
foreach ($codes as $i => &$code) { |
|
50
|
|
|
foreach ($tokens as $token) { |
|
51
|
|
|
if ($token['type'] === 'literal') { |
|
52
|
|
|
$code .= $token['string']; |
|
53
|
|
|
} else { |
|
54
|
|
|
$code .= mb_substr($token['pool'], $token['repetition'] * $i, $token['repetition']); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
unset($code); |
|
58
|
|
|
} |
|
59
|
|
|
$codes = array_slice(array_unique($codes), 0, $amount); |
|
60
|
|
|
if (count($codes) < $amount) { |
|
61
|
|
|
throw new Exception\RuntimeException('Unique combination pool exhausted.'); |
|
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return $codes; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths