1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Admingenerator\GeneratorBundle\Composer; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Process\Process; |
6
|
|
|
use Symfony\Component\Process\PhpExecutableFinder; |
7
|
|
|
use Composer\Script\CommandEvent; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class ScriptHandler |
11
|
|
|
* |
12
|
|
|
* Inspired from SensioDistributionBundle ScriptHandler |
13
|
|
|
* |
14
|
|
|
* @package Admingenerator\GeneratorBundle\Composer |
15
|
|
|
*/ |
16
|
|
|
class ScriptHandler |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Installs the assets under the admin/components directory into web root directory. |
20
|
|
|
* |
21
|
|
|
* @param $event CommandEvent A instance |
22
|
|
|
*/ |
23
|
|
|
public static function installAssets(CommandEvent $event) |
24
|
|
|
{ |
25
|
|
|
$options = self::getOptions($event); |
26
|
|
|
$consoleDir = self::getConsoleDir($event, 'install Generator assets'); |
27
|
|
|
if (null === $consoleDir) { |
28
|
|
|
return; |
29
|
|
|
} |
30
|
|
|
$webDir = $options['symfony-web-dir']; |
31
|
|
|
$symlink = ''; |
|
|
|
|
32
|
|
|
|
33
|
|
|
if (!self::hasDirectory($event, 'symfony-web-dir', $webDir, 'install Generator assets')) { |
34
|
|
|
return; |
35
|
|
|
} |
36
|
|
|
static::executeCommand($event, $consoleDir, 'admin:assets-install'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $configName |
41
|
|
|
* @param string $actionName |
42
|
|
|
*/ |
43
|
|
|
protected static function hasDirectory(CommandEvent $event, $configName, $path, $actionName) |
44
|
|
|
{ |
45
|
|
|
if (!is_dir($path)) { |
46
|
|
|
$event->getIO()->write(sprintf('The %s (%s) specified in composer.json was not found in %s, can not %s.', $configName, $path, getcwd(), $actionName)); |
47
|
|
|
|
48
|
|
|
return false; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return true; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $consoleDir |
56
|
|
|
* @param string $cmd |
57
|
|
|
*/ |
58
|
|
|
protected static function executeCommand(CommandEvent $event, $consoleDir, $cmd, $timeout = 300) |
59
|
|
|
{ |
60
|
|
|
$php = escapeshellarg(self::getPhp(false)); |
61
|
|
|
$phpArgs = implode(' ', array_map('escapeshellarg', self::getPhpArguments())); |
62
|
|
|
$console = escapeshellarg($consoleDir.'/console'); |
63
|
|
|
if ($event->getIO()->isDecorated()) { |
64
|
|
|
$console .= ' --ansi'; |
65
|
|
|
} |
66
|
|
|
$process = new Process($php.($phpArgs ? ' '.$phpArgs : '').' '.$console.' '.$cmd, null, null, null, $timeout); |
67
|
|
|
$process->run(function ($type, $buffer) use ($event) { $event->getIO()->write($buffer, false); }); |
68
|
|
|
if (!$process->isSuccessful()) { |
69
|
|
|
throw new \RuntimeException(sprintf('An error occurred when executing the "%s" command.', escapeshellarg($cmd))); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
protected static function getOptions(CommandEvent $event) |
74
|
|
|
{ |
75
|
|
|
$options = $event->getComposer()->getPackage()->getExtra(); |
76
|
|
|
$options['process-timeout'] = $event->getComposer()->getConfig()->get('process-timeout'); |
77
|
|
|
|
78
|
|
|
return $options; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
protected static function getPhp($includeArgs = true) |
82
|
|
|
{ |
83
|
|
|
$phpFinder = new PhpExecutableFinder(); |
84
|
|
|
if (!$phpPath = $phpFinder->find($includeArgs)) { |
85
|
|
|
throw new \RuntimeException('The php executable could not be found, add it to your PATH environment variable and try again'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $phpPath; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
protected static function getPhpArguments() |
92
|
|
|
{ |
93
|
|
|
$arguments = array(); |
94
|
|
|
$phpFinder = new PhpExecutableFinder(); |
95
|
|
|
if (method_exists($phpFinder, 'findArguments')) { |
96
|
|
|
$arguments = $phpFinder->findArguments(); |
97
|
|
|
} |
98
|
|
|
if (false !== $ini = php_ini_loaded_file()) { |
99
|
|
|
$arguments[] = '--php-ini='.$ini; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $arguments; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Returns a relative path to the directory that contains the `console` command. |
107
|
|
|
* |
108
|
|
|
* @param CommandEvent $event The command event. |
109
|
|
|
* @param string $actionName The name of the action |
110
|
|
|
* |
111
|
|
|
* @return string|null The path to the console directory, null if not found. |
112
|
|
|
*/ |
113
|
|
|
protected static function getConsoleDir(CommandEvent $event, $actionName) |
114
|
|
|
{ |
115
|
|
|
$options = self::getOptions($event); |
116
|
|
|
if (self::useNewDirectoryStructure($options)) { |
117
|
|
|
if (!self::hasDirectory($event, 'symfony-bin-dir', $options['symfony-bin-dir'], $actionName)) { |
118
|
|
|
return; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $options['symfony-bin-dir']; |
122
|
|
|
} |
123
|
|
|
if (!self::hasDirectory($event, 'symfony-app-dir', $options['symfony-app-dir'], 'execute command')) { |
124
|
|
|
return; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $options['symfony-app-dir']; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Returns true if the new directory structure is used. |
132
|
|
|
* |
133
|
|
|
* @param array $options Composer options |
134
|
|
|
* |
135
|
|
|
* @return bool |
136
|
|
|
*/ |
137
|
|
|
protected static function useNewDirectoryStructure(array $options) |
138
|
|
|
{ |
139
|
|
|
return isset($options['symfony-var-dir']) && is_dir($options['symfony-var-dir']); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
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.