|
1
|
|
|
<?php |
|
2
|
|
|
/****************************************************************************** |
|
3
|
|
|
* An implementation of dicto (scg.unibe.ch/dicto) in and for PHP. |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) 2016, 2015 Richard Klees <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* This software is licensed under The MIT License. You should have received |
|
8
|
|
|
* a copy of the licence along with the code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Lechimp\Dicto\Analysis; |
|
12
|
|
|
|
|
13
|
|
|
use Lechimp\Dicto\Definition as Def; |
|
14
|
|
|
use Lechimp\Dicto\Variables\Variable; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Performs the actual analysis of a ruleset over a query-object |
|
18
|
|
|
* using a specific rules to sql compiler. |
|
19
|
|
|
*/ |
|
20
|
|
|
class Analyzer { |
|
21
|
|
|
CONST LINES_BEFORE_AND_AFTER = 5; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var Def\Ruleset |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $ruleset; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var Query |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $query; |
|
32
|
|
|
|
|
33
|
2 |
|
public function __construct |
|
34
|
|
|
( Def\Ruleset $ruleset |
|
35
|
|
|
, Query $query |
|
36
|
|
|
) { |
|
37
|
2 |
|
$this->ruleset = $ruleset; |
|
38
|
2 |
|
$this->query = $query; |
|
39
|
2 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Run the analysis. |
|
43
|
|
|
* |
|
44
|
|
|
* @param \Closure $process_violation Expected to take violations and do whatever |
|
45
|
|
|
* @return null |
|
46
|
|
|
*/ |
|
47
|
2 |
|
public function run(\Closure $process_violation) { |
|
48
|
2 |
|
foreach ($this->ruleset->rules() as $rule) { |
|
49
|
2 |
|
$stmt = $rule->compile($this->query); |
|
50
|
2 |
|
while ($row = $stmt->fetch()) { |
|
51
|
2 |
|
$builder = $this->query->builder(); |
|
52
|
2 |
|
$expr = $builder->expr(); |
|
53
|
|
|
$stmt = $builder |
|
54
|
2 |
|
->select("source") |
|
55
|
2 |
|
->from($this->query->entity_table()) |
|
56
|
|
|
->where |
|
57
|
2 |
|
( $expr->eq("name", $row["file"]) |
|
58
|
2 |
|
, $expr->eq("type", Variable::FILE_TYPE) |
|
59
|
2 |
|
) |
|
60
|
2 |
|
->execute(); |
|
61
|
2 |
|
$file = $stmt->fetch(); |
|
62
|
2 |
|
if (!is_array($file)) { |
|
63
|
|
|
throw new \RuntimeException( |
|
64
|
|
|
"Could not find ".$row["file"]." in database."); |
|
65
|
|
|
} |
|
66
|
2 |
|
$process_violation($rule->to_violation($row, $file)); |
|
67
|
2 |
|
} |
|
68
|
2 |
|
} |
|
69
|
2 |
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|