Completed
Push — master ( cea077...33308d )
by Philipp
05:08
created

Dialect::addLocaleToHost()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 3
nc 2
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Pmochine\LaravelTongue;
4
5
use Illuminate\Http\RedirectResponse;
6
use Illuminate\Foundation\Application;
7
use Pmochine\LaravelTongue\Misc\Config;
8
use Pmochine\LaravelTongue\Accent\Accent;
9
use Pmochine\LaravelTongue\Localization\Localization;
10
11
/**
12
 * This class was written by
13
 * https://github.com/hoyvoy/laravel-subdomain-localization
14
 * Now is the time to have my own dialect with it :P.
15
 */
16
class Dialect
17
{
18
    /**
19
     * Our instance of the Laravel app.
20
     *
21
     * @var \Illuminate\Foundation\Application
22
     */
23
    protected $app = '';
24
25
    /**
26
     * An array that contains information about the current request.
27
     *
28
     * @var array
29
     */
30
    protected $parsed_url;
31
32
    /**
33
     * An array that contains all routes that should be translated.
34
     *
35
     * @var array
36
     */
37
    protected $translatedRoutes = [];
38
39
    public function __construct(Application $app)
40
    {
41
        $this->app = $app;
42
    }
43
44
    /**
45
     * Adds the detected locale to the current unlocalized URL.
46
     *
47
     * @return string
48
     */
49
    public function redirectUrl($url = null)
50
    {
51
        $parsed_url = parse_url($url ?? request()->fullUrl());
52
53
        $domain = Localization::domain();
54
55
        if (Config::beautify() && Localization::fromUrl() === Config::fallbackLocale()) {
56
            $parsed_url['host'] = $domain;
57
        } else {
58
            $parsed_url['host'] = tongue()->current().'.'.$domain;
59
        }
60
61
        return Accent::unparseUrl($parsed_url);
62
    }
63
64
    /**
65
     * Creates the redirect response.
66
     *
67
     * @param  string
68
     * @return \Illuminate\Http\RedirectResponse;
69
     */
70
    public function redirect(string $redirection)
71
    {
72
        // Save any flashed data for redirect
73
        app('session')->reflash();
74
75
        return new RedirectResponse($redirection, 302, ['Vary' => 'Accept-Language']);
76
    }
77
78
    /**
79
     * Translate the current route for the given locale.
80
     *
81
     * @param $locale
82
     *
83
     * @return bool|string
84
     */
85
    public function current($locale)
86
    {
87
        return $this->translate($this->currentRouteName(), Accent::currentRouteAttributes(), $locale);
88
    }
89
90
    /**
91
     * Get all Translations for the current URL.
92
     *
93
     * @param bool $excludeCurrentLocale
94
     *
95
     * @return array
96
     */
97
    public function translateAll($excludeCurrentLocale = true)
98
    {
99
        $versions = [];
100
101
        foreach (tongue()->speaking()->keys()->all() as $locale) {
102
            if ($excludeCurrentLocale && $locale == tongue()->current()) {
103
                continue;
104
            }
105
106
            if ($url = $this->current($locale)) {
107
                $versions[$locale] = $url;
108
            }
109
        }
110
111
        return $versions;
112
    }
113
114
    /**
115
     * Return translated URL from route.
116
     *
117
     * @param string       $routeName
118
     * @param string|false $routeAttributes
119
     * @param string|false $locale
120
     *
121
     * @return string|bool
122
     */
123
    public function translate($routeName, $routeAttributes = null, $locale = null)
124
    {
125
        // If no locale is given, we use the current locale
126
        if (! $locale) {
127
            $locale = tongue()->current();
128
        }
129
130
        if (empty($this->parsed_url)) {
131
            $this->parsed_url = Accent::parseCurrentUrl();
132
        }
133
134
        // Retrieve the current URL components
135
        $parsed_url = $this->parsed_url;
136
137
    
138
        $parsed_url['host'] = $this->addLocaleToHost($locale);
139
140
141
        // Resolve the translated route path for the given route name
142
        $translatedPath = Accent::findRoutePathByName($routeName, $locale);
143
144
        if ($translatedPath !== false) {
145
            $parsed_url['path'] = $translatedPath;
146
        }
147
148
        // If attributes are given, substitute them in the path
149
        if ($routeAttributes) {
150
            $parsed_url['path'] = Accent::substituteAttributesInRoute($routeAttributes, $parsed_url['path']);
151
        }
152
153
        return Accent::unparseUrl($parsed_url);
154
    }
155
156
    /**
157
     * If we have beautify on and the given $locale is the same
158
     * to the current locale and to the fallbackLocal. 
159
     * We don't need to add a subdomain to the host
160
     * 
161
     * @param string $locale
162
     * @return string
163
     */
164
    protected function addLocaleToHost($locale) 
165
    {
166
        if(Config::beautify() && $locale === tongue()->current() && $locale === Config::fallbackLocale()){
167
             return Localization::domain();
168
        }
169
170
        // Add locale to the host
171
        return $locale.'.'.Localization::domain();
172
    }
173
174
    /**
175
     * Interprets a translated route path for the given route name.
176
     *
177
     * @param $routeName
178
     *
179
     * @return mixed (but should be string if it exists!)
180
     */
181
    public function interpret($routeName)
182
    {
183
        $routePath = Accent::findRoutePathByName($routeName);
184
185
        if (! isset($this->translatedRoutes[$routeName])) {
186
            $this->translatedRoutes[$routeName] = $routePath;
187
        }
188
189
        return $routePath;
190
    }
191
192
    /**
193
     * Get the current route name.
194
     *
195
     * @return bool|string
196
     */
197
    protected function currentRouteName()
198
    {
199
        if (app('router')->currentRouteName()) {
200
            return app('router')->currentRouteName();
201
        }
202
203
        if (app('router')->current()) {
204
            return $this->findRouteNameByPath(app('router')->current()->uri());
205
        }
206
207
        return false;
208
    }
209
210
    /**
211
     * Find the route name matching the given route path.
212
     *
213
     * @param string $routePath
214
     *
215
     * @return bool|string
216
     */
217
    public function findRouteNameByPath($routePath)
218
    {
219
        foreach ($this->translatedRoutes as $name => $path) {
220
            if ($routePath == $path) {
221
                return $name;
222
            }
223
        }
224
225
        return false;
226
    }
227
}
228