コード例 #1
0
ファイル: PiBX_CodeGen.php プロジェクト: rocksolid-tn/pibx
 /**
  * Creating an object of PiBX_CodeGen starts the code generation.
  *
  * A valid schema-file has to be passed.
  * Options can be passed as an associative array with a value of a boolean "true".
  * This way the code generation can be customized.
  * 
  * Possible option-values are (keys of "$options"):
  *     - "typechecks" enables the generation of type-check code into setter methods.
  * 
  * @param string $schemaFile
  * @param array $options
  */
 public function __construct($schemaFile, $options)
 {
     $typeUsage = new PiBX_CodeGen_TypeUsage();
     // phase 1
     echo "Parsing schema file '{$schemaFile}'\n";
     $parser = new PiBX_CodeGen_SchemaParser($schemaFile, $typeUsage);
     $parsedTree = $parser->parse();
     // phase 2
     echo "Creating abstract syntax tree\n";
     $creator = new PiBX_CodeGen_ASTCreator($typeUsage);
     $parsedTree->accept($creator);
     $typeList = $creator->getTypeList();
     // phase 3
     print "Optimizing abstract syntax tree\n";
     print "    Before: " . count($typeList) . " type(s)\n";
     $usages = $typeUsage->getTypeUsages();
     $optimizer = new PiBX_CodeGen_ASTOptimizer($typeList, $typeUsage);
     $typeList = $optimizer->optimize();
     print "    After:  " . count($typeList) . " type(s)\n";
     // phase 4
     print "Creating binding.xml\n";
     $b = new PiBX_Binding_Creator();
     foreach ($typeList as &$type) {
         $type->accept($b);
     }
     file_put_contents('./output/binding.xml', $b->getXml());
     // phase 5
     print "Generating classes to: ./output\n";
     $generator = new PiBX_CodeGen_ClassGenerator();
     if ($options['typechecks'] === true) {
         $generator->enableTypeChecks();
     }
     foreach ($typeList as &$type) {
         $type->accept($generator);
     }
     if (!is_dir('output')) {
         mkdir('output');
     }
     foreach ($generator->getClasses() as $className => $classCode) {
         $code = "<?php\n" . $classCode;
         file_put_contents('output/' . $className . '.php', $code);
     }
 }
コード例 #2
0
 public function testEasyPoBinding()
 {
     $filepath = dirname(__FILE__) . '/../../_files/EasyPO/';
     $schemaFile = $filepath . '/easypo.xsd';
     $schema = simplexml_load_file($schemaFile);
     $bindingFile = file_get_contents($filepath . '/binding.xml');
     $typeUsage = new PiBX_CodeGen_TypeUsage();
     $parser = new PiBX_CodeGen_SchemaParser($schemaFile, $typeUsage);
     $parsedTree = $parser->parse();
     $creator = new PiBX_CodeGen_ASTCreator($typeUsage);
     $parsedTree->accept($creator);
     $typeList = $creator->getTypeList();
     $usages = $typeUsage->getTypeUsages();
     $optimizer = new PiBX_CodeGen_ASTOptimizer($typeList, $typeUsage);
     $typeList = $optimizer->optimize();
     $b = new PiBX_Binding_Creator();
     foreach ($typeList as &$type) {
         $type->accept($b);
     }
     $this->assertEquals($bindingFile, $b->getXml());
 }
コード例 #3
0
 public function testClassGenerationWithTypeChecks()
 {
     $filepath = dirname(__FILE__) . '/../../_files/EasyPO';
     $schemaFile = $filepath . '/easypo.xsd';
     $bindingFile = $filepath . '/binding.xml';
     $customerFile = $filepath . '/Customer_TypeChecked.php';
     $lineItemFile = $filepath . '/LineItem_TypeChecked.php';
     $purchaseOrderFile = $filepath . '/PurchaseOrder_TypeChecked.php';
     $shipperFile = $filepath . '/Shipper_TypeChecked.php';
     $typeUsage = new PiBX_CodeGen_TypeUsage();
     // most of this test-case follows the flow of PiBX_CodeGen
     // phase 1
     $parser = new PiBX_CodeGen_SchemaParser($schemaFile, $typeUsage);
     $parsedTree = $parser->parse();
     // phase 2
     $creator = new PiBX_CodeGen_ASTCreator($typeUsage);
     $parsedTree->accept($creator);
     $typeList = $creator->getTypeList();
     // phase 3
     $usages = $typeUsage->getTypeUsages();
     $optimizer = new PiBX_CodeGen_ASTOptimizer($typeList, $typeUsage);
     $typeList = $optimizer->optimize();
     // phase 4
     $b = new PiBX_Binding_Creator();
     foreach ($typeList as &$type) {
         $type->accept($b);
     }
     $this->assertEquals(file_get_contents($bindingFile), $b->getXml());
     // phase 5
     $generator = new PiBX_CodeGen_ClassGenerator();
     $generator->enableTypeChecks();
     foreach ($typeList as &$type) {
         $type->accept($generator);
     }
     $classes = $generator->getClasses();
     $this->assertEquals(4, count($classes));
     $this->assertEquals(file_get_contents($customerFile), "<?php\n" . $classes['Customer']);
     $this->assertEquals(file_get_contents($lineItemFile), "<?php\n" . $classes['LineItem']);
     $this->assertEquals(file_get_contents($purchaseOrderFile), "<?php\n" . $classes['PurchaseOrder']);
     $this->assertEquals(file_get_contents($shipperFile), "<?php\n" . $classes['Shipper']);
 }
コード例 #4
0
 public function testScenarioBooksClassesWithTypeChecks()
 {
     $filepath = dirname(__FILE__) . '/../_files/Books';
     $schemaFile = $filepath . '/books.xsd';
     $bindingFile = $filepath . '/binding.xml';
     $collectionFile = $filepath . '/Collection_TypeChecked.php';
     $bookTypeFile = $filepath . '/BookType_TypeChecked.php';
     $typeUsage = new PiBX_CodeGen_TypeUsage();
     // most of this test-case follows the flow of PiBX_CodeGen
     // phase 1
     $parser = new PiBX_CodeGen_SchemaParser($schemaFile, $typeUsage);
     $parsedTree = $parser->parse();
     // phase 2
     $creator = new PiBX_CodeGen_ASTCreator($typeUsage);
     $parsedTree->accept($creator);
     $typeList = $creator->getTypeList();
     // phase 3
     $usages = $typeUsage->getTypeUsages();
     $optimizer = new PiBX_CodeGen_ASTOptimizer($typeList, $typeUsage);
     $typeList = $optimizer->optimize();
     // phase 4
     $b = new PiBX_Binding_Creator();
     foreach ($typeList as &$type) {
         $type->accept($b);
     }
     $this->assertEquals(file_get_contents($bindingFile), $b->getXml());
     // phase 5
     $generator = new PiBX_CodeGen_ClassGenerator();
     $generator->enableTypeChecks();
     foreach ($typeList as &$type) {
         $type->accept($generator);
     }
     $classes = $generator->getClasses();
     $this->assertEquals(2, count($classes));
     $this->assertEquals(file_get_contents($collectionFile), "<?php\n" . $classes['Collection']);
     $this->assertEquals(file_get_contents($bookTypeFile), "<?php\n" . $classes['BookType']);
 }