Skip to content

Develop #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 3, 2023
Merged
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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ composer require okapi/code-transformer
- [Limitations](#limitations)
- [How it works](#how-it-works)
- [Testing](#testing)
- [TODO](#todo)



Expand Down Expand Up @@ -331,6 +332,18 @@ Give a ⭐ if this project helped you!



## TODO

- Add support for Production/Development environments:
- Production: Cache will not be checked for updates (better performance)
- Development: Cache will be checked for updates (better debugging experience)

- Create a flowchart (WIP)

- Cache lifetime



## 🙏 Thanks

- Big thanks to [lisachenko](https://github.com/lisachenko) for their pioneering
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "okapi/code-transformer",
"description": "PHP Code Transformer is a PHP library that allows you to modify and transform the source code of a loaded PHP class.",
"version": "1.3.1",
"version": "1.3.2",
"type": "library",
"homepage": "https://github.com/okapi-web/php-code-transformer",
"license": "MIT",
Expand Down
18 changes: 17 additions & 1 deletion src/CodeTransformerKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ public function __construct() {}
*/
protected bool $debug = false;

/**
* Throw an exception if the kernel is initialized twice.
* <br><b>Default:</b> {@link false}<br>
*
* If {@link false}, any subsequent call to {@link init()} will be
* ignored.
*
* @var bool
*/
protected bool $throwExceptionOnDoubleInitialization = false;

// endregion

/**
Expand Down Expand Up @@ -115,7 +126,12 @@ public static function init(): void
static::ensureNotKernelNamespace();

$instance = static::getInstance();
$instance->ensureNotInitialized();

if ($instance->throwExceptionOnDoubleInitialization) {
$instance->ensureNotInitialized();
} elseif ($instance->initialized) {
return;
}

// Initialize the services
$instance->preInit();
Expand Down
34 changes: 34 additions & 0 deletions tests/Functional/AlreadyInitializedKernelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Okapi\CodeTransformer\Tests\Functional;

use Okapi\CodeTransformer\Tests\Stubs\Kernel\EmptyKernel;
use Okapi\CodeTransformer\Tests\Stubs\Kernel\ExceptionOnDoubleInitializationKernel;
use Okapi\CodeTransformer\Tests\Util;
use Okapi\Singleton\Exceptions\AlreadyInitializedException;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;
use PHPUnit\Framework\TestCase;

#[RunTestsInSeparateProcesses]
class AlreadyInitializedKernelTest extends TestCase
{
public function testInitializedKernelTwice(): void
{
Util::clearCache();

EmptyKernel::init();
EmptyKernel::init();

$this->assertTrue(true);
}

public function testInitializeKernelTwiceWithExceptionOnDoubleInitializationOption(): void
{
Util::clearCache();

$this->expectException(AlreadyInitializedException::class);

ExceptionOnDoubleInitializationKernel::init();
ExceptionOnDoubleInitializationKernel::init();
}
}
13 changes: 13 additions & 0 deletions tests/Stubs/Kernel/EmptyKernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Okapi\CodeTransformer\Tests\Stubs\Kernel;

use Okapi\CodeTransformer\CodeTransformerKernel;
use Okapi\CodeTransformer\Tests\Util;

class EmptyKernel extends CodeTransformerKernel
{
protected ?string $cacheDir = Util::CACHE_DIR;

protected array $transformers = [];
}
23 changes: 23 additions & 0 deletions tests/Stubs/Kernel/ExceptionOnDoubleInitializationKernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/** @noinspection PhpUnhandledExceptionInspection */
namespace Okapi\CodeTransformer\Tests\Stubs\Kernel;

use Exception;

class ExceptionOnDoubleInitializationKernel extends EmptyKernel
{
private int $initCount = 0;

protected bool $throwExceptionOnDoubleInitialization = true;

protected function preInit(): void
{
$this->initCount++;

if ($this->initCount > 1) {
throw new Exception('I should not be initialized twice!');
}

parent::preInit();
}
}