Failed Conditions
Pull Request — experimental/3.1 (#2619)
by
unknown
40:55
created

ParseOutputCommand::parse()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 18
nc 5
nop 0
dl 0
loc 26
rs 8.439
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.lockon.co.jp/
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
 */
23
namespace Eccube\Service\Composer;
24
25
/**
26
 * Class ParseOutputCommand
27
 * @package Eccube\Service\Composer
28
 */
29
class ParseOutputCommand
30
{
31
    const REQUIRE_TYPE = 1;
32
    const INFO_TYPE = 2;
33
    const CONFIG_TYPE = 3;
34
    const LIST_TYPE = 4;
35
36
    private $output;
37
38
    private $type;
39
40
    /**
41
     * ParseOutputCommand constructor.
42
     * @param string $output
43
     * @param int    $type
44
     */
45
    public function __construct($output, $type)
46
    {
47
        $this->output = $output;
48
        $this->type = $type;
49
    }
50
51
    /**
52
     * Parse function
53
     *
54
     * @return array
55
     */
56
    public function parse()
57
    {
58
        switch ($this->type) {
59
            case self::REQUIRE_TYPE:
60
                $parseOutput = $this->parseRequire();
61
                break;
62
63
            case self::INFO_TYPE:
64
                $parseOutput = $this->parseInfo();
65
                break;
66
67
            case self::CONFIG_TYPE:
68
                $parseOutput = $this->parseConfig();
69
                break;
70
71
            case self::LIST_TYPE:
72
                $parseOutput = $this->parseList();
73
                break;
74
75
            default:
76
                $parseOutput = explode(PHP_EOL, str_replace('\r\n', PHP_EOL, $this->output));
77
                break;
78
        }
79
80
        return $parseOutput;
81
    }
82
83
    /**
84
     * Parse to array
85
     *
86
     * @return array
87
     */
88
    private function parseRequire()
89
    {
90
        $rowArray = explode(PHP_EOL, str_replace('\r\n', PHP_EOL, $this->output));
91
        $installedLogs = array_filter(
92
            array_map(
93
                function ($line) {
94
                    $matches = array();
95
                    preg_match('/^  - Installing (.*?) \((.*?)\) .*/', $line, $matches);
96
97
                    return $matches;
98
                },
99
                $rowArray
100
            )
101
        );
102
103
        // 'package name' => 'version'
104
        return array('installed' => array_column($installedLogs, 2, 1));
105
    }
106
107
    /**
108
     * Parse to array
109
     *
110
     * @return array
111
     */
112
    private function parseInfo()
113
    {
114
        $rowArray = explode(PHP_EOL, str_replace('\r\n', PHP_EOL, $this->output));
115
        $infoLogs = array_filter(array_map(function ($line) {
116
            $matches = array();
117
            preg_match('/^(name|descrip.|keywords|versions|type|license|source|dist|names)\s*:\s*(.*)$/', $line, $matches);
118
119
            return $matches;
120
        }, $rowArray));
121
122
        // 'name' => 'value'
123
        $result = array_column($infoLogs, 2, 1);
124
        $result['requires'] = $this->parseArrayInfoOutput($rowArray, 'requires');
125
        $result['requires (dev)'] = $this->parseArrayInfoOutput($rowArray, 'requires (dev)');
126
127
        return $result;
128
    }
129
130
    /**
131
     * Parse to array
132
     *
133
     * @return array|mixed
134
     */
135
    private function parseConfig()
136
    {
137
        $rowArray = explode(PHP_EOL, str_replace('\r\n', PHP_EOL, $this->output));
138
        $rowArray = array_filter($rowArray, function ($line) {
139
            return !preg_match('/^<warning>.*/', $line);
140
        });
141
142
        return $rowArray ? json_decode(array_shift($rowArray), true) : array();
143
    }
144
145
    /**
146
     * Parse to array
147
     *
148
     * @return array
149
     */
150
    private function parseList()
151
    {
152
        $rowArray = explode(PHP_EOL, str_replace('\r\n', PHP_EOL, $this->output));
153
        $rawConfig = array_map(function ($line) {
154
            $matches = array();
155
            preg_match('/^\[(.*?)\]\s?(.*)$/', $line, $matches);
156
157
            return $matches;
158
        }, $rowArray);
159
160
        $rawConfig = array_column($rawConfig, 2, 1);
161
162
        $result = array();
163
164
        foreach ($rawConfig as $path => $value) {
165
            $arr = &$result;
166
            $keys = explode('.', $path);
167
            foreach ($keys as $key) {
168
                $arr = &$arr[$key];
169
            }
170
            $arr = $value;
171
        }
172
173
        return $result;
174
    }
175
176
    /**
177
     * @param $rowArray
178
     * @param $key
179
     * @return array
180
     */
181
    private function parseArrayInfoOutput($rowArray, $key)
182
    {
183
        $result = array();
184
        $start = false;
185
        foreach ($rowArray as $line) {
186
            if ($line === $key) {
187
                $start = true;
188
                continue;
189
            }
190
            if ($start) {
191
                if (empty($line)) {
192
                    break;
193
                }
194
                $parts = explode(' ', $line);
195
                $result[$parts[0]] = $parts[1];
196
            }
197
        }
198
199
        return $result;
200
    }
201
}
202