Permalink
Browse files

added addUseClass method in ClassGenerator

  • Loading branch information...
1 parent 46d786f commit 868074569a15cccbbc7bf09b176bd211fdee28f9 @robertboloc committed Oct 8, 2012
@@ -67,6 +67,11 @@ class ClassGenerator extends AbstractGenerator
*/
protected $methods = array();
+ /**
+ * @var array Array of string names
+ */
+ protected $useClasses = array();
+
/**
* fromReflection() - build a Code Generation Php Object from a Class Reflection
*
@@ -479,6 +484,16 @@ public function addPropertyFromGenerator(PropertyGenerator $property)
return $this;
}
+ /**
+ * Add a class to "use" classes
+ *
+ * @param string $useClass
+ */
+ public function addUseClass($useClass)
+ {
+ $this->useClasses[] = $useClass;
+ }
+
/**
* getProperties()
*
@@ -505,6 +520,16 @@ public function getProperty($propertyName)
return false;
}
+ /**
+ * Returns the "use" classes
+ *
+ * @return array
+ */
+ public function getUseClasses()
+ {
+ return $this->useClasses;
+ }
+
/**
* hasProperty()
*
@@ -664,6 +689,14 @@ public function generate()
$output .= 'namespace ' . $namespace . ';' . self::LINE_FEED . self::LINE_FEED;
}
+ $useClasses = $this->getUseClasses();
+ if (!empty($useClasses)) {
+ foreach ($useClasses as $useClass) {
+ $output .= 'use ' . $useClass . ';' . self::LINE_FEED;
+ }
+ $output .= self::LINE_FEED;
+ }
+
if (null !== ($docBlock = $this->getDocBlock())) {
$docBlock->setIndentation('');
$output .= $docBlock->generate();
@@ -362,4 +362,19 @@ public function testPassingANamespacedClassnameShouldGenerateAClassnameWithoutIt
$received = $classGeneratorClass->generate();
$this->assertContains('class FunClass', $received, $received);
}
+
+ /**
+ * @group ZF2-151
+ */
+ public function testAddUseClass()
+ {
+ $classGenerator = new ClassGenerator();
+ $classGenerator->setName('My\Class');
+ $classGenerator->addUseClass('My\First\Use\Class');
+ $classGenerator->addUseClass('My\Second\Use\Class');
+ $generated = $classGenerator->generate();
+
+ $this->assertContains('use My\First\Use\Class;', $generated);
+ $this->assertContains('use My\Second\Use\Class;', $generated);
+ }
}

0 comments on commit 8680745

Please sign in to comment.