Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit ac387b6

Browse files
committed
Generate interfaces
1 parent ddd5942 commit ac387b6

6 files changed

+493
-2
lines changed

src/Generator/AbstractMemberGenerator.php

+18
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ abstract class AbstractMemberGenerator extends AbstractGenerator
1717
const FLAG_ABSTRACT = 0x01;
1818
const FLAG_FINAL = 0x02;
1919
const FLAG_STATIC = 0x04;
20+
const FLAG_INTERFACE = 0x08;
2021
const FLAG_PUBLIC = 0x10;
2122
const FLAG_PROTECTED = 0x20;
2223
const FLAG_PRIVATE = 0x40;
@@ -101,6 +102,23 @@ public function isAbstract()
101102
return (bool) ($this->flags & self::FLAG_ABSTRACT);
102103
}
103104

105+
/**
106+
* @param bool $isInterface
107+
* @return AbstractMemberGenerator
108+
*/
109+
public function setInterface($isInterface)
110+
{
111+
return (($isInterface) ? $this->addFlag(self::FLAG_INTERFACE) : $this->removeFlag(self::FLAG_INTERFACE));
112+
}
113+
114+
/**
115+
* @return bool
116+
*/
117+
public function isInterface()
118+
{
119+
return (bool) ($this->flags & self::FLAG_INTERFACE);
120+
}
121+
104122
/**
105123
* @param bool $isFinal
106124
* @return AbstractMemberGenerator

src/Generator/ClassGenerator.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
class ClassGenerator extends AbstractGenerator
1515
{
1616
const OBJECT_TYPE = "class";
17+
const IMPLEMENTS_KEYWORD = "implements";
1718

1819
const FLAG_ABSTRACT = 0x01;
1920
const FLAG_FINAL = 0x02;
@@ -955,7 +956,7 @@ public function generate()
955956
$implemented = $this->getImplementedInterfaces();
956957

957958
if (!empty($implemented)) {
958-
$output .= ' implements ' . implode(', ', $implemented);
959+
$output .= ' ' . static::IMPLEMENTS_KEYWORD . ' ' . implode(', ', $implemented);
959960
}
960961

961962
$output .= self::LINE_FEED . '{' . self::LINE_FEED . self::LINE_FEED;

src/Generator/InterfaceGenerator.php

+150
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @link http://github.com/zendframework/zf2 for the canonical source repository
6+
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license http://framework.zend.com/license/new-bsd New BSD License
8+
*/
9+
10+
namespace Zend\Code\Generator;
11+
12+
use Zend\Code\Reflection\ClassReflection;
13+
14+
class InterfaceGenerator extends ClassGenerator
15+
{
16+
const OBJECT_TYPE = 'interface';
17+
const IMPLEMENTS_KEYWORD = 'extends';
18+
19+
/**
20+
* Build a Code Generation Php Object from a Class Reflection
21+
*
22+
* @param ClassReflection $classReflection
23+
* @return InterfaceGenerator
24+
*/
25+
public static function fromReflection(ClassReflection $classReflection)
26+
{
27+
if (!$classReflection->isInterface()) {
28+
throw new Exception\InvalidArgumentException(sprintf(
29+
'Class %s is not a interface',
30+
$classReflection->getName()
31+
));
32+
}
33+
34+
// class generator
35+
$cg = new static($classReflection->getName());
36+
$methods = [];
37+
38+
$cg->setSourceContent($cg->getSourceContent());
39+
$cg->setSourceDirty(false);
40+
41+
if ($classReflection->getDocComment() != '') {
42+
$cg->setDocBlock(DocBlockGenerator::fromReflection($classReflection->getDocBlock()));
43+
}
44+
45+
// set the namespace
46+
if ($classReflection->inNamespace()) {
47+
$cg->setNamespaceName($classReflection->getNamespaceName());
48+
}
49+
50+
foreach ($classReflection->getMethods() as $reflectionMethod) {
51+
$className = ($cg->getNamespaceName())
52+
? $cg->getNamespaceName() . '\\' . $cg->getName()
53+
: $cg->getName();
54+
55+
if ($reflectionMethod->getDeclaringClass()->getName() == $className) {
56+
$methods[] = MethodGenerator::fromReflection($reflectionMethod);
57+
}
58+
}
59+
60+
foreach ($classReflection->getConstants() as $name => $value) {
61+
$cg->addConstant($name, $value);
62+
}
63+
64+
$cg->addMethods($methods);
65+
66+
return $cg;
67+
}
68+
69+
/**
70+
* Generate from array
71+
*
72+
* @configkey name string [required] Class Name
73+
* @configkey filegenerator FileGenerator File generator that holds this class
74+
* @configkey namespacename string The namespace for this class
75+
* @configkey docblock string The docblock information
76+
* @configkey constants
77+
* @configkey methods
78+
*
79+
* @throws Exception\InvalidArgumentException
80+
* @param array $array
81+
* @return InterfaceGenerator
82+
*/
83+
public static function fromArray(array $array)
84+
{
85+
if (! isset($array['name'])) {
86+
throw new Exception\InvalidArgumentException(
87+
'Class generator requires that a name is provided for this object'
88+
);
89+
}
90+
91+
$cg = new static($array['name']);
92+
foreach ($array as $name => $value) {
93+
// normalize key
94+
switch (strtolower(str_replace(['.', '-', '_'], '', $name))) {
95+
case 'containingfile':
96+
$cg->setContainingFileGenerator($value);
97+
break;
98+
case 'namespacename':
99+
$cg->setNamespaceName($value);
100+
break;
101+
case 'docblock':
102+
$docBlock = ($value instanceof DocBlockGenerator) ? $value : DocBlockGenerator::fromArray($value);
103+
$cg->setDocBlock($docBlock);
104+
break;
105+
case 'methods':
106+
$cg->addMethods($value);
107+
break;
108+
case 'constants':
109+
$cg->addConstants($value);
110+
break;
111+
}
112+
}
113+
114+
return $cg;
115+
}
116+
117+
/**
118+
* {@inheritdoc}
119+
*/
120+
public function addPropertyFromGenerator(PropertyGenerator $property)
121+
{
122+
return $this;
123+
}
124+
125+
/**
126+
* {@inheritdoc}
127+
*/
128+
public function addMethodFromGenerator(MethodGenerator $method)
129+
{
130+
$method->setInterface(true);
131+
132+
return parent::addMethodFromGenerator($method);
133+
}
134+
135+
/**
136+
* {@inheritdoc}
137+
*/
138+
public function setExtendedClass($extendedClass)
139+
{
140+
return $this;
141+
}
142+
143+
/**
144+
* {@inheritdoc}
145+
*/
146+
public function setAbstract($isAbstract)
147+
{
148+
return $this;
149+
}
150+
}

