1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Cecil/Cecil package. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) Arnaud Ligny <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Cecil\Assets; |
12
|
|
|
|
13
|
|
|
use Cecil\Builder; |
14
|
|
|
use Cecil\Collection\Page\Page; |
15
|
|
|
use Cecil\Config; |
16
|
|
|
use Cecil\Util; |
17
|
|
|
use Cocur\Slugify\Slugify; |
18
|
|
|
|
19
|
|
|
class Url |
20
|
|
|
{ |
21
|
|
|
/** @var Builder */ |
22
|
|
|
protected $builder; |
23
|
|
|
/** @var Config */ |
24
|
|
|
protected $config; |
25
|
|
|
/** @var Page|Asset|string|null */ |
26
|
|
|
protected $value; |
27
|
|
|
/** @var array */ |
28
|
|
|
protected $options; |
29
|
|
|
/** @var string */ |
30
|
|
|
protected $baseurl; |
31
|
|
|
/** @var string */ |
32
|
|
|
protected $url; |
33
|
|
|
/** @var Slugify */ |
34
|
|
|
private static $slugifier; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Creates an URL from string, Page or Asset. |
38
|
|
|
* |
39
|
|
|
* $options[ |
40
|
|
|
* 'canonical' => true, |
41
|
|
|
* 'format' => 'json', |
42
|
|
|
* ]; |
43
|
|
|
* |
44
|
|
|
* @param Builder $builder |
45
|
|
|
* @param Page|Asset|string|null $value |
46
|
|
|
* @param array|null $options |
47
|
|
|
*/ |
48
|
|
|
public function __construct(Builder $builder, $value, array $options = null) |
49
|
|
|
{ |
50
|
|
|
$this->builder = $builder; |
51
|
|
|
$this->config = $builder->getConfig(); |
52
|
|
|
if (!self::$slugifier instanceof Slugify) { |
53
|
|
|
self::$slugifier = Slugify::create(['regexp' => Page::SLUGIFY_PATTERN]); |
54
|
|
|
} |
55
|
|
|
$this->baseurl = (string) $this->config->get('baseurl'); |
56
|
|
|
|
57
|
|
|
// handles options |
58
|
|
|
$canonical = null; |
59
|
|
|
$format = null; |
60
|
|
|
$lang = $this->config->getLanguageDefault(); |
61
|
|
|
extract(is_array($options) ? $options : [], EXTR_IF_EXISTS); |
62
|
|
|
|
63
|
|
|
// canonical URL? |
64
|
|
|
$base = ''; |
65
|
|
|
if ((bool) $this->config->get('canonicalurl') || $canonical === true) { |
66
|
|
|
$base = rtrim($this->baseurl, '/'); |
67
|
|
|
} |
68
|
|
|
if ($canonical === false) { |
69
|
|
|
$base = ''; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// specific language? |
73
|
|
|
if ($lang !== $this->config->getLanguageDefault()) { |
74
|
|
|
$value = sprintf('%s/%s/', $lang, $value); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// value is empty (ie: `url()`) |
78
|
|
|
if (is_null($value) || empty($value) || $value == '/') { |
79
|
|
|
$this->url = $base.'/'; |
80
|
|
|
|
81
|
|
|
return; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
switch (true) { |
85
|
|
|
// Page |
86
|
|
|
case $value instanceof Page: |
87
|
|
|
if (!$format) { |
88
|
|
|
$format = $value->getVariable('output'); |
89
|
|
|
if (is_array($value->getVariable('output'))) { |
90
|
|
|
$format = $value->getVariable('output')[0]; |
91
|
|
|
} |
92
|
|
|
if (!$format) { |
93
|
|
|
$format = 'html'; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
$this->url = $base.'/'.ltrim($value->getUrl($format, $this->config), '/'); |
97
|
|
|
break; |
98
|
|
|
// Asset |
99
|
|
|
case $value instanceof Asset: |
100
|
|
|
$asset = $value; |
101
|
|
|
$this->url = $base.'/'.ltrim($asset['path'], '/'); |
102
|
|
|
break; |
103
|
|
|
// string |
104
|
|
|
case is_string($value): |
105
|
|
|
// potential Page ID |
106
|
|
|
$pageId = self::$slugifier->slugify($value); |
107
|
|
|
switch (true) { |
108
|
|
|
// External URL |
109
|
|
|
case Util\Url::isUrl($value): |
110
|
|
|
$this->url = $value; |
111
|
|
|
break; |
112
|
|
|
// Page ID as string |
113
|
|
|
case $this->builder->getPages()->has($pageId): |
114
|
|
|
$page = $this->builder->getPages()->get($pageId); |
115
|
|
|
$this->url = new self($this->builder, $page, $options); |
116
|
|
|
break; |
117
|
|
|
// asset as string |
118
|
|
|
case false !== strpos($value, '.') ? true : false: |
119
|
|
|
$this->url = $base.'/'.ltrim($value, '/'); |
120
|
|
|
break; |
121
|
|
|
// others cases? |
122
|
|
|
default: |
123
|
|
|
// others cases |
124
|
|
|
$this->url = $base.'/'.$value; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Returns URL. |
131
|
|
|
* |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
|
|
public function __toString(): string |
135
|
|
|
{ |
136
|
|
|
return $this->url; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Returns URL. |
141
|
|
|
* |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
|
|
public function getUrl(): string |
145
|
|
|
{ |
146
|
|
|
return $this->__toString(); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|