|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright 2017 Vladimir Jimenez |
|
5
|
|
|
* @license https://github.com/allejo/stakx/blob/master/LICENSE.md MIT |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace allejo\stakx\Twig; |
|
9
|
|
|
|
|
10
|
|
|
use allejo\stakx\Service; |
|
11
|
|
|
use Twig_Environment; |
|
12
|
|
|
|
|
13
|
|
|
class BaseUrlFunction implements StakxTwigFilter |
|
14
|
|
|
{ |
|
15
|
|
|
private $site; |
|
16
|
|
|
|
|
17
|
18 |
|
public function __invoke(Twig_Environment $env, $assetPath, $absolute = false) |
|
18
|
|
|
{ |
|
19
|
18 |
|
$globals = $env->getGlobals(); |
|
20
|
18 |
|
$this->site = $globals['site']; |
|
21
|
|
|
|
|
22
|
18 |
|
$url = $this->getUrl($absolute); |
|
23
|
18 |
|
$baseURL = $this->getBaseUrl(); |
|
24
|
18 |
|
$permalink = $this->guessAssetPath($assetPath); |
|
25
|
|
|
|
|
26
|
18 |
|
return $this->buildPermalink($url, $baseURL, $permalink); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
6 |
|
public static function get() |
|
30
|
|
|
{ |
|
31
|
6 |
|
return new \Twig_SimpleFunction('url', new self(), array( |
|
|
|
|
|
|
32
|
6 |
|
'needs_environment' => true, |
|
33
|
|
|
)); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
18 |
|
private function getUrl($absolute) |
|
37
|
|
|
{ |
|
38
|
18 |
|
$url = '/'; |
|
39
|
|
|
|
|
40
|
18 |
|
if (!$absolute) |
|
41
|
|
|
{ |
|
42
|
16 |
|
return $url; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
2 |
|
if (isset($this->site['url'])) |
|
46
|
|
|
{ |
|
47
|
2 |
|
$url = $this->site['url']; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
2 |
|
return ltrim($url, '/'); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
18 |
|
private function getBaseUrl() |
|
|
|
|
|
|
54
|
|
|
{ |
|
55
|
18 |
|
$base = ''; |
|
56
|
|
|
|
|
57
|
18 |
|
if (isset($this->site['baseurl'])) |
|
58
|
|
|
{ |
|
59
|
15 |
|
$base = $this->site['baseurl']; |
|
60
|
|
|
} |
|
61
|
3 |
|
elseif (isset($this->site['base'])) |
|
62
|
|
|
{ |
|
63
|
|
|
$base = $this->site['base']; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
18 |
|
return $base; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
18 |
|
private function guessAssetPath($assetPath) |
|
70
|
|
|
{ |
|
71
|
18 |
|
if (is_array($assetPath) || ($assetPath instanceof \ArrayAccess)) |
|
72
|
|
|
{ |
|
73
|
4 |
|
return (isset($assetPath['permalink'])) ? $assetPath['permalink'] : '/'; |
|
74
|
|
|
} |
|
75
|
14 |
|
elseif (is_null($assetPath)) |
|
76
|
|
|
{ |
|
77
|
1 |
|
return '/'; |
|
78
|
|
|
} |
|
79
|
13 |
|
elseif ($assetPath instanceof \SplFileInfo) |
|
80
|
|
|
{ |
|
81
|
|
|
return str_replace(Service::getWorkingDirectory(), '', $assetPath); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
13 |
|
return $assetPath; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @link https://stackoverflow.com/a/15575293 |
|
89
|
|
|
* @return string |
|
90
|
|
|
*/ |
|
91
|
18 |
View Code Duplication |
private function buildPermalink() |
|
|
|
|
|
|
92
|
|
|
{ |
|
93
|
18 |
|
$paths = array(); |
|
94
|
|
|
|
|
95
|
18 |
|
foreach (func_get_args() as $arg) |
|
96
|
|
|
{ |
|
97
|
18 |
|
if ($arg !== '') |
|
98
|
|
|
{ |
|
99
|
18 |
|
$paths[] = $arg; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
18 |
|
return preg_replace('#(?<!:)/+#','/', join('/', $paths)); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.