/** * Creates class code for given file descriptor * * @param FileDescriptor $file File descriptor * @param CodeStringBuffer $buffer Buffer to write code to * @param string|null $outputFile Output filename * * @return null */ private function _createClassFile(FileDescriptor $file, CodeStringBuffer $buffer, $outputFile = null, $phpNamespace = "") { $comment = new CommentStringBuffer(self::TAB, self::EOL); $date = strftime("%Y-%m-%d %H:%M:%S"); $comment->append('Auto generated from ' . basename($file->getName()) . ' at ' . $date); if ($file->getPackage()) { $comment->newline()->append($file->getPackage() . ' package'); } $buffer->append($comment); foreach ($file->getEnums() as $descriptor) { $this->_createEnum($descriptor, $buffer); } foreach ($file->getMessages() as $descriptor) { $this->_createClass($descriptor, $buffer, $phpNamespace); } $requiresString = ''; foreach ($file->getDependencies() as $dependency) { $requiresString .= sprintf('require_once \'%s\';', $this->_createOutputFilename($dependency->getName())); } $buffer->append($requiresString); if ($outputFile == null) { $outputFile = $this->_createOutputFilename($file->getName()); } if ($phpNamespace) { $namespaceDeclaration = " namespace {$phpNamespace};"; } else { $namespaceDeclaration = ""; } file_put_contents($outputFile, '<?php' . $namespaceDeclaration . PHP_EOL . $buffer); }
/** * Creates class code for given file descriptor * * @param FileDescriptor $file File descriptor * @param CodeStringBuffer $buffer Buffer to write code to * @param string|null $outputFile Output filename * * @return null */ private function _createClassFile(FileDescriptor $file, CodeStringBuffer $buffer, $outputFile = null) { $comment = new CommentStringBuffer(self::TAB, self::EOL); date_default_timezone_set("PRC"); $date = strftime("%Y-%m-%d %H:%M:%S"); $comment->append('Auto generated from ' . basename($file->getName()) . ' at ' . $date); if ($file->getPackage()) { $comment->newline()->append($file->getPackage() . ' package'); } $buffer->append($comment); foreach ($file->getEnums() as $name => $descriptor) { $this->_createEnum($descriptor, $buffer); } foreach ($file->getMessages() as $name => $descriptor) { $this->_createClass($descriptor, $buffer); } if ($descriptor = $file->getService()) { $this->_createService($file, $descriptor, $buffer); } $requiresString = ''; foreach ($file->getDependencies() as $dependency) { $requiresString .= sprintf('require_once \'%s\';', $this->_createOutputFilename($dependency->getName())); } if ($this->_useNativeNamespaces && !empty($requiresString)) { $requiresString = 'namespace {' . PHP_EOL . $requiresString . PHP_EOL . '}'; } $buffer->append($requiresString); if ($outputFile == null) { $outputFile = $this->_createOutputFilename($file->getName()); } file_put_contents($outputFile, '<?php' . PHP_EOL . $buffer); }
/** * Creates class code for given file descriptor * * @param FileDescriptor $file File descriptor * @param CodeStringBuffer $buffer Buffer to write code to * @param string|null $outputFile Output filename * * @return string|null $compiledFilename if one is created */ private function _createClassFile(FileDescriptor $file, CodeStringBuffer $buffer, $outputFile = null) { $comment = new CommentStringBuffer(self::TAB, self::EOL); $date = strftime("%Y-%m-%d %H:%M:%S"); $comment->append('Auto generated from ' . basename($file->getName()) . ' at ' . $date); if ($file->getPackage()) { $comment->newline()->append($file->getPackage() . ' package'); } if ($this->getSavePsrOutput()) { $this->_comment = $comment; } $buffer->append($comment); foreach ($file->getEnums() as $descriptor) { $this->_createEnum($descriptor, $buffer); } foreach ($file->getMessages() as $descriptor) { $this->_createClass($descriptor, $buffer); } // The PSR format doesn't use require_once dependency strings // so we can exit early if ($this->getSavePsrOutput()) { return null; } $requiresString = ''; foreach ($file->getDependencies() as $dependency) { $requiresString .= sprintf('require_once \'%s\';' . PHP_EOL, $dependency->getCompiledFilename()); } if ($this->_useNativeNamespaces && !empty($requiresString)) { $requiresString = 'namespace {' . PHP_EOL . $requiresString . PHP_EOL . '}'; } $buffer->append($requiresString); if ($outputFile == null) { $outputFile = $this->_createOutputFilename($file->getName()); } $compiledFilename = ''; if ($this->_shouldCompileInPlace) { $compiledFilename = dirname($file->getName()) . '/' . $outputFile; } else { $compiledFilename = $this->getTargetDir() . $outputFile; } file_put_contents($compiledFilename, '<?php' . PHP_EOL . $buffer); return $compiledFilename; }