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 RiakStore}. |
22
|
|
|
* |
23
|
|
|
* @since 1.0 |
24
|
|
|
* |
25
|
|
|
* @author Bernhard Schussek <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class RiakStoreGenerator implements ServiceGenerator |
28
|
|
|
{ |
29
|
|
|
private static $defaultOptions = array( |
30
|
|
|
'host' => '127.0.0.1', |
31
|
|
|
'port' => 8098, |
32
|
|
|
); |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
|
|
public function generateNewInstance($varName, Method $targetMethod, GeneratorRegistry $generatorRegistry, array $options = array()) |
38
|
|
|
{ |
39
|
|
|
Assert::keyExists($options, 'bucket', 'The "bucket" option is missing.'); |
40
|
|
|
|
41
|
|
|
$options = array_replace(self::$defaultOptions, $options); |
42
|
|
|
|
43
|
|
|
Assert::stringNotEmpty($options['bucket'], 'The "bucket" option must be a non-empty string. Got: %s'); |
44
|
|
|
Assert::stringNotEmpty($options['host'], 'The "host" option must be a non-empty string. Got: %s'); |
45
|
|
|
Assert::integer($options['port'], 'The "port" option must be an integer. Got: %s'); |
46
|
|
|
|
47
|
|
|
$escBucket = var_export($options['bucket'], true); |
48
|
|
|
$escHost = var_export($options['host'], true); |
49
|
|
|
$escPort = var_export($options['port'], true); |
50
|
|
|
|
51
|
|
|
$targetMethod->getClass()->addImports(array( |
52
|
|
|
new Import('Basho\Riak\Riak'), |
53
|
|
|
new Import('Webmozart\KeyValueStore\RiakStore'), |
54
|
|
|
)); |
55
|
|
|
|
56
|
|
|
$targetMethod->addBody( |
57
|
|
|
<<<EOF |
58
|
|
|
\$client = new Riak($escHost, $escPort); |
59
|
|
|
\$$varName = new RiakStore($escBucket, \$client); |
60
|
|
|
EOF |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|