Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Added addUse method in ClassGenerator #2702

Closed
wants to merge 5 commits into from

3 participants

@robertboloc

Also added a getUses() method.
Issue #2453

@weierophinney weierophinney was assigned
@weierophinney

This looks good -- one question, however: shouldn't it support aliasing? Something like:

$generator->setUseClass('Foo\Bar\Baz', 'BarBaz');

which would generate:

use Foo\Bar\Baz as BarBaz;
@robertboloc

I added the aliasing support

@ralphschindler
Collaborator

I think I'd prefer the api not talk about classes. More succinctly it would just be getUses(), addUse(), etc. The current use case is to add support for traits, which I am not sure are, taxonomically speaking, are close enough related to classes to call them such. Thoughts?

@robertboloc

I agree, I'll refactor the method names and variables :)

@weierophinney

@robertboloc thanks!

@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney Merge branch 'feature/zf2-151' into develop
Close #2702
8ce645b
@weierophinney weierophinney referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
36 library/Zend/Code/Generator/ClassGenerator.php
@@ -68,6 +68,11 @@ class ClassGenerator extends AbstractGenerator
protected $methods = array();
/**
+ * @var array Array of string names
+ */
+ protected $uses = array();
+
+ /**
* fromReflection() - build a Code Generation Php Object from a Class Reflection
*
* @param ClassReflection $classReflection
@@ -480,6 +485,19 @@ public function addPropertyFromGenerator(PropertyGenerator $property)
}
/**
+ * Add a class to "use" classes
+ *
+ * @param string $useClass
+ */
+ public function addUse($use, $useAlias = null)
+ {
+ if (!empty($useAlias)) {
+ $use .= ' as ' . $useAlias;
+ }
+ $this->uses[] = $use;
+ }
+
+ /**
* getProperties()
*
* @return PropertyGenerator[]
@@ -506,6 +524,16 @@ public function getProperty($propertyName)
}
/**
+ * Returns the "use" classes
+ *
+ * @return array
+ */
+ public function getUses()
+ {
+ return $this->uses;
+ }
+
+ /**
* hasProperty()
*
* @param string $propertyName
@@ -664,6 +692,14 @@ public function generate()
$output .= 'namespace ' . $namespace . ';' . self::LINE_FEED . self::LINE_FEED;
}
+ $uses = $this->getUses();
+ if (!empty($uses)) {
+ foreach ($uses as $use) {
+ $output .= 'use ' . $use . ';' . 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 testAddUses()
+ {
+ $classGenerator = new ClassGenerator();
+ $classGenerator->setName('My\Class');
+ $classGenerator->addUse('My\First\Use\Class');
+ $classGenerator->addUse('My\Second\Use\Class', 'MyAlias');
+ $generated = $classGenerator->generate();
+
+ $this->assertContains('use My\First\Use\Class;', $generated);
+ $this->assertContains('use My\Second\Use\Class as MyAlias;', $generated);
+ }
}
Something went wrong with that request. Please try again.