1414use Parser \Processor \IndexProcessor ;
1515use Parser \Processor \ScopeProcessor ;
1616
17+
1718class ContentManager {
1819 public function __construct (
1920 Parser $ parser ,
2021 IndexGenerator $ generator ,
2122 ContextResolver $ contextResolver ,
22- ScopeResolver $ scopeResolver ,
2323 Completer $ completer ,
2424 IndexProcessor $ indexProcessor ,
2525 ScopeProcessor $ scopeProcessor
2626 ){
2727 $ this ->parser = $ parser ;
2828 $ this ->generator = $ generator ;
2929 $ this ->contextResolver = $ contextResolver ;
30- $ this ->scopeResolver = $ scopeResolver ;
3130 $ this ->completer = $ completer ;
3231 $ this ->indexProcessor = $ indexProcessor ;
3332 $ this ->scopeProcessor = $ scopeProcessor ;
33+ $ this ->cachePool = [];
3434 }
3535 public function createCompletion (
3636 Project $ project ,
@@ -39,6 +39,7 @@ public function createCompletion(
3939 $ column ,
4040 $ file
4141 ){
42+ $ start = microtime (1 );
4243 $ entries = [];
4344 if ($ line ){
4445 list ($ lines , $ badLine , $ completionLine ) = $ this ->prepareContent (
@@ -47,19 +48,24 @@ public function createCompletion(
4748 $ column
4849 );
4950 try {
50- $ this ->updateFileIndex ($ project , $ lines , $ file );
51- $ scope = $ this ->findScope (
52- $ project , implode ("\n" , $ lines ), $ line , $ file
53- );
51+ $ scope = $ this ->processFileContent ($ project , $ lines , $ line , $ file );
52+ echo microtime (1 ) - $ start ;
53+ echo " for indexing preparing \n" ;
54+ echo microtime (1 ) - $ start ;
55+ echo " for scope preparing \n" ;
5456 }
5557 catch (\Exception $ e ){
5658 $ scope = new Scope ;
5759 }
5860 $ entries = $ this ->findEntries ($ project , $ scope , $ completionLine , $ column , $ lines );
61+ echo microtime (1 ) - $ start ;
62+ echo " for entries preparing \n" ;
5963 }
6064 elseif (!empty ($ content )) {
6165 $ this ->updateFileIndex ($ project , $ content , $ file );
6266 }
67+ echo microtime (1 ) - $ start ;
68+ echo " seconds for creaeting completion \n" ;
6369
6470 return [
6571 "entries " => $ entries ,
@@ -70,9 +76,6 @@ protected function findEntries(Project $project, Scope $scope, $badLine, $column
7076 $ context = $ this ->contextResolver ->getContext ($ badLine , $ column );
7177 return $ this ->completer ->getEntries ($ project , $ context , $ scope );
7278 }
73- protected function findScope (Project $ project , $ content , $ line , $ file ){
74- return $ this ->scopeResolver ->findScope ($ project , $ content , $ line , $ file );
75- }
7679 /**
7780 * @TODO
7881 * Should check for bad lines
@@ -89,8 +92,12 @@ protected function prepareContent($content, $line, $column){
8992 $ lines [$ line -1 ] = "" ;
9093 return [$ lines , trim ($ badLine ), trim ($ completionLine )];
9194 }
92- protected function updateFileIndex (Project $ project , $ lines , $ file ){
93- //gc_disable();
95+
96+ /**
97+ *
98+ * @return Scope
99+ */
100+ protected function processFileContent (Project $ project , $ lines , $ line , $ file ){
94101 if (is_array ($ lines )){
95102 $ content = implode ("\n" , $ lines );
96103 }
@@ -104,23 +111,44 @@ protected function updateFileIndex(Project $project, $lines, $file){
104111 if (!$ fqcn ){
105112 return ;
106113 }
107- $ this ->indexProcessor ->clearResultNodes ();
108- $ parser = $ this ->parser ;
109- $ parser ->addProcessor ($ this ->indexProcessor );
110- $ nodes = $ parser ->parseContent ($ fqcn , $ file , $ content );
111- $ this ->generator ->processFileNodes (
112- $ project ->getIndex (),
113- $ fqcn ,
114- $ nodes
115- );
116- //gc_enable();
114+ if (!array_key_exists ($ file , $ this ->cachePool )){
115+ $ this ->cachePool [$ file ] = [0 , null , null ];
116+ }
117+ if ($ this ->isValidCache ($ file , $ content )){
118+ list ($ hash , $ indexNodes , $ scopeNodes ) = $ this ->cachePool [$ file ];
119+ }
120+ else {
121+ $ this ->indexProcessor ->clearResultNodes ();
122+ $ this ->scopeProcessor ->clearResultNodes ();
123+ $ this ->scopeProcessor ->setIndex ($ project ->getIndex ());
124+ $ this ->scopeProcessor ->setLine ($ line );
125+ $ parser = $ this ->parser ;
126+ $ parser ->addProcessor ($ this ->indexProcessor );
127+ $ parser ->addProcessor ($ this ->scopeProcessor );
128+ $ nodes = $ parser ->parseContent ($ fqcn , $ file , $ content );
129+ $ this ->generator ->processFileNodes (
130+ $ project ->getIndex (),
131+ $ fqcn ,
132+ $ nodes
133+ );
134+ $ scopeNodes = $ this ->scopeProcessor ->getResultNodes ();
135+ $ contentHash = hash ('sha1 ' , $ content );
136+ $ this ->cachePool [$ file ] = [$ contentHash , $ nodes , $ scopeNodes ];
137+ }
138+ return $ scopeNodes [0 ];
139+ }
140+
141+ private function isValidCache ($ file , $ content ){
142+ $ contentHash = hash ('sha1 ' , $ content );
143+ list ($ hash ) = $ this ->cachePool [$ file ];
144+ return $ hash === $ contentHash ;
117145 }
118146
119147 private $ parser ;
120148 private $ generator ;
121149 private $ contextResolver ;
122- private $ scopeResolver ;
123150 private $ completer ;
124151 private $ indexProcessor ;
125152 private $ scopeProcessor ;
153+ private $ cachePool ;
126154}
0 commit comments