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\Discovery; |
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 JsonDiscovery}. |
23
|
|
|
* |
24
|
|
|
* @since 1.0 |
25
|
|
|
* |
26
|
|
|
* @author Bernhard Schussek <[email protected]> |
27
|
|
|
*/ |
28
|
|
View Code Duplication |
class JsonDiscoveryGenerator implements ServiceGenerator |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
28 |
|
public function generateNewInstance($varName, Method $targetMethod, GeneratorRegistry $generatorRegistry, array $options = array()) |
34
|
|
|
{ |
35
|
28 |
|
Assert::keyExists($options, 'root-dir', 'The "root-dir" option is missing.'); |
36
|
|
|
|
37
|
28 |
|
if (!isset($options['path'])) { |
38
|
|
|
$options['path'] = $targetMethod->getClass()->getDirectory().'/bindings.json'; |
39
|
|
|
} |
40
|
|
|
|
41
|
28 |
|
Assert::stringNotEmpty($options['root-dir'], 'The "root-dir" option should be a non-empty string. Got: %s'); |
42
|
28 |
|
Assert::stringNotEmpty($options['path'], 'The "path" option should be a non-empty string. Got: %s'); |
43
|
|
|
|
44
|
28 |
|
$path = Path::makeAbsolute($options['path'], $options['root-dir']); |
45
|
28 |
|
$relPath = Path::makeRelative($path, $targetMethod->getClass()->getDirectory()); |
46
|
|
|
|
47
|
28 |
|
$escPath = '__DIR__.'.var_export('/'.$relPath, true); |
48
|
|
|
|
49
|
28 |
|
$targetMethod->getClass()->addImport(new Import('Puli\Discovery\JsonDiscovery')); |
50
|
28 |
|
$targetMethod->getClass()->addImport(new Import('Puli\Discovery\Binding\Initializer\ResourceBindingInitializer')); |
51
|
|
|
|
52
|
28 |
|
$targetMethod->addBody(sprintf( |
53
|
28 |
|
"$%s = new JsonDiscovery(%s, array(\n new ResourceBindingInitializer(\$repo),\n));", |
54
|
28 |
|
$varName, |
55
|
|
|
$escPath |
56
|
28 |
|
)); |
57
|
28 |
|
} |
58
|
|
|
} |
59
|
|
|
|
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.