|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Devhelp\PiwikBundle\Command\Param; |
|
4
|
|
|
|
|
5
|
|
|
use Devhelp\Piwik\Api\Api; |
|
6
|
|
|
use Devhelp\Piwik\Api\Method\Method; |
|
7
|
|
|
use Psr\Log\LoggerInterface; |
|
8
|
|
|
use Psr\Log\NullLogger; |
|
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Finds an api method in a container that is either registered as a service or is available through |
|
13
|
|
|
* the given api service by calling getMethod |
|
14
|
|
|
*/ |
|
15
|
|
|
class MethodFinder |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var ContainerInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
private $container; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var LoggerInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
private $logger; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct(ContainerInterface $container, LoggerInterface $logger = null) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->container = $container; |
|
30
|
|
|
$this->logger = $logger ? $logger : new NullLogger(); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param string $methodArg |
|
35
|
|
|
* @param string|null $apiName |
|
36
|
|
|
* @return Method |
|
37
|
|
|
*/ |
|
38
|
|
|
public function find($methodArg, $apiName = null) |
|
39
|
|
|
{ |
|
40
|
|
|
if ($this->container->has($methodArg)) { |
|
41
|
|
|
$method = $this->container->get($methodArg); |
|
42
|
|
|
|
|
43
|
|
|
$this->logger->debug("$methodArg resolved to a service"); |
|
44
|
|
|
|
|
45
|
|
|
if ($apiName) { |
|
|
|
|
|
|
46
|
|
|
$this->logger->warning("'$apiName' api name will be ignored"); |
|
47
|
|
|
} |
|
48
|
|
|
} else { |
|
49
|
|
|
$api = $this->getApi($apiName); |
|
50
|
|
|
|
|
51
|
|
|
$method = $api->getMethod($methodArg); |
|
52
|
|
|
|
|
53
|
|
|
$this->logger->debug("$methodArg resolved to a method name"); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$this->checkIsInstanceOfMethod($methodArg, $method); |
|
57
|
|
|
|
|
58
|
|
|
return $method; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param string|null $apiName |
|
63
|
|
|
* @return Api |
|
64
|
|
|
*/ |
|
65
|
|
|
private function getApi($apiName) |
|
66
|
|
|
{ |
|
67
|
|
|
$apiServiceId = $this->getApiServiceId($apiName); |
|
68
|
|
|
|
|
69
|
|
|
if (!$this->container->has($apiServiceId)) { |
|
70
|
|
|
throw new \InvalidArgumentException("'$apiName' api service does not exist"); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return $this->container->get($apiServiceId); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param string|null $apiName |
|
78
|
|
|
* @return string |
|
79
|
|
|
*/ |
|
80
|
|
|
private function getApiServiceId($apiName) |
|
81
|
|
|
{ |
|
82
|
|
|
$serviceId = 'devhelp_piwik.api'; |
|
83
|
|
|
|
|
84
|
|
|
if ($apiName) { |
|
|
|
|
|
|
85
|
|
|
$serviceId .= '.'.$apiName; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $serviceId; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param string $methodArg |
|
93
|
|
|
* @param object $method |
|
94
|
|
|
*/ |
|
95
|
|
|
private function checkIsInstanceOfMethod($methodArg, $method) |
|
96
|
|
|
{ |
|
97
|
|
|
if (!$method instanceof Method) { |
|
98
|
|
|
throw new \InvalidArgumentException( |
|
99
|
|
|
sprintf( |
|
100
|
|
|
"'%s' object is of invalid class, expected Devhelp\\Piwik\\Api\\Method\\Method, got %s", |
|
101
|
|
|
$methodArg, |
|
102
|
|
|
get_class($method) |
|
103
|
|
|
) |
|
104
|
|
|
); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: