1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the puli/manager package. |
5
|
|
|
* |
6
|
|
|
* (c) Bernhard Schussek <[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
|
|
|
namespace Puli\Manager\Factory\Generator\KeyValueStore; |
13
|
|
|
|
14
|
|
|
use Puli\Manager\Api\Factory\Generator\GeneratorRegistry; |
15
|
|
|
use Puli\Manager\Api\Factory\Generator\ServiceGenerator; |
16
|
|
|
use Puli\Manager\Api\Php\Import; |
17
|
|
|
use Puli\Manager\Api\Php\Method; |
18
|
|
|
use Puli\Manager\Assert\Assert; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Generates the setup code for a {@link PredisStore}. |
22
|
|
|
* |
23
|
|
|
* @since 1.0 |
24
|
|
|
* |
25
|
|
|
* @author Bernhard Schussek <[email protected]> |
26
|
|
|
*/ |
27
|
|
View Code Duplication |
class PredisStoreGenerator implements ServiceGenerator |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
private static $defaultOptions = array( |
30
|
|
|
'host' => '127.0.0.1', |
31
|
|
|
'port' => 6379, |
32
|
|
|
); |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
|
|
public function generateNewInstance($varName, Method $targetMethod, GeneratorRegistry $generatorRegistry, array $options = array()) |
38
|
|
|
{ |
39
|
|
|
$options = array_replace(self::$defaultOptions, $options); |
40
|
|
|
|
41
|
|
|
Assert::stringNotEmpty($options['host'], 'The "host" option must be a non-empty string. Got: %s'); |
42
|
|
|
Assert::integer($options['port'], 'The "port" option must be an integer. Got: %s'); |
43
|
|
|
|
44
|
|
|
$escHost = var_export($options['host'], true); |
45
|
|
|
$escPort = var_export($options['port'], true); |
46
|
|
|
|
47
|
|
|
$targetMethod->getClass()->addImports(array( |
48
|
|
|
new Import('Predis\Client'), |
49
|
|
|
new Import('Webmozart\KeyValueStore\PredisStore'), |
50
|
|
|
)); |
51
|
|
|
|
52
|
|
|
$targetMethod->addBody( |
53
|
|
|
<<<EOF |
54
|
|
|
\$client = new Client(array( |
55
|
|
|
'host' => $escHost, |
56
|
|
|
'port' => $escPort, |
57
|
|
|
)); |
58
|
|
|
\$$varName = new PredisStore(\$client); |
59
|
|
|
EOF |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.