Skip to content

Commit e9a85e3

Browse files
committed
Added Options service
1 parent 9196a7e commit e9a85e3

File tree

3 files changed

+118
-1
lines changed

3 files changed

+118
-1
lines changed

src/CodeTransformerKernel.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Okapi\CodeTransformer;
44

55
use Okapi\CodeTransformer\Exception\Kernel\DirectKernelInitializationException;
6+
use Okapi\CodeTransformer\Service\Options;
67
use Okapi\Singleton\Singleton;
78

89
/**
@@ -49,12 +50,29 @@ public static function init(
4950
$instance->ensureNotAlreadyInitialized();
5051

5152
if ($instance->transformers) {
52-
// TODO: Register services
53+
Options::setOptions(
54+
cacheDir: $cacheDir,
55+
cacheFileMode: $cacheFileMode,
56+
debug: $debug,
57+
);
58+
59+
$instance->registerServices();
5360
}
5461

5562
$instance->setInitialized();
5663
}
5764

65+
/**
66+
* Register the services.
67+
*
68+
* @return void
69+
*/
70+
protected function registerServices(): void
71+
{
72+
// Options provider
73+
Options::register();
74+
}
75+
5876
/**
5977
* Make sure that the kernel is not called from this class.
6078
*

src/Service/Options.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
namespace Okapi\CodeTransformer\Service;
4+
5+
use Okapi\Path\Path;
6+
use Okapi\Singleton\Singleton;
7+
8+
/**
9+
* # Options
10+
*
11+
* The `Options` class provides access to the options passed to the `CodeTransformerKernel`.
12+
*/
13+
class Options implements ServiceInterface
14+
{
15+
use Singleton;
16+
17+
/**
18+
* The application directory.
19+
*
20+
* @var string
21+
*/
22+
public static string $appDir;
23+
24+
/**
25+
* The cache directory.
26+
*
27+
* @var string
28+
*/
29+
public static string $cacheDir;
30+
31+
/**
32+
* The cache file mode.
33+
*
34+
* @var int
35+
*/
36+
public static int $cacheFileMode;
37+
38+
/**
39+
* Enable debug mode. This will disable the cache.
40+
*
41+
* @var bool
42+
*/
43+
public static bool $debug;
44+
45+
/**
46+
* Options constructor.
47+
*
48+
* @param string|null $cacheDir
49+
* @param int|null $cacheFileMode
50+
* @param bool|null $debug
51+
*/
52+
public static function setOptions(
53+
?string $cacheDir,
54+
?int $cacheFileMode,
55+
?bool $debug,
56+
): void {
57+
$rootDir = getcwd();
58+
59+
if ($rootDir === false) {
60+
$rootDir = Path::resolve(Path::join(__DIR__, '../../../../..'));
61+
}
62+
63+
self::$appDir = $rootDir;
64+
self::$cacheDir = $cacheDir ?? Path::join($rootDir, 'cache/code-transformer');
65+
self::$cacheFileMode = $cacheFileMode ?? (0777 & ~umask());
66+
self::$debug = $debug ?? false;
67+
}
68+
69+
/**
70+
* Register the options.
71+
*
72+
* @return void
73+
*/
74+
public static function register(): void
75+
{
76+
$instance = self::getInstance();
77+
$instance->ensureNotAlreadyInitialized();
78+
79+
$instance->setInitialized();
80+
}
81+
}

src/Service/ServiceInterface.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Okapi\CodeTransformer\Service;
4+
5+
/**
6+
* # Service Interface
7+
*
8+
* This interface is used to define a service.
9+
*/
10+
interface ServiceInterface
11+
{
12+
/**
13+
* Register a service.
14+
*
15+
* @return void
16+
*/
17+
public static function register(): void;
18+
}

0 commit comments

Comments
 (0)