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
|
|
|
/** |
22
|
|
|
* @var Def\Ruleset |
23
|
|
|
*/ |
24
|
|
|
protected $ruleset; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Query |
28
|
|
|
*/ |
29
|
|
|
protected $query; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var ReportGenerator |
33
|
|
|
*/ |
34
|
|
|
protected $generator; |
35
|
|
|
|
36
|
2 |
|
public function __construct |
37
|
|
|
( Def\Ruleset $ruleset |
38
|
|
|
, Query $query |
39
|
|
|
, ReportGenerator $generator |
40
|
|
|
) { |
41
|
2 |
|
$this->ruleset = $ruleset; |
42
|
2 |
|
$this->query = $query; |
43
|
2 |
|
$this->generator = $generator; |
44
|
2 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Run the analysis. |
48
|
|
|
* |
49
|
|
|
* @return null |
50
|
|
|
*/ |
51
|
2 |
|
public function run() { |
52
|
2 |
|
$this->generator->start_ruleset($this->ruleset); |
53
|
2 |
|
foreach ($this->ruleset->rules() as $rule) { |
54
|
2 |
|
$this->generator->start_rule($rule); |
55
|
2 |
|
$stmt = $rule->compile($this->query); |
56
|
2 |
|
while ($row = $stmt->fetch()) { |
57
|
2 |
|
$builder = $this->query->builder(); |
58
|
2 |
|
$expr = $builder->expr(); |
59
|
|
|
$stmt = $builder |
60
|
2 |
|
->select("source") |
61
|
2 |
|
->from($this->query->entity_table()) |
62
|
|
|
->where |
63
|
2 |
|
( $expr->eq("name", $row["file"]) |
64
|
2 |
|
, $expr->eq("type", Variable::FILE_TYPE) |
65
|
2 |
|
) |
66
|
2 |
|
->execute(); |
67
|
2 |
|
$file = $stmt->fetch(); |
68
|
2 |
|
if (!is_array($file)) { |
69
|
|
|
throw new \RuntimeException( |
70
|
|
|
"Could not find ".$row["file"]." in database."); |
71
|
|
|
} |
72
|
2 |
|
$this->generator->report_violation($rule->to_violation($row, $file)); |
73
|
2 |
|
} |
74
|
2 |
|
} |
75
|
2 |
|
} |
76
|
|
|
} |
77
|
|
|
|