|
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
|
32 |
|
|
|
38
|
|
|
/** |
|
39
|
32 |
|
* {@inheritdoc} |
|
40
|
|
|
*/ |
|
41
|
31 |
|
public function generateNewInstance($varName, Method $targetMethod, GeneratorRegistry $generatorRegistry, array $options = array()) |
|
42
|
|
|
{ |
|
43
|
31 |
|
Assert::keyExists($options, 'rootDir', 'The "rootDir" option is missing.'); |
|
44
|
31 |
|
|
|
45
|
|
|
$options = array_replace(self::$defaultOptions, $options); |
|
46
|
31 |
|
|
|
47
|
|
|
$path = Path::makeAbsolute($options['path'], $options['rootDir']); |
|
48
|
31 |
|
$relPath = Path::makeRelative($path, $targetMethod->getClass()->getDirectory()); |
|
49
|
31 |
|
|
|
50
|
31 |
|
$flags = array(); |
|
51
|
31 |
|
|
|
52
|
31 |
|
if (!$options['serializeStrings']) { |
|
53
|
|
|
$flags[] = 'JsonFileStore::NO_SERIALIZE_STRINGS'; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
if (!$options['serializeArrays']) { |
|
57
|
|
|
$flags[] = 'JsonFileStore::NO_SERIALIZE_ARRAYS'; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
if (!$options['serializeArrays']) { |
|
61
|
|
|
$flags[] = 'JsonFileStore::NO_ESCAPE_SLASH'; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if ($options['prettyPrint']) { |
|
65
|
|
|
$flags[] = 'JsonFileStore::PRETTY_PRINT'; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$targetMethod->getClass()->addImport(new Import('Webmozart\KeyValueStore\JsonFileStore')); |
|
69
|
|
|
|
|
70
|
|
|
$targetMethod->addBody(sprintf('$%s = new JsonFileStore(%s%s%s);', |
|
71
|
|
|
$varName, |
|
72
|
|
|
$flags ? "\n " : '', |
|
73
|
|
|
'__DIR__.'.var_export('/'.$relPath, true), |
|
74
|
|
|
$flags ? ",\n ".implode("\n | ", $flags)."\n" : '' |
|
75
|
|
|
)); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|