hughcube-php /
composer-proxy-plugin
| 1 | <?php |
||
| 2 | |||
| 3 | /* |
||
| 4 | * This file is part of the Composer Proxy Plugin package. |
||
| 5 | * |
||
| 6 | * (c) hugh.li <[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 HughCube\Composer\ProxyPlugin\Config; |
||
| 13 | |||
| 14 | use Composer\Composer; |
||
| 15 | use Composer\IO\IOInterface; |
||
| 16 | use Composer\Json\JsonFile; |
||
| 17 | use Seld\JsonLint\ParsingException; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Plugin Config builder. |
||
| 21 | * |
||
| 22 | * @author hugh.li <[email protected]> |
||
| 23 | */ |
||
| 24 | class ConfigBuilder |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @var Config |
||
| 28 | */ |
||
| 29 | protected static $config; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Build the config of plugin. |
||
| 33 | * |
||
| 34 | * @param Composer $composer The composer |
||
| 35 | * @param IOInterface|null $io The composer input/output |
||
| 36 | * |
||
| 37 | * @throws ParsingException |
||
| 38 | * |
||
| 39 | * @return Config |
||
| 40 | */ |
||
| 41 | public static function build(Composer $composer, IOInterface $io = null) |
||
| 42 | { |
||
| 43 | if (!static::$config instanceof Config) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 44 | $config = self::getConfigBase($composer, $io); |
||
| 45 | static::$config = new Config($config); |
||
| 46 | } |
||
| 47 | |||
| 48 | return static::$config; |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Get the base of data. |
||
| 53 | * |
||
| 54 | * @param Composer $composer The composer |
||
| 55 | * @param IOInterface|null $io The composer input/output |
||
| 56 | * |
||
| 57 | * @throws ParsingException |
||
| 58 | * |
||
| 59 | * @return array |
||
| 60 | */ |
||
| 61 | private static function getConfigBase(Composer $composer, IOInterface $io = null) |
||
| 62 | { |
||
| 63 | $globalPackageConfig = self::getGlobalConfig($composer, 'composer', $io); |
||
| 64 | $globalConfig = self::getGlobalConfig($composer, 'config', $io); |
||
| 65 | $packageConfig = self::drawProxyConfig($composer->getPackage()->getConfig()); |
||
| 66 | |||
| 67 | return array_merge($globalPackageConfig, $globalConfig, $packageConfig); |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Get the data of the global config. |
||
| 72 | * |
||
| 73 | * @param Composer $composer The composer |
||
| 74 | * @param string $filename The filename |
||
| 75 | * @param IOInterface|null $io The composer input/output |
||
| 76 | * |
||
| 77 | * @throws ParsingException |
||
| 78 | * |
||
| 79 | * @return array |
||
| 80 | */ |
||
| 81 | private static function getGlobalConfig(Composer $composer, $filename, IOInterface $io = null) |
||
| 82 | { |
||
| 83 | $config = array(); |
||
| 84 | |||
| 85 | $home = self::getComposerHome($composer); |
||
| 86 | if (false == $home) { |
||
|
0 ignored issues
–
show
|
|||
| 87 | return $config; |
||
| 88 | } |
||
| 89 | |||
| 90 | $file = new JsonFile($home.'/'.$filename.'.json'); |
||
| 91 | if (!$file->exists()) { |
||
| 92 | return $config; |
||
| 93 | } |
||
| 94 | |||
| 95 | $config = self::drawProxyConfig($file->read()); |
||
| 96 | |||
| 97 | if (!empty($config) && $io instanceof IOInterface && $io->isDebug()) { |
||
| 98 | $io->write('Loading proxies config in file '.$file->getPath()); |
||
| 99 | } |
||
| 100 | |||
| 101 | return $config; |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Get the home directory of composer. |
||
| 106 | * |
||
| 107 | * @param Composer $composer The composer |
||
| 108 | * |
||
| 109 | * @return string|null |
||
| 110 | */ |
||
| 111 | private static function getComposerHome(Composer $composer) |
||
| 112 | { |
||
| 113 | if (null == $composer->getConfig()) { |
||
| 114 | return null; |
||
| 115 | } |
||
| 116 | |||
| 117 | if (!$composer->getConfig()->has('home')) { |
||
| 118 | return null; |
||
| 119 | } |
||
| 120 | |||
| 121 | return $composer->getConfig()->get('home'); |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Draw the config of proxy. |
||
| 126 | * |
||
| 127 | * @param mixed $data |
||
| 128 | * |
||
| 129 | * @return array |
||
| 130 | */ |
||
| 131 | private static function drawProxyConfig($data) |
||
| 132 | { |
||
| 133 | if (isset($data['config'], $data['config']['proxies']) && is_array($data['config']['proxies'])) { |
||
| 134 | return $data['config']['proxies']; |
||
| 135 | } |
||
| 136 | |||
| 137 | return []; |
||
| 138 | } |
||
| 139 | } |
||
| 140 |