Skip to content
Browse files

added addUseClass method in ClassGenerator

  • Loading branch information...
1 parent 17c7687 commit 4cf730f22127bc8793ffa8ee31197f2e7a178831 @robertboloc committed Oct 8, 2012
View
33 library/Zend/Code/Generator/ClassGenerator.php
@@ -68,6 +68,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
*
* @param ClassReflection $classReflection
@@ -480,6 +485,16 @@ public function addPropertyFromGenerator(PropertyGenerator $property)
}
/**
+ * Add a class to "use" classes
+ *
+ * @param string $useClass
+ */
+ public function addUseClass($useClass)
+ {
+ $this->useClasses[] = $useClass;
+ }
+
+ /**
* getProperties()
*
* @return PropertyGenerator[]
@@ -506,6 +521,16 @@ public function getProperty($propertyName)
}
/**
+ * Returns the "use" classes
+ *
+ * @return array
+ */
+ public function getUseClasses()
+ {
+ return $this->useClasses;
+ }
+
+ /**
* hasProperty()
*
* @param string $propertyName
@@ -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();
View
15 tests/ZendTest/Code/Generator/ClassGeneratorTest.php
@@ -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 4cf730f

Please sign in to comment.
Something went wrong with that request. Please try again.