|
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\Repository; |
|
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 JsonRepository}. |
|
23
|
|
|
* |
|
24
|
|
|
* @since 1.0 |
|
25
|
|
|
* |
|
26
|
|
|
* @author Bernhard Schussek <[email protected]> |
|
27
|
|
|
*/ |
|
28
|
|
|
class JsonRepositoryGenerator implements ServiceGenerator |
|
29
|
|
|
{ |
|
30
|
|
|
private static $defaultOptions = array( |
|
31
|
|
|
'optimize' => false, |
|
32
|
|
|
'change-stream' => array( |
|
33
|
|
|
'type' => 'json', |
|
34
|
|
|
), |
|
35
|
|
|
); |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* {@inheritdoc} |
|
39
|
|
|
*/ |
|
40
|
40 |
|
public function generateNewInstance($varName, Method $targetMethod, GeneratorRegistry $generatorRegistry, array $options = array()) |
|
41
|
|
|
{ |
|
42
|
40 |
|
Assert::keyExists($options, 'root-dir', 'The "root-dir" option is missing.'); |
|
43
|
|
|
|
|
44
|
39 |
|
$options = array_replace_recursive(self::$defaultOptions, $options); |
|
45
|
|
|
|
|
46
|
39 |
|
if (!isset($options['path'])) { |
|
47
|
8 |
|
$options['path'] = $targetMethod->getClass()->getDirectory().'/path-mappings.json'; |
|
48
|
8 |
|
} |
|
49
|
|
|
|
|
50
|
39 |
|
Assert::stringNotEmpty($options['path'], 'The "path" option should be a non-empty string. Got: %s'); |
|
51
|
38 |
|
Assert::stringNotEmpty($options['root-dir'], 'The "root-dir" option should be a non-empty string. Got: %s'); |
|
52
|
37 |
|
Assert::boolean($options['optimize'], 'The "optimize" option should be a boolean. Got: %s'); |
|
53
|
36 |
|
Assert::isArray($options['change-stream'], 'The "change-stream" option should be an array. Got: %s'); |
|
54
|
|
|
|
|
55
|
35 |
|
$path = Path::makeAbsolute($options['path'], $options['root-dir']); |
|
56
|
35 |
|
$relPath = Path::makeRelative($path, $targetMethod->getClass()->getDirectory()); |
|
57
|
35 |
|
$relBaseDir = Path::makeRelative($options['root-dir'], $targetMethod->getClass()->getDirectory()); |
|
58
|
|
|
|
|
59
|
35 |
|
$escPath = '__DIR__.'.var_export('/'.$relPath, true); |
|
60
|
|
|
$escBaseDir = $relBaseDir |
|
61
|
35 |
|
? '__DIR__.'.var_export('/'.$relBaseDir, true) |
|
62
|
35 |
|
: '__DIR__'; |
|
63
|
|
|
|
|
64
|
35 |
|
if ($options['optimize']) { |
|
65
|
5 |
|
$streamGenerator = $generatorRegistry->getServiceGenerator(GeneratorRegistry::CHANGE_STREAM, $options['change-stream']['type']); |
|
66
|
5 |
|
$streamOptions = $options['change-stream']; |
|
67
|
5 |
|
$streamOptions['root-dir'] = $options['root-dir']; |
|
68
|
5 |
|
$streamGenerator->generateNewInstance('stream', $targetMethod, $generatorRegistry, $streamOptions); |
|
69
|
|
|
|
|
70
|
5 |
|
$targetMethod->getClass()->addImport(new Import('Puli\\Repository\\OptimizedJsonRepository')); |
|
71
|
|
|
|
|
72
|
5 |
|
$targetMethod->addBody(sprintf( |
|
73
|
5 |
|
'$%s = new OptimizedJsonRepository(%s, %s, $stream);', |
|
74
|
5 |
|
$varName, |
|
75
|
5 |
|
$escPath, |
|
76
|
|
|
$escBaseDir |
|
77
|
5 |
|
)); |
|
78
|
5 |
|
} else { |
|
79
|
31 |
|
$targetMethod->getClass()->addImport(new Import('Puli\\Repository\\JsonRepository')); |
|
80
|
|
|
|
|
81
|
30 |
|
$targetMethod->addBody(sprintf( |
|
82
|
30 |
|
'$%s = new JsonRepository(%s, %s);', |
|
83
|
30 |
|
$varName, |
|
84
|
30 |
|
$escPath, |
|
85
|
|
|
$escBaseDir |
|
86
|
30 |
|
)); |
|
87
|
|
|
} |
|
88
|
35 |
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|