Completed
Push — master ( c6f12e...8beaec )
by Daniel
01:57
created

ComposerPackagesListing::mergePackageAttributes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 8
nc 2
nop 2
1
<?php
2
3
/**
4
 *
5
 * The MIT License (MIT)
6
 *
7
 * Copyright (c) 2017 Daniel Popiniuc
8
 *
9
 * Permission is hereby granted, free of charge, to any person obtaining a copy
10
 * of this software and associated documentation files (the "Software"), to deal
11
 * in the Software without restriction, including without limitation the rights
12
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
 * copies of the Software, and to permit persons to whom the Software is
14
 * furnished to do so, subject to the following conditions:
15
 *
16
 * The above copyright notice and this permission notice shall be included in all
17
 * copies or substantial portions of the Software.
18
 *
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
 * SOFTWARE.
26
 *
27
 */
28
29
namespace danielgp\composer_packages_listing;
30
31
/**
32
 * usefull functions to get quick results
33
 *
34
 * @author Daniel Popiniuc
35
 */
36
trait ComposerPackagesListing
37
{
38
39
    use Basics;
40
41
    /**
42
     * Decision between Main or Development packages
43
     *
44
     * @param array $inParametersArray
45
     * @return string
46
     */
47
    private function decisionPackageOrPackageDev($inParametersArray) {
48
        $sReturn = 'packages';
49
        if (array_key_exists('Dev', $inParametersArray)) {
50
            $sReturn = 'packages-dev';
51
        }
52
        return $sReturn;
53
    }
54
55
    /**
56
     * Exposes few Environment details
57
     *
58
     * @return array
59
     */
60
    protected function exposeEnvironmentDetails() {
61
        $knownValues = [
62
            'AMD64' => 'x64 (64 bit)',
63
            'i386'  => 'x86 (32 bit)',
64
            'i586'  => 'x86 (32 bit)',
65
        ];
66
        return [
67
            'Host Name'                     => php_uname('n'),
68
            'Machine Type'                  => php_uname('m'),
69
            'Operating System Architecture' => $knownValues[php_uname('m')],
70
            'Operating System Name'         => php_uname('s'),
71
            'Operating System Version'      => php_uname('r').' '.php_uname('v'),
72
        ];
73
    }
74
75
    /**
76
     *
77
     * @return array
78
     */
79
    protected function exposePhpDetails($skipAging = false) {
80
        $aReturn = [
81
            'Aging'           => $this->getPkgAging($this->getFileModifiedTimestampOfFile(PHP_BINARY, 'Y-m-d')),
82
            'Architecture'    => (PHP_INT_SIZE === 4 ? 'x86 (32 bit)' : 'x64 (64 bit)'),
83
            'Description'     => 'PHP is a popular general-purpose scripting language'
84
            .' that is especially suited to web development',
85
            'Homepage'        => 'https://secure.php.net/',
86
            'License'         => 'PHP License v3.01',
87
            'Package Name'    => 'ZendEngine/PHP',
88
            'Product'         => 'PHP',
89
            'Time'            => $this->getFileModifiedTimestampOfFile(PHP_BINARY, 'l, d F Y H:i:s'),
90
            'Time as PHP no.' => $this->getFileModifiedTimestampOfFile(PHP_BINARY, 'PHPtime'),
91
            'Type'            => 'scripting language',
92
            'Url'             => 'https://github.com/php/php-src',
93
            'Vendor'          => 'The PHP Group',
94
            'Version'         => PHP_VERSION,
95
            'Version no.'     => PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION.'.'.PHP_RELEASE_VERSION,
96
        ];
97
        if ($skipAging) {
98
            unset($aReturn['Aging']);
99
        }
100
        return $aReturn;
101
    }
102
103
    /**
104
     * Returns Modified date and time of a given file
105
     *
106
     * @param string $fileName
107
     * @param string $format
108
     * @param boolean $resultInUtc
109
     * @return string
110
     */
111
    protected function getFileModifiedTimestampOfFile($fileName, $format = 'Y-m-d H:i:s', $resultInUtc = false) {
112
        if (!file_exists($fileName)) {
113
            return ['error' => $fileName.' was not found'];
114
        }
115
        $info = new \SplFileInfo($fileName);
116
        if ($format === 'PHPtime') {
117
            return $info->getMTime();
118
        }
119
        $sReturn = date($format, $info->getMTime());
120
        if ($resultInUtc) {
121
            $sReturn = gmdate($format, $info->getMTime());
122
        }
123
        return $sReturn;
124
    }
125
126
    /**
127
     * Returns a complete list of packages and respective details from a composer.lock file
128
     * (kept for compatibility reason)
129
     *
130
     * @param string $fileIn
131
     * @param boolean $devInstead true for Development, false for Production
132
     * @param boolean $skipAging true for skipping, false for not
133
     * @return array
134
     */
135
    protected function getPackageDetailsFromGivenComposerLockFile($fileIn, $devInstead = false, $skipAging = false) {
136
        $inParametersArray = [];
137
        if ($devInstead) {
138
            $inParametersArray['Dev'] = true;
139
        }
140
        if ($skipAging) {
141
            $inParametersArray['Skip Aging'] = true;
142
        }
143
        return $this->getPackageDetailsFromGivenComposerLockFileEnhanced($fileIn, $inParametersArray);
144
    }
145
146
    /**
147
     * Returns a complete list of packages and respective details from a composer.lock file
148
     *
149
     * @param string $fileIn
150
     * @param array $inParametersArray
151
     * @return array
152
     */
153
    protected function getPackageDetailsFromGivenComposerLockFileEnhanced($fileIn, $inParametersArray = []) {
154
        if (!file_exists($fileIn)) {
155
            return ['error' => $fileIn.' was not found'];
156
        }
157
        $alnfo    = [];
158
        $packages = $this->getPkgFileInListOfPackageArrayOut($fileIn);
159
        $pkgType  = $this->decisionPackageOrPackageDev($inParametersArray);
160
        foreach ($packages[$pkgType] as $key => $value) {
161
            $keyToUse = $value['name'];
162
            if (array_key_exists('Not Grouped By Name', $inParametersArray)) {
163
                $keyToUse = $key;
164
            }
165
            $alnfo[$keyToUse] = $this->mergePackageAttributes($value, $inParametersArray);
166
            ksort($alnfo[$keyToUse]);
167
        }
168
        ksort($alnfo);
169
        return $alnfo;
170
    }
171
172
    private function getPkgFileInListOfPackageArrayOut($fileToRead) {
173
        $handle       = fopen($fileToRead, 'r');
174
        $fileContents = fread($handle, filesize($fileToRead));
175
        fclose($handle);
176
        return json_decode($fileContents, true);
177
    }
178
179
    private function mergePackageAttributes($value, $inParametersArray) {
180
        $atr   = $this->getPkgOptAtributeAll($value, '---');
181
        $basic = $this->getPkgBasicInfo($value, '---');
182
        $vrs   = $this->getPkgVersion($value, '---');
183
        $tmng  = $this->getPkgTiming($value, '---');
184
        if (array_key_exists('Skip Aging', $inParametersArray)) {
185
            unset($tmng['Aging']);
186
        }
187
        return array_merge($atr, $basic, $vrs, $tmng);
188
    }
189
190
}
191