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 22
33namespace Okapi \CodeTransformer ;
44
5+ use Okapi \CodeTransformer \Exception \Kernel \DirectKernelInitializationException ;
56use 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}
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