Skip to content

Commit 9df387c

Browse files
committed
changed IndexGenerator strategy to parse all php project file
recursively
1 parent 82b40f2 commit 9df387c

8 files changed

Lines changed: 284 additions & 108 deletions

File tree

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
"require-dev": {
2525
"behat/behat": "~3.0",
2626
"peridot-php/peridot": "~1.15",
27-
"peridot-php/leo": "~1.4"
27+
"peridot-php/leo": "~1.4",
28+
"phpspec/prophecy": "^1.5",
29+
"peridot-php/peridot-prophecy-plugin": "~1.0"
2830
}
2931
}

peridot.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
use Evenement\EventEmitterInterface;
3+
use Peridot\Plugin\Prophecy\ProphecyPlugin;
4+
5+
return function (EventEmitterInterface $emitter) {
6+
$plugin = new ProphecyPlugin($emitter);
7+
};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
use Generator\FilesFinder;
4+
use Utils\PathResolver;
5+
use Entity\Project;
6+
use Entity\Index;
7+
use Prophecy\Argument;
8+
9+
describe('FilesFinder', function () {
10+
beforeEach(function () {
11+
$this->mock = $this->getProphet()->prophesize(PathResolver::class);
12+
$this->mock->getDirFilesRecursive(Argument::any())->willReturn([
13+
'/test/TestClass.php',
14+
'/some/AnotherFile.php',
15+
'/some/yaml/file.yml',
16+
'/composer.json',
17+
'/peridot.php'
18+
]);
19+
$this->mock->relative(Argument::any(), Argument::any())->will(function ($args) {
20+
return $args[1];
21+
});
22+
$this->files = new FilesFinder($this->mock->reveal());
23+
$this->project = new Project(new Index, "/project");
24+
});
25+
describe('->getProjectFiles()', function () {
26+
it('returns all php files from project', function () {
27+
expect($this->files->getProjectFiles($this->project))->to->be->equal([
28+
'/test/TestClass.php',
29+
'/some/AnotherFile.php',
30+
'/peridot.php'
31+
]);
32+
});
33+
});
34+
});

specs/utils/pathresolver.spec.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
use Utils\PathResolver;
4+
use Phine\Path\Path;
5+
6+
describe('PathResolver', function () {
7+
describe('->relative()', function () {
8+
beforeEach(function () {
9+
$this->path = new PathResolver(new Path);
10+
});
11+
it('returns path from parent', function () {
12+
expect(
13+
$this->path->relative('/path/root', '/path/root/folder/project')
14+
)->to->be->equal('folder/project');
15+
});
16+
it('returns path from parent with ./', function () {
17+
expect(
18+
$this->path->relative('/path/root/', '/path/root/folder/project', true)
19+
)->to->be->equal('./folder/project');
20+
});
21+
it('returns path from sibling', function () {
22+
expect(
23+
$this->path->relative('/path/root/another/project/inner/', '/path/root/folder/project')
24+
)->to->be->equal('../../../folder/project');
25+
});
26+
});
27+
});

src/Generator/FilesFinder.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Generator;
4+
5+
use Utils\PathResolver;
6+
use Entity\Project;
7+
8+
class FilesFinder
9+
{
10+
public function __construct(PathResolver $path)
11+
{
12+
$this->path = $path;
13+
}
14+
15+
public function getProjectFiles(Project $project)
16+
{
17+
return $this->filterFiles(
18+
$project,
19+
$this->path->getDirFilesRecursive(
20+
$project->getRootDir()
21+
)
22+
);
23+
}
24+
25+
public function getChangedProjectFiles(Project $project)
26+
{
27+
throw new \Exception("Not implemented yet");
28+
}
29+
30+
protected function filterFiles(Project $project, $files)
31+
{
32+
$projectFiles = [];
33+
foreach ($files as $file) {
34+
if (!preg_match('/\.php$/', $file)) {
35+
continue;
36+
}
37+
$projectFiles[] = $this->path->relative($project->getRootDir(), $file);
38+
}
39+
return $projectFiles;
40+
}
41+
/** @var PathResolver */
42+
private $path;
43+
}

src/Generator/IndexGenerator.php

