Issues (3641)

Router/src/Spryker/Zed/Router/RouterConfig.php (1 issue)

1
<?php
2
3
/**
4
 * Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace Spryker\Zed\Router;
9
10
use Spryker\Shared\Kernel\KernelConstants;
11
use Spryker\Shared\Router\RouterConstants;
12
use Spryker\Zed\Kernel\AbstractBundleConfig;
13
use Spryker\Zed\Router\Business\Generator\UrlGenerator;
14
use Spryker\Zed\Router\Business\UrlMatcher\CompiledUrlMatcher;
15
16
/**
17
 * @method \Spryker\Shared\Router\RouterConfig getSharedConfig()
18
 */
19
class RouterConfig extends AbstractBundleConfig
20
{
21
    /**
22
     * @var string
23
     */
24
    protected const DIRECTORY_NAME_MERCHANT_PORTAL_APPLICATION = 'MerchantPortalApplication';
25
26
    /**
27
     * Specification:
28
     * - Returns a Router configuration which makes use of a Router cache.
29
     *
30
     * @api
31
     *
32
     * @see \Symfony\Component\Routing\Router::setOptions()
33
     *
34
     * @return array<string, mixed>
35
     */
36
    public function getBackofficeRouterConfiguration(): array
37
    {
38
        return [
39
            'cache_dir' => $this->getBackofficeCachePathIfCacheEnabled(),
40
            'generator_class' => UrlGenerator::class,
41
            'matcher_class' => CompiledUrlMatcher::class,
42
        ];
43
    }
44
45
    /**
46
     * @return string|null
47
     */
48
    protected function getBackofficeCachePathIfCacheEnabled(): ?string
49
    {
50
        if ($this->get(RouterConstants::BACKOFFICE_IS_CACHE_ENABLED, true)) {
51
            return $this->get(RouterConstants::BACKOFFICE_CACHE_PATH, $this->getBackofficeRouterCachePath());
52
        }
53
54
        return null;
55
    }
56
57
    /**
58
     * Specification:
59
     * - Defines the default path to the Router cache files.
60
     * - Can be redefined on Yves or Zed configs.
61
     *
62
     * @api
63
     *
64
     * @return string
65
     */
66
    public function getBackofficeRouterCachePath(): string
67
    {
68
        return sprintf(
69
            '%s/src/Generated/Router/Backoffice/codeBucket%s/',
70
            APPLICATION_ROOT_DIR,
71
            APPLICATION_CODE_BUCKET,
72
        );
73
    }
74
75
    /**
76
     * Specification:
77
     * - Returns a Merchant Portal Router configuration which makes use of a Router cache.
78
     *
79
     * @api
80
     *
81
     * @see \Symfony\Component\Routing\Router::setOptions()
82
     *
83
     * @return array<string, mixed>
84
     */
85
    public function getMerchantPortalRouterConfiguration(): array
86
    {
87
        return [
88
            'cache_dir' => $this->getMerchantPortalRouterCachePath(),
89
            'generator_class' => UrlGenerator::class,
90
            'matcher_class' => CompiledUrlMatcher::class,
91
        ];
92
    }
93
94
    /**
95
     * Specification:
96
     * - Defines the default path to the MerchantPortal Router cache files.
97
     *
98
     * @api
99
     *
100
     * @return string
101
     */
102
    public function getMerchantPortalRouterCachePath(): string
103
    {
104
        return sprintf(
105
            '%s/src/Generated/Router/MerchantPortal/codeBucket%s/',
106
            APPLICATION_ROOT_DIR,
107
            APPLICATION_CODE_BUCKET,
108
        );
109
    }
110
111
    /**
112
     * Specification:
113
     * - Returns a Router configuration which makes use of a Router cache.
114
     *
115
     * @api
116
     *
117
     * @see \Symfony\Component\Routing\Router::setOptions()
118
     *
119
     * @return array<string, mixed>
120
     */
121
    public function getBackendGatewayRouterConfiguration(): array
122
    {
123
        return [
124
            'cache_dir' => $this->getBackendGatewayCachePathIfCacheEnabled(),
125
            'generator_class' => UrlGenerator::class,
126
            'matcher_class' => CompiledUrlMatcher::class,
127
        ];
128
    }
129
130
    /**
131
     * @return string|null
132
     */
133
    protected function getBackendGatewayCachePathIfCacheEnabled(): ?string
134
    {
135
        if ($this->get(RouterConstants::BACKEND_GATEWAY_IS_CACHE_ENABLED, true)) {
136
            return $this->get(RouterConstants::BACKEND_GATEWAY_CACHE_PATH, $this->getBackendGatewayRouterCachePath());
137
        }
138
139
        return null;
140
    }
141
142
    /**
143
     * Specification:
144
     * - Defines the default path to the Router cache files.
145
     * - Can be redefined on Yves or Zed configs.
146
     *
147
     * @api
148
     *
149
     * @return string
150
     */
151
    public function getBackendGatewayRouterCachePath(): string
152
    {
153
        return sprintf(
154
            '%s/src/Generated/Router/BackendGateway/codeBucket%s/',
155
            APPLICATION_ROOT_DIR,
156
            APPLICATION_CODE_BUCKET,
157
        );
158
    }
159
160
    /**
161
     * @api
162
     *
163
     * Specification:
164
     * - Returns a Router configuration which makes use of a Router cache.
165
     *
166
     * @see \Symfony\Component\Routing\Router::setOptions()
167
     *
168
     * @return array<string, mixed>
169
     */
170
    public function getRouterConfiguration(): array
171
    {
172
        return [
173
            'cache_dir' => $this->getCachePathIfCacheEnabled(),
174
            'generator_class' => UrlGenerator::class,
175
            'matcher_class' => CompiledUrlMatcher::class,
176
        ];
177
    }
178
179
    /**
180
     * Specification:
181
     * - Returns a Router configuration which does not make use of a Router cache.
182
     * - Fallback for development which is executed when the cached Router is not able to match.
183
     *
184
     * @api
185
     *
186
     * @see \Symfony\Component\Routing\Router::setOptions()
187
     *
188
     * @return array<string, mixed>
189
     */
190
    public function getDevelopmentRouterConfiguration(): array
191
    {
192
        $routerConfiguration = $this->getRouterConfiguration();
193
        $routerConfiguration['cache_dir'] = null;
194
195
        return $routerConfiguration;
196
    }
197
198
    /**
199
     * @return string|null
200
     */
201
    protected function getCachePathIfCacheEnabled(): ?string
202
    {
203
        if ($this->get(RouterConstants::ZED_IS_CACHE_ENABLED, true)) {
204
            return $this->get(RouterConstants::ZED_CACHE_PATH, $this->getSharedConfig()->getDefaultRouterCachePath());
205
        }
206
207
        return null;
208
    }
209
210
    /**
211
     * Specification:
212
     * - Returns an array of directories where Controller are placed.
213
     * - Used to build the Router cache.
214
     *
215
     * @api
216
     *
217
     * @return array<string>
218
     */
219
    public function getControllerDirectories(): array
220
    {
221
        $controllerDirectories = [];
222
        $srcDirectory = $this->getSourceDirectory();
223
        $vendorDirectory = $this->getVendorDirectory();
224
225
        foreach ($this->get(KernelConstants::PROJECT_NAMESPACES) as $projectNamespace) {
226
            $controllerDirectories[] = sprintf('%s/%s/Zed/*/Communication/Controller/', $srcDirectory, $projectNamespace);
227
        }
228
229
        foreach ($this->get(KernelConstants::CORE_NAMESPACES) as $coreNamespace) {
230
            $composerPackageNamespace = strtolower(preg_replace('/([a-z0-9]|[A-Z0-9])([A-Z0-9])/', '$1-$2', $coreNamespace));
231
232
            $controllerDirectories[] = sprintf(
233
                '%s/%s/*/src/%s/Zed/*/Communication/Controller/',
234
                $vendorDirectory,
235
                $composerPackageNamespace,
236
                $coreNamespace,
237
            );
238
        }
239
240
        return $this->filterDirectories($controllerDirectories);
241
    }
242
243
    /**
244
     * @return string
245
     */
246
    protected function getSourceDirectory(): string
247
    {
248
        return rtrim(APPLICATION_SOURCE_DIR, '/');
249
    }
250
251
    /**
252
     * @return string
253
     */
254
    protected function getVendorDirectory(): string
255
    {
256
        return rtrim(APPLICATION_VENDOR_DIR, '/');
257
    }
258
259
    /**
260
     * @param array<string> $directories
261
     *
262
     * @return array<string>
263
     */
264
    protected function filterDirectories(array $directories): array
265
    {
266
        return array_filter($directories, 'glob');
267
    }
268
269
    /**
270
     * Specification:
271
     * - Returns if the SSl is enabled.
272
     * - When it is enabled and the current request is not secure, the Router will redirect to a secured URL.
273
     *
274
     * @api
275
     *
276
     * @return bool
277
     */
278
    public function isSslEnabled(): bool
279
    {
280
        return $this->get(RouterConstants::ZED_IS_SSL_ENABLED, true);
281
    }
282
283
    /**
284
     * Specification:
285
     * - Returns SSl excluded Route names.
286
     * - When SSL is enabled and the current Route name is excluded, the Router will not redirect to a secured URL.
287
     *
288
     * @api
289
     *
290
     * @return array<string>
291
     */
292
    public function getSslExcludedRouteNames(): array
293
    {
294
        return $this->get(RouterConstants::ZED_SSL_EXCLUDED_ROUTE_NAMES, []);
295
    }
296
297
    /**
298
     * Specification:
299
     * - Returns a list of directories that are not allowed for Backoffice controllers.
300
     *
301
     * @api
302
     *
303
     * @return list<string>
304
     */
305
    public function getNotAllowedBackofficeControllerDirectories(): array
306
    {
307
        return [
0 ignored issues
show
Bug Best Practice introduced by
The expression return array(static::DIR...ANT_PORTAL_APPLICATION) returns the type array<integer,string> which is incompatible with the documented return type Spryker\Zed\Router\list.
Loading history...
308
            static::DIRECTORY_NAME_MERCHANT_PORTAL_APPLICATION,
309
        ];
310
    }
311
}
312