|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Coyote\Services\TwigBridge\Extensions; |
|
4
|
|
|
|
|
5
|
|
|
use Coyote\Http\Factories\MediaFactory; |
|
6
|
|
|
use Coyote\Services\Media\MediaInterface; |
|
7
|
|
|
use Coyote\Services\Thumbnail\Objects\Microblog; |
|
8
|
|
|
use Twig_Extension; |
|
9
|
|
|
use Twig_SimpleFunction; |
|
10
|
|
|
|
|
11
|
|
|
class Media extends Twig_Extension |
|
12
|
|
|
{ |
|
13
|
|
|
use MediaFactory; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* {@inheritDoc} |
|
17
|
|
|
*/ |
|
18
|
|
|
public function getName() |
|
19
|
|
|
{ |
|
20
|
|
|
return 'TwigBridge_Extension_Media'; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @return array |
|
25
|
|
|
*/ |
|
26
|
|
|
public function getFunctions() |
|
27
|
|
|
{ |
|
28
|
|
|
return [ |
|
29
|
|
|
// funkcja generuje URL do zdjecia usera lub domyslny avatar jezeli brak |
|
30
|
|
|
new Twig_SimpleFunction('user_photo', [$this, 'userPhoto']), |
|
31
|
|
|
new Twig_SimpleFunction('logo', [$this, 'logo']), |
|
32
|
|
|
new Twig_SimpleFunction('thumbnail', [$this, 'thumbnail']) |
|
33
|
|
|
]; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param string $filename |
|
38
|
|
|
* @return string |
|
39
|
|
|
* @throws \Exception |
|
40
|
|
|
*/ |
|
41
|
|
|
public function userPhoto($filename) |
|
42
|
|
|
{ |
|
43
|
|
|
return (string) $this->getMediaUrl('photo', $filename, 'img/avatar.png'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param string $filename |
|
48
|
|
|
* @return string |
|
49
|
|
|
*/ |
|
50
|
|
|
public function logo($filename) |
|
51
|
|
|
{ |
|
52
|
|
|
return (string) $this->getMediaUrl('logo', $filename, 'img/logo-gray.png'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Generate thumbnail URL for microblog attachments... |
|
57
|
|
|
* |
|
58
|
|
|
* @param MediaInterface $media |
|
59
|
|
|
* @return string |
|
60
|
|
|
*/ |
|
61
|
|
|
public function thumbnail(MediaInterface $media) |
|
62
|
|
|
{ |
|
63
|
|
|
return $media->url()->thumbnail(new Microblog()); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param string $factory |
|
68
|
|
|
* @param string $filename |
|
69
|
|
|
* @return string |
|
70
|
|
|
* @throws \Exception |
|
71
|
|
|
*/ |
|
72
|
|
|
private function getMediaUrl($factory, $filename, $placeholder) |
|
73
|
|
|
{ |
|
74
|
|
|
if (!$filename) { |
|
75
|
|
|
return cdn($placeholder); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
if (is_string($filename)) { |
|
79
|
|
|
return $this->getMediaFactory()->make($factory, ['file_name' => $filename])->url(); |
|
80
|
|
|
} elseif ($filename instanceof MediaInterface) { |
|
81
|
|
|
return $filename->getFilename() ? $filename->url() : cdn($placeholder); |
|
82
|
|
|
} else { |
|
83
|
|
|
throw new \Exception('Parameter needs to be either string or MediaInterface object.'); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|