@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1<?php
2
3/**
4 * A simple wrapper for PhutilURI, to be aware of the
5 * relative/absolute context, and other minor things.
6 */
7final class PhutilURIHelper extends Phobject {
8
9 /**
10 * String version of your original URI.
11 * @var string
12 */
13 private $uriStr;
14
15 /**
16 * Structured version of your URI.
17 * @var PhutilURI
18 */
19 private $phutilUri;
20
21 /**
22 * @param string|PhutilURI $uri
23 */
24 public function __construct($uri) {
25
26 // Keep the original string for basic checks.
27 $this->uriStr = phutil_string_cast($uri);
28
29 // A PhutilURI may be useful. If available, import that as-is.
30 // Note that the constructor PhutilURI(string) is a bit expensive.
31 if ($uri instanceof PhutilURI) {
32 $this->phutilUri = $uri;
33 }
34 }
35
36 /**
37 * Check if the URI points to Phorge itself.
38 * @return bool
39 */
40 public function isSelf() {
41 // The backend prefers a PhutilURI object, if available.
42 $uri = $this->phutilUri ? $this->phutilUri : $this->uriStr;
43 return PhabricatorEnv::isSelfURI($uri);
44 }
45
46 /**
47 * Check whenever an URI is just a simple fragment without path and protocol.
48 * @return bool
49 */
50 public function isAnchor() {
51 return $this->isStartingWithChar('#');
52 }
53
54 /**
55 * Check whenever an URI starts with a slash (no protocol, etc.)
56 * @return bool
57 */
58 public function isStartingWithSlash() {
59 return $this->isStartingWithChar('/');
60 }
61
62 /**
63 * A sane default.
64 */
65 public function __toString() {
66 return $this->uriStr;
67 }
68
69 /**
70 * Check whenever the URI starts with the provided character.
71 * @param string $char String that MUST have length of 1.
72 * @return bool
73 */
74 private function isStartingWithChar($char) {
75 return strncmp($this->uriStr, $char, 1) === 0;
76 }
77
78}