Skip to content

Commit 224345c

Browse files
committed
added events for index generation and project loading
1 parent 0d5a06c commit 224345c

6 files changed

Lines changed: 95 additions & 13 deletions

File tree

src/Application/HTTP/App.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
use Command\ErrorCommand;
88
use DI\Container;
99
use Application\BaseApplication;
10+
use Symfony\Component\EventDispatcher\EventDispatcher;
1011

1112
class App extends BaseApplication
1213
{
13-
1414
public function __construct($noFsIO)
1515
{
1616
$this->router = new Router;
1717
parent::__construct($noFsIO);
18+
$this->dispatcher = $this->container->get(EventDispatcher::class);
1819
}
1920
public function handle($request, $response, $data)
2021
{
@@ -25,7 +26,11 @@ public function handle($request, $response, $data)
2526
protected function getArguments($request, $response, $data)
2627
{
2728
$arguments = $this->parseQuery($request->getQuery(), $data);
28-
$arguments["project"] = $this->loadProject($arguments);
29+
$project = $this->loadProject($arguments);
30+
$this->dispatcher->dispatch('project.load', new ProjectLoadEvent(
31+
$project
32+
));
33+
$arguments["project"] = $project;
2934
return $arguments;
3035
}
3136
protected function setResponseHeaders($response)
@@ -88,4 +93,6 @@ protected function createEmptyProject($rootDir)
8893
$project = new Project(new Index, $rootDir);
8994
return $project;
9095
}
96+
97+
private $dispatcher;
9198
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Application\HTTP;
4+
5+
use Symfony\Component\EventDispatcher\Event;
6+
7+
class ProjectLoadEvent extends Event
8+
{
9+
public function __construct($project)
10+
{
11+
$this->project = $project;
12+
}
13+
14+
public $project;
15+
}

src/Entity/Index.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
use Entity\Node\InterfaceData;
66
use Entity\Node\ClassData;
77

8-
class Index {
8+
class Index
9+
{
910
private $fqcns = [];
1011
private $classes = [];
1112
private $interfaces = [];
@@ -15,14 +16,16 @@ class Index {
1516
private $implements = [];
1617
private $parsedFiles = [];
1718

18-
public function getFQCNs(){
19+
public function getFQCNs()
20+
{
1921
return $this->fqcns;
2022
}
2123

2224
/**
2325
* @return FQCN
2426
*/
25-
public function findFQCNByFile($file){
27+
public function findFQCNByFile($file)
28+
{
2629
if(!array_key_exists($file, $this->flippedClassMap)){
2730
return null;
2831
}

src/Entity/Project.php

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,51 @@
22

33
namespace Entity;
44

5-
class Project{
5+
class Project
6+
{
67
private $index;
78
private $rootFolder;
9+
private $plugins = [];
810

9-
public function __construct(Index $index, $rootFolder = ""){
11+
public function __construct(Index $index, $rootFolder = "")
12+
{
1013
$this->index = $index;
1114
$this->rootFolder = $rootFolder;
1215
}
13-
public function getRootFolder(){
16+
public function getRootFolder()
17+
{
1418
return $this->rootFolder;
1519
}
16-
public function getRootDir(){
20+
public function getRootDir()
21+
{
1722
return $this->getRootFolder();
1823
}
1924

2025
/**
2126
* Returns project's index
2227
* @return Index
2328
*/
24-
public function getIndex(){
29+
public function getIndex()
30+
{
2531
return $this->index;
2632
}
27-
public function setIndex(Index $index){
33+
public function setIndex(Index $index)
34+
{
2835
$this->index = $index;
2936
}
37+
public function getPlugins()
38+
{
39+
return $this->plugins;
40+
}
41+
public function addPlugin($key, $plugin)
42+
{
43+
$this->plugins[$key] = $plugin;
44+
}
45+
public function getPlugin($key)
46+
{
47+
if (array_key_exists($key, $this->plugins)) {
48+
return $this->plugins[$key];
49+
}
50+
return [];
51+
}
3052
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Generator;
4+
5+
use Symfony\Component\EventDispatcher\Event;
6+
use Entity\Project;
7+
8+
class IndexGenerationEvent extends Event
9+
{
10+
public function __construct(Project $project)
11+
{
12+
$this->project = $project;
13+
}
14+
15+
/** @return Project */
16+
public function getProject()
17+
{
18+
return $this->project;
19+
}
20+
21+
/** @var Project */
22+
private $project;
23+
}

src/Generator/IndexGenerator.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@
1212
use Utils\ClassUtils;
1313
use Psr\Log\LoggerInterface;
1414
use Parser\Processor\IndexProcessor;
15+
use Symfony\Component\EventDispatcher\EventDispatcher;
1516

1617
class IndexGenerator
1718
{
19+
const BEFORE_GENERATION = 'index.before_generation';
20+
const AFTER_GENERATION = 'index.after_generation';
1821
/**
1922
* Array of plugin classes
2023
* @var array
@@ -63,22 +66,26 @@ class IndexGenerator
6366
*/
6467
protected $processor;
6568

69+
/** @var EventDispatcher */
70+
protected $dispatcher;
71+
6672
public function __construct(
6773
PathResolver $path,
6874
Composer $composer,
6975
ClassUtils $class,
7076
LoggerInterface $logger,
7177
IndexProcessor $processor,
78+
EventDispatcher $dispatcher,
7279
$verbose = false
73-
)
74-
{
80+
) {
7581
$this->path = $path;
7682
$this->composer = $composer;
7783
$this->classUtils = $class;
7884
$this->logger = $logger;
7985
$this->plugins = array();
8086
$this->verbose = $verbose;
8187
$this->processor = $processor;
88+
$this->dispatcher = $dispatcher;
8289
}
8390

8491
public function getComposerUtils(){
@@ -95,11 +102,16 @@ public function getNamespaceUtils(){
95102

96103
public function generateIndex(Project $project)
97104
{
105+
$event = new IndexGenerationEvent($project);
106+
$this->dispatcher->dispatch(self::BEFORE_GENERATION, $event);
107+
98108
$index = $project->getIndex();
99109
$this->populateClassMapIndex($project);
100110

101111
$this->generateProjectIndex($index);
102112

113+
$this->dispatcher->dispatch(self::AFTER_GENERATION, $event);
114+
103115
return $index;
104116
}
105117

0 commit comments

Comments
 (0)