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 license along with the code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Lechimp\Dicto\Indexer; |
12
|
|
|
|
13
|
|
|
use Lechimp\Dicto\Regexp; |
14
|
|
|
use Psr\Log\LoggerInterface as Log; |
15
|
|
|
use League\Flysystem\Adapter\Local; |
16
|
|
|
use League\Flysystem\Filesystem; |
17
|
|
|
use Lechimp\Flightcontrol\Flightcontrol; |
18
|
|
|
use Lechimp\Flightcontrol\File; |
19
|
|
|
use Lechimp\Flightcontrol\FSObject; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Creates an index of source files. |
23
|
|
|
*/ |
24
|
|
|
class Indexer { |
25
|
|
|
/** |
26
|
|
|
* @var Log |
27
|
|
|
*/ |
28
|
|
|
protected $log; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Insert |
32
|
|
|
*/ |
33
|
|
|
protected $insert; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var \PhpParser\Parser |
37
|
|
|
*/ |
38
|
|
|
protected $parser; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var ASTVisitor[] |
42
|
|
|
*/ |
43
|
|
|
protected $ast_visitors; |
44
|
|
|
|
45
|
53 |
View Code Duplication |
public function __construct(Log $log, \PhpParser\Parser $parser, Insert $insert, array $ast_visitors) { |
|
|
|
|
46
|
53 |
|
$this->log = $log; |
47
|
53 |
|
$this->parser = $parser; |
48
|
53 |
|
$this->insert = $insert; |
49
|
|
|
$this->ast_visitors = array_map(function (ASTVisitor $v) { |
50
|
25 |
|
return $v; |
51
|
53 |
|
}, $ast_visitors); |
52
|
53 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Index a directory. |
56
|
|
|
* |
57
|
|
|
* @param string $path |
58
|
|
|
* @param array $ignore_paths |
59
|
|
|
* @return null |
60
|
|
|
*/ |
61
|
7 |
|
public function index_directory($path, array $ignore_paths) { |
62
|
|
|
$ignore_paths_re = array_map(function($ignore) { |
63
|
7 |
|
return new Regexp($ignore); |
64
|
7 |
|
}, $ignore_paths); |
65
|
7 |
|
$fc = $this->init_flightcontrol($path); |
66
|
7 |
|
$fc->directory("/") |
67
|
7 |
|
->recurseOn() |
68
|
|
|
->filter(function(FSObject $obj) use (&$ignore_paths_re) { |
69
|
7 |
|
foreach ($ignore_paths_re as $re) { |
70
|
7 |
|
if ($re->match($obj->path())) { |
71
|
7 |
|
return false; |
72
|
|
|
} |
73
|
7 |
|
} |
74
|
7 |
|
return true; |
75
|
7 |
|
}) |
76
|
7 |
|
->foldFiles(null, function($_, File $file) use ($path) { |
77
|
|
|
try { |
78
|
7 |
|
$this->index_file($path, $file->path()); |
79
|
|
|
} |
80
|
7 |
|
catch (\PhpParser\Error $e) { |
81
|
|
|
$this->log->error("in ".$file->path().": ".$e->getMessage()); |
82
|
|
|
} |
83
|
7 |
|
}); |
84
|
7 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Initialize the filesystem abstraction. |
88
|
|
|
* |
89
|
|
|
* @return Flightcontrol |
90
|
|
|
*/ |
91
|
7 |
|
public function init_flightcontrol($path) { |
92
|
7 |
|
$adapter = new Local($path, LOCK_EX, Local::SKIP_LINKS); |
93
|
7 |
|
$flysystem = new Filesystem($adapter); |
94
|
7 |
|
return new Flightcontrol($flysystem); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $base_dir |
99
|
|
|
* @param string $path |
100
|
|
|
* @return null |
101
|
|
|
*/ |
102
|
|
|
public function index_file($base_dir, $path) { |
103
|
|
|
assert('is_string($base_dir)'); |
104
|
|
|
assert('is_string($path)'); |
105
|
|
|
$this->log->info("indexing: ".$path); |
106
|
|
|
$full_path = "$base_dir/$path"; |
107
|
|
|
$content = file_get_contents($full_path); |
108
|
|
|
if ($content === false) { |
109
|
|
|
throw \InvalidArgumentException("Can't read file $path."); |
110
|
|
|
} |
111
|
|
|
$this->index_content($path, $content); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param string $path |
116
|
|
|
* @param string $content |
117
|
|
|
* @return null |
118
|
|
|
*/ |
119
|
53 |
|
public function index_content($path, $content) { |
120
|
53 |
|
assert('is_string($path)'); |
121
|
53 |
|
assert('is_string($content)'); |
122
|
|
|
|
123
|
53 |
|
$stmts = $this->parser->parse($content); |
124
|
53 |
|
if ($stmts === null) { |
125
|
|
|
throw new \RuntimeException("Can't parse file $path."); |
126
|
|
|
} |
127
|
|
|
|
128
|
53 |
|
$traverser = new \PhpParser\NodeTraverser; |
129
|
53 |
|
$location = new LocationImpl($path, $content); |
130
|
53 |
|
$visitor = new BaseVisitor($location, $this->insert); |
131
|
53 |
|
$traverser->addVisitor($visitor); |
132
|
53 |
|
foreach ($this->ast_visitors as $visitor) { |
133
|
25 |
|
$traverser->addVisitor(new AdapterVisitor($location, $this->insert, $visitor)); |
134
|
53 |
|
} |
135
|
53 |
|
$traverser->traverse($stmts); |
136
|
53 |
|
} |
137
|
|
|
} |
138
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.