File size: 4,810 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php

namespace Mautic\CoreBundle\DependencyInjection\Builder;

use Mautic\CoreBundle\DependencyInjection\Builder\Metadata\ConfigMetadata;
use Mautic\CoreBundle\DependencyInjection\Builder\Metadata\EntityMetadata;
use Mautic\CoreBundle\DependencyInjection\Builder\Metadata\PermissionClassMetadata;

class BundleMetadataBuilder
{
    private array $ipLookupServices = [];

    private array $ormConfig = [];

    private array $serializerConfig = [];

    private array $pluginMetadata = [];

    /**
     * @var array
     */
    private $coreMetadata = [];

    public function __construct(
        private array $symfonyBundles,
        private array $paths
    ) {
        $this->buildMetadata();
    }

    public function getCoreBundleMetadata(): array
    {
        return $this->coreMetadata;
    }

    public function getPluginMetadata(): array
    {
        return $this->pluginMetadata;
    }

    public function getIpLookupServices(): array
    {
        return $this->ipLookupServices;
    }

    public function getOrmConfig(): array
    {
        return $this->ormConfig;
    }

    public function getSerializerConfig(): array
    {
        return $this->serializerConfig;
    }

    private function buildMetadata(): void
    {
        foreach ($this->symfonyBundles as $symfonyBundle => $namespace) {
            // Plugin
            if (str_contains($namespace, 'MauticPlugin\\')) {
                $this->pluginMetadata[$symfonyBundle] = $this->buildPluginMetadata($namespace, $symfonyBundle);

                continue;
            }

            // Core bundle
            if (str_contains($namespace, 'Mautic\\')) {
                $this->coreMetadata[$symfonyBundle] = $this->buildCoreMetadata($namespace, $symfonyBundle);

                continue;
            }

            // Otherwise not a Mautic bundle so ignore
        }

        // Make CoreBundle the first in the core bundle list
        if (!isset($this->coreMetadata['MauticCoreBundle'])) {
            // Not always set for tests
            return;
        }

        $coreBundle = $this->coreMetadata['MauticCoreBundle'];
        unset($this->coreMetadata['MauticCoreBundle']);
        $this->coreMetadata = array_merge(['MauticCoreBundle' => $coreBundle], $this->coreMetadata);
    }

    private function buildPluginMetadata(string $namespace, string $symfonyBundle): array
    {
        $relativePath  = $this->paths['plugins'].'/'.$symfonyBundle;
        $metadataArray = $this->getMetadata(true, $namespace, $symfonyBundle, $symfonyBundle, $relativePath);

        $metadata = new BundleMetadata($metadataArray);
        $this->completMetadata($metadata);

        return $metadata->toArray();
    }

    private function buildCoreMetadata(string $namespace, string $symfonyBundle): array
    {
        $bundleName    = str_replace('Mautic', '', $symfonyBundle);
        $relativePath  = $this->paths['bundles'].'/'.$bundleName;
        $metadataArray = $this->getMetadata(false, $namespace, $symfonyBundle, $bundleName, $relativePath);

        $metadata = new BundleMetadata($metadataArray);
        $this->completMetadata($metadata);

        return $metadata->toArray();
    }

    private function getMetadata(bool $isPlugin, string $namespace, string $symfonyBundle, string $bundleName, string $relativePath): array
    {
        return [
            'isPlugin'          => $isPlugin,
            'base'              => str_replace('Bundle', '', $bundleName),
            'bundle'            => $bundleName,
            'relative'          => $relativePath,
            'directory'         => realpath($this->paths['root'].'/'.$relativePath),
            'namespace'         => preg_replace('#\\\[^\\\]*$#', '', $namespace),
            'symfonyBundleName' => $symfonyBundle,
            'bundleClass'       => $namespace,
        ];
    }

    private function completMetadata(BundleMetadata $metadata): void
    {
        $configParser = new ConfigMetadata($metadata);
        $configParser->build();

        if ($foundIpLookupServices = $configParser->getIpLookupServices()) {
            $this->ipLookupServices = array_merge($foundIpLookupServices, $this->ipLookupServices);
        }

        (new PermissionClassMetadata($metadata))->build();

        $this->buildMappings($metadata);
    }

    private function buildMappings(BundleMetadata $metadata): void
    {
        $mappingParser = new EntityMetadata($metadata);
        $mappingParser->build();

        $bundleName = $metadata->getBundleName();

        if ($ormMappings = $mappingParser->getOrmConfig()) {
            $this->ormConfig[$bundleName] = $ormMappings;
        }

        if ($serializerConfig = $mappingParser->getSerializerConfig()) {
            $this->serializerConfig[$bundleName] = $serializerConfig;
        }
    }
}