public function generatePHP(array $imports = array()) { if (!isset($this->gClass)) { throw new ClassWritingException('Klasse nicht gesetzt. (zuerst setClass() dann write().'); } $use = NULL; /* merge mit params */ foreach ($imports as $iClass) { $alias = NULL; if (is_array($iClass)) { list($iClass, $alias) = $iClass; } if (!$iClass instanceof GClass) { throw new \Psc\Exception('Imports können nur vom typ GClass sein'); } $this->addImport($iClass, $alias); } if (count($this->foundImports) > 0) { $useImports = array(); foreach ($this->foundImports as $alias => $gClass) { // wir filten nach den imports, die im selben Namespace sind und keinen besonderen Alias haben if ($alias !== $gClass->getClassName() || $gClass->getNamespace() !== $this->gClass->getNamespace()) { $useImports[$alias] = $gClass; } } /* Render PHP */ $classAlias = function ($gClass, $alias) { if ($alias === $gClass->getClassName()) { return $gClass->getFQN(); } else { return $gClass->getFQN() . ' AS ' . $alias; } }; if (count($useImports) > 0) { $use .= "\n"; // abstand zu namespace (neue Zeile) if ($this->useStyle === self::USE_STYLE_LINES) { $use .= A::joinc($useImports, "use %s;\n", $classAlias); } else { $use .= 'use ' . A::implode($useImports, ",\n ", $classAlias); $use .= ';'; $use .= "\n"; // abstand zu class (neue Zeile) } } } $php = TPL::miniTemplate($this->template, array('class' => $this->gClass->php(), 'use' => $use, 'namespace' => $this->gClass->getNamespace() != NULL ? 'namespace ' . ltrim($this->gClass->getNamespace(), '\\') . ';' : NULL)); return $php; }
public function testWithGClass() { $gClass = new GClass('\\ePaper42\\Entities\\Journal'); $gClass->setParentClass(new GClass('\\Psc\\XML\\Object')); $gClass->createProperty('title')->createDocBlock("Titel des Objektes (Name der Zeitschrift)\n")->addSimpleAnnotation('xml:XmlElement'); $gClass->createProperty('key')->createDocBlock("Key des Objektes\n")->addSimpleAnnotation('xml:XmlElement'); $gClass->createProperty('volumes')->createDocBlock("Volumes des Journals (Jahreszahlen)\n")->addSimpleAnnotation('xml:XmlElement', array('name' => 'volumne', 'wrapper' => 'volumeList', 'type' => 'ePaper42\\Entities\\Volume')); $classCode = <<<'CLASS_END' class Journal extends \Psc\XML\Object { /** * Titel des Objektes (Name der Zeitschrift) * * @xml:XmlElement */ protected $title; /** * Key des Objektes * * @xml:XmlElement */ protected $key; /** * Volumes des Journals (Jahreszahlen) * * @xml:XmlElement(name="volumne", wrapper="volumeList", type="ePaper42\Entities\Volume") */ protected $volumes; } CLASS_END; //file_put_contents('D:\fixture.txt',$classCode); //file_put_contents('D:\actual.txt',$gClass->php()); $this->assertEquals($classCode, $gClass->php()); }
/** * @TODO test interfaces und extends! */ public function testClass() { $cr = "\n"; $class = GClass::factory(__NAMESPACE__ . '\\TestClass'); $this->assertInstanceOf('Psc\\Code\\Generate\\GMethod', $class->getMethod('method2')); $this->assertInstanceOf('Psc\\Code\\Generate\\GProperty', $class->getProperty('prop1')); $gClass = new GClass('Psc\\Code\\Generate\\GClass'); $this->assertEquals('GClass', $gClass->getClassName()); $this->assertEquals('\\Psc\\Code\\Generate', $gClass->getNamespace()); $this->assertEquals('Psc\\Code\\Generate\\GClass', $gClass->getFQN()); /* test final + abstract zeuch */ $this->assertFalse($gClass->isFinal()); $this->assertFalse($gClass->isAbstract()); $gClass->setFinal(TRUE); $this->assertTrue($gClass->isFinal()); $gClass->setAbstract(TRUE); $this->assertTrue($gClass->isAbstract()); $gClass->setModifier(GClass::MODIFIER_ABSTRACT, FALSE); $gClass->setModifier(GClass::MODIFIER_FINAL, FALSE); $this->assertFalse($gClass->isAbstract()); $this->assertFalse($gClass->isFinal()); /* testClass (denn da wissen wir die line-nummern besser und die ist auch abstract */ $testClass = new GClass(new ReflectionClass(__NAMESPACE__ . '\\TestClass')); $this->assertTrue($testClass->isAbstract()); $this->assertFalse($testClass->isFinal()); $this->assertEquals(self::$startLine, $testClass->getStartLine()); $this->assertEquals(self::$endLine, $testClass->getEndLine()); $testHint = new GClass('SomeClassForAHint'); $this->assertEquals('class SomeClassForAHint {' . $cr . '}', $testHint->php(), sprintf("output: '%s'", $testHint->php())); //file_put_contents('D:\fixture.txt', self::$testClassCode); //file_put_contents('D:\compiled.txt',$testClass->php()); $this->assertEquals(self::$testClassCode, $testClass->php(), 'Code für Klasse ist nicht identisch'); //identisch bis auf whitespaces! (das ist irgendwie ein bissl variabel, aber okay // geiler wäre halt assertEqualsCode, hmmm das ginge sogar mit token_get_all und so? }
public function testDocBlock() { $gClass = new GClass('Psc\\XML\\TestObjects\\Bla'); $gClass->createDocBlock()->addSimpleAnnotation('xml:XmlObject'); $classCode = <<<'CLASS_END' /** * @xml:XmlObject */ class Bla { } CLASS_END; $this->assertEquals($classCode, $gClass->php()); }