|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BWC\Share\Composer; |
|
4
|
|
|
|
|
5
|
|
|
use Composer\Script\Event; |
|
6
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
7
|
|
|
use Symfony\Component\Finder\Finder; |
|
8
|
|
|
|
|
9
|
|
|
class Script |
|
10
|
|
|
{ |
|
11
|
|
|
static function vendor2web(Event $event) { |
|
|
|
|
|
|
12
|
|
|
$options = self::getOptions($event); |
|
13
|
|
|
if (isset($options['vendor2web']) && is_array($options['vendor2web'])) { |
|
14
|
|
|
print "Installing vendor assets to web\n"; |
|
15
|
|
|
$installType = $options['symfony-assets-install']; // hard|symlink|relative |
|
16
|
|
|
$webDir = $options['web-dir-full']; |
|
17
|
|
|
$vendorDir = $options['vendor-dir-full']; |
|
18
|
|
|
if ($installType != 'hard' && $installType != 'symlink' && $installType != 'relative') { |
|
19
|
|
|
print "ERROR: Unknown install type $installType \n"; |
|
20
|
|
|
return; |
|
21
|
|
|
} |
|
22
|
|
|
if (!is_dir($webDir)) { |
|
23
|
|
|
print "ERROR: No web dir $webDir \n"; |
|
24
|
|
|
return; |
|
25
|
|
|
} |
|
26
|
|
|
if (!is_dir(($vendorDir))) { |
|
27
|
|
|
print "ERROR: No vendor dir $vendorDir \n"; |
|
28
|
|
|
return; |
|
29
|
|
|
} |
|
30
|
|
|
if (!is_dir($webDir.DIRECTORY_SEPARATOR.'vendor')) { |
|
31
|
|
|
mkdir($webDir.DIRECTORY_SEPARATOR.'vendor'); |
|
32
|
|
|
} |
|
33
|
|
|
self::_deleteOldWebVendors($webDir); |
|
34
|
|
|
print "Installing new vendors to web:\n"; |
|
35
|
|
|
foreach ($options['vendor2web'] as $vendor=>$destination) { |
|
36
|
|
|
self::_installVendorToWeb($vendor, $destination, $webDir, $vendorDir, $installType); |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
static private function _deleteOldWebVendors($webDir) { |
|
42
|
|
|
$finder = new Finder(); |
|
43
|
|
|
$finder->directories()->in($webDir.DIRECTORY_SEPARATOR.'vendor')->depth(1); |
|
44
|
|
|
if ($finder->count()) { |
|
45
|
|
|
print "Deleting old vendors in web:\n"; |
|
46
|
|
|
foreach ($finder as $dir) { |
|
47
|
|
|
/** @var $name \SplFileInfo */ |
|
48
|
|
|
$name = $dir->getRelativePathname(); |
|
49
|
|
|
$fn = $webDir.DIRECTORY_SEPARATOR.'vendor'.DIRECTORY_SEPARATOR.$name; |
|
50
|
|
|
print " $name\n"; |
|
51
|
|
|
@unlink($fn); |
|
|
|
|
|
|
52
|
|
|
@rmdir($fn); |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
static private function _installVendorToWeb($vendor, $destination, $webDir, $vendorDir, $installType) { |
|
|
|
|
|
|
58
|
|
|
print " $vendor => $destination\n"; |
|
59
|
|
|
$source = $vendorDir.DIRECTORY_SEPARATOR.$vendor; |
|
60
|
|
|
$dir = $webDir.DIRECTORY_SEPARATOR.'vendor'; |
|
61
|
|
|
if (!is_dir($dir)) { |
|
62
|
|
|
mkdir($dir); |
|
63
|
|
|
} |
|
64
|
|
|
$target = $dir.DIRECTORY_SEPARATOR.$destination; |
|
65
|
|
|
if (!file_exists($source)) { |
|
66
|
|
|
print "WARNING: Source $vendor does not exist\n"; |
|
67
|
|
|
return; |
|
68
|
|
|
} |
|
69
|
|
|
if (file_exists($target)) { |
|
70
|
|
|
@unlink($target); |
|
|
|
|
|
|
71
|
|
|
@rmdir($target); |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
// TODO target might contain some dir... should be created if not exist |
|
74
|
|
|
symlink($source, $target); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
static protected function getOptions(Event $event) { |
|
78
|
|
|
$options = array_merge( |
|
79
|
|
|
array( |
|
80
|
|
|
'symfony-app-dir' => 'app', |
|
81
|
|
|
'symfony-web-dir' => 'web' |
|
82
|
|
|
), |
|
83
|
|
|
$event->getComposer()->getPackage()->getExtra() |
|
84
|
|
|
); |
|
85
|
|
|
$options['symfony-assets-install'] = getenv('SYMFONY_ASSETS_INSTALL') ?: @$options['symfony-assets-install']; |
|
86
|
|
|
if (!$options['symfony-assets-install']) $options['symfony-assets-install'] = 'symlink'; |
|
87
|
|
|
$options['process-timeout'] = $event->getComposer()->getConfig()->get('process-timeout'); |
|
88
|
|
|
$options['vendor-dir'] = $event->getComposer()->getConfig()->get('vendor-dir'); |
|
89
|
|
|
$composerJsonFile = realpath(\Composer\Factory::getComposerFile()); |
|
90
|
|
|
$rootDir = dirname($composerJsonFile); |
|
91
|
|
|
$options['root-dir'] = $rootDir; |
|
92
|
|
|
$options['app-dir-full'] = $rootDir.DIRECTORY_SEPARATOR.$options['symfony-app-dir']; |
|
93
|
|
|
$options['web-dir-full'] = $rootDir.DIRECTORY_SEPARATOR.$options['symfony-web-dir']; |
|
94
|
|
|
|
|
95
|
|
|
if ('/' == substr($options['vendor-dir'], 0, 1) || |
|
96
|
|
|
':' == substr($options['vendor-dir'], 1, 1) |
|
97
|
|
|
) { |
|
98
|
|
|
$options['vendor-dir-full'] = $options['vendor-dir']; |
|
99
|
|
|
} else { |
|
100
|
|
|
$options['vendor-dir-full'] = $rootDir.DIRECTORY_SEPARATOR.$options['vendor-dir']; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return $options; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.