public function getData() { $nsPieces = explode('\\', $this->class->getNamespace()); $nsPathes = array(); $nsStr = ""; foreach ($nsPieces as $n) { if (strlen($nsStr) > 0) { $nsStr .= '\\'; } $nsStr .= $n; $nsPathes[$n] = $nsStr; } return array("classDefinition" => $this->class, "compilerFile" => $this->compilerFile, "className" => $this->class->getName(), "classNamespace" => $this->class->getNamespace(), "fullName" => $this->class->getCompleteName(), "methods" => $this->class->getMethods(), "namespacePieces" => $nsPathes); }
/** * Build class * * @param ClassDefinition $class * @param string $indent * @return string */ protected function buildClass(ClassDefinition $class, $indent) { $source = <<<EOF <?php namespace {$class->getNamespace()}; EOF; $source .= new DocBlock($class->getDocBlock(), '') . "\n"; if ($class->isFinal()) { $source .= 'final '; } elseif ($class->isAbstract()) { $source .= 'abstract '; } $source .= $class->getType() . ' ' . $class->getName(); if ($class->getExtendsClass()) { $extendsClassDefinition = $class->getExtendsClassDefinition(); if (!$extendsClassDefinition) { throw new \RuntimeException('Class "' . $class->getName() . '" does not have a extendsClassDefinition'); } $source .= ' extends ' . ($extendsClassDefinition->isBundled() ? '' : '\\') . $extendsClassDefinition->getCompleteName(); } if ($implementedInterfaces = $class->getImplementedInterfaces()) { $interfaces = array_map(function ($val) { return '\\' . $val; }, $implementedInterfaces); $keyword = $class->getType() == 'interface' ? ' extends ' : ' implements '; $source .= $keyword . join(', ', $interfaces); } $source .= PHP_EOL . '{' . PHP_EOL; foreach ($class->getConstants() as $constant) { $source .= $this->buildConstant($constant, $indent) . PHP_EOL . PHP_EOL; } foreach ($class->getProperties() as $property) { $source .= $this->buildProperty($property, $indent) . PHP_EOL . PHP_EOL; } $source .= PHP_EOL; foreach ($class->getMethods() as $method) { if ($method->isInternal()) { continue; } $source .= $this->buildMethod($method, $class->getType() === 'interface', $indent) . "\n\n"; } return $source . '}' . PHP_EOL; }
/** * Checks if a class implements an interface * * @param ClassDefinition $classDefinition * @param ClassDefinition $interfaceDefinition * @throws CompilerException */ public function checkInterfaceImplements(ClassDefinition $classDefinition, ClassDefinition $interfaceDefinition) { foreach ($interfaceDefinition->getMethods() as $method) { if (!$classDefinition->hasMethod($method->getName())) { throw new CompilerException("Class " . $classDefinition->getCompleteName() . " must implement a method called: \"" . $method->getName() . "\" as requirement of interface: \"" . $interfaceDefinition->getCompleteName() . "\""); } if ($method->hasParameters()) { $implementedMethod = $classDefinition->getMethod($method->getName()); if ($implementedMethod->getNumberOfRequiredParameters() > $method->getNumberOfRequiredParameters() || $implementedMethod->getNumberOfParameters() < $method->getNumberOfParameters()) { throw new CompilerException("Class " . $classDefinition->getCompleteName() . "::" . $method->getName() . "() does not have the same number of required parameters in interface: \"" . $interfaceDefinition->getCompleteName() . "\""); } } } }
protected function buildClass(ClassDefinition $class) { $source = <<<EOF <?php namespace {$class->getNamespace()}; EOF; if ($class->isFinal()) { $source .= 'final '; } elseif ($class->isAbstract()) { $source .= 'abstract '; } $source .= $class->getType() . ' ' . $class->getName(); if ($extendsClassDefinition = $class->getExtendsClassDefinition()) { $source .= ' extends \\' . $extendsClassDefinition->getCompleteName(); } if ($implementedInterfaces = $class->getImplementedInterfaces()) { $interfaces = array_map(function ($val) { return '\\' . $val; }, $implementedInterfaces); $source .= ' implements ' . join(', ', $interfaces); } $source .= PHP_EOL . '{' . PHP_EOL; foreach ($class->getConstants() as $constant) { $source .= $this->buildConstant($constant) . PHP_EOL; } foreach ($class->getProperties() as $property) { $source .= $this->buildProperty($property) . PHP_EOL; } $source .= PHP_EOL; foreach ($class->getMethods() as $method) { $source .= $this->buildMethod($method, $class->getType() === 'interface') . "\n\n"; } return $source . '}' . PHP_EOL; }