1 | <?php |
||
13 | trait PermalinkDocumentTrait |
||
14 | { |
||
15 | /** @var string */ |
||
16 | protected $permalink = null; |
||
17 | |||
18 | /** @var array */ |
||
19 | protected $redirects = null; |
||
20 | |||
21 | /** Extensions that need to be stripped from permalinks. */ |
||
22 | private static $extensionsToStrip = ['twig']; |
||
23 | |||
24 | /** |
||
25 | * {@inheritdoc} |
||
26 | */ |
||
27 | 1 | public function handleSpecialRedirects() |
|
30 | |||
31 | /** |
||
32 | * {@inheritdoc} |
||
33 | */ |
||
34 | 6 | final public function getTargetFile() |
|
35 | { |
||
36 | 6 | $permalink = $this->getPermalink(); |
|
37 | 6 | $missingFile = (substr($permalink, -1) == '/'); |
|
38 | 6 | $permalink = str_replace('/', DIRECTORY_SEPARATOR, $permalink); |
|
39 | |||
40 | 6 | if ($missingFile) |
|
41 | { |
||
42 | 2 | $permalink = rtrim($permalink, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'index.html'; |
|
43 | } |
||
44 | |||
45 | 6 | return ltrim($permalink, DIRECTORY_SEPARATOR); |
|
46 | } |
||
47 | |||
48 | /** |
||
49 | * {@inheritdoc} |
||
50 | */ |
||
51 | 25 | final public function getPermalink() |
|
61 | |||
62 | /** |
||
63 | * {@inheritdoc} |
||
64 | */ |
||
65 | 2 | final public function getRedirects() |
|
76 | |||
77 | /** |
||
78 | * Get the permalink based off the location of where the file is relative to the website. This permalink is to be |
||
79 | * used as a fallback in the case that a permalink is not explicitly specified in the Front Matter. |
||
80 | * |
||
81 | * @return string |
||
82 | */ |
||
83 | 3 | final protected function getPathPermalink() |
|
84 | { |
||
85 | // Remove the protocol of the path, if there is one and prepend a '/' to the beginning |
||
86 | 3 | $cleanPath = preg_replace('/[\w|\d]+:\/\//', '', $this->getRelativeFilePath()); |
|
87 | 3 | $cleanPath = ltrim($cleanPath, DIRECTORY_SEPARATOR); |
|
88 | |||
89 | // Handle vfs:// paths by replacing their forward slashes with the OS appropriate directory separator |
||
90 | 3 | if (DIRECTORY_SEPARATOR !== '/') |
|
91 | { |
||
92 | $cleanPath = str_replace('/', DIRECTORY_SEPARATOR, $cleanPath); |
||
93 | } |
||
94 | |||
95 | // Check the first folder and see if it's a data folder (starts with an underscore) intended for stakx |
||
96 | 3 | $folders = explode(DIRECTORY_SEPARATOR, $cleanPath); |
|
97 | |||
98 | 3 | if (substr($folders[0], 0, 1) === '_') |
|
99 | { |
||
100 | array_shift($folders); |
||
101 | } |
||
102 | |||
103 | 3 | $cleanPath = implode(DIRECTORY_SEPARATOR, $folders); |
|
104 | |||
105 | 3 | return $cleanPath; |
|
106 | } |
||
107 | |||
108 | /** |
||
109 | * Sanitize a permalink to remove unsupported characters or multiple '/' and replace spaces with hyphens. |
||
110 | * |
||
111 | * @param string $permalink A permalink |
||
112 | * |
||
113 | * @return string $permalink The sanitized permalink |
||
114 | */ |
||
115 | 25 | final private function sanitizePermalink($permalink) |
|
142 | } |
||
143 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.