|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\MediaLibrary\UrlGenerator; |
|
4
|
|
|
|
|
5
|
|
|
use Spatie\MediaLibrary\Models\Media; |
|
6
|
|
|
use Spatie\MediaLibrary\Conversion\Conversion; |
|
7
|
|
|
use Spatie\MediaLibrary\PathGenerator\PathGenerator; |
|
8
|
|
|
use Illuminate\Contracts\Config\Repository as Config; |
|
9
|
|
|
|
|
10
|
|
|
abstract class BaseUrlGenerator implements UrlGenerator |
|
11
|
|
|
{ |
|
12
|
|
|
/** @var \Spatie\MediaLibrary\Models\Media */ |
|
13
|
|
|
protected $media; |
|
14
|
|
|
|
|
15
|
|
|
/** @var \Spatie\MediaLibrary\Conversion\Conversion|null */ |
|
16
|
|
|
protected $conversion; |
|
17
|
|
|
|
|
18
|
|
|
/** @var \Spatie\MediaLibrary\PathGenerator\PathGenerator */ |
|
19
|
|
|
protected $pathGenerator; |
|
20
|
|
|
|
|
21
|
|
|
/** @var \Illuminate\Contracts\Config\Repository */ |
|
22
|
|
|
protected $config; |
|
23
|
|
|
|
|
24
|
|
|
/** @param \Illuminate\Contracts\Config\Repository $config */ |
|
25
|
|
|
public function __construct(Config $config) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->config = $config; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param \Spatie\MediaLibrary\Models\Media $media |
|
32
|
|
|
* |
|
33
|
|
|
* @return \Spatie\MediaLibrary\UrlGenerator\UrlGenerator |
|
34
|
|
|
*/ |
|
35
|
|
|
public function setMedia(Media $media): UrlGenerator |
|
36
|
|
|
{ |
|
37
|
|
|
$this->media = $media; |
|
38
|
|
|
|
|
39
|
|
|
return $this; |
|
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param \Spatie\MediaLibrary\Conversion\Conversion $conversion |
|
44
|
|
|
* |
|
45
|
|
|
* @return \Spatie\MediaLibrary\UrlGenerator\UrlGenerator |
|
46
|
|
|
*/ |
|
47
|
|
|
public function setConversion(Conversion $conversion): UrlGenerator |
|
48
|
|
|
{ |
|
49
|
|
|
$this->conversion = $conversion; |
|
50
|
|
|
|
|
51
|
|
|
return $this; |
|
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param \Spatie\MediaLibrary\PathGenerator\PathGenerator $pathGenerator |
|
56
|
|
|
* |
|
57
|
|
|
* @return \Spatie\MediaLibrary\UrlGenerator\UrlGenerator |
|
58
|
|
|
*/ |
|
59
|
|
|
public function setPathGenerator(PathGenerator $pathGenerator): UrlGenerator |
|
60
|
|
|
{ |
|
61
|
|
|
$this->pathGenerator = $pathGenerator; |
|
62
|
|
|
|
|
63
|
|
|
return $this; |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/* |
|
67
|
|
|
* Get the path to the requested file relative to the root of the media directory. |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getPathRelativeToRoot(): string |
|
70
|
|
|
{ |
|
71
|
|
|
if (is_null($this->conversion)) { |
|
72
|
|
|
return $this->pathGenerator->getPath($this->media).($this->media->file_name); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $this->pathGenerator->getPathForConversions($this->media) |
|
76
|
|
|
.pathinfo($this->media->file_name, PATHINFO_FILENAME) |
|
77
|
|
|
.'-'.$this->conversion->getName() |
|
78
|
|
|
.'.' |
|
79
|
|
|
.$this->conversion->getResultExtension($this->media->extension); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function rawUrlEncodeFilename(string $path = ''): string |
|
83
|
|
|
{ |
|
84
|
|
|
return pathinfo($path, PATHINFO_DIRNAME).'/'.rawurlencode(pathinfo($path, PATHINFO_BASENAME)); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function versionUrl(string $path = ''): string |
|
88
|
|
|
{ |
|
89
|
|
|
if (! $this->config->get('medialibrary.version_urls')) { |
|
90
|
|
|
return $path; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return "{$path}?v={$this->media->updated_at->timestamp}"; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
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.