-
Notifications
You must be signed in to change notification settings - Fork 0
/
Page.php
101 lines (83 loc) · 2.55 KB
/
Page.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
namespace Xanweb\C5\Request;
use Concrete\Core\Http\Request;
use Concrete\Core\Localization\Localization;
use Concrete\Core\Multilingual\Page\Section\Section;
use Concrete\Core\Page\Page as ConcretePage;
use Concrete\Core\Support\Facade\Url;
use League\Url\UrlInterface;
use Xanweb\Common\Traits\ApplicationTrait;
use Xanweb\Common\Traits\SingletonTrait;
use Xanweb\C5\Request\Traits\AttributesTrait;
/**
* @method static string getCollectionName()
* @method static string getCollectionDescription()
* @method static string getCollectionPath()
* @method static string getCollectionDatePublic()
* @method static \DateTime|null getCollectionDatePublicObject()
* @method static string getCollectionDateLastModified()
*/
class Page
{
use ApplicationTrait;
use AttributesTrait;
use SingletonTrait;
private Request $request;
protected array $cache = [];
public function __construct()
{
$this->request = $this->app(Request::class);
}
public function url(): ?URLInterface
{
$c = self::getConcretePage();
if ($c !== null) {
return self::get()->cache['url'] ??= Url::to($c);
}
return null;
}
public static function getLocale(): string
{
$rp = self::get();
if (!isset($rp->cache['locale'])) {
$section = Section::getCurrentSection();
$locale = is_object($section) ? $section->getLocale() : null;
$rp->cache['locale'] = $locale ?? Localization::activeLocale();
}
return $rp->cache['locale'];
}
public static function getLanguage(): string
{
return \current(\explode('_', self::getLocale()));
}
public static function isEditMode(): bool
{
$rp = self::get();
return $rp->cache['isEditMode'] ??= (($c = self::getConcretePage()) !== null && $c->isEditMode());
}
public static function getAttribute($ak, $mode = false)
{
$c = self::getConcretePage();
return ($c !== null) ? self::_getAttribute($c, $ak, $mode) : null;
}
public function __call($name, $arguments)
{
$c = $this->request->getCurrentPage();
if ($c !== null) {
return $c->$name(...$arguments);
}
}
public static function __callStatic($name, $arguments)
{
return self::get()->$name(...$arguments);
}
/**
* Get Current Page Object.
*
* @return ConcretePage|null
*/
protected static function getConcretePage(): ?ConcretePage
{
return self::get()->request->getCurrentPage();
}
}