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
|
|
|
use Webmozart\PathUtil\Path; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Generates the setup code for a {@link JsonFileStore}. |
23
|
|
|
* |
24
|
|
|
* @since 1.0 |
25
|
|
|
* |
26
|
|
|
* @author Bernhard Schussek <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class JsonFileStoreGenerator implements ServiceGenerator |
29
|
|
|
{ |
30
|
|
|
private static $defaultOptions = array( |
31
|
|
|
'path' => 'data.json', |
32
|
|
|
'serializeStrings' => true, |
33
|
|
|
'serializeArrays' => true, |
34
|
|
|
'escapeSlash' => true, |
35
|
|
|
'prettyPrint' => false, |
36
|
|
|
); |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
32 |
|
public function generateNewInstance($varName, Method $targetMethod, GeneratorRegistry $generatorRegistry, array $options = array()) |
42
|
|
|
{ |
43
|
32 |
|
Assert::keyExists($options, 'rootDir', 'The "rootDir" option is missing.'); |
44
|
|
|
|
45
|
31 |
|
$options = array_replace(self::$defaultOptions, $options); |
46
|
|
|
|
47
|
31 |
|
$path = Path::makeAbsolute($options['path'], $options['rootDir']); |
48
|
31 |
|
$relPath = Path::makeRelative($path, $targetMethod->getClass()->getDirectory()); |
49
|
|
|
|
50
|
31 |
|
$flags = array(); |
51
|
|
|
|
52
|
31 |
|
if (!$options['serializeStrings']) { |
53
|
27 |
|
$flags[] = 'JsonFileStore::NO_SERIALIZE_STRINGS'; |
54
|
27 |
|
} |
55
|
|
|
|
56
|
31 |
|
if (!$options['serializeArrays']) { |
57
|
27 |
|
$flags[] = 'JsonFileStore::NO_SERIALIZE_ARRAYS'; |
58
|
27 |
|
} |
59
|
|
|
|
60
|
31 |
|
if (!$options['serializeArrays']) { |
61
|
27 |
|
$flags[] = 'JsonFileStore::NO_ESCAPE_SLASH'; |
62
|
27 |
|
} |
63
|
|
|
|
64
|
31 |
|
if ($options['prettyPrint']) { |
65
|
27 |
|
$flags[] = 'JsonFileStore::PRETTY_PRINT'; |
66
|
27 |
|
} |
67
|
|
|
|
68
|
31 |
|
$targetMethod->getClass()->addImport(new Import('Webmozart\KeyValueStore\JsonFileStore')); |
69
|
|
|
|
70
|
31 |
|
$targetMethod->addBody(sprintf('$%s = new JsonFileStore(%s%s%s);', |
71
|
31 |
|
$varName, |
72
|
31 |
|
$flags ? "\n " : '', |
73
|
31 |
|
'__DIR__.'.var_export('/'.$relPath, true), |
74
|
31 |
|
$flags ? ",\n ".implode("\n | ", $flags)."\n" : '' |
75
|
31 |
|
)); |
76
|
31 |
|
} |
77
|
|
|
} |
78
|
|
|
|