File tree Expand file tree Collapse file tree 3 files changed +65
-1
lines changed Expand file tree Collapse file tree 3 files changed +65
-1
lines changed Original file line number Diff line number Diff line change 2
2
3
3
namespace Okapi \CodeTransformer ;
4
4
5
+ use Okapi \CodeTransformer \Exception \Kernel \DirectKernelInitializationException ;
5
6
use Okapi \Singleton \Singleton ;
6
7
7
8
/**
@@ -41,5 +42,31 @@ public static function init(
41
42
?string $ cacheDir ,
42
43
?int $ cacheFileMode = null ,
43
44
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
+ }
45
72
}
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments