Skip to content

Commit 48cb751

Browse files
committed
Added "no direct initialization" logic
1 parent 8aba28e commit 48cb751

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

src/CodeTransformerKernel.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Okapi\CodeTransformer;
44

5+
use Okapi\CodeTransformer\Exception\Kernel\DirectKernelInitializationException;
56
use Okapi\Singleton\Singleton;
67

78
/**
@@ -41,5 +42,31 @@ public static function init(
4142
?string $cacheDir,
4243
?int $cacheFileMode = null,
4344
bool $debug = false,
44-
): void {}
45+
): void {
46+
self::ensureNotKernelNamespace();
47+
48+
$instance = self::getInstance();
49+
$instance->ensureNotAlreadyInitialized();
50+
51+
if ($instance->transformers) {
52+
// TODO: Register services
53+
}
54+
55+
$instance->setInitialized();
56+
}
57+
58+
/**
59+
* Make sure that the kernel is not called from this class.
60+
*
61+
* @return void
62+
*/
63+
private static function ensureNotKernelNamespace(): void
64+
{
65+
// Get current namespace and class name
66+
$namespace = get_called_class();
67+
68+
if ($namespace === CodeTransformerKernel::class) {
69+
throw new DirectKernelInitializationException;
70+
}
71+
}
4572
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Okapi\CodeTransformer\Exception\Kernel;
4+
5+
use Okapi\CodeTransformer\Exception\KernelException;
6+
7+
/**
8+
* # Initialize Kernel Exception
9+
*
10+
* This exception is thrown when the CodeTransformerKernel is initialized
11+
* directly.
12+
*/
13+
class DirectKernelInitializationException extends KernelException
14+
{
15+
/**
16+
* DirectKernelInitializationException constructor.
17+
*/
18+
public function __construct()
19+
{
20+
parent::__construct(
21+
"Cannot initialize CodeTransformerKernel directly. " .
22+
"Please extend from CodeTransformerKernel and call the init() method.",
23+
);
24+
}
25+
}

src/Exception/KernelException.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Okapi\CodeTransformer\Exception;
4+
5+
/**
6+
* # Kernel Exception
7+
*
8+
* Base exception for all `CodeTransformerKernel` exceptions.
9+
*/
10+
abstract class KernelException extends CodeTransformerException
11+
{
12+
}

0 commit comments

Comments
 (0)