1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* |
5
|
|
|
* |
6
|
|
|
* |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace MagentoHackathon\Composer; |
10
|
|
|
|
11
|
|
|
use MagentoHackathon\Composer\Magento\ProjectConfig; |
12
|
|
|
|
13
|
|
|
class Helper |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var \SplFileInfo |
17
|
|
|
*/ |
18
|
|
|
protected $projectRoot; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var ProjectConfig |
22
|
|
|
*/ |
23
|
|
|
protected $magentoProjectConfig; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param \SplFileInfo $projectRoot |
27
|
|
|
*/ |
28
|
|
|
public function __construct(\SplFileInfo $projectRoot) |
29
|
|
|
{ |
30
|
|
|
if (!file_exists($projectRoot->getPathname().'/composer.json')) { |
31
|
|
|
throw new \InvalidArgumentException('no composer.json found in project root'); |
32
|
|
|
} |
33
|
|
|
$this->projectRoot = $projectRoot; |
34
|
|
|
|
35
|
|
|
$reader = new \Eloquent\Composer\Configuration\ConfigurationReader; |
36
|
|
|
$composerJsonObject = $reader->read($this->projectRoot.'/composer.json'); |
37
|
|
|
$this->magentoProjectConfig = new ProjectConfig( |
38
|
|
|
(array)$composerJsonObject->extra(), |
39
|
|
|
(array)$composerJsonObject |
40
|
|
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function getVendorDirectory() |
44
|
|
|
{ |
45
|
|
|
/* |
46
|
|
|
$reader = new \Eloquent\Composer\Configuration\ConfigurationReader; |
47
|
|
|
$composerJsonObject = $reader->read($this->projectRoot.'/composer.json'); |
48
|
|
|
return $composerJsonObject->vendorName(); |
49
|
|
|
*/ |
50
|
|
|
return new \SplFileInfo($this->projectRoot.'/vendor'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function getInstalledPackages() |
54
|
|
|
{ |
55
|
|
|
|
56
|
|
|
$installedJsonObject = json_decode(file_get_contents( |
57
|
|
|
$this->getVendorDirectory()->getPathname().'/composer/installed.json' |
58
|
|
|
), true); |
59
|
|
|
return $installedJsonObject; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return ProjectConfig |
64
|
|
|
*/ |
65
|
|
|
public function getMagentoProjectConfig() |
66
|
|
|
{ |
67
|
|
|
return $this->magentoProjectConfig; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function getPackageByName($name) |
71
|
|
|
{ |
72
|
|
|
$result = null; |
73
|
|
|
foreach ($this->getInstalledPackages() as $package) { |
74
|
|
|
if ($package['name'] == $name) { |
75
|
|
|
$result = $package; |
76
|
|
|
break; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
return $result; |
80
|
|
|
} |
81
|
|
|
|
82
|
3 |
|
public static function initMagentoRootDir( |
83
|
|
|
ProjectConfig $projectConfig, |
84
|
|
|
\Composer\IO\IOInterface $io, |
85
|
|
|
\Composer\Util\Filesystem $filesystem, |
86
|
|
|
$vendorDir |
87
|
|
|
) { |
88
|
3 |
|
if (false === $projectConfig->hasMagentoRootDir()) { |
89
|
|
|
$projectConfig->setMagentoRootDir( |
90
|
|
|
$io->ask( |
91
|
|
|
sprintf('please define your magento root dir [%s]', ProjectConfig::DEFAULT_MAGENTO_ROOT_DIR), |
92
|
|
|
ProjectConfig::DEFAULT_MAGENTO_ROOT_DIR |
93
|
|
|
) |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
3 |
|
$magentoRootDirPath = $projectConfig->getMagentoRootDir(); |
98
|
3 |
|
$magentoRootDir = new \SplFileInfo($magentoRootDirPath); |
99
|
|
|
|
100
|
3 |
|
if (!is_dir($magentoRootDirPath) |
101
|
|
|
&& $io->askConfirmation( |
102
|
3 |
|
'magento root dir "' . $magentoRootDirPath . '" missing! create now? [Y,n] ' |
103
|
|
|
) |
104
|
|
|
) { |
105
|
|
|
$filesystem->ensureDirectoryExists($magentoRootDir); |
106
|
|
|
$io->write('magento root dir "' . $magentoRootDirPath . '" created'); |
107
|
|
|
} |
108
|
|
|
|
109
|
3 |
|
if (!is_dir($magentoRootDirPath)) { |
110
|
|
|
$dir = self::joinFilePath($vendorDir, $magentoRootDirPath); |
|
|
|
|
111
|
|
|
} |
112
|
3 |
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* join 2 paths |
116
|
|
|
* |
117
|
|
|
* @param $path1 |
118
|
|
|
* @param $path2 |
119
|
|
|
* @param $delimiter |
120
|
|
|
* @param bool $prependDelimiter |
121
|
|
|
* @param string $additionalPrefix |
122
|
|
|
* |
123
|
|
|
* @internal param $url1 |
124
|
|
|
* @internal param $url2 |
125
|
|
|
* |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
|
|
public static function joinPath($path1, $path2, $delimiter, $prependDelimiter = false, $additionalPrefix = '') |
129
|
|
|
{ |
130
|
|
|
$prefix = $additionalPrefix . $prependDelimiter ? $delimiter : ''; |
131
|
|
|
|
132
|
|
|
return $prefix . join( |
133
|
|
|
$delimiter, |
134
|
|
|
array( |
135
|
|
|
explode($path1, $delimiter), |
136
|
|
|
explode($path2, $delimiter) |
137
|
|
|
) |
138
|
|
|
); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param $path1 |
143
|
|
|
* @param $path2 |
144
|
|
|
* |
145
|
|
|
* @return string |
146
|
|
|
*/ |
147
|
|
|
public static function joinFilePath($path1, $path2) |
148
|
|
|
{ |
149
|
|
|
return self::joinPath($path1, $path2, DIRECTORY_SEPARATOR, true); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.