|
1
|
|
|
<?php |
|
2
|
|
|
namespace Germania\Random; |
|
3
|
|
|
|
|
4
|
|
|
use Pimple\Container; |
|
5
|
|
|
use Pimple\ServiceProviderInterface; |
|
6
|
|
|
|
|
7
|
|
|
use SecurityLib\Strength; |
|
8
|
|
|
use RandomLib\Factory; |
|
9
|
|
|
|
|
10
|
|
|
class RandomServiceProvider implements ServiceProviderInterface |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var integer |
|
15
|
|
|
*/ |
|
16
|
|
|
public $default_random_length = 256; |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var integer Default: 5 |
|
21
|
|
|
*/ |
|
22
|
|
|
public $default_random_strenth = Strength::MEDIUM; |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param int $length Random string length |
|
28
|
|
|
* @param int $strength Random generator strength |
|
29
|
|
|
*/ |
|
30
|
5 |
|
public function __construct( $length = null, $strength = null) |
|
31
|
|
|
{ |
|
32
|
5 |
|
$this->default_random_length = $length ?: $this->default_random_length; |
|
33
|
5 |
|
$this->default_random_strenth = $strength ?: $this->default_random_strenth; |
|
34
|
5 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @implements ServiceProviderInterface |
|
39
|
|
|
*/ |
|
40
|
4 |
|
public function register(Container $dic) |
|
41
|
|
|
{ |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @return int |
|
45
|
|
|
*/ |
|
46
|
1 |
|
$dic['RandomGenerator.Length'] = function( $dic ) { |
|
|
|
|
|
|
47
|
5 |
|
return $this->default_random_length; |
|
48
|
|
|
}; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return \SecurityLib\Strength |
|
52
|
|
|
*/ |
|
53
|
1 |
|
$dic['RandomGenerator.Strength'] = function( $dic ) { |
|
|
|
|
|
|
54
|
5 |
|
return new Strength( $this->default_random_strenth ); |
|
55
|
|
|
}; |
|
56
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @return \RandomLib\Generator |
|
60
|
|
|
*/ |
|
61
|
1 |
|
$dic['RandomGenerator'] = function( $dic ) { |
|
62
|
5 |
|
$random_string_factory = new Factory; |
|
63
|
5 |
|
$strength = $dic['RandomGenerator.Strength']; |
|
64
|
5 |
|
return $random_string_factory->getGenerator( $strength ); |
|
65
|
|
|
}; |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Factory for creating a Callable that returns a 256 characters random string. |
|
70
|
|
|
* |
|
71
|
|
|
* @return Callable |
|
72
|
|
|
*/ |
|
73
|
2 |
|
$dic['RandomGenerator.Callable'] = $dic->protect(function( $length = null ) use ( $dic ) { |
|
74
|
5 |
|
$length = $length ?: $dic['RandomGenerator.Length']; |
|
75
|
5 |
|
return $dic['RandomGenerator']->generateString( $length ); |
|
76
|
5 |
|
}); |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
5 |
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.