ComposerParser::psr4Namespaces()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file is part of web-stack
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Slick\WebStack\Infrastructure;
13
14
use JsonException;
15
use RuntimeException;
16
use Slick\WebStack\Infrastructure\Exception\InvalidComposerFile;
17
18
/**
19
 * ComposerParser
20
 *
21
 * @package Slick\WebStack\Infrastructure
22
 */
23
final class ComposerParser
24
{
25
    /**
26
     * @var array<string, mixed>
27
     */
28
    private array $data;
29
30
    /**
31
     * @throws JsonException
32
     */
33
    public function __construct(string $composerFile)
34
    {
35
        if (!is_file($composerFile) || !$composerContents = file_get_contents($composerFile)) {
36
            throw new InvalidComposerFile("Composer file '$composerFile' does not exist or is not readable.");
37
        }
38
        $this->data = json_decode(json: $composerContents, associative: true, flags: JSON_THROW_ON_ERROR);
39
    }
40
41
    /**
42
     * Returns the inflated name of the application.
43
     *
44
     * @return string The inflated name of the application.
45
     */
46
    public function appName(): string
47
    {
48
        return $this->inflateName($this->data["name"]);
49
    }
50
51
    /**
52
     * Returns the version.
53
     *
54
     * @return string The version.
55
     */
56
    public function version(): string
57
    {
58
        return (string) $this->data["version"];
59
    }
60
61
    /**
62
     * Inflates the name.
63
     *
64
     * @param string $name The name to inflate.
65
     *
66
     * @return string The inflated name.
67
     */
68
    private function inflateName(string $name): string
69
    {
70
        $parts = explode('/', str_replace(['-', '_', '.'], ' ', $name));
71
        $owner = ucwords(trim($parts[0]));
72
        $app = ucfirst(trim($parts[1]));
73
        return "$owner's $app";
74
    }
75
76
    /**
77
     * Returns the description of the application.
78
     *
79
     * @return string The description of the application.
80
     */
81
    public function description(): string
82
    {
83
        return (string) $this->data["description"];
84
    }
85
86
    /**
87
     * Returns the autoload paths for the given key.
88
     *
89
     * @param string $key The autoload path key. Default is "psr-4".
90
     * @return array<string, string> The autoload paths for the given key.
91
     */
92
    public function autoload(string $key = "psr-4"): array
93
    {
94
        return (array) $this->data["autoload"][$key];
95
    }
96
97
    /**
98
     * Returns the PSR-4 namespaces declared in the autoload configuration.
99
     *
100
     * @return array<string> An array of PSR-4 namespaces.
101
     */
102
    public function psr4Namespaces(): array
103
    {
104
        return array_keys($this->autoload());
105
    }
106
}
107