Skip to content

Commit

Permalink
removing duplications from compiler files
Browse files Browse the repository at this point in the history
  • Loading branch information
niden committed Dec 1, 2023
1 parent c447b11 commit db5c742
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 83 deletions.
46 changes: 5 additions & 41 deletions src/CompilerFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Zephir\Exception\IllegalStateException;
use Zephir\Exception\ParseException;
use Zephir\FileSystem\FileSystemInterface;
use Zephir\Traits\CompilerTrait;

use function array_map;
use function count;
Expand All @@ -47,7 +48,6 @@
use function preg_match;
use function realpath;
use function sprintf;
use function str_repeat;
use function str_replace;
use function strpos;
use function strtolower;
Expand All @@ -64,6 +64,7 @@
*/
final class CompilerFile implements FileInterface
{
use CompilerTrait;
use LoggerAwareTrait;

/**
Expand Down Expand Up @@ -161,18 +162,7 @@ public function applyClassHeaders(CompilationContext $compilationContext): void
{
$classDefinition = $this->classDefinition;

$separators = str_repeat('../', count(explode('\\', $classDefinition->getCompleteName())) - 1);

$code = PHP_EOL;
$code .= '#ifdef HAVE_CONFIG_H' . PHP_EOL;
$code .= '#include "' . $separators . 'ext_config.h"' . PHP_EOL;
$code .= '#endif' . PHP_EOL;
$code .= PHP_EOL;

$code .= '#include <php.h>' . PHP_EOL;
$code .= '#include "' . $separators . 'php_ext.h"' . PHP_EOL;
$code .= '#include "' . $separators . 'ext.h"' . PHP_EOL;
$code .= PHP_EOL;
$code = $this->generateCodeHeadersPre($classDefinition);

if ('class' == $classDefinition->getType()) {
$code .= '#include <Zend/zend_operators.h>' . PHP_EOL;
Expand All @@ -181,24 +171,8 @@ public function applyClassHeaders(CompilationContext $compilationContext): void
} else {
$code .= '#include <Zend/zend_exceptions.h>' . PHP_EOL;
}
$code .= PHP_EOL;

$code .= '#include "kernel/main.h"' . PHP_EOL;

if ('class' == $classDefinition->getType()) {
foreach ($compilationContext->headersManager->get() as $header => $one) {
$code .= '#include "' . $header . '.h"' . PHP_EOL;
}
}

if (count($this->headerCBlocks) > 0) {
$code .= implode(PHP_EOL, $this->headerCBlocks) . PHP_EOL;
}

/**
* Prepend the required files to the header
*/
$compilationContext->codePrinter->preOutput($code);
$this->generateClassHeadersPost($code, $classDefinition, $compilationContext);
}

/**
Expand Down Expand Up @@ -399,17 +373,7 @@ public function compile(Compiler $compiler, StringsManager $stringsManager): voi

$completeName = $classDefinition->getCompleteName();

$path = str_replace('\\', DIRECTORY_SEPARATOR, strtolower($completeName));

$filePath = 'ext/' . $path . '.zep.c';
$filePathHeader = 'ext/' . $path . '.zep.h';

if (strpos($path, DIRECTORY_SEPARATOR)) {
$dirname = dirname($filePath);
if (!is_dir($dirname)) {
mkdir($dirname, 0755, true);
}
}
[$path, $filePath, $filePathHeader] = $this->calculatePaths($completeName);

/**
* If the file does not exist we create it for the first time
Expand Down
47 changes: 5 additions & 42 deletions src/CompilerFileAnonymous.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,17 @@
use Zephir\Class\Definition\Definition;
use Zephir\Code\Printer;
use Zephir\Compiler\FileInterface;
use Zephir\Traits\CompilerTrait;

use function count;
use function dirname;
use function explode;
use function file_exists;
use function file_put_contents;
use function hash_file;
use function implode;
use function is_dir;
use function md5;
use function mkdir;
use function str_repeat;
use function str_replace;
use function strpos;
use function strtolower;
Expand All @@ -44,6 +43,7 @@
*/
final class CompilerFileAnonymous implements FileInterface
{
use CompilerTrait;
use LoggerAwareTrait;

protected Definition $classDefinition;
Expand Down Expand Up @@ -114,17 +114,7 @@ public function compile(Compiler $compiler, StringsManager $stringsManager): voi

$completeName = $this->classDefinition->getCompleteName();

$path = str_replace('\\', DIRECTORY_SEPARATOR, strtolower($completeName));

$filePath = 'ext/' . $path . '.zep.c';
$filePathHeader = 'ext/' . $path . '.zep.h';

if (strpos($path, DIRECTORY_SEPARATOR)) {
$dirname = dirname($filePath);
if (!is_dir($dirname)) {
mkdir($dirname, 0755, true);
}
}
[$path, $filePath, $filePathHeader] = $this->calculatePaths($completeName);

/**
* If the file does not exist we create it for the first time
Expand Down Expand Up @@ -225,39 +215,12 @@ private function compileClass(CompilationContext $compilationContext): void
*/
$classDefinition->compile($compilationContext);

$separators = str_repeat('../', count(explode('\\', $classDefinition->getCompleteName())) - 1);

$code = PHP_EOL;
$code .= '#ifdef HAVE_CONFIG_H' . PHP_EOL;
$code .= '#include "' . $separators . 'ext_config.h"' . PHP_EOL;
$code .= '#endif' . PHP_EOL;
$code .= PHP_EOL;

$code .= '#include <php.h>' . PHP_EOL;
$code .= '#include "' . $separators . 'php_ext.h"' . PHP_EOL;
$code .= '#include "' . $separators . 'ext.h"' . PHP_EOL;
$code .= PHP_EOL;
$code = $this->generateCodeHeadersPre($classDefinition);

$code .= '#include <Zend/zend_operators.h>' . PHP_EOL;
$code .= '#include <Zend/zend_exceptions.h>' . PHP_EOL;
$code .= '#include <Zend/zend_interfaces.h>' . PHP_EOL;
$code .= PHP_EOL;

$code .= '#include "kernel/main.h"' . PHP_EOL;

if ('class' == $classDefinition->getType()) {
foreach ($compilationContext->headersManager->get() as $header => $one) {
$code .= '#include "' . $header . '.h"' . PHP_EOL;
}
}

if (count($this->headerCBlocks) > 0) {
$code .= implode(PHP_EOL, $this->headerCBlocks) . PHP_EOL;
}

/**
* Prepend the required files to the header
*/
$compilationContext->codePrinter->preOutput($code);
$this->generateClassHeadersPost($code, $classDefinition, $compilationContext);
}
}

0 comments on commit db5c742

Please sign in to comment.