Skip to content

Commit 97ccbe9

Browse files
committed
Added Production/Development environment
1 parent efc0a1c commit 97ccbe9

File tree

4 files changed

+84
-10
lines changed

4 files changed

+84
-10
lines changed

src/CodeTransformerKernel.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Okapi\CodeTransformer\Core\DI;
1010
use Okapi\CodeTransformer\Core\Exception\Kernel\DirectKernelInitializationException;
1111
use Okapi\CodeTransformer\Core\Options;
12+
use Okapi\CodeTransformer\Core\Options\Environment;
1213
use Okapi\CodeTransformer\Core\StreamFilter;
1314
use Okapi\Singleton\Singleton;
1415

@@ -18,9 +19,12 @@
1819
* This class is the heart of the Code Transformer library.
1920
* It manages an environment for Code Transformation.
2021
*
21-
* 1. Extends this class and define a list of transformers in the
22-
* `$transformers` property.
23-
* 2. Call the `init()` method early in the application lifecycle.
22+
* 1. Extend this class and define a list of transformers in the
23+
* {@link $transformers} property.
24+
* 2. Call the {@link init()} method early in the application lifecycle.
25+
*
26+
* If you want to modify the kernel options dynamically, override the
27+
* {@link configureOptions()} method.
2428
*/
2529
abstract class CodeTransformerKernel
2630
{
@@ -80,6 +84,19 @@ public function __construct() {}
8084
*/
8185
protected bool $debug = false;
8286

87+
/**
88+
* The environment in which the application is running.
89+
* <br><b>Default:</b> {@link Environment::DEVELOPMENT}<br><br>
90+
*
91+
* If {@link Environment::PRODUCTION}, the cache will not be checked for
92+
* updates (better performance).<br>
93+
* If {@link Environment::DEVELOPMENT}, the cache will be checked for
94+
* updates (better development experience).
95+
*
96+
* @var Environment
97+
*/
98+
protected Environment $environment = Environment::DEVELOPMENT;
99+
83100
/**
84101
* Throw an exception if the kernel is initialized twice.
85102
* <br><b>Default:</b> {@link false}<br>
@@ -163,6 +180,7 @@ protected function preInit(): void
163180
cacheDir: $this->cacheDir,
164181
cacheFileMode: $this->cacheFileMode,
165182
debug: $this->debug,
183+
environment: $this->environment,
166184
);
167185

168186
// Add the transformers

src/Core/AutoloadInterceptor/ClassLoader.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Okapi\CodeTransformer\Core\Cache\CacheStateManager;
99
use Okapi\CodeTransformer\Core\Matcher\TransformerMatcher;
1010
use Okapi\CodeTransformer\Core\Options;
11+
use Okapi\CodeTransformer\Core\Options\Environment;
1112
use Okapi\CodeTransformer\Core\StreamFilter;
1213
use Okapi\CodeTransformer\Core\StreamFilter\FilterInjector;
1314
use Okapi\Path\Path;
@@ -108,13 +109,28 @@ public function findFile($namespacedClass): false|string
108109
// Query cache state
109110
$cacheState = $this->cacheStateManager->queryCacheState($filePath);
110111

111-
// Check if the file is cached and up to date
112-
if ($cacheState?->isFresh() && !$this->options->isDebug()) {
112+
// When debugging, bypass the caching mechanism
113+
if ($this->options->isDebug()) {
114+
// ...
115+
}
116+
117+
// In production mode, use the cache without checking if it is fresh
118+
elseif ($this->options->getEnvironment() === Environment::PRODUCTION
119+
&& $cacheState
120+
) {
113121
// Use the cached file if transformations have been applied
114122
// Or return the original file if no transformations have been applied
115123
return $cacheState->getFilePath() ?? $filePath;
116124
}
117125

126+
// In development mode, check if the cache is fresh
127+
elseif ($this->options->getEnvironment() === Environment::DEVELOPMENT
128+
&& $cacheState
129+
&& $cacheState->isFresh()
130+
) {
131+
return $cacheState->getFilePath() ?? $filePath;
132+
}
133+
118134

119135
// Check if the class should be transformed
120136
if (!$this->transformerMatcher->match($namespacedClass, $filePath)) {

src/Core/Options.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Composer\Autoload\ClassLoader;
66
use Okapi\CodeTransformer\CodeTransformerKernel;
7+
use Okapi\CodeTransformer\Core\Options\Environment;
78
use Okapi\Path\Path;
89
use ReflectionClass;
910

@@ -45,6 +46,13 @@ class Options implements ServiceInterface
4546
*/
4647
private bool $debug;
4748

49+
/**
50+
* The environment in which the application is running.
51+
*
52+
* @var Environment
53+
*/
54+
private Environment $environment;
55+
4856
// endregion
4957

5058
/**
@@ -63,12 +71,14 @@ class Options implements ServiceInterface
6371
*
6472
* @param string|null $cacheDir
6573
* @param int|null $cacheFileMode
66-
* @param bool|null $debug
74+
* @param bool $debug
75+
* @param Environment $environment
6776
*/
6877
public function setOptions(
69-
?string $cacheDir,
70-
?int $cacheFileMode,
71-
?bool $debug,
78+
?string $cacheDir,
79+
?int $cacheFileMode,
80+
bool $debug,
81+
Environment $environment,
7282
): void {
7383
$composerRef = new ReflectionClass(ClassLoader::class);
7484
$composerDir = $composerRef->getFileName();
@@ -77,7 +87,8 @@ public function setOptions(
7787
$this->appDir = $rootDir;
7888
$this->cacheDir = $cacheDir ?? Path::join($rootDir, $this->defaultCacheDir);
7989
$this->cacheFileMode = $cacheFileMode ?? (0777 & ~umask());
80-
$this->debug = $debug ?? false;
90+
$this->debug = $debug;
91+
$this->environment = $environment;
8192
}
8293

8394
// endregion
@@ -125,4 +136,12 @@ public function isDebug(): bool
125136
{
126137
return $this->debug;
127138
}
139+
140+
/**
141+
* Get the environment in which the application is running.
142+
*/
143+
public function getEnvironment(): Environment
144+
{
145+
return $this->environment;
146+
}
128147
}

src/Core/Options/Environment.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Okapi\CodeTransformer\Core\Options;
4+
5+
/**
6+
* # Environment
7+
*
8+
* The environment in which the application is running.
9+
*/
10+
enum Environment
11+
{
12+
/**
13+
* Cache will not be checked for updates (better performance).
14+
*/
15+
case PRODUCTION;
16+
17+
/**
18+
* Cache will be checked for updates (better development experience).
19+
*/
20+
case DEVELOPMENT;
21+
}

0 commit comments

Comments
 (0)