22
33namespace Okapi \CodeTransformer ;
44
5+ use DI \Attribute \Inject ;
56use Okapi \CodeTransformer \Exception \Kernel \DirectKernelInitializationException ;
67use Okapi \CodeTransformer \Service \AutoloadInterceptor ;
78use Okapi \CodeTransformer \Service \CacheStateManager ;
9+ use Okapi \CodeTransformer \Service \DI ;
810use Okapi \CodeTransformer \Service \Options ;
911use Okapi \CodeTransformer \Service \StreamFilter ;
1012use Okapi \CodeTransformer \Service \TransformerContainer ;
@@ -24,55 +26,127 @@ abstract class CodeTransformerKernel
2426{
2527 use Singleton;
2628
29+ // region Settings
30+
31+ /**
32+ * The cache directory.
33+ * <br><b>Default:</b> ROOT_DIR/cache/code-transformer<br>
34+ *
35+ * @var string|null
36+ */
37+ protected ?string $ cacheDir = null ;
38+
39+ /**
40+ * The cache file mode.
41+ * <br><b>Default:</b> 0777 & ~{@link umask()}<br>
42+ *
43+ * @var int|null
44+ */
45+ protected ?int $ cacheFileMode = null ;
46+
47+ /**
48+ * Enable debug mode. This will disable the cache.
49+ * <br><b>Default:</b> false<br>
50+ *
51+ * @var bool
52+ */
53+ protected bool $ debug = false ;
54+
55+ // endregion
56+
2757 /**
2858 * List of transformers to be applied.
2959 *
3060 * @var class-string<Transformer>[]
3161 */
3262 protected array $ transformers = [];
3363
64+ // region DI
65+
66+ #[Inject]
67+ private Options $ options ;
68+
69+ #[Inject]
70+ protected TransformerContainer $ transformerContainer ;
71+
72+ #[Inject]
73+ private CacheStateManager $ cacheStateManager ;
74+
75+ #[Inject]
76+ private StreamFilter $ streamFilter ;
77+
78+ #[Inject]
79+ private AutoloadInterceptor $ autoloadInterceptor ;
80+
3481 /**
35- * Initialize the kernel.
82+ * Make the constructor public to allow the DI container to instantiate the class.
83+ */
84+ public function __construct () {}
85+
86+ // endregion
87+
88+ /**
89+ * Resolve instance with dependency injection.
3690 *
37- * @param string|null $cacheDir The cache directory.
38- * <br><b>Default:</b> ROOT_DIR/cache/code-transformer<br>
39- * @param int|null $cacheFileMode The cache file mode.
40- * <br><b>Default:</b> 0777 & ~{@link umask()}<br>
41- * @param bool|null $debug Enable debug mode. This will disable the cache.
42- * <br><b>Default:</b> false<br>
91+ * @inheritDoc
92+ */
93+ public static function getInstance (): static
94+ {
95+ if (!isset (static ::$ instance )) {
96+ static ::registerDependencyInjection ();
97+
98+ static ::$ instance = DI ::get (static ::class);
99+ }
100+
101+ return static ::$ instance ;
102+ }
103+
104+ /**
105+ * Initialize the kernel.
43106 *
44107 * @return void
45108 */
46- public static function init (
47- ?string $ cacheDir ,
48- ?int $ cacheFileMode = null ,
49- bool $ debug = false ,
50- ): void {
51- self ::ensureNotKernelNamespace ();
52-
53- $ instance = self ::getInstance ();
54- $ instance ->ensureNotInitialized ();
109+ public static function init (): void
110+ {
111+ static ::ensureNotKernelNamespace ();
55112
56- // Only initialize the kernel if there are transformers
57- if ($ instance ->transformers ) {
58- // Pre-initialize the services
113+ $ instance = static ::getInstance ();
114+ $ instance ->ensureNotInitialized ();
59115
60- // Set options
61- Options::setOptions (
62- cacheDir: $ cacheDir ,
63- cacheFileMode: $ cacheFileMode ,
64- debug: $ debug ,
65- );
116+ // Initialize the services
117+ $ instance ->preInit ();
118+ $ instance ->registerServices ();
119+ $ instance ->registerAutoloadInterceptor ();
66120
67- // Add the transformers
68- TransformerContainer:: addTransformers ( $ instance -> transformers );
121+ $ instance -> setInitialized ();
122+ }
69123
70- // Register the services
71- $ instance ->registerServices ();
72- $ instance ->registerAutoloadInterceptor ();
73- }
124+ /**
125+ * Register the dependency injection.
126+ *
127+ * @return void
128+ */
129+ protected static function registerDependencyInjection (): void
130+ {
131+ DI ::getInstance ()->register ();
132+ }
74133
75- $ instance ->setInitialized ();
134+ /**
135+ * Pre-initialize the services.
136+ *
137+ * @return void
138+ */
139+ protected function preInit (): void
140+ {
141+ // Set options
142+ $ this ->options ->setOptions (
143+ cacheDir: $ this ->cacheDir ,
144+ cacheFileMode: $ this ->cacheFileMode ,
145+ debug: $ this ->debug ,
146+ );
147+
148+ // Add the transformers
149+ $ this ->transformerContainer ->addTransformers ($ this ->transformers );
76150 }
77151
78152 /**
@@ -83,16 +157,16 @@ public static function init(
83157 protected function registerServices (): void
84158 {
85159 // Options provider
86- Options:: register ();
160+ $ this -> options -> register ();
87161
88162 // Manage the user-defined transformers
89- TransformerContainer:: register ();
163+ $ this -> transformerContainer -> register ();
90164
91165 // Cache path manager
92- CacheStateManager:: register ();
166+ $ this -> cacheStateManager -> register ();
93167
94168 // Stream filter -> Source transformer
95- StreamFilter:: register ();
169+ $ this -> streamFilter -> register ();
96170 }
97171
98172 /**
@@ -103,15 +177,15 @@ protected function registerServices(): void
103177 protected function registerAutoloadInterceptor (): void
104178 {
105179 // Overload the composer class loaders
106- AutoloadInterceptor:: register ();
180+ $ this -> autoloadInterceptor -> register ();
107181 }
108182
109183 /**
110184 * Make sure that the kernel is not called from this class.
111185 *
112186 * @return void
113187 */
114- private static function ensureNotKernelNamespace (): void
188+ protected static function ensureNotKernelNamespace (): void
115189 {
116190 // Get current namespace and class name
117191 $ namespace = get_called_class ();
0 commit comments