Overview

Namespaces

  • None
  • Peg
    • Cli
      • Commands
    • Config
    • Custom
      • Command
        • Action
          • Generate
          • Parse
      • CommandLine
      • Config
      • Localization
      • Utilities
    • Lib
      • Definitions
        • Element
      • Generator
      • Lexers
      • Plugins
      • Signals
        • Data
          • Definitions
          • Lexers
        • Type
  • PHP

Classes

  • Exporter
  • Importer
  • StandardType
  • Symbols
  • Type
  • Overview
  • Namespace
  • Class
  • Tree
  • Todo
  1: <?php
  2: /**
  3:  * @author Jefferson González
  4:  * @license MIT
  5:  * @link http://github.com/peg-org/peg-src Source code.
  6:  */
  7: 
  8: namespace Peg\Lib\Definitions;
  9: 
 10: use Peg\Lib\Utilities\Json;
 11: 
 12: /**
 13:  * Loads cached definitions into a symbols object.
 14:  */
 15: class Importer extends \Peg\Lib\Signals\Signal
 16: {
 17:     /**
 18:      * Reference to the symbols object that is going to be 
 19:      * populated by the importer.
 20:      * @var \Peg\Lib\Definitions\Symbols 
 21:      */
 22:     public $symbols;
 23:     
 24:     /**
 25:      * Path where reside the cached files.
 26:      * @var string
 27:      */
 28:     public $definitions_path;
 29:     
 30:     /**
 31:      * Format used to load the symbols.
 32:      * @see \Peg\Lib\Definitions\Type
 33:      * @var string
 34:      */
 35:     public $import_type;
 36:     
 37:     /**
 38:      * Data that is send each time a signal is launched.
 39:      * @var \Peg\Lib\Signals\Data\Definitions\ImportMessage 
 40:      */
 41:     private $signal_data;
 42:     
 43:     /**
 44:      * Constructor.
 45:      * @param \Peg\Lib\Definitions\Symbols $symbols The table to populate.
 46:      * @param string $path The path where resides the cached files.
 47:      * @param string $import_type The type of cache files to import.
 48:      */
 49:     public function __construct(
 50:         \Peg\Lib\Definitions\Symbols &$symbols,
 51:         $path = null, 
 52:         $import_type=\Peg\Lib\Definitions\Type::JSON
 53:     )
 54:     {
 55:         $this->symbols =& $symbols;
 56:         $this->definitions_path = $path;
 57:         $this->import_type = $import_type;
 58:         $this->signal_data = new \Peg\Lib\Signals\Data\Definitions\ImportMessage;
 59:     }
 60:     
 61:     /**
 62:      * Begin importing definitions to the symbols object specified on constructor.
 63:      */
 64:     public function Start()
 65:     {
 66:         if(!file_exists($this->definitions_path))
 67:         {
 68:             throw new \Exception(
 69:                 t("Trying to import symbols from a non existent directory.")
 70:             );
 71:         }
 72:         
 73:         $this->SendMessage(
 74:             sprintf(
 75:                 t("Starting import of definitions stored in %s format."),
 76:                 $this->import_type
 77:             )
 78:         );
 79:         
 80:         if($this->import_type == Type::JSON)
 81:         {
 82:             $this->LoadFromJSON($this->definitions_path);
 83:         }
 84:         else
 85:         {
 86:             $this->LoadFromPHP($this->definitions_path);
 87:         }
 88:         
 89:         $this->SendMessage(t("Import completed."));
 90:     }
 91:     
 92:     /**
 93:      * Load all kind of symbols from php files previously created
 94:      * by \Peg\Lib\Definitions\Exporter.
 95:      * @param string $path Directory where the php files reside.
 96:      */
 97:     private function LoadFromPHP($path)
 98:     {
 99:         $this->definitions_path = rtrim($path, "/\\") . "/";
100:         $this->import_type = Type::PHP;
101:         
102:         // This variable is used by the php definition files to reference
103:         // this object and populate it with elements.
104:         $symbols =& $this->symbols;
105:         
106:         if(file_exists($this->definitions_path . "constants.php"))
107:         {
108:             $this->SendMessage(t("Loading constants.php"));
109:             include($this->definitions_path . "constants.php");
110:         }
111:         
112:         if(file_exists($this->definitions_path . "enumerations.php"))
113:         {
114:             $this->SendMessage(t("Loading enumerations.php"));
115:             include($this->definitions_path . "enumerations.php");
116:         }
117:         
118:         if(file_exists($this->definitions_path . "type_definitions.php"))
119:         {
120:             $this->SendMessage(t("Loading type_definitions.php"));
121:             include($this->definitions_path . "type_definitions.php");
122:         }
123:         
124:         if(file_exists($this->definitions_path . "variables.php"))
125:         {
126:             $this->SendMessage(t("Loading variables.php"));
127:             include($this->definitions_path . "variables.php");
128:         }
129:         
130:         if(file_exists($this->definitions_path . "functions.php"))
131:         {
132:             $this->SendMessage(t("Loading functions.php"));
133:             include($this->definitions_path . "functions.php");
134:         }
135:         
136:         if(file_exists($this->definitions_path . "classes.php"))
137:         {
138:             $this->SendMessage(t("Loading classes.php"));
139:             include($this->definitions_path . "classes.php");
140:         }
141:     }
142: 
143:     /**
144:      * Load all kind of symbols from json files previously created
145:      * by \Peg\Lib\Definitions\Exporter.
146:      * @param string $path Directory where the json files reside.
147:      */
148:     private function LoadFromJSON($path)
149:     {
150:         $this->definitions_path = rtrim($path, "/\\") . "/";
151:         $this->import_type = Type::JSON;
152: 
153:         if(file_exists($this->definitions_path . "constants.json"))
154:         {
155:             $this->SendMessage(t("Loading constants.json"));
156:             $this->LoadConstantsFromJson();
157:         }
158:         
159:         if(file_exists($this->definitions_path . "enumerations.json"))
160:         {
161:             $this->SendMessage(t("Loading enumerations.json"));
162:             $this->LoadEnumerationsFromJson();
163:         }
164:         
165:         if(file_exists($this->definitions_path . "type_definitions.json"))
166:         {
167:             $this->SendMessage(t("Loading type_definitions.json"));
168:             $this->LoadTypeDefFromJson();
169:         }
170:         
171:         if(file_exists($this->definitions_path . "variables.json"))
172:         {
173:             $this->SendMessage(t("Loading variables.json"));
174:             $this->LoadGlobalVariablesFromJson();
175:         }
176:         
177:         if(file_exists($this->definitions_path . "functions.json"))
178:         {
179:             $this->SendMessage(t("Loading functions.json"));
180:             $this->LoadFunctionsFromJson();
181:         }
182:         
183:         if(file_exists($this->definitions_path . "classes.json"))
184:         {
185:             $this->SendMessage(t("Loading classes.json"));
186:             $this->LoadClassesFromJson();
187:         }
188:     }
189: 
190:     /**
191:      * Load all constant symbols from constants.json
192:      */
193:     private function LoadConstantsFromJson()
194:     {
195:         $constants_def = Json::Decode(
196:             file_get_contents($this->definitions_path . "constants.json")
197:         );
198: 
199:         foreach($constants_def as $header => $namespaces)
200:         {
201:             $this->symbols->AddHeader($header);
202:             
203:             foreach($namespaces as $namespace => $constants)
204:             {
205:                 foreach($constants as $constant_name => $constant_data)
206:                 {
207:                     if(!isset($constant_data["description"]))
208:                         $constant_data["description"] = "";
209:                     
210:                     $constant = new Element\Constant(
211:                         $constant_name,
212:                         $constant_data["value"],
213:                         $constant_data["description"]
214:                     );
215: 
216:                     $this->symbols->headers[$header]->AddConstant(
217:                         $constant,
218:                         $namespace
219:                     );
220:                 }
221:             }
222:         }
223: 
224:         unset($constants_def);
225:     }
226: 
227:     /**
228:      * Load all enumeration symbols from enumerations.json
229:      */
230:     private function LoadEnumerationsFromJson()
231:     {
232:         $enumerations_def = Json::Decode(
233:             file_get_contents($this->definitions_path . "enumerations.json")
234:         );
235: 
236:         foreach($enumerations_def as $header => $namespaces)
237:         {
238:             $this->symbols->AddHeader($header);
239:             
240:             foreach($namespaces as $namespace => $enumerations)
241:             {
242:                 foreach($enumerations as $enumeration_name => $enumeration_data)
243:                 {
244:                     if(!isset($enumeration_data["description"]))
245:                         $enumeration_data["description"] = "";
246:                     
247:                     $enumeration = new Element\Enumeration(
248:                         $enumeration_name,
249:                         $enumeration_data["options"],
250:                         $enumeration_data["description"]
251:                     );
252: 
253:                     $this->symbols->headers[$header]->AddEnumeration(
254:                         $enumeration,
255:                         $namespace
256:                     );
257:                 }
258:             }
259:         }
260: 
261:         unset($enumerations_def);
262:     }
263: 
264:     /**
265:      * Load all typedef symbols from type_definitions.json
266:      */
267:     private function LoadTypeDefFromJson()
268:     {
269:         $typedef_def = Json::Decode(file_get_contents(
270:             $this->definitions_path . "type_definitions.json")
271:         );
272: 
273:         foreach($typedef_def as $header => $namespaces)
274:         {
275:             $this->symbols->AddHeader($header);
276:             
277:             foreach($namespaces as $namespace => $typedefs)
278:             {
279:                 foreach($typedefs as $typedef_name => $typedef_data)
280:                 {
281:                     if(!isset($typedef_data["description"]))
282:                         $typedef_data["description"] = "";
283:                     
284:                     $typedef = new Element\TypeDef(
285:                         $typedef_name,
286:                         $typedef_data["type"],
287:                         $typedef_data["description"]
288:                     );
289: 
290:                     $this->symbols->headers[$header]->AddTypeDef(
291:                         $typedef,
292:                         $namespace
293:                     );
294:                 }
295:             }
296:         }
297: 
298:         unset($typedef_def);
299:     }
300:     
301:     /**
302:      * Load all global variable symbols from variables.json
303:      */
304:     private function LoadGlobalVariablesFromJson()
305:     {
306:         $variables_def = Json::Decode(file_get_contents(
307:             $this->definitions_path . "variables.json")
308:         );
309: 
310:         foreach($variables_def as $header => $namespaces)
311:         {
312:             $this->symbols->AddHeader($header);
313:             
314:             foreach($namespaces as $namespace => $variables)
315:             {
316:                 foreach($variables as $variable_name => $variable_data)
317:                 {
318:                     if(!isset($variable_data["description"]))
319:                         $variable_data["description"] = "";
320:                     
321:                     $variable = new Element\GlobalVariable(
322:                         $variable_name,
323:                         $variable_data["type"],
324:                         $variable_data["description"]
325:                     );
326: 
327:                     $this->symbols->headers[$header]->AddGlobalVariable(
328:                         $variable,
329:                         $namespace
330:                     );
331:                 }
332:             }
333:         }
334: 
335:         unset($variables_def);
336:     }
337:     
338:     /**
339:      * Load all function symbols from functions.json
340:      */
341:     private function LoadFunctionsFromJson()
342:     {
343:         $functions_def = Json::Decode(
344:             file_get_contents(
345:                 $this->definitions_path . "functions.json"
346:             )
347:         );
348: 
349:         foreach($functions_def as $header => $namespaces)
350:         {
351:             $this->symbols->AddHeader($header);
352:             
353:             foreach($namespaces as $namespace => $functions)
354:             {
355:                 foreach($functions as $function_name => $function_overloads)
356:                 {
357:                     $function = new Element\FunctionElement($function_name);
358:                     
359:                     foreach($function_overloads as $index=>$function_overload)
360:                     {
361:                         $overload = new Element\Overload(
362:                             $function_overload["description"]
363:                         );
364:                         
365:                         $overload->SetReturnType(
366:                             new Element\ReturnType(
367:                                 $function_overload["return_type"]
368:                             )
369:                         );
370:                         
371:                         if(isset($function_overload["parameters"]))
372:                         {
373:                             foreach($function_overload["parameters"] as $parameter)
374:                             {
375:                                 if(!isset($parameter["value"]))
376:                                     $parameter["value"] = "";
377:                                 
378:                                 if(!isset($parameter["description"]))
379:                                     $parameter["description"] = "";
380: 
381:                                 $param = new Element\Parameter(
382:                                     $parameter["name"], 
383:                                     $parameter["type"], 
384:                                     $parameter["value"],
385:                                     $parameter["description"]
386:                                 );
387:                                 
388:                                 if(!isset($parameter["is_array"]))
389:                                     $param->is_array = false;
390:                                 else
391:                                     $param->is_array = $parameter["is_array"];
392:                                 
393:                                 $overload->AddParameter($param);
394:                             }
395:                         }
396:                         
397:                         $function->AddOverload($overload);
398:                     }
399:                     
400:                     $this->symbols->headers[$header]->AddFunction(
401:                         $function,
402:                         $namespace
403:                     );
404:                 }
405:             }
406:         }
407: 
408:         unset($functions_def);
409:     }
410:     
411:     /**
412:      * Load all class symbols from classes.json, its enumerations from
413:      * class_enumerations.json and variables from class_variables.json.
414:      */
415:     private function LoadClassesFromJson()
416:     {
417:         $classes_def = Json::Decode(
418:             file_get_contents(
419:                 $this->definitions_path . "classes.json"
420:             )
421:         );
422:         
423:         $enumerations_def = array();
424:         
425:         if(file_exists($this->definitions_path . "class_enumerations.json"))
426:         {
427:             $enumerations_def = Json::Decode(
428:                 file_get_contents(
429:                     $this->definitions_path . "class_enumerations.json"
430:                 )
431:             );
432:         }
433:         
434:         $variables_def = array();
435:         
436:         if(file_exists($this->definitions_path . "class_variables.json"))
437:         {
438:             $variables_def = Json::Decode(
439:                 file_get_contents(
440:                     $this->definitions_path . "class_variables.json"
441:                 )
442:             );
443:         }
444: 
445:         foreach($classes_def as $header => $namespaces)
446:         {
447:             $this->symbols->AddHeader($header);
448:             
449:             foreach($namespaces as $namespace => $classes)
450:             {
451:                 foreach($classes as $class_name => $methods)
452:                 {   
453:                     $class = new Element\ClassElement(
454:                         $class_name
455:                     );
456:                     
457:                     // Set class details
458:                     if(isset($methods["_description"]))
459:                     {
460:                         $class->description = $methods["_description"];
461:                         unset($methods["_description"]);
462:                     }
463:                     
464:                     if(isset($methods["_parents"]))
465:                     {
466:                         $class->AddParents($methods["_parents"]);
467:                         unset($methods["_parents"]);
468:                     }
469:                     
470:                     if(isset($methods["_struct"]))
471:                     {
472:                         $class->struct = true;
473:                         unset($methods["_struct"]);
474:                     }
475:                     
476:                     if(isset($methods["_forward_declaration"]))
477:                     {
478:                         $class->forward_declaration = true;
479:                         unset($methods["_forward_declaration"]);
480:                     }
481:                     
482:                     if(isset($methods["_platforms"]))
483:                     {
484:                         $class->platforms = $methods["_platforms"];
485:                         unset($methods["_platforms"]);
486:                     }
487:                     
488:                     // Add methods
489:                     foreach($methods as $method_name => $method_overloads)
490:                     {
491:                         $method = new Element\FunctionElement($method_name);
492:                         
493:                         foreach($method_overloads as $method_overload)
494:                         {
495:                             $overload = new Element\Overload(
496:                                 $method_overload["description"]
497:                             );
498: 
499:                             $overload->SetReturnType(
500:                                 new Element\ReturnType(
501:                                     $method_overload["return_type"]
502:                                 )
503:                             );
504:                             
505:                             if(isset($method_overload["constant"]))
506:                                 $overload->constant = $method_overload["constant"];
507: 
508:                             if(isset($method_overload["static"]))
509:                                 $overload->static = $method_overload["static"];
510: 
511:                             if(isset($method_overload["virtual"]))
512:                                 $overload->virtual = $method_overload["virtual"];
513: 
514:                             if(isset($method_overload["pure_virtual"]))
515:                                 $overload->pure_virtual = $method_overload["pure_virtual"];
516: 
517:                             if(isset($method_overload["protected"]))
518:                                 $overload->protected = $method_overload["protected"];
519: 
520:                             if(isset($method_overload["parameters"]))
521:                             {
522:                                 foreach($method_overload["parameters"] as $parameter)
523:                                 {
524:                                     if(!isset($parameter["value"]))
525:                                         $parameter["value"] = "";
526:                                     
527:                                     if(!isset($parameter["description"]))
528:                                         $parameter["description"] = "";
529: 
530:                                     $param = new Element\Parameter(
531:                                         $parameter["name"], 
532:                                         $parameter["type"], 
533:                                         $parameter["value"],
534:                                         $parameter["description"]
535:                                     );
536: 
537:                                     if(isset($parameter["is_array"]))
538:                                     {
539:                                         $param->is_array = true;
540:                                     }
541: 
542:                                     $overload->AddParameter($param);
543:                                 }
544:                             }
545: 
546:                             $method->AddOverload($overload);
547:                         }
548:                         
549:                         $class->AddMethod($method);
550:                     }
551:                     
552:                     // Add enumerations
553:                     if(isset($enumerations_def[$header][$namespace][$class_name]))
554:                     {
555:                         foreach($enumerations_def[$header][$namespace][$class_name] as $enumeration_name=>$enumeration_data)
556:                         {
557:                             if(!isset($enumeration_data["description"]))
558:                                 $enumeration_data["description"] = "";
559:                             
560:                             $class->AddEnumeration(
561:                                 new Element\Enumeration(
562:                                     $enumeration_name,
563:                                     $enumeration_data["options"],
564:                                     $enumeration_data["description"]
565:                                 )
566:                             );
567:                         }
568:                     }
569:                     
570:                     // Add variables
571:                     if(isset($variables_def[$header][$namespace][$class_name]))
572:                     {
573:                         foreach($variables_def[$header][$namespace][$class_name] as $variable_name=>$variable_options)
574:                         {
575:                             $variable = new Element\ClassVariable(
576:                                 $variable_name, 
577:                                 $variable_options["type"]
578:                             );
579:                             
580:                             if(isset($variable_options["static"]))
581:                                 $variable->static = $variable_options["static"];
582: 
583:                             if(isset($variable_options["mutable"]))
584:                                 $variable->mutable = $variable_options["mutable"];
585: 
586:                             if(isset($variable_options["protected"]))
587:                                 $variable->protected = $variable_options["protected"];
588: 
589:                             if(isset($variable_options["public"]))
590:                                 $variable->public = $variable_options["public"];
591:                             
592:                             if(isset($variable_options["description"]))
593:                                 $variable->description = $variable_options["description"];
594:                             
595:                             $class->AddVariable($variable);
596:                         }
597:                     }
598:                     
599:                     $this->symbols->headers[$header]->AddClass(
600:                         $class,
601:                         $namespace
602:                     );
603:                 }
604:             }
605:         }
606: 
607:         unset($classes_def);
608:         unset($enumerations_def);
609:         unset($variables_def);
610:     }
611:     
612:     /**
613:      * Sends a signal with message of current task being performed.
614:      * @param string $message
615:      */
616:     private function SendMessage($message)
617:     {
618:         $this->signal_data->message = $message;
619:         
620:         $this->Send(
621:             \Peg\Lib\Signals\Type\Definitions::IMPORT_MESSAGE,
622:             $this->signal_data
623:         );
624:     }
625: 
626: }
PEG Api API documentation generated by ApiGen 2.8.0