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
|
|
|
|
30
|
|
|
/** @var string */ |
31
|
|
|
protected $baseurl; |
32
|
|
|
/** @var string */ |
33
|
|
|
protected $version; |
34
|
|
|
|
35
|
|
|
/** @var bool */ |
36
|
|
|
protected $canonical; |
37
|
|
|
/** @var bool */ |
38
|
|
|
protected $addhash; |
39
|
|
|
/** @var string */ |
40
|
|
|
protected $format; |
41
|
|
|
|
42
|
|
|
/** @var Slugify */ |
43
|
|
|
private static $slugifier; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param Builder $builder |
47
|
|
|
*/ |
48
|
|
|
public function __construct(Builder $builder, $value, array $options) |
|
|
|
|
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
|
|
|
$this->version = (string) $this->builder->time; |
57
|
|
|
|
58
|
|
|
switch ($value) { |
59
|
|
|
case 'value': |
60
|
|
|
// code... |
61
|
|
|
break; |
62
|
|
|
|
63
|
|
|
default: |
64
|
|
|
// code... |
65
|
|
|
break; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Creates an URL. |
71
|
|
|
* |
72
|
|
|
* $options[ |
73
|
|
|
* 'canonical' => true, |
74
|
|
|
* 'addhash' => false, |
75
|
|
|
* 'format' => 'json', |
76
|
|
|
* ]; |
77
|
|
|
* |
78
|
|
|
* @param Page|Asset|string|null $value |
79
|
|
|
* @param array|null $options |
80
|
|
|
* |
81
|
|
|
* @return mixed |
82
|
|
|
*/ |
83
|
|
|
public function createUrl($value = null, $options = null) |
84
|
|
|
{ |
85
|
|
|
$base = ''; |
86
|
|
|
|
87
|
|
|
// handles options |
88
|
|
|
$canonical = null; |
89
|
|
|
$addhash = false; |
90
|
|
|
$format = null; |
91
|
|
|
extract(is_array($options) ? $options : [], EXTR_IF_EXISTS); |
92
|
|
|
|
93
|
|
|
// canonicalurl? |
94
|
|
|
if ((bool) $this->config->get('canonicalurl') || $canonical === true) { |
95
|
|
|
$base = rtrim($this->baseurl, '/'); |
96
|
|
|
} |
97
|
|
|
if ($canonical === false) { |
98
|
|
|
$base = ''; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
// value is empty: url() |
102
|
|
|
if (empty($value) || $value == '/') { |
103
|
|
|
return $base.'/'; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// value is a Page item |
107
|
|
|
if ($value instanceof Page) { |
108
|
|
|
if (!$format) { |
109
|
|
|
$format = $value->getVariable('output'); |
110
|
|
|
if (is_array($value->getVariable('output'))) { |
111
|
|
|
$format = $value->getVariable('output')[0]; |
112
|
|
|
} |
113
|
|
|
if (!$format) { |
114
|
|
|
$format = 'html'; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
$url = $value->getUrl($format, $this->config); |
118
|
|
|
$url = $base.'/'.ltrim($url, '/'); |
119
|
|
|
|
120
|
|
|
return $url; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
// value is an Asset object |
124
|
|
|
if ($value instanceof Asset) { |
125
|
|
|
$asset = $value; |
126
|
|
|
$url = $asset['path']; |
127
|
|
|
if ($addhash) { |
128
|
|
|
$url .= '?'.$this->version; |
129
|
|
|
} |
130
|
|
|
$url = $base.'/'.ltrim($url, '/'); |
131
|
|
|
$asset['path'] = $url; |
132
|
|
|
|
133
|
|
|
return $asset; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
// value is an external URL |
137
|
|
|
if (Util::isExternalUrl($value)) { |
138
|
|
|
$url = $value; |
139
|
|
|
|
140
|
|
|
return $url; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
// value is a string |
144
|
|
|
$value = Util::joinPath($value); |
145
|
|
|
|
146
|
|
|
// DEBUG |
147
|
|
|
dump($value); |
148
|
|
|
dump(strrpos($value, '.')); |
149
|
|
|
|
150
|
|
|
// value is (certainly) a path to a ressource (ie: 'path/file.pdf') |
151
|
|
|
if (false !== strpos($value, '.')) { |
152
|
|
|
$url = $value; |
153
|
|
|
if ($addhash) { |
154
|
|
|
$url .= '?'.$this->version; |
155
|
|
|
} |
156
|
|
|
$url = $base.'/'.ltrim($url, '/'); |
157
|
|
|
|
158
|
|
|
return $url; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
// others cases |
162
|
|
|
$url = $base.'/'.$value; |
163
|
|
|
|
164
|
|
|
// value is a page ID (ie: 'path/my-page') |
165
|
|
|
try { |
166
|
|
|
$pageId = self::$slugifier->slugify($value); |
167
|
|
|
$page = $this->builder->getPages()->get($pageId); |
168
|
|
|
$url = $this->createUrl($page, $options); |
169
|
|
|
} catch (\DomainException $e) { |
170
|
|
|
// nothing to do |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return $url; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function isCanonical(array $options) |
|
|
|
|
177
|
|
|
{ |
178
|
|
|
if ((bool) $this->config->get('canonicalurl') || $canonical === true) { |
|
|
|
|
179
|
|
|
return true; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return false; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function extractOptions() |
186
|
|
|
{ |
187
|
|
|
$canonical = null; |
|
|
|
|
188
|
|
|
$addhash = false; |
|
|
|
|
189
|
|
|
$format = null; |
|
|
|
|
190
|
|
|
extract(is_array($options) ? $options : [], EXTR_IF_EXISTS); |
|
|
|
|
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.