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
|
|
|
| Main Functions |
17
|
|
|
| ------------------------------------------------------------------------------------------------ |
18
|
|
|
*/ |
19
|
|
|
/** |
20
|
|
|
* Add a collection of repositories described by the given configuration |
21
|
|
|
* to the given package and the global repository manager. |
22
|
|
|
* |
23
|
|
|
* @param \Composer\Package\RootPackageInterface $root |
24
|
|
|
* @param \Arcanedev\Composer\Entities\PluginState $state |
25
|
|
|
*/ |
26
|
|
|
private function addRepositories(RootPackageInterface $root, PluginState $state) |
27
|
|
|
{ |
28
|
|
|
if (isset($this->json['repositories'])) { |
29
|
|
|
$prepend = $state->shouldPrependRepositories(); |
30
|
|
|
$repoManager = $this->composer->getRepositoryManager(); |
|
|
|
|
31
|
|
|
$repositories = []; |
32
|
|
|
|
33
|
|
|
foreach ($this->json['repositories'] as $repoJson) { |
|
|
|
|
34
|
|
|
$this->addRepository($repoManager, $repositories, $repoJson, $prepend); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** @var \Composer\Package\RootPackageInterface $unwrapped */ |
38
|
|
|
$unwrapped = self::unwrapIfNeeded($root, 'setRepositories'); |
39
|
|
|
$mergedRepos = $prepend |
40
|
|
|
? array_merge($repositories, $root->getRepositories()) |
41
|
|
|
: array_merge($root->getRepositories(), $repositories); |
42
|
|
|
|
43
|
|
|
$unwrapped->setRepositories($mergedRepos); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/* ------------------------------------------------------------------------------------------------ |
48
|
|
|
| Other Functions |
49
|
|
|
| ------------------------------------------------------------------------------------------------ |
50
|
|
|
*/ |
51
|
|
|
/** |
52
|
|
|
* Add a repository to collection of repositories. |
53
|
|
|
* |
54
|
|
|
* @param \Composer\Repository\RepositoryManager $repoManager |
55
|
|
|
* @param array $repositories |
56
|
|
|
* @param array $repoJson |
57
|
|
|
* @param bool $prepend |
58
|
|
|
*/ |
59
|
|
|
private function addRepository( |
60
|
|
|
RepositoryManager $repoManager, array &$repositories, $repoJson, $prepend |
61
|
|
|
) { |
62
|
|
|
if (isset($repoJson['type'])) { |
63
|
|
|
$this->logger->info( |
|
|
|
|
64
|
|
|
$prepend ? "Prepending {$repoJson['type']} repository" : "Adding {$repoJson['type']} repository" |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
$repository = $repoManager->createRepository($repoJson['type'], $repoJson); |
68
|
|
|
|
69
|
|
|
$prepend |
70
|
|
|
? $repoManager->prependRepository($repository) |
71
|
|
|
: $repoManager->addRepository($repository); |
72
|
|
|
|
73
|
|
|
$repositories[] = $repository; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: