Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 113
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
translate_composer_trait
0.00% covered (danger)
0.00%
0 / 113
0.00% covered (danger)
0.00%
0 / 4
1332
0.00% covered (danger)
0.00%
0 / 1
 write
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 1
110
 writeError
0.00% covered (danger)
0.00%
0 / 68
0.00% covered (danger)
0.00%
0 / 1
552
 get_composer_error
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 strip_format
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 *
4 * This file is part of the phpBB Forum Software package.
5 *
6 * @copyright (c) phpBB Limited <https://www.phpbb.com>
7 * @license GNU General Public License, version 2 (GPL-2.0)
8 *
9 * For full copyright and license information, please see
10 * the docs/CREDITS.txt file.
11 *
12 */
13
14namespace phpbb\composer\io;
15
16use Symfony\Component\Console\Output\OutputInterface;
17
18/**
19 * Trait to translate the composer Output
20 */
21trait translate_composer_trait
22{
23    /**
24     * @var \phpbb\language\language
25     */
26    protected $language;
27
28    /**
29     * @var array
30     */
31    protected $composer_error = [];
32
33    /**
34     * {@inheritdoc}
35     */
36    public function write($messages, $newline = true, $verbosity = self::NORMAL)
37    {
38        $messages = (array) $messages;
39        $translated_messages = [];
40
41        foreach ($messages as $message)
42        {
43            $level = 0;
44            if (is_array($message))
45            {
46                $lang_key = $message[0];
47                $parameters = $message[1];
48                if (count($message) > 2)
49                {
50                    $level = $message[2];
51                }
52            }
53            else
54            {
55                $lang_key = $message;
56                $parameters = [];
57            }
58
59            $message = trim($this->strip_format($lang_key), "\n\r");
60
61            if ($this->output->getVerbosity() === OutputInterface::VERBOSITY_DEBUG)
62            {
63                // Do nothing
64            }
65            else if (strpos($message, 'Deleting ') === 0)
66            {
67                $elements = explode(' ', $message);
68                $lang_key = 'COMPOSER_DELETING';
69                $parameters = [$elements[1]];
70            }
71
72            $translated_message = $this->language->lang_array($lang_key, $parameters);
73
74            switch ($level)
75            {
76                case 1:
77                    $translated_message = '<info>' . $translated_message . '</info>';
78                    break;
79                case 2:
80                    $translated_message = '<comment>' . $translated_message . '</comment>';
81                    break;
82                case 3:
83                    $translated_message = '<warning>' . $translated_message . '</warning>';
84                    break;
85                case 4:
86                    $translated_message = '<error>' . $translated_message . '</error>';
87                    break;
88            }
89
90            $translated_messages[] = $translated_message;
91        }
92
93        parent::write($translated_messages, $newline);
94    }
95
96    /**
97     * {@inheritdoc}
98     */
99    public function writeError($messages, $newline = true, $verbosity = self::NORMAL)
100    {
101        $messages = (array) $messages;
102        $translated_messages = [];
103
104        foreach ($messages as $message)
105        {
106            $level = 0;
107            if (is_array($message))
108            {
109                $lang_key = $message[0];
110                $parameters = $message[1];
111                if (count($message) > 2)
112                {
113                    $level = $message[2];
114                }
115            }
116            else
117            {
118                $lang_key = $message;
119                $parameters = [];
120            }
121
122            $message = trim($this->strip_format($lang_key), "\n\r");
123
124            if ($message === 'Your requirements could not be resolved to an installable set of packages.')
125            {
126                $this->composer_error[] = ['COMPOSER_ERROR_CONFLICT', []];
127
128                if ($this->output->getVerbosity() < OutputInterface::VERBOSITY_DEBUG)
129                {
130                    continue;
131                }
132            }
133            else if (strpos($message, '  Problem ') === 0)
134            {
135                if ($this->output->getVerbosity() < OutputInterface::VERBOSITY_VERBOSE)
136                {
137                    continue;
138                }
139
140                $lang_key = "\n" . htmlentities($message) . "\n";
141                $level = 4;
142            }
143            else if ($message === 'Updating dependencies')
144            {
145                $lang_key = 'COMPOSER_UPDATING_DEPENDENCIES';
146                $level = 1;
147            }
148            else if ($message === 'Loading composer repositories with package information')
149            {
150                $lang_key = 'COMPOSER_LOADING_REPOSITORIES';
151                $level = 1;
152            }
153            else if (strpos($message, 'could not be fully loaded, package information was loaded from the local cache and may be out of date') !== false)
154            {
155                $end_repo = strpos($message, 'could not be fully loaded, package information was loaded from the local cache and may be out of date');
156                $repo = substr($message, 0, $end_repo - 1);
157
158                $lang_key = 'COMPOSER_REPOSITORY_UNAVAILABLE';
159                $parameters = [$repo];
160                $level = 3;
161            }
162            else if (strpos($message, 'file could not be downloaded') !== false)
163            {
164                continue;
165            }
166            else if (strpos($message, '  - Installing ') === 0)
167            {
168                $elements = explode(' ', $message);
169                $lang_key = 'COMPOSER_INSTALLING_PACKAGE';
170                $parameters = [$elements[4], trim($elements[5], '()')];
171            }
172            else if ($message === 'Nothing to install or update')
173            {
174                $lang_key = 'COMPOSER_UPDATE_NOTHING';
175                $level = 3;
176            }
177            else if ($message === '    Downloading')
178            {
179                continue;
180            }
181            else if ($message === '    Loading from cache')
182            {
183                continue;
184            }
185            else if ($message === 'Writing lock file')
186            {
187                continue;
188            }
189            else if ($message === '    Extracting archive')
190            {
191                continue;
192            }
193            else if (empty($message))
194            {
195                continue;
196            }
197
198            $translated_message = $this->language->lang_array($lang_key, $parameters);
199
200            switch ($level)
201            {
202                case 1:
203                    $translated_message = '<info>' . $translated_message . '</info>';
204                    break;
205                case 2:
206                    $translated_message = '<comment>' . $translated_message . '</comment>';
207                    break;
208                case 3:
209                    $translated_message = '<warning>' . $translated_message . '</warning>';
210                    break;
211                case 4:
212                    $translated_message = '<error>' . $translated_message . '</error>';
213                    break;
214            }
215
216            $translated_messages[] = $translated_message;
217        }
218
219        parent::writeError($translated_messages, $newline);
220    }
221
222    public function get_composer_error()
223    {
224        $error = '';
225        foreach ($this->composer_error as $error_line)
226        {
227            $error .= $this->language->lang_array($error_line[0], $error_line[1]);
228            $error .= "\n";
229        }
230
231        $this->composer_error = [];
232
233        return $error;
234    }
235
236    protected function strip_format($message)
237    {
238        return str_replace([
239            '<info>', '</info>',
240            '<warning>', '</warning>',
241            '<comment>', '</comment>',
242            '<error>', '</error>',
243        ], '', $message);
244    }
245}