Issues (3641)

Yves/Application/Routing/AbstractRouter.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\Yves\Application\Routing;
9
10
use Silex\Application;
11
use Spryker\Shared\Application\Communication\ControllerServiceBuilder;
12
use Spryker\Shared\Kernel\Communication\BundleControllerActionInterface;
13
use Spryker\Yves\Kernel\AbstractPlugin;
14
use Spryker\Yves\Kernel\ClassResolver\Controller\ControllerResolver;
15
use Spryker\Yves\Kernel\Controller\RouteNameResolver;
16
use Symfony\Component\HttpFoundation\RedirectResponse;
17
use Symfony\Component\Routing\Generator\UrlGenerator;
18
use Symfony\Component\Routing\RequestContext;
19
use Symfony\Component\Routing\RouteCollection;
20
use Symfony\Component\Routing\RouterInterface;
21
22
/**
23
 * @deprecated Will be removed without replacement.
24
 *
25
 * @see Router Module.
26
 */
27
abstract class AbstractRouter extends AbstractPlugin implements RouterInterface
28
{
29
    /**
30
     * @var \Symfony\Component\Routing\RequestContext
31
     */
32
    protected $context;
33
34
    /**
35
     * @var bool
36
     */
37
    protected $sslEnabled = false;
38
39
    /**
40
     * {@inheritDoc}
41
     *
42
     * @param \Symfony\Component\Routing\RequestContext $context
43
     *
44
     * @return void
45
     */
46
    public function setContext(RequestContext $context)
47
    {
48
        $this->context = $context;
49
    }
50
51
    /**
52
     * @inheritDoc
53
     */
54
    public function getContext(): RequestContext
55
    {
56
        return $this->context;
57
    }
58
59
    /**
60
     * @inheritDoc
61
     */
62
    public function getRouteCollection()
63
    {
64
        return new RouteCollection();
65
    }
66
67
    /**
68
     * @return bool
69
     */
70
    public function isSslEnabled()
71
    {
72
        return $this->sslEnabled;
73
    }
74
75
    /**
76
     * @param bool $status
77
     *
78
     * @return $this
79
     */
80
    public function setSsl($status)
81
    {
82
        $this->sslEnabled = $status;
83
84
        return $this;
85
    }
86
87
    /**
88
     * @param string $pathInfo
89
     *
90
     * @return array|null
91
     */
92
    protected function checkScheme($pathInfo)
93
    {
94
        $wantedScheme = $this->isSslEnabled() ? 'https' : 'http';
95
        if ($this->getContext()->getScheme() !== $wantedScheme) {
96
            $url = $wantedScheme . '://' . $this->context->getHost() . $pathInfo;
97
98
            return [
99
                '_controller' => function ($url) {
100
                    return new RedirectResponse($url, 301);
101
                },
102
                '_route' => null,
103
                'url' => $url,
104
            ];
105
        }
106
107
        return null;
108
    }
109
110
    /**
111
     * @param string $pathInfo
112
     * @param string|int $referenceType
113
     *
114
     * @return string
115
     */
116
    protected function getUrlOrPathForType($pathInfo, $referenceType)
117
    {
118
        $url = $pathInfo;
119
        $scheme = $this->context->getScheme();
120
121
        if (
122
            $referenceType !== static::NETWORK_PATH &&
123
            ($scheme === 'http' && $this->sslEnabled === true || $scheme === 'https' && $this->sslEnabled === false)
124
        ) {
125
            $referenceType = static::ABSOLUTE_URL;
126
        }
127
128
        switch ($referenceType) {
129
            case static::ABSOLUTE_URL:
130
            case static::NETWORK_PATH:
131
                $url = $this->buildUrl($pathInfo, $referenceType);
132
133
                break;
134
            case static::ABSOLUTE_PATH:
135
                $url = $pathInfo;
136
137
                break;
138
            case static::RELATIVE_PATH:
139
                $url = UrlGenerator::getRelativePath($this->context->getPathInfo(), $pathInfo);
140
141
                break;
142
        }
143
144
        return $url;
145
    }
146
147
    /**
148
     * @param string $pathInfo
149
     * @param string|int $referenceType
150
     *
151
     * @return string
152
     */
153
    private function buildUrl($pathInfo, $referenceType)
154
    {
155
        $scheme = $this->getScheme();
156
        $port = $this->getPortPart($scheme);
157
        $schemeAuthority = $referenceType === static::NETWORK_PATH ? '//' : "$scheme://";
158
        $schemeAuthority .= $this->context->getHost() . $port;
159
160
        return $schemeAuthority . $this->context->getBaseUrl() . $pathInfo;
161
    }
162
163
    /**
164
     * @param string $scheme
165
     *
166
     * @return string
167
     */
168
    private function getPortPart($scheme)
169
    {
170
        $port = '';
171
        if ($scheme === 'http' && $this->context->getHttpPort() !== 80) {
172
            $port = ':' . $this->context->getHttpPort();
173
        } elseif ($scheme === 'https' && $this->context->getHttpsPort() !== 443) {
174
            $port = ':' . $this->context->getHttpsPort();
175
        }
176
177
        return $port;
178
    }
179
180
    /**
181
     * @return string
182
     */
183
    private function getScheme()
184
    {
185
        $scheme = $this->context->getScheme();
186
        if (is_bool($this->sslEnabled)) {
0 ignored issues
show
The condition is_bool($this->sslEnabled) is always true.
Loading history...
187
            $scheme = ($this->sslEnabled) ? 'https' : 'http';
188
        }
189
190
        return $scheme;
191
    }
192
193
    /**
194
     * @param \Silex\Application $application
195
     * @param \Spryker\Shared\Kernel\Communication\BundleControllerActionInterface $bundleControllerAction
196
     * @param \Spryker\Yves\Kernel\Controller\RouteNameResolver $routeResolver
197
     *
198
     * @return string
199
     */
200
    protected function createServiceForController(
201
        Application $application,
202
        BundleControllerActionInterface $bundleControllerAction,
203
        RouteNameResolver $routeResolver
204
    ) {
205
        $controllerResolver = new ControllerResolver();
206
        $service = (new ControllerServiceBuilder())->createServiceForController(
207
            $application,
208
            $bundleControllerAction,
209
            $controllerResolver,
210
            $routeResolver,
211
        );
212
213
        return $service;
214
    }
215
}
216