|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* AnimeDb package. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Peter Gribanov <[email protected]> |
|
6
|
|
|
* @copyright Copyright (c) 2011, Peter Gribanov |
|
7
|
|
|
* @license http://opensource.org/licenses/GPL-3.0 GPL v3 |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace AnimeDb\Bundle\AppBundle\Service; |
|
10
|
|
|
|
|
11
|
|
|
use Symfony\Bundle\FrameworkBundle\Routing\Router; |
|
12
|
|
|
use Symfony\Component\HttpKernel\Fragment\FragmentHandler; |
|
13
|
|
|
use Symfony\Component\HttpKernel\Controller\ControllerReference; |
|
14
|
|
|
|
|
15
|
|
|
class TwigExtension extends \Twig_Extension |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var Router |
|
19
|
|
|
*/ |
|
20
|
|
|
private $router; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var FragmentHandler |
|
24
|
|
|
*/ |
|
25
|
|
|
private $handler; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var WidgetsContainer |
|
29
|
|
|
*/ |
|
30
|
|
|
private $widgets; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param Router $router |
|
34
|
|
|
* @param FragmentHandler $handler |
|
35
|
|
|
* @param WidgetsContainer $widgets |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct(Router $router, FragmentHandler $handler, WidgetsContainer $widgets) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->router = $router; |
|
40
|
|
|
$this->handler = $handler; |
|
41
|
|
|
$this->widgets = $widgets; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @return array |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getFilters() |
|
48
|
|
|
{ |
|
49
|
|
|
return [ |
|
|
|
|
|
|
50
|
|
|
'favicon' => new \Twig_SimpleFilter('favicon', [$this, 'favicon']), |
|
51
|
|
|
]; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return array |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getFunctions() |
|
58
|
|
|
{ |
|
59
|
|
|
return [ |
|
|
|
|
|
|
60
|
|
|
'widgets' => new \Twig_SimpleFunction('widgets', [$this, 'widgets'], ['is_safe' => ['html']]), |
|
61
|
|
|
]; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param string $url |
|
66
|
|
|
* |
|
67
|
|
|
* @return string|false |
|
68
|
|
|
*/ |
|
69
|
|
|
public function favicon($url) |
|
70
|
|
|
{ |
|
71
|
|
|
return $url ? $this->router->generate('media_favicon', ['host' => parse_url($url, PHP_URL_HOST)]) : false; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param string $place |
|
76
|
|
|
* @param array $attributes |
|
77
|
|
|
* @param array $options |
|
78
|
|
|
* |
|
79
|
|
|
* @return string |
|
80
|
|
|
*/ |
|
81
|
|
|
public function widgets($place, array $attributes = [], array $options = []) |
|
82
|
|
|
{ |
|
83
|
|
|
$result = ''; |
|
84
|
|
|
foreach ($this->widgets->getWidgetsForPlace($place) as $controller) { |
|
85
|
|
|
$result .= $this->handler->render( |
|
86
|
|
|
new ControllerReference($controller, $attributes, []), |
|
87
|
|
|
'hinclude', |
|
88
|
|
|
$options |
|
89
|
|
|
); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return $result; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return string |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getName() |
|
99
|
|
|
{ |
|
100
|
|
|
return 'anime_db_app_extension'; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
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.