Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format based on [Keep a Changelog](https://keepachangelog.com)
and this project adheres to [Semantic Versioning](https://semver.org).

## [Unreleased]
- Added Block Manager to Allow Modification of C-Headers Via Advanced Components of Zephir


## [0.19.0] - 2025-05-13
### Added
Expand Down
55 changes: 55 additions & 0 deletions src/BlockManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/**
* This file is part of the Zephir.
*
* (c) Phalcon Team <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Zephir;

use InvalidArgumentException;

use function array_merge;

/**
* Manages the c-headers that must be added to a file
*/
class BlockManager
{
/**
* List of headers.
*/
protected array $blocks = [];

/**
* Adds a header path to the manager.
*
* @throws InvalidArgumentException
*/
#[SuppressWarnings("php:S4790")]
public function add(string $block): BlockManager
{
$this->blocks[md5($block)] = $block;

return $this;
}

/**
* Returns a set of headers merged.
*/
public function get(): array
{
return $this->blocks;
}

public function isEmpty(): bool
{
return empty($this->blocks);
}
}
4 changes: 4 additions & 0 deletions src/CompilationContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ class CompilationContext
* Global consecutive for try/catch blocks.
*/
public int $currentTryCatch = 0;
/**
* Represents the c-blocks added to the file.
*/
public ?BlockManager $blockManager = null;
/**
* Current cycle/loop block.
*/
Expand Down
12 changes: 9 additions & 3 deletions src/CompilerFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ final class CompilerFile implements FileInterface

private array $functionDefinitions = [];

private array $headerCBlocks = [];

private BlockManager $headerCBlocks;
/**
* Original internal representation (IR) of the file.
*/
Expand All @@ -101,6 +100,8 @@ public function __construct(
$this->logger = new NullLogger();
$this->aliasManager = $aliasManager;
$this->filesystem = $filesystem;

$this->headerCBlocks = new BlockManager();
}

/**
Expand Down Expand Up @@ -275,6 +276,11 @@ public function compile(Compiler $compiler, StringsManager $stringsManager): voi
*/
$compilationContext->headersManager = new HeadersManager();

/**
* C-Block manager.
*/
$compilationContext->blockManager = $this->headerCBlocks;

/**
* Main code-printer for the file.
*/
Expand Down Expand Up @@ -522,7 +528,7 @@ public function preCompile(Compiler $compiler): void
break;

case 'cblock':
$this->headerCBlocks[] = $topStatement['value'];
$this->headerCBlocks->add($topStatement['value']);
break;

case 'function':
Expand Down
9 changes: 8 additions & 1 deletion src/CompilerFileAnonymous.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ final class CompilerFileAnonymous implements FileInterface

protected ?string $compiledFile = null;
protected bool $external = false;
protected array $headerCBlocks = [];
protected BlockManager $headerCBlocks;
protected ?string $namespace = null;

/**
Expand All @@ -55,6 +55,8 @@ public function __construct(
protected ?CompilationContext $context = null
) {
$this->logger = new NullLogger();

$this->headerCBlocks = new BlockManager();
}

/**
Expand All @@ -81,6 +83,11 @@ public function compile(Compiler $compiler, StringsManager $stringsManager): voi
$compilationContext->backend = $compiler->backend;
$compilationContext->headersManager = new HeadersManager();

/**
* C-Block manager.
*/
$compilationContext->blockManager = $this->headerCBlocks;

/**
* Main code-printer for the file.
*/
Expand Down
7 changes: 5 additions & 2 deletions src/Traits/CompilerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,11 @@ protected function generateClassHeadersPost(
}
}

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

if (!$this->headerCBlocks->isEmpty()) {
$code .= implode(PHP_EOL, $this->headerCBlocks->get()) . PHP_EOL;
$code .= PHP_EOL;
}

/**
Expand Down
39 changes: 39 additions & 0 deletions tests/Zephir/BlockManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

/**
* This file is part of the Zephir.
*
* (c) Phalcon Team <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace Zephir\Test;

use PHPUnit\Framework\TestCase;
use Zephir\BlockManager;

final class BlockManagerTest extends TestCase
{
public function testBasics(): void
{
$testSubject = new BlockManager();

$testSubject->add('test1');
$testSubject->add('test2');
$testSubject->add('test1');

$this->assertFalse($testSubject->isEmpty());

$this->assertEquals(
[
'5a105e8b9d40e1329780d62ea2265d8a' => 'test1',
'ad0234829205b9033196ba818f7a872b' => 'test2',
],
$testSubject->get(),
);
}
}
Loading