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

Commit cb5c5bb

Browse files
committed
Merge branch 'develop'
Merging develop to master in preparation for 2.6.0 release.
2 parents 2a69bd4 + 6ac8cd9 commit cb5c5bb

12 files changed

+689
-70
lines changed

CHANGELOG.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,33 @@
22

33
All notable changes to this project will be documented in this file, in reverse chronological order by release.
44

5+
## 2.6.0 - TBD
6+
7+
### Added
8+
9+
- [#12](https://github.com/zendframework/zend-code/pull/12) adds the ability to
10+
generate arrays using either long/standard syntax (`array(...)`) or short
11+
syntax (`[...]`). This can be accomplished by setting the value type to
12+
`ValueGenerator::TYPE_ARRAY_SHORT` instead of using `TYPE_ARRAY`.
13+
Additionally, you can use `TYPE_ARRAY_LONG` instead of `TYPE_ARRAY`; the two
14+
constants are synonyms.
15+
- [#11](https://github.com/zendframework/zend-code/pull/11) adds the ability to
16+
generate interfaces via the new class `Zend\Code\Generator\InterfaceGenerator`.
17+
18+
### Deprecated
19+
20+
- Nothing.
21+
22+
### Removed
23+
24+
- Nothing.
25+
26+
### Fixed
27+
28+
- [#20](https://github.com/zendframework/zend-code/pull/20) updates
29+
the zend-eventmanager dependency to `^2.6|^3.0`, and changes its
30+
internal usage to use the `triggerEventUntil()` signature.
31+
532
## 2.5.3 - 2015-11-18
633

734
### Added

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
},
1515
"require": {
1616
"php": ">=5.5",
17-
"zendframework/zend-eventmanager": "~2.5"
17+
"zendframework/zend-eventmanager": "^2.6|^3.0"
1818
},
1919
"require-dev": {
2020
"doctrine/common": ">=2.1",
21-
"zendframework/zend-stdlib": "~2.5",
21+
"zendframework/zend-stdlib": "~2.7",
2222
"fabpot/php-cs-fixer": "1.7.*",
2323
"phpunit/PHPUnit": "~4.0"
2424
},

src/Annotation/AnnotationManager.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ public function createAnnotation(array $annotationData)
9898
]);
9999

100100
$eventManager = $this->getEventManager();
101-
$results = $eventManager->trigger($event, function ($r) {
101+
$results = $eventManager->triggerEventUntil(function ($r) {
102102
return (is_object($r));
103-
});
103+
}, $event);
104104

105105
$annotation = $results->last();
106106

src/Generator/AbstractMemberGenerator.php

Lines changed: 18 additions & 0 deletions
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

Lines changed: 2 additions & 1 deletion
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;
Lines changed: 150 additions & 0 deletions
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

Lines changed: 10 additions & 1 deletion
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());
@@ -143,6 +145,9 @@ public static function fromArray(array $array)
143145
case 'final':
144146
$method->setFinal($value);
145147
break;
148+
case 'interface':
149+
$method->setInterface($value);
150+
break;
146151
case 'static':
147152
$method->setStatic($value);
148153
break;
@@ -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) {

src/Generator/ValueGenerator.php

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,22 @@ class ValueGenerator extends AbstractGenerator
1818
/**#@+
1919
* Constant values
2020
*/
21-
const TYPE_AUTO = 'auto';
22-
const TYPE_BOOLEAN = 'boolean';
23-
const TYPE_BOOL = 'bool';
24-
const TYPE_NUMBER = 'number';
25-
const TYPE_INTEGER = 'integer';
26-
const TYPE_INT = 'int';
27-
const TYPE_FLOAT = 'float';
28-
const TYPE_DOUBLE = 'double';
29-
const TYPE_STRING = 'string';
30-
const TYPE_ARRAY = 'array';
31-
const TYPE_CONSTANT = 'constant';
32-
const TYPE_NULL = 'null';
33-
const TYPE_OBJECT = 'object';
34-
const TYPE_OTHER = 'other';
21+
const TYPE_AUTO = 'auto';
22+
const TYPE_BOOLEAN = 'boolean';
23+
const TYPE_BOOL = 'bool';
24+
const TYPE_NUMBER = 'number';
25+
const TYPE_INTEGER = 'integer';
26+
const TYPE_INT = 'int';
27+
const TYPE_FLOAT = 'float';
28+
const TYPE_DOUBLE = 'double';
29+
const TYPE_STRING = 'string';
30+
const TYPE_ARRAY = 'array';
31+
const TYPE_ARRAY_SHORT = 'array_short';
32+
const TYPE_ARRAY_LONG = 'array_long';
33+
const TYPE_CONSTANT = 'constant';
34+
const TYPE_NULL = 'null';
35+
const TYPE_OBJECT = 'object';
36+
const TYPE_OTHER = 'other';
3537
/**#@-*/
3638

3739
const OUTPUT_MULTIPLE_LINE = 'multipleLine';
@@ -254,6 +256,8 @@ protected function getValidatedType($type)
254256
self::TYPE_DOUBLE,
255257
self::TYPE_STRING,
256258
self::TYPE_ARRAY,
259+
self::TYPE_ARRAY_SHORT,
260+
self::TYPE_ARRAY_LONG,
257261
self::TYPE_CONSTANT,
258262
self::TYPE_NULL,
259263
self::TYPE_OBJECT,
@@ -317,12 +321,21 @@ public function generate()
317321
$type = $this->getAutoDeterminedType($value);
318322
}
319323

320-
if ($type == self::TYPE_ARRAY) {
324+
$isArrayType = in_array($type, [self::TYPE_ARRAY, self::TYPE_ARRAY_LONG, self::TYPE_ARRAY_SHORT]);
325+
326+
if ($isArrayType) {
321327
foreach ($value as &$curValue) {
322328
if ($curValue instanceof self) {
323329
continue;
324330
}
325-
$curValue = new self($curValue, self::TYPE_AUTO, self::OUTPUT_MULTIPLE_LINE, $this->getConstants());
331+
332+
if (is_array($curValue)) {
333+
$newType = $type;
334+
} else {
335+
$newType = self::TYPE_AUTO;
336+
}
337+
338+
$curValue = new self($curValue, $newType, self::OUTPUT_MULTIPLE_LINE, $this->getConstants());
326339
}
327340
}
328341

@@ -348,7 +361,17 @@ public function generate()
348361
$output .= $value;
349362
break;
350363
case self::TYPE_ARRAY:
351-
$output .= 'array(';
364+
case self::TYPE_ARRAY_LONG:
365+
case self::TYPE_ARRAY_SHORT:
366+
if ($type == self::TYPE_ARRAY_SHORT) {
367+
$startArray = '[';
368+
$endArray = ']';
369+
} else {
370+
$startArray = 'array(';
371+
$endArray = ')';
372+
}
373+
374+
$output .= $startArray;
352375
if ($this->outputMode == self::OUTPUT_MULTIPLE_LINE) {
353376
$output .= self::LINE_FEED . str_repeat($this->indentation, $this->arrayDepth + 1);
354377
}
@@ -384,7 +407,7 @@ public function generate()
384407
}
385408
$output .= self::LINE_FEED . str_repeat($this->indentation, $this->arrayDepth);
386409
}
387-
$output .= ')';
410+
$output .= $endArray;
388411
break;
389412
case self::TYPE_OTHER:
390413
default:

0 commit comments

Comments
 (0)