|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Analyzer abstraction |
|
4
|
|
|
* |
|
5
|
|
|
* @package CacheCheck\Service\Analyzer |
|
6
|
|
|
* @author Tim Lochmüller |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace HDNET\CacheCheck\Service\Analyzer; |
|
10
|
|
|
|
|
11
|
|
|
use HDNET\CacheCheck\Exception; |
|
12
|
|
|
use TYPO3\CMS\Core\Database\DatabaseConnection; |
|
13
|
|
|
use TYPO3\CMS\Core\SingletonInterface; |
|
14
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Analyzer abstraction |
|
18
|
|
|
* |
|
19
|
|
|
* @author Tim Lochmüller |
|
20
|
|
|
*/ |
|
21
|
|
|
abstract class AbstractAnalyzer implements AnalyzerInterface, SingletonInterface |
|
22
|
|
|
{ |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* get dynamic values from database |
|
26
|
|
|
* |
|
27
|
|
|
* @param array $queryValues |
|
28
|
|
|
* |
|
29
|
|
|
* @return string|null |
|
30
|
|
|
*/ |
|
31
|
|
|
protected function getDynamicFromDatabase(array $queryValues) |
|
32
|
|
|
{ |
|
33
|
|
|
$databaseConnection = $this->getDatabaseConnection(); |
|
34
|
|
|
$checkValues = [ |
|
35
|
|
|
'SELECT', |
|
36
|
|
|
'FROM', |
|
37
|
|
|
'WHERE', |
|
38
|
|
|
'GROUPBY', |
|
39
|
|
|
'ORDERBY', |
|
40
|
|
|
'LIMIT' |
|
41
|
|
|
]; |
|
42
|
|
|
foreach ($checkValues as $name) { |
|
43
|
|
|
if (!array_key_exists($name, $queryValues)) { |
|
44
|
|
|
$queryValues[$name] = ''; |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$res = $databaseConnection->exec_SELECT_queryArray($queryValues); |
|
49
|
|
|
if (!$res) { |
|
50
|
|
|
return new Exception('Invalid SQL Query: ' . var_export($checkValues, true), 2346273846783); |
|
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
$result = $databaseConnection->sql_fetch_row($res); |
|
53
|
|
|
if (!isset($result[0])) { |
|
54
|
|
|
return new Exception('No single value is found in the SQL Query', 324528943578); |
|
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
return $result[0]; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Get an analyzer |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $name |
|
63
|
|
|
* |
|
64
|
|
|
* @return AnalyzerInterface |
|
65
|
|
|
*/ |
|
66
|
|
|
protected function getAnalyzer($name) |
|
67
|
|
|
{ |
|
68
|
|
|
return GeneralUtility::makeInstance('HDNET\\CacheCheck\\Service\\Analyzer\\' . $name); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Get database connection |
|
73
|
|
|
* |
|
74
|
|
|
* @return DatabaseConnection |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function getDatabaseConnection() |
|
77
|
|
|
{ |
|
78
|
|
|
return $GLOBALS['TYPO3_DB']; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
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.