-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
AssetUtil.php
206 lines (177 loc) · 5.99 KB
/
AssetUtil.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
/*
* This file is part of the Foxy package.
*
* (c) François Pluchino <francois.pluchino@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Foxy\Util;
use Composer\Installer\InstallationManager;
use Composer\Package\Link;
use Composer\Package\PackageInterface;
use Foxy\Asset\AssetManagerInterface;
use Foxy\Asset\AssetPackage;
/**
* Helper for Foxy.
*
* @author François Pluchino <francois.pluchino@gmail.com>
*/
class AssetUtil
{
/**
* Get the name for the asset dependency.
*
* @param PackageInterface $package The package
*
* @return string
*/
public static function getName(PackageInterface $package)
{
return AssetPackage::COMPOSER_PREFIX.str_replace(array('/'), '--', $package->getName());
}
/**
* Get the path of asset file.
*
* @param InstallationManager $installationManager The installation manager
* @param AssetManagerInterface $assetManager The asset manager
* @param PackageInterface $package The package
* @param array $configPackages The packages defined in config
*
* @return null|string
*/
public static function getPath(InstallationManager $installationManager, AssetManagerInterface $assetManager, PackageInterface $package, array $configPackages = array())
{
$path = null;
if (static::isAsset($package, $configPackages)) {
$installPath = $installationManager->getInstallPath($package);
$filename = $installPath.'/'.$assetManager->getPackageName();
$path = file_exists($filename) ? str_replace('\\', '/', realpath($filename)) : null;
}
return $path;
}
/**
* Check if the package is available for Foxy.
*
* @param PackageInterface $package The package
* @param array $configPackages The packages defined in config
*
* @return bool
*/
public static function isAsset(PackageInterface $package, array $configPackages = array())
{
$projectConfig = self::getProjectActivation($package, $configPackages);
$enabled = false !== $projectConfig;
return $enabled && (static::hasExtraActivation($package)
|| static::hasPluginDependency($package->getRequires())
|| static::hasPluginDependency($package->getDevRequires())
|| true === $projectConfig);
}
/**
* Check if foxy is enabled in extra section of package.
*
* @param PackageInterface $package The package
*
* @return bool
*/
public static function hasExtraActivation(PackageInterface $package)
{
$extra = $package->getExtra();
return isset($extra['foxy']) && true === $extra['foxy'];
}
/**
* Check if the package contains assets.
*
* @param Link[] $requires The require links
*
* @return bool
*/
public static function hasPluginDependency(array $requires)
{
$assets = false;
foreach ($requires as $require) {
if ('foxy/foxy' === $require->getTarget()) {
$assets = true;
break;
}
}
return $assets;
}
/**
* Check if the package is enabled by the project config.
*
* @param PackageInterface $package The package
* @param array $configPackages The packages defined in config
*
* @return bool
*/
public static function isProjectActivation(PackageInterface $package, array $configPackages)
{
return true === self::getProjectActivation($package, $configPackages);
}
/**
* Format the asset package.
*
* @param PackageInterface $package The composer package
* @param string $packageName The package name
* @param array $packageValue The package value
*
* @return array
*/
public static function formatPackage(PackageInterface $package, $packageName, array $packageValue)
{
$packageValue['name'] = $packageName;
if (!isset($packageValue['version'])) {
$extra = $package->getExtra();
$version = $package->getPrettyVersion();
if (0 === strpos($version, 'dev-') && isset($extra['branch-alias'][$version])) {
$version = $extra['branch-alias'][$version];
}
$packageValue['version'] = self::formatVersion(str_replace('-dev', '', $version));
}
return $packageValue;
}
/**
* Format the version for the asset package.
*
* @param string $version The branch alias version
*
* @return string
*/
private static function formatVersion($version)
{
$version = str_replace(array('x', 'X', '*'), '0', $version);
$exp = explode('.', $version);
if (($size = \count($exp)) < 3) {
for ($i = $size; $i < 3; ++$i) {
$exp[] = '0';
}
}
return $exp[0].'.'.$exp[1].'.'.$exp[2];
}
/**
* Get the activation of the package defined in the project config.
*
* @param PackageInterface $package The package
* @param array $configPackages The packages defined in config
*
* @return null|bool returns NULL, if the package isn't defined in the project config
*/
private static function getProjectActivation(PackageInterface $package, array $configPackages)
{
$name = $package->getName();
$value = null;
foreach ($configPackages as $pattern => $activation) {
if (\is_int($pattern) && \is_string($activation)) {
$pattern = $activation;
$activation = true;
}
if ((0 === strpos($pattern, '/') && preg_match($pattern, $name)) || fnmatch($pattern, $name)) {
$value = $activation;
break;
}
}
return $value;
}
}