1<?php
2
3/*
4 * This file is part of Composer.
5 *
6 * (c) Nils Adermann <naderman@naderman.de>
7 *     Jordi Boggiano <j.boggiano@seld.be>
8 *
9 * For the full copyright and license information, please view the LICENSE
10 * file that was distributed with this source code.
11 */
12
13namespace Composer\Repository;
14
15use Composer\Package\PackageInterface;
16
17/**
18 * Composite repository.
19 *
20 * @author Beau Simensen <beau@dflydev.com>
21 */
22class CompositeRepository extends BaseRepository
23{
24    /**
25     * List of repositories
26     * @var array
27     */
28    private $repositories;
29
30    /**
31     * Constructor
32     * @param array $repositories
33     */
34    public function __construct(array $repositories)
35    {
36        $this->repositories = array();
37        foreach ($repositories as $repo) {
38            $this->addRepository($repo);
39        }
40    }
41
42    /**
43     * Returns all the wrapped repositories
44     *
45     * @return array
46     */
47    public function getRepositories()
48    {
49        return $this->repositories;
50    }
51
52    /**
53     * {@inheritdoc}
54     */
55    public function hasPackage(PackageInterface $package)
56    {
57        foreach ($this->repositories as $repository) {
58            /* @var $repository RepositoryInterface */
59            if ($repository->hasPackage($package)) {
60                return true;
61            }
62        }
63
64        return false;
65    }
66
67    /**
68     * {@inheritdoc}
69     */
70    public function findPackage($name, $constraint)
71    {
72        foreach ($this->repositories as $repository) {
73            /* @var $repository RepositoryInterface */
74            $package = $repository->findPackage($name, $constraint);
75            if (null !== $package) {
76                return $package;
77            }
78        }
79
80        return null;
81    }
82
83    /**
84     * {@inheritdoc}
85     */
86    public function findPackages($name, $constraint = null)
87    {
88        $packages = array();
89        foreach ($this->repositories as $repository) {
90            /* @var $repository RepositoryInterface */
91            $packages[] = $repository->findPackages($name, $constraint);
92        }
93
94        return $packages ? call_user_func_array('array_merge', $packages) : array();
95    }
96
97    /**
98     * {@inheritdoc}
99     */
100    public function search($query, $mode = 0, $type = null)
101    {
102        $matches = array();
103        foreach ($this->repositories as $repository) {
104            /* @var $repository RepositoryInterface */
105            $matches[] = $repository->search($query, $mode, $type);
106        }
107
108        return $matches ? call_user_func_array('array_merge', $matches) : array();
109    }
110
111    /**
112     * {@inheritdoc}
113     */
114    public function getPackages()
115    {
116        $packages = array();
117        foreach ($this->repositories as $repository) {
118            /* @var $repository RepositoryInterface */
119            $packages[] = $repository->getPackages();
120        }
121
122        return $packages ? call_user_func_array('array_merge', $packages) : array();
123    }
124
125    /**
126     * {@inheritdoc}
127     */
128    public function removePackage(PackageInterface $package)
129    {
130        foreach ($this->repositories as $repository) {
131            /* @var $repository RepositoryInterface */
132            $repository->removePackage($package);
133        }
134    }
135
136    /**
137     * {@inheritdoc}
138     */
139    public function count()
140    {
141        $total = 0;
142        foreach ($this->repositories as $repository) {
143            /* @var $repository RepositoryInterface */
144            $total += $repository->count();
145        }
146
147        return $total;
148    }
149
150    /**
151     * Add a repository.
152     * @param RepositoryInterface $repository
153     */
154    public function addRepository(RepositoryInterface $repository)
155    {
156        if ($repository instanceof self) {
157            foreach ($repository->getRepositories() as $repo) {
158                $this->addRepository($repo);
159            }
160        } else {
161            $this->repositories[] = $repository;
162        }
163    }
164}
165