Lines changed: 86 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -18,56 +18,6 @@ class IndexGenerator
1818
{
1919
const BEFORE_GENERATION = 'index.before_generation';
2020
const AFTER_GENERATION = 'index.after_generation';
21-
/**
22-
* Array of plugin classes
23-
* @var array
24-
*/
25-
public $plugins;
26-
27-
/**
28-
* Verbosity
29-
*
30-
* @var bool
31-
*/
32-
private $verbose;
33-
34-
/**
35-
*
36-
*
37-
* @var PathResolver
38-
*/
39-
protected $path;
40-
41-
/**
42-
* Object with Composer-specific functions
43-
*
44-
* @var Utils\ComposerUtils
45-
*/
46-
protected $composer;
47-
48-
/**
49-
* Object for work with class-information
50-
*
51-
* @var Utils\ClassUtils
52-
*/
53-
protected $class;
54-
55-
56-
/**
57-
*
58-
* @var LoggerInterface
59-
*/
60-
protected $logger;
61-
62-
63-
/**
64-
*
65-
* @var IndexProcessor
66-
*/
67-
protected $processor;
68-
69-
/** @var EventDispatcher */
70-
protected $dispatcher;
7121

7222
public function __construct(
7323
PathResolver $path,
@@ -76,6 +26,7 @@ public function __construct(
7626
LoggerInterface $logger,
7727
IndexProcessor $processor,
7828
EventDispatcher $dispatcher,
29+
FilesFinder $filesFinder,
7930
$verbose = false
8031
) {
8132
$this->path = $path;
@@ -86,18 +37,7 @@ public function __construct(
8637
$this->verbose = $verbose;
8738
$this->processor = $processor;
8839
$this->dispatcher = $dispatcher;
89-
}
90-
91-
public function getComposerUtils(){
92-
return $this->composer;
93-
}
94-
95-
public function getClassUtils(){
96-
return $this->classUtils;
97-
}
98-
99-
public function getNamespaceUtils(){
100-
return $this->namespaceUtils;
40+
$this->filesFinder = $filesFinder;
10141
}
10242

10343
public function generateIndex(Project $project)
@@ -108,22 +48,24 @@ public function generateIndex(Project $project)
10848
$index = $project->getIndex();
10949
$this->populateClassMapIndex($project);
11050

111-
$this->generateProjectIndex($index);
51+
$this->generateProjectIndex($project);
11252

11353
$this->dispatcher->dispatch(self::AFTER_GENERATION, $event);
11454

11555
return $index;
11656
}
11757

118-
public function generateProjectIndex(Index $index){
58+
public function generateProjectIndex(Project $project)
59+
{
11960
// You know what this mean
12061
gc_disable();
121-
$classMap = $index->getClassMap();
62+
$index = $project->getIndex();
12263
$globalTime = 0;
12364
$process = 0;
12465
$done = 0;
125-
$all = count($classMap);
126-
foreach($index->getClassMap() as $fqcn => $file) {
66+
$files = $this->filesFinder->getProjectFiles($project);
67+
$all = count($files);
68+
foreach ($files as $file) {
12769
$start = microtime(1);
12870
$this->processFile($index, $file, false, false);
12971
$end = microtime(1) - $start;
@@ -139,12 +81,15 @@ public function generateProjectIndex(Index $index){
13981
gc_enable();
14082
}
14183

142-
public function processFile(Index $index, $file,
143-
$rewrite=false, $createCache=true
144-
){
84+
public function processFile(
85+
Index $index,
86+
$file,
87+
$rewrite = false,
88+
$createCache = true
89+
) {
14590
$this->getLogger()
14691
->addInfo("processing $file");
147-
if($index->isParsed($file) && !$rewrite){
92+
if ($index->isParsed($file) && !$rewrite) {
14893
return;
14994
}
15095
$startParser = microtime(1);
@@ -160,30 +105,92 @@ public function processFile(Index $index, $file,
160105
$this->processFileNodes($index, $nodes);
161106
$index->addParsedFile($file);
162107
}
163-
public function processFileNodes(Index $index, $nodes){
108+
public function processFileNodes(Index $index, $nodes)
109+
{
164110
$this->getLogger()->addDebug('Processing nodes ' . count($nodes));
165-
foreach($nodes as $node){
166-
if($node instanceof ClassData){
111+
foreach ($nodes as $node) {
112+
if ($node instanceof ClassData) {
167113
$this->getLogger()->addDebug('Processing node ' . $node->fqcn->toString());
168114
$index->addFQCN($node->fqcn);
169115
$index->addClass($node);
170-
}
171-
elseif($node instanceof InterfaceData){
116+
} elseif ($node instanceof InterfaceData) {
172117
$this->getLogger()->addDebug('Processing node ' . $node->fqcn->toString());
173118
$index->addFQCN($node->fqcn);
174119
$index->addInterface($node);
175120
}
176121
}
177122
}
178123

179-
public function getLogger(){
124+
public function getLogger()
125+
{
180126
return $this->logger;
181127
}
182128

183-
protected function populateClassMapIndex(Project $project){
129+
protected function populateClassMapIndex(Project $project)
130+
{
184131
$classMap = $this->getComposerUtils()->getCanonicalClassMap($project->getRootDir());
185132
$index = $project->getIndex();
186133
$index->setClassMap($classMap);
187134
}
188-
}
189135

136+
public function getComposerUtils()
137+
{
138+
return $this->composer;
139+
}
140+
141+
public function getClassUtils()
142+
{
143+
return $this->classUtils;
144+
}
145+
146+
public function getNamespaceUtils()
147+
{
148+
return $this->namespaceUtils;
149+
}
150+
151+
/**
152+
* Verbosity
153+
*
154+
* @var bool
155+
*/
156+
private $verbose;
157+
158+
/**
159+
*
160+
*
161+
* @var PathResolver
162+
*/
163+
protected $path;
164+
165+
/**
166+
* Object with Composer-specific functions
167+
*
168+
* @var Utils\ComposerUtils
169+
*/
170+
protected $composer;
171+
172+
/**
173+
* Object for work with class-information
174+
*
175+
* @var Utils\ClassUtils
176+
*/
177+
protected $class;
178+
179+
/**
180+
*
181+
* @var LoggerInterface
182+
*/
183+
protected $logger;
184+
185+
/**
186+
*
187+
* @var IndexProcessor
188+
*/
189+
protected $processor;
190+
191+
/** @var EventDispatcher */
192+
protected $dispatcher;
193+
194+
/** @var FilesFinder */
195+
protected $filesFinder;
196+
}

0 commit comments

Comments
 (0)