|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Thinktomorrow\Chief\Urls; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\RedirectResponse; |
|
6
|
|
|
use Illuminate\Http\Response; |
|
7
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
8
|
|
|
use Thinktomorrow\Chief\Concerns\Morphable\Morphables; |
|
9
|
|
|
use Thinktomorrow\Chief\Concerns\Morphable\NotFoundMorphKey; |
|
10
|
|
|
use Thinktomorrow\Chief\States\Publishable\PreviewMode; |
|
11
|
|
|
|
|
12
|
|
|
class ChiefResponse extends Response |
|
13
|
|
|
{ |
|
14
|
10 |
|
final public function __construct($content = '', int $status = 200, array $headers = []) |
|
15
|
|
|
{ |
|
16
|
10 |
|
parent::__construct($content, $status, $headers); |
|
17
|
7 |
|
} |
|
18
|
|
|
|
|
19
|
|
|
public static function fromSlug(string $slug, $locale = null) |
|
20
|
|
|
{ |
|
21
|
10 |
|
if (!$locale) { |
|
22
|
|
|
$locale = app()->getLocale(); |
|
|
|
|
|
|
23
|
8 |
|
} |
|
24
|
|
|
|
|
25
|
7 |
|
try { |
|
26
|
|
|
$urlRecord = UrlRecord::findBySlug($slug, $locale); |
|
27
|
|
|
|
|
28
|
|
|
$model = Morphables::instance($urlRecord->model_type)->find($urlRecord->model_id); |
|
29
|
3 |
|
|
|
30
|
1 |
|
if ($urlRecord->isRedirect()) { |
|
31
|
|
|
|
|
32
|
1 |
|
// If model is not found, it probably means it is archived or removed |
|
33
|
|
|
// So we detect the model based on the redirect target url. |
|
34
|
1 |
|
if (!$model) { |
|
35
|
|
|
$targetUrlRecord = $urlRecord->redirectTo(); |
|
36
|
|
|
|
|
37
|
|
|
$targetModel = Morphables::instance($targetUrlRecord->model_type)->find($targetUrlRecord->model_id); |
|
38
|
1 |
|
|
|
39
|
|
|
if (!$targetModel) { |
|
40
|
|
|
throw new \DomainException('Corrupt target model for this url request. Model by reference ['.$targetUrlRecord->model_type.'@'.$targetUrlRecord->model_id.'] has probably been archived or deleted.'); |
|
41
|
2 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
return static::createRedirect($targetModel->url($locale)); |
|
44
|
4 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
return static::createRedirect($model->url($locale)); |
|
47
|
|
|
} |
|
48
|
4 |
|
|
|
49
|
|
|
if (!$model) { |
|
50
|
|
|
throw new \DomainException('Corrupt target model for this url request. Model by reference ['.$urlRecord->model_type.'@'.$urlRecord->model_id.'] has probably been archived or deleted.'); |
|
51
|
2 |
|
} |
|
52
|
1 |
|
|
|
53
|
|
|
if (method_exists($model, 'isPublished') && ! $model->isPublished()) { |
|
54
|
|
|
|
|
55
|
|
|
/** When admin is logged in and this request is in preview mode, we allow the view */ |
|
56
|
3 |
|
if (! PreviewMode::fromRequest()->check()) { |
|
57
|
4 |
|
throw new NotFoundHttpException('Model found for request ['. $slug .'] but it is not published.'); |
|
58
|
3 |
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return new static($model->renderView(), 200); |
|
62
|
|
|
} catch (UrlRecordNotFound | NotFoundMorphKey $e) { |
|
63
|
3 |
|
if (config('thinktomorrow.chief.strict')) { |
|
64
|
|
|
throw $e; |
|
65
|
|
|
} |
|
66
|
3 |
|
} |
|
67
|
|
|
|
|
68
|
3 |
|
throw new NotFoundHttpException('No url or model found for request ['. $slug .'] for locale ['.$locale.'].'); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
private static function createRedirect(string $url) |
|
72
|
|
|
{ |
|
73
|
|
|
return new RedirectResponse($url, 301, []); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|