1
|
|
|
<?php namespace Arcanedev\Composer\Entities\PackageTraits; |
2
|
|
|
|
3
|
|
|
use Arcanedev\Composer\Entities\PluginState; |
4
|
|
|
use Composer\Package\RootPackageInterface; |
5
|
|
|
use Composer\Repository\RepositoryManager; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Trait RepositoriesTrait |
9
|
|
|
* |
10
|
|
|
* @package Arcanedev\Composer\Entities\PackageTraits |
11
|
|
|
* @author ARCANEDEV <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
trait RepositoriesTrait |
14
|
|
|
{ |
15
|
|
|
/* ------------------------------------------------------------------------------------------------ |
16
|
|
|
| Properties |
17
|
|
|
| ------------------------------------------------------------------------------------------------ |
18
|
|
|
*/ |
19
|
|
|
/** @var array $json */ |
20
|
|
|
protected $json = []; |
21
|
|
|
|
22
|
|
|
/* ------------------------------------------------------------------------------------------------ |
23
|
|
|
| Getters & Setters |
24
|
|
|
| ------------------------------------------------------------------------------------------------ |
25
|
|
|
*/ |
26
|
|
|
/** |
27
|
|
|
* Get composer. |
28
|
|
|
* |
29
|
|
|
* @return \Composer\Composer |
30
|
|
|
*/ |
31
|
|
|
abstract public function getComposer(); |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Get the Logger. |
35
|
|
|
* |
36
|
|
|
* @return \Arcanedev\Composer\Utilities\Logger |
37
|
|
|
*/ |
38
|
|
|
abstract public function getLogger(); |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get the json. |
42
|
|
|
* |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
|
|
abstract public function getJson(); |
46
|
|
|
|
47
|
|
|
/* ------------------------------------------------------------------------------------------------ |
48
|
|
|
| Main Functions |
49
|
|
|
| ------------------------------------------------------------------------------------------------ |
50
|
|
|
*/ |
51
|
|
|
/** |
52
|
|
|
* Add a collection of repositories described by the given configuration |
53
|
|
|
* to the given package and the global repository manager. |
54
|
|
|
* |
55
|
|
|
* @param \Composer\Package\RootPackageInterface $root |
56
|
|
|
* @param \Arcanedev\Composer\Entities\PluginState $state |
57
|
|
|
*/ |
58
|
|
|
private function addRepositories(RootPackageInterface $root, PluginState $state) |
59
|
|
|
{ |
60
|
|
|
$json = $this->getJson(); |
61
|
|
|
|
62
|
|
|
if (isset($json['repositories'])) { |
63
|
|
|
$prepend = $state->shouldPrependRepositories(); |
64
|
|
|
$repoManager = $this->getComposer()->getRepositoryManager(); |
65
|
|
|
$repositories = []; |
66
|
|
|
|
67
|
|
|
foreach ($json['repositories'] as $repoJson) { |
68
|
|
|
$this->addRepository($repoManager, $repositories, $repoJson, $prepend); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** @var \Composer\Package\RootPackageInterface $unwrapped */ |
72
|
|
|
$unwrapped = self::unwrapIfNeeded($root, 'setRepositories'); |
73
|
|
|
$mergedRepos = $prepend |
74
|
|
|
? array_merge($repositories, $root->getRepositories()) |
75
|
|
|
: array_merge($root->getRepositories(), $repositories); |
76
|
|
|
|
77
|
|
|
$unwrapped->setRepositories($mergedRepos); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/* ------------------------------------------------------------------------------------------------ |
82
|
|
|
| Other Functions |
83
|
|
|
| ------------------------------------------------------------------------------------------------ |
84
|
|
|
*/ |
85
|
|
|
/** |
86
|
|
|
* Add a repository to collection of repositories. |
87
|
|
|
* |
88
|
|
|
* @param \Composer\Repository\RepositoryManager $repoManager |
89
|
|
|
* @param array $repositories |
90
|
|
|
* @param array $repoJson |
91
|
|
|
* @param bool $prepend |
92
|
|
|
*/ |
93
|
|
|
private function addRepository( |
94
|
|
|
RepositoryManager $repoManager, array &$repositories, $repoJson, $prepend |
95
|
|
|
) { |
96
|
|
|
if (isset($repoJson['type'])) { |
97
|
|
|
$this->getLogger()->info( |
98
|
|
|
$prepend ? "Prepending {$repoJson['type']} repository" : "Adding {$repoJson['type']} repository" |
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
$repository = $repoManager->createRepository($repoJson['type'], $repoJson); |
102
|
|
|
|
103
|
|
|
$prepend |
104
|
|
|
? $repoManager->prependRepository($repository) |
105
|
|
|
: $repoManager->addRepository($repository); |
106
|
|
|
|
107
|
|
|
$repositories[] = $repository; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|