src/Generator/MethodGenerator.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ class MethodGenerator extends AbstractMemberGenerator
3434
*/
3535
public static function fromReflection(MethodReflection $reflectionMethod)
3636
{
37-
$method = new static();
37+
$method = new static();
38+
$declaringClass = $reflectionMethod->getDeclaringClass();
3839

3940
$method->setSourceContent($reflectionMethod->getContents(false));
4041
$method->setSourceDirty(false);
@@ -53,6 +54,7 @@ public static function fromReflection(MethodReflection $reflectionMethod)
5354
$method->setVisibility(self::VISIBILITY_PUBLIC);
5455
}
5556

57+
$method->setInterface($declaringClass->isInterface());
5658
$method->setStatic($reflectionMethod->isStatic());
5759

5860
$method->setName($reflectionMethod->getName());
@@ -142,6 +144,9 @@ public static function fromArray(array $array)
142144
break;
143145
case 'final':
144146
$method->setFinal($value);
147+
case 'interface':
148+
$method->setInterface($value);
149+
break;
145150
break;
146151
case 'static':
147152
$method->setStatic($value);
@@ -294,6 +299,10 @@ public function generate()
294299
return $output . ';';
295300
}
296301

302+
if ($this->isInterface()) {
303+
return $output . ';';
304+
}
305+
297306
$output .= self::LINE_FEED . $indent . '{' . self::LINE_FEED;
298307

299308
if ($this->body) {

0 commit comments

Comments
 (0)