Passed
Push — master ( 4e42f9...eb61a8 )
by Jonathan
03:42
created

ComposerService::extractPsr4Paths()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 18
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 2
1
<?php
2
3
/**
4
 * webtrees-mod-translationtool: MyArtJaub Translation Tool Module for webtrees
5
 *
6
 * @package MyArtJaub\Webtrees\Module
7
 * @subpackage TranslationTool
8
 * @author Jonathan Jaubart <[email protected]>
9
 * @copyright Copyright (c) 2020, Jonathan Jaubart
10
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3
11
 */
12
13
declare(strict_types=1);
14
15
namespace MyArtJaub\Webtrees\Module\TranslationTool\Services;
16
17
use Composer\Composer;
18
use Composer\Factory;
19
use Composer\IO\NullIO;
20
use Composer\Package\PackageInterface;
21
use Fisharebest\Webtrees\Webtrees;
22
23
/**
24
 * Service for accessing Composer data for the root package
25
 *
26
 */
27
class ComposerService
28
{
29
    
30
    /**
31
     * Checks whether a Composer package is a MyArtJaub one.
32
     *
33
     * @param PackageInterface $package
34
     * @return bool
35
     */
36
    public function isMyArtJaubPackage(PackageInterface $package): bool
37
    {
38
        list($vendor) = explode('/', $package->getName(), 2);
39
        return $vendor == self::MYARTJAUB_VENDOR;
40
    }
41
    
42
    /**
43
     * Name of the MyArtJaub modules' vendor
44
     * @var string MYARTJAUB_VENDOR
45
     * */
46
    private const MYARTJAUB_VENDOR = 'jon48';
47
    
48
    /**
49
     * List all the PSR-4 paths used in MyArtJaub packages autoloading.
50
     * The returned array is composed of items with the structure:
51
     *      - array [
52
     *              0 => Package Name
53
     *              1 => Array of normalised paths
54
     *          ]
55
     *
56
     * @return array
57
     */
58
    public function listMyArtJaubPackagesPaths(): array
59
    {
60
        $composer = Factory::create(new NullIO(), Webtrees::ROOT_DIR . 'composer.json');
61
        
62
        $packages = $composer->getRepositoryManager()
63
            ->getLocalRepository()
64
            ->getPackages();
65
            
66
        /** @var array<PackageInterface> $maj_packages */
67
        $maj_packages = array();
68
        foreach ($packages as $package) {
69
            if ($this->isMyArtJaubPackage($package)) {
70
                $maj_packages[] = $this->extractPsr4Paths($composer, $package);
71
            }
72
        }
73
        
74
        return $maj_packages;
75
    }
76
    
77
    /**
78
     * Extract and normalise the PSR-4 paths used in a package autoloading.
79
     * The returned array is a 2-tuple with the structure:
80
     *      - array [
81
     *              0 => Package Name
82
     *              1 => Array of normalised paths
83
     *          ]
84
     *
85
     * @param Composer $composer
86
     * @param PackageInterface $package
87
     * @return array
88
     */
89
    private function extractPsr4Paths(Composer $composer, PackageInterface $package): array
90
    {
91
        $autoload_generator = $composer->getAutoloadGenerator();
92
        
93
        $package_map = $autoload_generator->buildPackageMap(
94
            $composer->getInstallationManager(),
95
            $composer->getPackage(),
96
            [$package]
97
        );
98
        array_shift($package_map);
99
        $autoloads = $autoload_generator->parseAutoloads($package_map, $composer->getPackage());
100
        $psr4_paths = [];
101
        foreach ($autoloads['psr-4'] as $psr4_ns_paths) {
102
            foreach ($psr4_ns_paths as $psr4_ns_path) {
103
                $psr4_paths[] = realpath($psr4_ns_path);
104
            }
105
        }
106
        return [$package, $psr4_paths];
107
    }
108
}
109