1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Thinktomorrow\Chief\Urls; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\RedirectResponse; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use Illuminate\Http\Response; |
8
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
9
|
|
|
use Thinktomorrow\Chief\Concerns\Morphable\Morphables; |
10
|
|
|
use Thinktomorrow\Chief\Concerns\Morphable\NotFoundMorphKey; |
11
|
|
|
use Thinktomorrow\Chief\Concerns\Publishable\PreviewMode; |
12
|
|
|
|
13
|
|
|
class ChiefResponse extends Response |
14
|
|
|
{ |
15
|
|
|
// public static function fromRequest(Request $request = null, $locale = null) |
|
|
|
|
16
|
|
|
// { |
17
|
|
|
// if (!$request) { |
18
|
|
|
// $request = request(); |
19
|
|
|
// } |
20
|
|
|
// if (!$locale) { |
21
|
|
|
// $locale = app()->getLocale(); |
22
|
|
|
// } |
23
|
|
|
// |
24
|
|
|
// return static::fromSlug($request->path(), $locale); |
25
|
|
|
// } |
26
|
|
|
|
27
|
11 |
|
public static function fromSlug(string $slug, $locale = null) |
28
|
|
|
{ |
29
|
11 |
|
if (!$locale) { |
30
|
8 |
|
$locale = app()->getLocale(); |
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
try { |
34
|
11 |
|
$urlRecord = UrlRecord::findBySlug($slug, $locale); |
35
|
|
|
|
36
|
9 |
|
$model = Morphables::instance($urlRecord->model_type)->find($urlRecord->model_id); |
37
|
|
|
|
38
|
8 |
|
if ($urlRecord->isRedirect()) { |
39
|
|
|
|
40
|
|
|
// If model is not found, it probably means it is archived or removed |
41
|
|
|
// So we detect the model based on the redirect target url. |
42
|
3 |
|
if (!$model) { |
43
|
1 |
|
$targetUrlRecord = $urlRecord->redirectTo(); |
44
|
1 |
|
$targetModel = Morphables::instance($targetUrlRecord->model_type)->find($targetUrlRecord->model_id); |
45
|
|
|
|
46
|
1 |
|
return static::createRedirect($targetModel->url($locale)); |
47
|
|
|
} |
48
|
|
|
|
49
|
2 |
|
return static::createRedirect($model->url($locale)); |
50
|
|
|
} |
51
|
|
|
|
52
|
5 |
|
if (method_exists($model, 'isPublished') && ! $model->isPublished()) { |
53
|
|
|
|
54
|
|
|
/** When admin is logged in and this request is in preview mode, we allow the view */ |
55
|
3 |
|
if (! PreviewMode::fromRequest()->check()) { |
56
|
2 |
|
throw new NotFoundHttpException('Model found for request ['. $slug .'] but it is not published.'); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
3 |
|
return new static($model->renderView(), 200); |
61
|
5 |
|
} catch (UrlRecordNotFound | NotFoundMorphKey $e) { |
62
|
3 |
|
if (config('thinktomorrow.chief.strict')) { |
63
|
|
|
throw $e; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
3 |
|
throw new NotFoundHttpException('No url or model found for request ['. $slug .'] for locale ['.$locale.'].'); |
68
|
|
|
} |
69
|
|
|
|
70
|
3 |
|
private static function createRedirect(string $url) |
71
|
|
|
{ |
72
|
3 |
|
return new RedirectResponse($url, 301, []); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.