|
| 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 | +} |
0 commit comments