1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WellCommerce\Bundle\GeneratorBundle\Manipulator; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
6
|
|
|
use Symfony\Component\HttpKernel\Bundle\BundleInterface; |
7
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
8
|
|
|
use Symfony\Component\Yaml\Yaml; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class ConfigurationManipulator |
12
|
|
|
* |
13
|
|
|
* @author Adam Piotrowski <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class ConfigurationManipulator |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var KernelInterface |
19
|
|
|
*/ |
20
|
|
|
private $kernel; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $file; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var Filesystem |
29
|
|
|
*/ |
30
|
|
|
private $filesystem; |
31
|
|
|
|
32
|
|
|
public function __construct(KernelInterface $kernel) |
33
|
|
|
{ |
34
|
|
|
$this->kernel = $kernel; |
35
|
|
|
$this->filesystem = new Filesystem(); |
36
|
|
|
$this->file = $this->kernel->getRootDir() . '/config/wellcommerce.yml'; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function addBundle(BundleInterface $bundle) |
40
|
|
|
{ |
41
|
|
|
$code = $this->getImportCode($bundle); |
42
|
|
|
$currentContents = file_get_contents($this->file); |
43
|
|
|
|
44
|
|
|
if (false === strpos($currentContents, $code)) { |
45
|
|
|
$lastImportedPath = $this->findLastImportedPath($currentContents); |
46
|
|
|
$importsPosition = strpos($currentContents, 'imports:'); |
47
|
|
|
$lastImportPosition = strpos($currentContents, $lastImportedPath, $importsPosition); |
48
|
|
|
$targetPosition = strpos($currentContents, "\n", $lastImportPosition); |
49
|
|
|
$newContents = substr($currentContents, 0, $targetPosition) . "\n" . $code . substr($currentContents, $targetPosition); |
50
|
|
|
|
51
|
|
|
$this->filesystem->dumpFile($this->file, $newContents); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function removeBundle(BundleInterface $bundle) |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
private function getImportCode(BundleInterface $bundle) |
61
|
|
|
{ |
62
|
|
|
return sprintf(<<<EOF |
63
|
|
|
- { resource: "@%s/Resources/config/config.yml" } |
64
|
|
|
EOF |
65
|
|
|
, |
66
|
|
|
$bundle->getName() |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
private function findLastImportedPath(string $content) |
71
|
|
|
{ |
72
|
|
|
$data = Yaml::parse($content); |
73
|
|
|
if (!isset($data['imports'])) { |
74
|
|
|
return false; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$lastImport = end($data['imports']); |
78
|
|
|
if (!isset($lastImport['resource'])) { |
79
|
|
|
return false; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $lastImport['resource']; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.