Completed
Push — master ( fa51a8...2eea30 )
by Sebastien
07:47
created

Composer::getCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * Drush Cerbere command line tools.
5
 * Copyright (C) 2015 - Sebastien Malot <[email protected]>
6
 *
7
 * This program is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 2 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License along
18
 * with this program; if not, write to the Free Software Foundation, Inc.,
19
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
 */
21
22
namespace Cerbere\Parser;
23
24
use Cerbere\Model\Project;
25
use Composer\Json\JsonFile;
26
use Composer\Package\Locker;
27
use Composer\Repository\RepositoryManager;
28
use ComposerLockParser\Package;
29
30
/**
31
 * Class Composer
32
 * @package Cerbere\Parser
33
 */
34
class Composer extends Ini
35
{
36
    /**
37
     * @var Project[]
38
     */
39
    protected $projects;
40
41
    /**
42
     *
43
     */
44
    public function __construct()
45
    {
46
47
    }
48
49
    /**
50
     * @return string
51
     */
52
    public function getCode()
53
    {
54
        return 'composer';
55
    }
56
57
    /**
58
     * @return Project[]
59
     */
60
    public function getProjects()
61
    {
62
        return $this->projects;
63
    }
64
65
    /**
66
     * @param string $content
67
     * @param string $filename
68
     *
69
     * @throws \Exception
70
     */
71
    public function processContent($content, $filename = null)
72
    {
73
        throw new \Exception('Not supported');
74
    }
75
76
    /**
77
     * @param string $filename
78
     */
79
    public function processFile($filename)
80
    {
81
        $composerInfo = new \ComposerLockParser\ComposerInfo($filename);
82
        $composerInfo->parse();
83
84
        $this->projects = array();
85
        // Default value if not autodetected.
86
        $core = '8.x';
87
88
        /** @var Package $package */
89
        foreach ($composerInfo->getPackages() as $package) {
90
            if (strpos($package->getName(), 'drupal/') === 0 && $source = $package->getSource()) {
91
                $name = $this->getName($package);
92
                $version = $this->getVersion($package, $core);
93
                $core = substr($version, 0, 1) . '.x';
94
                $project = new Project($name, $core, $version, $package->getTime());
95
                $this->projects[] = $project;
96
            }
97
        }
98
    }
99
100
    /**
101
     * @parser string $filename
102
     *
103
     * @return bool
104
     */
105
    public function supportedFile($filename)
106
    {
107
        return preg_match('/\.lock/i', $filename) > 0;
108
    }
109
110
    /**
111
     * @param Package $package
112
     *
113
     * @return string
114
     */
115
    protected function getName(Package $package)
116
    {
117
        if ($package->getName() == 'drupal/core') {
118
            return 'drupal';
119
        } else {
120
            return preg_replace('/^drupal\//', '', $package->getName());
121
        }
122
    }
123
124
    /**
125
     * @param Package $package
126
     * @param string $core
127
     *
128
     * @return string
129
     */
130
    protected function getVersion(Package $package, $core)
131
    {
132
        $source = $package->getSource();
133
134
        if (preg_match('/^[0-9]+\.x\-/', $source['reference'])) {
135
            return $source['reference'];
136
        } elseif ($this->getName($package) == 'drupal') {
137
            return $package->getVersion();
138
        } else {
139
            $version = 'N/A';
140
141
            if (preg_match('/^([0-9]+\.[0-9]+)\.[0-9]+(.*)/', $package->getVersion(), $match)) {
142
                $version = $match[1] . $match[2];
143
144
                return $core . '-' . $version;
145
            }
146
147
            return $version;
148
        }
149
    }
150
}
151