1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Helthe Turbolinks package. |
5
|
|
|
* |
6
|
|
|
* (c) Carl Alexander <[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 Helthe\Bundle\TurbolinksBundle\Composer; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Process\Process; |
15
|
|
|
use Symfony\Component\Process\PhpExecutableFinder; |
16
|
|
|
use Composer\Script\Event; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* ScriptHandler for the HeltheTurbolinksBundle. |
20
|
|
|
* |
21
|
|
|
* @author Carl Alexander <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class ScriptHandler |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Install the component assets under the symfony bundle for better integration |
27
|
|
|
* with Symfony. |
28
|
|
|
* |
29
|
|
|
* @param Event $event |
30
|
|
|
*/ |
31
|
|
|
public static function installAssets(Event $event) |
32
|
|
|
{ |
33
|
|
|
$options = self::getOptions($event); |
34
|
|
|
$consoleDir = self::getConsoleDir($event, 'turbolinks install'); |
35
|
|
|
|
36
|
|
|
static::executeCommand($event, $consoleDir, 'helthe:turbolinks:install', $options['process-timeout']); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Execute command. |
41
|
|
|
* |
42
|
|
|
* @param Event $event |
43
|
|
|
* @param string $consoleDir |
44
|
|
|
* @param string $cmd |
45
|
|
|
* @param integer $timeout |
46
|
|
|
* |
47
|
|
|
* @throws \RuntimeException |
48
|
|
|
*/ |
49
|
|
|
protected static function executeCommand(Event $event, $consoleDir, $cmd, $timeout = 300) |
50
|
|
|
{ |
51
|
|
|
$php = escapeshellarg(self::getPhp()); |
52
|
|
|
$console = escapeshellarg($consoleDir.'/console'); |
53
|
|
|
if ($event->getIO()->isDecorated()) { |
54
|
|
|
$console .= ' --ansi'; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$process = new Process($php.' '.$console.' '.$cmd, null, null, null, $timeout); |
58
|
|
|
$process->run(function ($type, $buffer) { echo $buffer; }); |
59
|
|
|
if (!$process->isSuccessful()) { |
60
|
|
|
throw new \RuntimeException(sprintf('An error occurred when executing the "%s" command.', escapeshellarg($cmd))); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get the default options. |
66
|
|
|
* |
67
|
|
|
* @param Event $event |
68
|
|
|
* |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
|
|
protected static function getOptions(Event $event) |
72
|
|
|
{ |
73
|
|
|
$options = array_merge(array( |
74
|
|
|
'symfony-app-dir' => 'app', |
75
|
|
|
'symfony-bin-dir' => 'bin', |
76
|
|
|
), $event->getComposer()->getPackage()->getExtra()); |
77
|
|
|
|
78
|
|
|
$options['process-timeout'] = $event->getComposer()->getConfig()->get('process-timeout'); |
79
|
|
|
|
80
|
|
|
return $options; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get path to the PHP executable. |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
* |
88
|
|
|
* @throws \RuntimeException |
89
|
|
|
*/ |
90
|
|
|
protected static function getPhp() |
91
|
|
|
{ |
92
|
|
|
$phpFinder = new PhpExecutableFinder; |
93
|
|
|
if (!$phpPath = $phpFinder->find()) { |
94
|
|
|
throw new \RuntimeException('The php executable could not be found, add it to your PATH environment variable and try again'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $phpPath; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Returns a relative path to the directory that contains the `console` command. |
102
|
|
|
* |
103
|
|
|
* @param Event $event The command event. |
104
|
|
|
* @param string $actionName The name of the action |
105
|
|
|
* |
106
|
|
|
* @return string|null The path to the console directory, null if not found. |
107
|
|
|
*/ |
108
|
|
|
protected static function getConsoleDir(Event $event, $actionName) |
109
|
|
|
{ |
110
|
|
|
$options = self::getOptions($event); |
111
|
|
|
|
112
|
|
|
if (self::useNewDirectoryStructure($options)) { |
113
|
|
|
if (!self::hasDirectory($event, 'symfony-bin-dir', $options['symfony-bin-dir'], $actionName)) { |
114
|
|
|
return; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $options['symfony-bin-dir']; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
if (!self::hasDirectory($event, 'symfony-app-dir', $options['symfony-app-dir'], 'execute command')) { |
121
|
|
|
return; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $options['symfony-app-dir']; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Returns true if the new directory structure is used. |
129
|
|
|
* |
130
|
|
|
* @param array $options Composer options |
131
|
|
|
* |
132
|
|
|
* @return bool |
133
|
|
|
*/ |
134
|
|
|
protected static function useNewDirectoryStructure(array $options) |
135
|
|
|
{ |
136
|
|
|
return isset($options['symfony-var-dir']) && is_dir($options['symfony-var-dir']); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
protected static function hasDirectory(Event $event, $configName, $path, $actionName) |
140
|
|
|
{ |
141
|
|
|
if (!is_dir($path)) { |
142
|
|
|
$event->getIO()->write(sprintf('The %s (%s) specified in composer.json was not found in %s, can not %s.', $configName, $path, getcwd(), $actionName)); |
143
|
|
|
|
144
|
|
|
return false; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return true; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|