-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathIndexGenerator.php
More file actions
207 lines (184 loc) · 6.19 KB
/
Copy pathIndexGenerator.php
File metadata and controls
207 lines (184 loc) · 6.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
namespace Padawan\Framework\Generator;
use Padawan\Domain\Project\Node\ClassData;
use Padawan\Domain\Project\Node\InterfaceData;
use Padawan\Domain\Event\IndexGenerationEvent;
use Padawan\Framework\Domain\Project\InMemoryIndex;
use Padawan\Domain\Project;
use Padawan\Domain\Project\File;
use Padawan\Domain\Project\Index;
use Padawan\Domain\Scope\FileScope;
use Padawan\Framework\Utils\PathResolver;
use Padawan\Framework\Utils\Composer;
use Padawan\Framework\Utils\ClassUtils;
use Psr\Log\LoggerInterface;
use Padawan\Parser\Walker\IndexGeneratingWalker;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Padawan\Domain\Generator\IndexGenerator as IndexGeneratorInterface;
class IndexGenerator implements IndexGeneratorInterface
{
const BEFORE_GENERATION = 'index.before_generation';
const AFTER_GENERATION = 'index.after_generation';
public function __construct(
PathResolver $path,
ClassUtils $class,
LoggerInterface $logger,
EventDispatcher $dispatcher,
FilesFinder $filesFinder,
IndexGeneratingWalker $walker
) {
$this->path = $path;
$this->classUtils = $class;
$this->logger = $logger;
$this->walker = $walker;
$this->dispatcher = $dispatcher;
$this->filesFinder = $filesFinder;
}
public function generateIndex(Project $project)
{
$event = new IndexGenerationEvent($project);
$this->dispatcher->dispatch(self::BEFORE_GENERATION, $event);
$index = $project->getIndex();
$this->generateProjectIndex($project);
$this->dispatcher->dispatch(self::AFTER_GENERATION, $event);
return $index;
}
public function generateProjectIndex(Project $project, $rewrite = true)
{
// You know what this mean
gc_disable();
$index = $project->getIndex();
$globalTime = 0;
$done = 0;
$files = $this->filesFinder->findProjectFiles($project);
$all = count($files);
foreach ($files as $file) {
try {
$start = microtime(1);
$this->processFile($index, $file, $rewrite);
$end = microtime(1) - $start;
$this->getLogger()->debug("Indexing: [$end]s");
$this->getLogger()->debug("Memory: " . memory_get_usage());
$globalTime += $end;
++$done;
$process = floor($done / $all * 100);
$this->getLogger()->info("Progress: $process%");
} catch (\Exception $e) {
$this->getLogger()->error(get_class($e).": ".$e->getMessage().". Trace: ".$e->getTraceAsString());
} catch (\Error $e) {
$this->getLogger()->error(get_class($e).": ".$e->getMessage().". Trace: ".$e->getTraceAsString());
}
}
$this->getLogger()->info("[ $globalTime ]");
gc_enable();
}
public function processFile(
Index $index,
$filePath,
$rewrite = true
) {
$this->getLogger()
->info("processing $filePath");
$file = $index->findFileByPath($filePath);
if (empty($file)) {
$file = new File($filePath);
}
$filePath = $this->path->getAbsolutePath($file->path());
$content = $this->path->read($filePath);
$hash = sha1($content);
if ($rewrite || $file->isChanged($hash)) {
$scope = $this->createScopeForFile($file, $content, $index, $rewrite);
$this->processFileScope(
$file,
$index,
$scope,
$hash
);
}
}
public function createScopeForFile(File $file, $content, Index $index, $rewrite = true)
{
$startParser = microtime(1);
$walker = $this->walker;
$parser = $this->getClassUtils()->getParser();
$parser->addWalker($walker);
$parser->setIndex($index);
$scope = $parser->parseContent($file->path(), $content, null);
$end = microtime(1) - $startParser;
$this->getLogger()->info("Parsing: [$end]s");
return $scope;
}
public function processFileScope(File $file, Index $index, FileScope $scope = null, $hash = null)
{
if (empty($scope)) {
return;
}
if (empty($hash)) {
throw new \Exception("Contents hash could not be empty");
}
$this->getLogger()->debug(sprintf("Processing %s classes", count($scope->getClasses())));
$this->getLogger()->debug(sprintf("Processing %s interfaces", count($scope->getInterfaces())));
$this->getLogger()->debug(sprintf("Processing %s functions", count($scope->getFunctions())));
foreach ($scope->getClasses() as $classData) {
$this->getLogger()->debug('Processing node ' . $classData->fqcn->toString());
}
foreach ($scope->getInterfaces() as $interfaceData) {
$this->getLogger()->debug('Processing node ' . $interfaceData->fqcn->toString());
}
foreach ($scope->getFunctions() as $functionData) {
$this->getLogger()->debug('Processing node ' . $functionData->name);
}
$file->updateScope($scope, $hash);
$index->addFile($file);
}
/** @return LoggerInterface */
public function getLogger()
{
return $this->logger;
}
public function getWalker()
{
return $this->walker;
}
/** @return ClassUtils */
public function getClassUtils()
{
return $this->classUtils;
}
public function getNamespaceUtils()
{
return $this->namespaceUtils;
}
/**
*
*
* @var PathResolver
*/
protected $path;
/**
* Object with Composer-specific functions
*
* @var Composer
*/
protected $composer;
/**
* Object for work with class-information
*
* @var ClassUtils
*/
protected $class;
/**
*
* @var LoggerInterface
*/
protected $logger;
/**
*
* @var IndexGeneratingWalker
*/
protected $walker;
/** @var EventDispatcher */
protected $dispatcher;
/** @var FilesFinder */
protected $filesFinder;
}