|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Superdesk Web Publisher Template Engine Bundle. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright 2019 Sourcefabric z.u. and contributors. |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please see the |
|
9
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
|
10
|
|
|
* |
|
11
|
|
|
* @copyright 2019 Sourcefabric z.ú |
|
12
|
|
|
* @license http://www.superdesk.org/license |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace SWP\Bundle\TemplatesSystemBundle\DataCollector; |
|
16
|
|
|
|
|
17
|
|
|
use SWP\Component\TemplatesSystem\Gimme\Loader\LoaderInterface; |
|
18
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
19
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
20
|
|
|
use Symfony\Component\HttpKernel\DataCollector\DataCollector; |
|
21
|
|
|
|
|
22
|
|
|
class MetaLoaderCollector extends DataCollector |
|
23
|
|
|
{ |
|
24
|
|
|
protected $traceableChainLoader; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct(LoaderInterface $traceableChainLoader) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->traceableChainLoader = $traceableChainLoader; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function collect(Request $request, Response $response, \Exception $exception = null) |
|
32
|
|
|
{ |
|
33
|
|
|
$data = $this->traceableChainLoader->getData(); |
|
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
foreach ($data['calledLoaders'] as $class => $loader) { |
|
36
|
|
|
$totalDuration = 0; |
|
37
|
|
|
foreach ($loader['calls'] as $call) { |
|
38
|
|
|
$totalDuration = $totalDuration + $call['duration']; |
|
39
|
|
|
} |
|
40
|
|
|
$loader['totalDuration'] = $totalDuration; |
|
41
|
|
|
|
|
42
|
|
|
$data['calledLoaders'][$class] = $loader; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$this->data = $data; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @return array |
|
50
|
|
|
*/ |
|
51
|
|
|
public function getData() |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->data; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return string |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getName() |
|
60
|
|
|
{ |
|
61
|
|
|
return 'meta_loader_collector'; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function reset() |
|
65
|
|
|
{ |
|
66
|
|
|
return; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: