File size: 6,538 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php

namespace Mautic\CoreBundle\Helper;

use Mautic\CoreBundle\Loader\ParameterLoader;

class PathsHelper
{
    /**
     * @var array<string, string>
     */
    private array $paths;

    /**
     * @var string
     */
    private $theme;

    private string $imagePath;

    private string $dashboardImportDir;

    private string $dashboardUserImportDir;

    private string $kernelCacheDir;

    private string $kernelLogsDir;

    private string $kernelRootDir;

    private string $temporaryDir;

    private ?\Mautic\UserBundle\Entity\User $user;

    private string $importLeadsDir;

    public function __construct(UserHelper $userHelper, CoreParametersHelper $coreParametersHelper, string $cacheDir, string $logsDir, string $rootDir)
    {
        $root                         = $rootDir.'/app'; // Do not rename the variable, used in paths_helper.php
        $projectRoot                  = $this->getVendorRootPath();
        $this->user                   = $userHelper->getUser();
        $this->theme                  = $coreParametersHelper->get('theme');
        $this->imagePath              = $this->removeTrailingSlash((string) $coreParametersHelper->get('image_path'));
        $this->dashboardImportDir     = $this->removeTrailingSlash((string) $coreParametersHelper->get('dashboard_import_dir'));
        $this->importLeadsDir         = $this->removeTrailingSlash((string) $coreParametersHelper->get('import_leads_dir'));
        $this->temporaryDir           = $this->removeTrailingSlash((string) $coreParametersHelper->get('tmp_path'));
        $this->dashboardUserImportDir = $this->removeTrailingSlash((string) $coreParametersHelper->get('dashboard_import_user_dir'));
        $this->kernelCacheDir         = $this->removeTrailingSlash($cacheDir);
        $this->kernelLogsDir          = $this->removeTrailingSlash($logsDir);
        $this->kernelRootDir          = $this->removeTrailingSlash($root);

        $paths = [];
        include $root.'/config/paths_helper.php';

        $this->paths = $paths;
    }

    public function getLocalConfigurationFile(): string
    {
        return ParameterLoader::getLocalConfigFile($this->kernelRootDir);
    }

    public function getCachePath(): string
    {
        return $this->getSystemPath('cache', true);
    }

    public function getRootPath(): string
    {
        return $this->getSystemPath('root', true);
    }

    public function getTemporaryPath(): string
    {
        return $this->getSystemPath('tmp', true);
    }

    public function getLogsPath(): string
    {
        return $this->getSystemPath('logs', true);
    }

    public function getImagePath(): string
    {
        return $this->getSystemPath('images', true);
    }

    public function getTranslationsPath(): string
    {
        return $this->getSystemPath('translations', true);
    }

    public function getThemesPath(): string
    {
        return $this->getSystemPath('themes', true);
    }

    public function getAssetsPath(): string
    {
        return $this->getSystemPath('assets', true);
    }

    public function getMediaPath(): string
    {
        return $this->getSystemPath('media', true);
    }

    public function getCoreBundlesPath(): string
    {
        return $this->getSystemPath('bundles', true);
    }

    public function getPluginsPath(): string
    {
        return $this->getSystemPath('plugins', true);
    }

    public function getImportLeadsPath(): string
    {
        return $this->getSystemPath('import.leads.dir', true);
    }

    /**
     * Returns absolute path to the root directory where the "vendor" directory is located.
     */
    public function getVendorRootPath(): string
    {
        $reflection = new \ReflectionClass(\Composer\Autoload\ClassLoader::class);

        return dirname($reflection->getFileName(), 3);
    }

    /**
     * Get the path to specified area.  Returns relative by default with the exception of cache and log
     * which will be absolute regardless of $fullPath setting.
     *
     * @param string $name
     * @param bool   $fullPath
     *
     * @return string
     *
     * @throws \InvalidArgumentException
     */
    public function getSystemPath($name, $fullPath = false)
    {
        switch ($name) {
            case 'currentTheme':
            case 'current_theme':
                $path = $this->paths['themes'].'/'.$this->theme;
                break;

            case 'cache':
                return $this->kernelCacheDir;
            case 'logs':
                return $this->kernelLogsDir;
            case 'temporary':
            case 'tmp':
                if (!file_exists($this->temporaryDir)) {
                    mkdir($this->temporaryDir, 0777, true);
                }

                return $this->temporaryDir;
            case 'images':
                $path = $this->imagePath;
                break;

            case 'dashboard.user':
            case 'dashboard.global':
                // these are absolute regardless as they are configurable
                $globalPath = $this->dashboardImportDir;

                if ('dashboard.global' == $name) {
                    return $globalPath;
                }

                if (!$userPath = $this->dashboardUserImportDir) {
                    $userPath = $globalPath;
                }

                $userPath .= '/'.$this->user->getId();

                if (!file_exists($userPath)) {
                    mkdir($userPath, 0777, true);
                }

                return $userPath;

            case 'import.leads.dir':
                return $this->importLeadsDir;

            default:
                if (isset($this->paths[$name])) {
                    $path = $this->paths[$name];
                } elseif (str_contains($name, '_root')) {
                    // Assume system root if one is not set specifically
                    $path = $this->paths['root'];
                } else {
                    throw new \InvalidArgumentException("$name does not exist.");
                }
        }

        if (!$fullPath) {
            return $path;
        }

        $rootPath = (!empty($this->paths[$name.'_root'])) ? $this->paths[$name.'_root'] : $this->paths['root'];
        if (!str_contains($path, $rootPath)) {
            return $rootPath.'/'.$path;
        }

        return $path;
    }

    private function removeTrailingSlash(string $dir): string
    {
        if (str_ends_with($dir, '/')) {
            $dir = substr($dir, 0, -1);
        }

        return $dir;
    }
}